DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Public Attributes | Static Public Attributes | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Objects.SqlString Struct Reference

The most simple implementation of a SQL string with a small size More...

Inheritance diagram for Deveel.Data.Sql.Objects.SqlString:
Deveel.Data.Sql.Objects.ISqlString Deveel.Data.Serialization.ISerializable Deveel.Data.Sql.Objects.ISqlObject

Classes

class  StringEnumerator
 

Public Member Functions

 SqlString (char[] chars)
 Initializes a new instance of the SqlString structure with the given set of characters. More...
 
 SqlString (char[] chars, int length)
 Initializes a new instance of the SqlString structure. More...
 
 SqlString (string source)
 
 SqlString (byte[] bytes, int offset, int length)
 
 SqlString (byte[] bytes)
 
int IComparable. CompareTo (object obj)
 
int IComparable< ISqlObject >. CompareTo (ISqlObject other)
 
bool ISqlObject. IsComparableTo (ISqlObject other)
 Checks if the current object is comparable with the given one. More...
 
int CompareTo (ISqlString other)
 
IEnumerator< char > GetEnumerator ()
 
IEnumerator IEnumerable. GetEnumerator ()
 
void ISerializable. GetData (SerializeData data)
 
string ToString (Encoding encoding)
 
TextReader GetInput (Encoding encoding)
 
bool Equals (SqlString other)
 
override bool Equals (object obj)
 
override int GetHashCode ()
 
byte[] ToByteArray (Encoding encoding)
 
SqlString Concat (ISqlString other)
 
int GetByteCount (Encoding encoding)
 
override string ToString ()
 
TypeCode IConvertible. GetTypeCode ()
 
bool IConvertible. ToBoolean (IFormatProvider provider)
 
char IConvertible. ToChar (IFormatProvider provider)
 
sbyte IConvertible. ToSByte (IFormatProvider provider)
 
byte IConvertible. ToByte (IFormatProvider provider)
 
short IConvertible. ToInt16 (IFormatProvider provider)
 
ushort IConvertible. ToUInt16 (IFormatProvider provider)
 
int IConvertible. ToInt32 (IFormatProvider provider)
 
uint IConvertible. ToUInt32 (IFormatProvider provider)
 
long IConvertible. ToInt64 (IFormatProvider provider)
 
ulong IConvertible. ToUInt64 (IFormatProvider provider)
 
float IConvertible. ToSingle (IFormatProvider provider)
 
double IConvertible. ToDouble (IFormatProvider provider)
 
decimal IConvertible. ToDecimal (IFormatProvider provider)
 
DateTime IConvertible. ToDateTime (IFormatProvider provider)
 
string IConvertible. ToString (IFormatProvider provider)
 
object IConvertible. ToType (Type conversionType, IFormatProvider provider)
 
SqlBoolean ToBoolean ()
 
SqlNumber ToNumber ()
 
SqlDateTime ToDateTime ()
 
SqlBinary ToBinary ()
 
char[] ToCharArray ()
 

Public Attributes

const int MaxLength = Int16.MaxValue
 The maximum length of characters a SqlString can handle. More...
 

Static Public Attributes

static readonly SqlString Null = new SqlString(null, 0, true)
 The null instance of a string. More...
 

Properties

bool IsNull [get, private set]
 
Encoding ISqlString. Encoding [get]
 
string Value [get]
 
char this[long index] [get]
 
long Length [get, private set]
 
- Properties inherited from Deveel.Data.Sql.Objects.ISqlString
Encoding Encoding [get]
 
long Length [get]
 
char this[long offset] [get]
 
- Properties inherited from Deveel.Data.Sql.Objects.ISqlObject
bool IsNull [get]
 Gets a boolean value indicating if the object is NULL. More...
 

Private Member Functions

 SqlString (char[] chars, int length, bool isNull)
 
 SqlString (ObjectData data)
 

Static Private Member Functions

static char[] GetChars (byte[] bytes, int offset, int length)
 

Private Attributes

readonly byte[] source
 

Detailed Description

The most simple implementation of a SQL string with a small size

Instances of this object handle strings that are not backed by large objects and can handle a fixed length of characters.

The encoding of the string is dependent from the StringType that defines an object, but the default is UnicodeEncoding.

See also
ISqlString

Definition at line 42 of file SqlString.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Objects.SqlString.SqlString ( char[]  chars,
int  length,
bool  isNull 
)
inlineprivate

Definition at line 55 of file SqlString.cs.

