19 using System.Collections.Generic;
26 namespace Deveel.Data.Sql.Objects {
46 public const int MaxLength = Int16.MaxValue;
55 private SqlString(
char[] chars,
int length,
bool isNull) : this() {
59 if (length > MaxLength)
60 throw new ArgumentOutOfRangeException(
"length");
62 source = Encoding.Unicode.GetBytes(chars);
63 Length = Encoding.Unicode.GetCharCount(source);
66 IsNull = isNull || source == null;
75 : this(chars, chars == null ? 0 : chars.Length) {
85 : this(chars, length, false) {
89 : this(source == null ? (char[]) null : source.ToCharArray()) {
92 public SqlString(byte[] bytes,
int offset,
int length)
93 : this(GetChars(bytes, offset, length)) {
97 : this(bytes, 0, bytes == null ? 0 : bytes.Length) {
102 source = data.
GetValue<byte[]>(
"Source");
105 private static char[]
GetChars(byte[] bytes,
int offset,
int length) {
109 return Encoding.Unicode.GetChars(bytes, offset, length);
112 int IComparable.CompareTo(
object obj) {
120 public bool IsNull {
get;
private set; }
123 get {
return Encoding.Unicode; }
126 public string Value {
127 get {
return source == null ? null : Encoding.Unicode.GetString(source, 0, source.Length); }
130 public char this[
long index] {
132 if (index > Int32.MaxValue)
133 throw new ArgumentOutOfRangeException(
"index");
138 throw new ArgumentOutOfRangeException(
"index");
140 var chars = Encoding.Unicode.GetChars(source);
151 throw new ArgumentNullException(
"other");
153 if (IsNull && other.
IsNull)
162 return String.Compare(Value, otherString.Value, StringComparison.Ordinal);
165 throw new NotImplementedException(
"Comparison with long strong not implemented yet.");
172 IEnumerator IEnumerable.GetEnumerator() {
173 return GetEnumerator();
176 public long Length {
get;
private set; }
188 if (!encoding.Equals(Encoding.Unicode))
189 bytes = Encoding.Convert(Encoding.Unicode, encoding, bytes);
191 return encoding.GetString(bytes, 0, bytes.Length);
196 return TextReader.Null;
198 if (encoding == null)
199 encoding = Encoding.Unicode;
202 if (!encoding.Equals(Encoding.Unicode))
203 bytes = Encoding.Convert(Encoding.Unicode, encoding, bytes);
205 var stream =
new MemoryStream(bytes);
206 return new StreamReader(stream, encoding);
210 if (source == null && other.
source == null)
216 if (source.Length != other.
source.Length)
219 for (
int i = 0; i < source.Length; i++) {
229 public override bool Equals(
object obj) {
244 foreach (var item
in source) {
245 hash = hash*23 + item.GetHashCode();
253 if (encoding == null)
254 throw new ArgumentNullException(
"encoding");
259 return (byte[]) source.Clone();
263 if (other == null || other.
IsNull)
268 var length = (int) (Length + otheString.Length);
269 if (length >= MaxLength)
270 throw new ArgumentException(
"The final string will be over the maximum length");
272 var sourceChars = ToCharArray();
273 var otherChars = otheString.ToCharArray();
274 var destChars =
new char[length];
276 Array.Copy(sourceChars, 0, destChars, 0, (
int)Length);
277 Array.Copy(otherChars, 0, destChars, (
int)Length, (
int)otheString.Length);
281 var sb =
new StringBuilder(Int16.MaxValue);
282 using (var output =
new StringWriter(sb)) {
284 using (var reader = GetInput(Encoding.Unicode)) {
285 var buffer =
new char[2048];
287 while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) {
288 output.Write(buffer, 0, count);
293 using (var reader = other.
GetInput(Encoding.Unicode)) {
294 var buffer =
new char[2048];
296 while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) {
297 output.Write(buffer, 0, count);
305 var s = sb.ToString();
309 var outChars =
new char[sb.Length];
310 sb.CopyTo(0, outChars, 0, sb.Length);
311 return new SqlString(outChars, outChars.Length);
319 var bytes = Encoding.Convert(Encoding.Unicode, encoding, source);
323 #region StringEnumerator
327 private int index = -1;
331 this.sqlString = sqlString;
332 length = (int) sqlString.
Length;
339 return ++index < length;
346 public char Current {
349 throw new InvalidOperationException();
351 return sqlString[index];
355 object IEnumerator.Current {
356 get {
return Current; }
366 TypeCode IConvertible.GetTypeCode() {
367 return TypeCode.String;
370 bool IConvertible.ToBoolean(IFormatProvider provider) {
371 return Convert.ToBoolean(Value, provider);
374 char IConvertible.ToChar(IFormatProvider provider) {
375 return Convert.ToChar(Value, provider);
378 sbyte IConvertible.ToSByte(IFormatProvider provider) {
379 return Convert.ToSByte(Value, provider);
382 byte IConvertible.ToByte(IFormatProvider provider) {
383 return Convert.ToByte(Value, provider);
386 short IConvertible.ToInt16(IFormatProvider provider) {
387 return Convert.ToInt16(Value, provider);
390 ushort IConvertible.ToUInt16(IFormatProvider provider) {
391 return Convert.ToUInt16(Value, provider);
394 int IConvertible.ToInt32(IFormatProvider provider) {
395 return Convert.ToInt32(Value, provider);
398 uint IConvertible.ToUInt32(IFormatProvider provider) {
399 return Convert.ToUInt32(Value, provider);
402 long IConvertible.ToInt64(IFormatProvider provider) {
403 return Convert.ToInt64(Value, provider);
406 ulong IConvertible.ToUInt64(IFormatProvider provider) {
407 return Convert.ToUInt64(Value, provider);
410 float IConvertible.ToSingle(IFormatProvider provider) {
411 return Convert.ToSingle(Value, provider);
414 double IConvertible.ToDouble(IFormatProvider provider) {
415 return Convert.ToDouble(Value, provider);
418 decimal IConvertible.ToDecimal(IFormatProvider provider) {
419 return Convert.ToDecimal(Value, provider);
422 DateTime IConvertible.ToDateTime(IFormatProvider provider) {
423 return Convert.ToDateTime(Value, provider);
426 string IConvertible.ToString(IFormatProvider provider) {
427 return Convert.ToString(Value, provider);
430 object IConvertible.ToType(Type conversionType, IFormatProvider provider) {
431 if (conversionType == typeof (
bool))
432 return Convert.ToBoolean(Value, provider);
433 if (conversionType == typeof (byte))
434 return Convert.ToByte(Value, provider);
435 if (conversionType == typeof (sbyte))
436 return Convert.ToSByte(Value, provider);
437 if (conversionType == typeof (
short))
438 return Convert.ToInt16(Value, provider);
439 if (conversionType == typeof (ushort))
440 return Convert.ToUInt16(Value, provider);
441 if (conversionType == typeof (
int))
442 return Convert.ToInt32(Value, provider);
443 if (conversionType == typeof (uint))
444 return Convert.ToUInt32(Value, provider);
445 if (conversionType == typeof (
long))
446 return Convert.ToInt64(Value, provider);
447 if (conversionType == typeof (ulong))
448 return Convert.ToUInt64(Value, provider);
449 if (conversionType == typeof (
float))
450 return Convert.ToSingle(Value, provider);
451 if (conversionType == typeof (
double))
452 return Convert.ToDouble(Value, provider);
453 if (conversionType == typeof (decimal))
454 return Convert.ToDecimal(Value, provider);
455 if (conversionType == typeof (
string))
456 return Convert.ToString(Value, provider);
458 if (conversionType == typeof (
char[]))
459 return ToCharArray();
461 if (conversionType == typeof (
SqlNumber))
467 if (conversionType == typeof (
SqlBinary))
470 throw new InvalidCastException(
String.Format(
"Cannot convet SQL STRING to {0}", conversionType.FullName));
498 var bytes = ToByteArray(Encoding.Unicode);
506 return Encoding.Unicode.GetChars(source);
byte[] ToByteArray(Encoding encoding)
override bool Equals(object obj)
void GetData(SerializeData data)
A long string in the system.
StringEnumerator(SqlString sqlString)
static bool TryParse(string s, out SqlNumber value)
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
SqlString(byte[] bytes, int offset, int length)
void SetValue(string key, Type type, object value)
readonly SqlString sqlString
bool IsNull
Gets a boolean value indicating if the object is NULL.
static char[] GetChars(byte[] bytes, int offset, int length)
override int GetHashCode()
Defines the contract for a valid SQL Object
SqlString(char[] chars, int length, bool isNull)
IEnumerator< char > GetEnumerator()
static readonly SqlDateTime Null
string ToString(Encoding encoding)
int CompareTo(ISqlString other)
SqlString(ObjectData data)
TextReader GetInput(Encoding encoding)
static readonly SqlNumber Null
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
override string ToString()
TextReader GetInput(Encoding encoding)
int GetByteCount(Encoding encoding)
SqlString(char[] chars)
Initializes a new instance of the SqlString structure with the given set of characters.
SqlString Concat(ISqlString other)
object GetValue(string key)
bool Equals(SqlString other)
SqlString(char[] chars, int length)
Initializes a new instance of the SqlString structure.
static bool TryParse(string s, out SqlDateTime value)
Deveel.Data.Sql.Objects.SqlString SqlString