55  : this() {
56  if (chars == null) {
57  source = null;
58  } else {
59  if (length > MaxLength)
60  throw new ArgumentOutOfRangeException("length");
61 
62  source = Encoding.Unicode.GetBytes(chars);
63  Length = Encoding.Unicode.GetCharCount(source);
64  }
65 
66  IsNull = isNull || source == null;
67  }
const int MaxLength
The maximum length of characters a SqlString can handle.
Definition: SqlString.cs:46
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
Deveel.Data.Sql.Objects.SqlString.SqlString ( char[]  chars)
inline

Initializes a new instance of the SqlString structure with the given set of characters.

Parameters
charsThe chars.

Definition at line 74 of file SqlString.cs.

75  : this(chars, chars == null ? 0 : chars.Length) {
76  }
Deveel.Data.Sql.Objects.SqlString.SqlString ( char[]  chars,
int  length 
)
inline

Initializes a new instance of the SqlString structure.

Parameters
charsThe chars.
lengthThe length.
Exceptions
System.ArgumentOutOfRangeExceptionlength

Definition at line 84 of file SqlString.cs.

85  : this(chars, length, false) {
86  }
Deveel.Data.Sql.Objects.SqlString.SqlString ( string  source)
inline

Definition at line 88 of file SqlString.cs.

89  : this(source == null ? (char[]) null : source.ToCharArray()) {
90  }
Deveel.Data.Sql.Objects.SqlString.SqlString ( byte[]  bytes,
int  offset,
int  length 
)
inline

Definition at line 92 of file SqlString.cs.

93  : this(GetChars(bytes, offset, length)) {
94  }
static char[] GetChars(byte[] bytes, int offset, int length)
Definition: SqlString.cs:105
Deveel.Data.Sql.Objects.SqlString.SqlString ( byte[]  bytes)
inline

Definition at line 96 of file SqlString.cs.

97  : this(bytes, 0, bytes == null ? 0 : bytes.Length) {
98  }
Deveel.Data.Sql.Objects.SqlString.SqlString ( ObjectData  data)
inlineprivate

Definition at line 100 of file SqlString.cs.

101  : this() {
102  source = data.GetValue<byte[]>("Source");
103  }

Member Function Documentation

int IComparable. Deveel.Data.Sql.Objects.SqlString.CompareTo ( object  obj)
inline

Definition at line 112 of file SqlString.cs.

112  {
113  return CompareTo((ISqlString) obj);
114  }
int IComparable. CompareTo(object obj)
Definition: SqlString.cs:112
int IComparable<ISqlObject>. Deveel.Data.Sql.Objects.SqlString.CompareTo ( ISqlObject  other)
inline

Definition at line 116 of file SqlString.cs.

116  {
117  return CompareTo((ISqlString) other);
118  }
int IComparable. CompareTo(object obj)
Definition: SqlString.cs:112
int Deveel.Data.Sql.Objects.SqlString.CompareTo ( ISqlString  other)
inline

Definition at line 149 of file SqlString.cs.

149  {
150  if (other == null)
151  throw new ArgumentNullException("other");
152 
153  if (IsNull && other.IsNull)
154  return 0;
155  if (IsNull)
156  return 1;
157  if (other.IsNull)
158  return -1;
159 
160  if (other is SqlString) {
161  var otherString = (SqlString) other;
162  return String.Compare(Value, otherString.Value, StringComparison.Ordinal);
163  }
164 
165  throw new NotImplementedException("Comparison with long strong not implemented yet.");
166  }
A long string in the system.
SqlString(char[] chars, int length, bool isNull)
Definition: SqlString.cs:55
SqlString Deveel.Data.Sql.Objects.SqlString.Concat ( ISqlString  other)
inline

Definition at line 262 of file SqlString.cs.

262  {
263  if (other == null || other.IsNull)
264  return this;
265 
266  if (other is SqlString) {
267  var otheString = (SqlString) other;
268  var length = (int) (Length + otheString.Length);
269  if (length >= MaxLength)
270  throw new ArgumentException("The final string will be over the maximum length");
271 
272  var sourceChars = ToCharArray();
273  var otherChars = otheString.ToCharArray();
274  var destChars = new char[length];
275 
276  Array.Copy(sourceChars, 0, destChars, 0, (int)Length);
277  Array.Copy(otherChars, 0, destChars, (int)Length, (int)otheString.Length);
278  return new SqlString(destChars, length);
279  }
280 
281  var sb = new StringBuilder(Int16.MaxValue);
282  using (var output = new StringWriter(sb)) {
283  // First read the current stream
284  using (var reader = GetInput(Encoding.Unicode)) {
285  var buffer = new char[2048];
286  int count;
287  while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) {
288  output.Write(buffer, 0, count);
289  }
290  }
291 
292  // Then read the second stream
293  using (var reader = other.GetInput(Encoding.Unicode)) {
294  var buffer = new char[2048];
295  int count;
296  while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) {
297  output.Write(buffer, 0, count);
298  }
299  }
300 
301  output.Flush();
302  }
303 
304 #if PCL
305  var s = sb.ToString();
306  return new SqlString(s);
307 
308 #else
309  var outChars = new char[sb.Length];
310  sb.CopyTo(0, outChars, 0, sb.Length);
311  return new SqlString(outChars, outChars.Length);
312 #endif
313  }
const int MaxLength
The maximum length of characters a SqlString can handle.
Definition: SqlString.cs:46
SqlString(char[] chars, int length, bool isNull)
Definition: SqlString.cs:55
TextReader GetInput(Encoding encoding)
Definition: SqlString.cs:194
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
bool Deveel.Data.Sql.Objects.SqlString.Equals ( SqlString  other)
inline

Definition at line 209 of file SqlString.cs.

209  {
210  if (source == null && other.source == null)
211  return true;
212 
213  if (source == null)
214  return false;
215 
216  if (source.Length != other.source.Length)
217  return false;
218 
219  for (int i = 0; i < source.Length; i++) {
220  var c1 = source[i];
221  var c2 = other.source[i];
222  if (!c1.Equals(c2))
223  return false;
224  }
225 
226  return true;
227  }
override bool Deveel.Data.Sql.Objects.SqlString.Equals ( object  obj)
inline

Definition at line 229 of file SqlString.cs.

229  {
230  if (obj is SqlNull && IsNull)
231  return true;
232 
233  return Equals((SqlString) obj);
234  }
bool Equals(SqlString other)
Definition: SqlString.cs:209
int Deveel.Data.Sql.Objects.SqlString.GetByteCount ( Encoding  encoding)
inline

Definition at line 315 of file SqlString.cs.

315  {
316  if (IsNull)
317  return 0;
318 
319  var bytes = Encoding.Convert(Encoding.Unicode, encoding, source);
320  return bytes.Length;
321  }
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
static char [] Deveel.Data.Sql.Objects.SqlString.GetChars ( byte[]  bytes,
int  offset,
int  length 
)
inlinestaticprivate

Definition at line 105 of file SqlString.cs.

105  {
106  if (bytes == null)
107  return null;
108 
109  return Encoding.Unicode.GetChars(bytes, offset, length);
110  }
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
void ISerializable. Deveel.Data.Sql.Objects.SqlString.GetData ( SerializeData  data)
inline

Implements Deveel.Data.Serialization.ISerializable.

Definition at line 178 of file SqlString.cs.

178  {
179  data.SetValue("Source", source);
180  }
void SetValue(string key, Type type, object value)
IEnumerator<char> Deveel.Data.Sql.Objects.SqlString.GetEnumerator ( )
inline

Definition at line 168 of file SqlString.cs.

168  {
169  return new StringEnumerator(this);
170  }
IEnumerator IEnumerable. Deveel.Data.Sql.Objects.SqlString.GetEnumerator ( )
inline

Definition at line 172 of file SqlString.cs.

172  {
173  return GetEnumerator();
174  }
IEnumerator< char > GetEnumerator()
Definition: SqlString.cs:168
override int Deveel.Data.Sql.Objects.SqlString.GetHashCode ( )
inline

Definition at line 236 of file SqlString.cs.

236  {
237  if (source == null)
238  return 0;
239 
240  unchecked {
241  int hash = 17;
242 
243  // get hash code for all items in array
244  foreach (var item in source) {
245  hash = hash*23 + item.GetHashCode();
246  }
247 
248  return hash;
249  }
250  }
TextReader Deveel.Data.Sql.Objects.SqlString.GetInput ( Encoding  encoding)
inline

Implements Deveel.Data.Sql.Objects.ISqlString.

Definition at line 194 of file SqlString.cs.

194  {
195  if (IsNull)
196  return TextReader.Null;
197 
198  if (encoding == null)
199  encoding = Encoding.Unicode;
200 
201  var bytes = source;
202  if (!encoding.Equals(Encoding.Unicode))
203  bytes = Encoding.Convert(Encoding.Unicode, encoding, bytes);
204 
205  var stream = new MemoryStream(bytes);
206  return new StreamReader(stream, encoding);
207  }
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
TypeCode IConvertible. Deveel.Data.Sql.Objects.SqlString.GetTypeCode ( )
inline

Definition at line 366 of file SqlString.cs.

366  {
367  return TypeCode.String;
368  }
bool ISqlObject. Deveel.Data.Sql.Objects.SqlString.IsComparableTo ( ISqlObject  other)
inline

Checks if the current object is comparable with the given one.

Parameters
otherThe other ISqlObject to compare.
Returns
Returns true if the current object is comparable with the given one, false otherwise.

Implements Deveel.Data.Sql.Objects.ISqlObject.

Definition at line 145 of file SqlString.cs.

145  {
146  return other is ISqlString;
147  }
SqlBinary Deveel.Data.Sql.Objects.SqlString.ToBinary ( )
inline

Definition at line 497 of file SqlString.cs.

497  {
498  var bytes = ToByteArray(Encoding.Unicode);
499  return new SqlBinary(bytes);
500  }
byte[] ToByteArray(Encoding encoding)
Definition: SqlString.cs:252
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
bool IConvertible. Deveel.Data.Sql.Objects.SqlString.ToBoolean ( IFormatProvider  provider)
inline

Definition at line 370 of file SqlString.cs.

370  {
371  return Convert.ToBoolean(Value, provider);
372  }
SqlBoolean Deveel.Data.Sql.Objects.SqlString.ToBoolean ( )
inline

Definition at line 473 of file SqlString.cs.

473  {
474  SqlBoolean value;
475  if (!SqlBoolean.TryParse(Value, out value))
476  return SqlBoolean.Null; // TODO: Should we throw an exception?
477 
478  return value;
479  }
byte IConvertible. Deveel.Data.Sql.Objects.SqlString.ToByte ( IFormatProvider  provider)
inline

Definition at line 382 of file SqlString.cs.

382  {
383  return Convert.ToByte(Value, provider);
384  }
byte [] Deveel.Data.Sql.Objects.SqlString.ToByteArray ( Encoding  encoding)
inline

Definition at line 252 of file SqlString.cs.

252  {
253  if (encoding == null)
254  throw new ArgumentNullException("encoding");
255 
256  if (source == null)
257  return new byte[0];
258 
259  return (byte[]) source.Clone();
260  }
char IConvertible. Deveel.Data.Sql.Objects.SqlString.ToChar ( IFormatProvider  provider)
inline

Definition at line 374 of file SqlString.cs.

374  {
375  return Convert.ToChar(Value, provider);
376  }
char [] Deveel.Data.Sql.Objects.SqlString.ToCharArray ( )
inline

Definition at line 502 of file SqlString.cs.

502  {
503  if (source == null)
504  return new char[0];
505 
506  return Encoding.Unicode.GetChars(source);
507  }
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
DateTime IConvertible. Deveel.Data.Sql.Objects.SqlString.ToDateTime ( IFormatProvider  provider)
inline

Definition at line 422 of file SqlString.cs.

422  {
423  return Convert.ToDateTime(Value, provider);
424  }
SqlDateTime Deveel.Data.Sql.Objects.SqlString.ToDateTime ( )
inline

Definition at line 489 of file SqlString.cs.

489  {
490  SqlDateTime value;
491  if (!SqlDateTime.TryParse(Value, out value))
492  return SqlDateTime.Null; // TODO: Shoudl we throw an exception?
493 
494  return value;
495  }
decimal IConvertible. Deveel.Data.Sql.Objects.SqlString.ToDecimal ( IFormatProvider  provider)
inline

Definition at line 418 of file SqlString.cs.

418  {
419  return Convert.ToDecimal(Value, provider);
420  }
double IConvertible. Deveel.Data.Sql.Objects.SqlString.ToDouble ( IFormatProvider  provider)
inline

Definition at line 414 of file SqlString.cs.

414  {
415  return Convert.ToDouble(Value, provider);
416  }
short IConvertible. Deveel.Data.Sql.Objects.SqlString.ToInt16 ( IFormatProvider  provider)
inline

Definition at line 386 of file SqlString.cs.

386  {
387  return Convert.ToInt16(Value, provider);
388  }
int IConvertible. Deveel.Data.Sql.Objects.SqlString.ToInt32 ( IFormatProvider  provider)
inline

Definition at line 394 of file SqlString.cs.

394  {
395  return Convert.ToInt32(Value, provider);
396  }
long IConvertible. Deveel.Data.Sql.Objects.SqlString.ToInt64 ( IFormatProvider  provider)
inline

Definition at line 402 of file SqlString.cs.

402  {
403  return Convert.ToInt64(Value, provider);
404  }
SqlNumber Deveel.Data.Sql.Objects.SqlString.ToNumber ( )
inline

Definition at line 481 of file SqlString.cs.

481  {
482  SqlNumber value;
483  if (!SqlNumber.TryParse(Value, out value))
484  return SqlNumber.Null; // TODO: Shoudl we throw an exception?
485 
486  return value;
487  }
sbyte IConvertible. Deveel.Data.Sql.Objects.SqlString.ToSByte ( IFormatProvider  provider)
inline

Definition at line 378 of file SqlString.cs.

378  {
379  return Convert.ToSByte(Value, provider);
380  }
float IConvertible. Deveel.Data.Sql.Objects.SqlString.ToSingle ( IFormatProvider  provider)
inline

Definition at line 410 of file SqlString.cs.

410  {
411  return Convert.ToSingle(Value, provider);
412  }
string Deveel.Data.Sql.Objects.SqlString.ToString ( Encoding  encoding)
inline

Definition at line 182 of file SqlString.cs.

182  {
183  if (IsNull)
184  return null;
185 
186  var bytes = source;
187 
188  if (!encoding.Equals(Encoding.Unicode))
189  bytes = Encoding.Convert(Encoding.Unicode, encoding, bytes);
190 
191  return encoding.GetString(bytes, 0, bytes.Length);
192  }
Encoding ISqlString. Encoding
Definition: SqlString.cs:122
override string Deveel.Data.Sql.Objects.SqlString.ToString ( )
inline

Definition at line 362 of file SqlString.cs.

362  {
363  return Value;
364  }
string IConvertible. Deveel.Data.Sql.Objects.SqlString.ToString ( IFormatProvider  provider)
inline

Definition at line 426 of file SqlString.cs.

426  {
427  return Convert.ToString(Value, provider);
428  }
object IConvertible. Deveel.Data.Sql.Objects.SqlString.ToType ( Type  conversionType,
IFormatProvider  provider 
)
inline

Definition at line 430 of file SqlString.cs.

430  {
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);
457 
458  if (conversionType == typeof (char[]))
459  return ToCharArray();
460 
461  if (conversionType == typeof (SqlNumber))
462  return ToNumber();
463  if (conversionType == typeof (SqlBoolean))
464  return ToBoolean();
465  if (conversionType == typeof (SqlDateTime))
466  return ToDateTime();
467  if (conversionType == typeof (SqlBinary))
468  return ToBinary();
469 
470  throw new InvalidCastException(String.Format("Cannot convet SQL STRING to {0}", conversionType.FullName));
471  }
A long string in the system.
ushort IConvertible. Deveel.Data.Sql.Objects.SqlString.ToUInt16 ( IFormatProvider  provider)
inline

Definition at line 390 of file SqlString.cs.

390  {
391  return Convert.ToUInt16(Value, provider);
392  }
uint IConvertible. Deveel.Data.Sql.Objects.SqlString.ToUInt32 ( IFormatProvider  provider)
inline

Definition at line 398 of file SqlString.cs.

398  {
399  return Convert.ToUInt32(Value, provider);
400  }
ulong IConvertible. Deveel.Data.Sql.Objects.SqlString.ToUInt64 ( IFormatProvider  provider)
inline

Definition at line 406 of file SqlString.cs.

406  {
407  return Convert.ToUInt64(Value, provider);
408  }

Member Data Documentation

const int Deveel.Data.Sql.Objects.SqlString.MaxLength = Int16.MaxValue

The maximum length of characters a SqlString can handle.

Definition at line 46 of file SqlString.cs.

readonly SqlString Deveel.Data.Sql.Objects.SqlString.Null = new SqlString(null, 0, true)
static

The null instance of a string.

Definition at line 51 of file SqlString.cs.

readonly byte [] Deveel.Data.Sql.Objects.SqlString.source
private

Definition at line 53 of file SqlString.cs.

Property Documentation

Encoding ISqlString. Deveel.Data.Sql.Objects.SqlString.Encoding
get

Definition at line 122 of file SqlString.cs.

bool Deveel.Data.Sql.Objects.SqlString.IsNull
getprivate set

Definition at line 120 of file SqlString.cs.

long Deveel.Data.Sql.Objects.SqlString.Length
getprivate set

Definition at line 176 of file SqlString.cs.

char Deveel.Data.Sql.Objects.SqlString.this[long index]
get

Definition at line 130 of file SqlString.cs.

string Deveel.Data.Sql.Objects.SqlString.Value
get

Definition at line 126 of file SqlString.cs.


The documentation for this struct was generated from the following file: