18 using System.Globalization;
27 namespace Deveel.Data.Types {
32 public const int DefaultMaxSize = Int16.MaxValue;
35 : base(
"STRING", typeCode) {
37 throw new ArgumentNullException(
"encoding");
39 AssertIsString(typeCode);
51 Locale =
new CultureInfo(locale);
55 var encoding = data.
GetString(
"Encoding");
56 Encoding = Encoding.GetEncoding(encoding);
61 if (!IsStringType(sqlType))
62 throw new ArgumentException(
String.Format(
"The type {0} is not a valid STRING type.", sqlType),
"sqlType");
69 public int MaxSize {
get;
private set; }
72 get {
return MaxSize; }
75 public override bool IsStorable {
86 public CultureInfo Locale {
get;
private set; }
88 private CompareInfo Collator {
91 if (collator != null) {
95 collator = Locale.CompareInfo;
102 public Encoding Encoding {
get;
private set; }
107 data.
SetValue(
"Locale", Locale.Name);
108 if (Encoding != null)
109 data.
SetValue(
"Encoding", Encoding.WebName);
118 var sb =
new StringBuilder(Name);
120 sb.AppendFormat(
"({0})", MaxSize);
122 return sb.ToString();
135 return base.GetObjectType();
142 return typeof (
string);
146 return typeof (Stream);
148 return base.GetRuntimeType();
153 if (!base.Equals(other))
157 if (stringType.MaxSize != MaxSize)
160 if (Locale == null && stringType.Locale == null)
162 if (Locale == null && stringType.Locale != null)
164 if (Locale != null && stringType.Locale == null)
167 if (Locale != null && stringType.Locale != null)
168 return Locale.NativeName.
Equals(stringType.Locale.NativeName);
175 return TypeCode.GetHashCode() ^ MaxSize.GetHashCode();
182 var stringType = (StringType) type;
184 if (Locale == null || stringType.Locale == null)
191 return Locale.
Equals(stringType.Locale);
199 if (value == null && IsNull)
201 if (!IsNull && value == null)
206 var s1 = value.ToString();
207 var s2 = pattern.ToString();
212 var likeResult = IsLike(value, pattern);
213 if (likeResult.IsNull)
216 return likeResult.Not();
221 throw new ArgumentException();
223 var s = (ISqlString) obj;
227 if (destType == typeof (
string))
230 throw new InvalidCastException();
238 throw new ArgumentException();
240 if (!(b is ISqlString)) {
242 throw new NotSupportedException();
247 var y = (ISqlString) b;
251 return base.Add(a, b);
299 return String.Format(
"The input string {0} of type {1} is not compatible with any of the formats for SQL Type {2} ( {3} )",
301 TypeCode.ToString().ToUpperInvariant(),
302 sqlType.ToString().ToUpperInvariant(),
303 String.Join(
", ", formats));
311 if (
String.Equals(s,
"1"))
313 if (
String.Equals(s,
"2"))
316 throw new InvalidCastException(
String.Format(
"Could not convert string '{0}' of type '{1}' to BOOLEAN", s, TypeCode.ToString().ToUpperInvariant()));
329 string str = value.
Value.ToString();
339 castedValue = ToBoolean(str);
344 var num = ToNumber(str);
348 castedValue =
new SqlNumber(num.ToInt32());
354 var num = ToNumber(str);
358 castedValue =
new SqlNumber(num.ToInt64());
365 var num = ToNumber(str);
369 castedValue =
new SqlNumber(num.ToDouble());
377 castedValue = ToNumber(str);
388 castedValue = ToDate(str);
391 castedValue = ToTime(str);
394 castedValue = ToTimeStamp(str);
397 castedValue = ToDateTime(str);
403 castedValue =
new SqlBinary(Encoding.Unicode.GetBytes(str));
413 throw new InvalidCastException();
421 throw new ArgumentNullException(
"x");
425 throw new ArgumentException(
"Cannot compare objects that are not strings.");
436 return LexicographicalOrder((ISqlString)x, (ISqlString)y);
438 return Collator.Compare(x.ToString(), y.ToString());
445 long str1Size = str1.
Length;
446 long str2Size = str2.
Length;
447 if (str1Size < 32 * 1024 &&
448 str2Size < 32 * 1024) {
449 return String.Compare(str1.ToString(), str2.ToString(), StringComparison.Ordinal);
455 long size =
System.Math.Min(str1Size, str2Size);
469 if (str1Size > str2Size) {
472 }
else if (str1Size < str2Size) {
482 }
catch (IOException e) {
483 throw new Exception(
"IO Error: " + e.Message);
488 var writer =
new BinaryWriter(stream);
493 writer.Write((byte)3);
495 writer.Write((byte)1);
504 var bytes = ((
SqlString) sqlString).ToByteArray(Encoding);
505 writer.Write((byte) 2);
506 writer.Write(bytes.Length);
509 var longString = (SqlLongString) sqlString;
510 writer.Write((byte) 4);
511 writer.Write(longString.ObjectId.StoreId);
512 writer.Write(longString.ObjectId.Id);
514 throw new FormatException(
String.Format(
"The object of type '{0}' is not handled by {1}", obj.GetType(), ToString()));
519 var reader =
new BinaryReader(stream);
520 var type = reader.ReadByte();
528 var length = reader.ReadInt32();
529 var bytes = reader.ReadBytes(length);
531 var chars = Encoding.GetChars(bytes);
537 var storeId = reader.ReadInt32();
538 var objId = reader.ReadInt64();
539 var refObjId =
new ObjectId(storeId, objId);
542 throw new NotImplementedException();
545 throw new FormatException(
"Invalid type code in deserialization.");
553 var length = s.GetByteCount(Encoding);
556 return 1 + 4 + length;
563 throw new ArgumentException();
SqlBoolean IsNotLike(ISqlString value, ISqlString pattern)
bool IsNull
Gets a value that indicates if this object is materialized as null.
override bool IsComparable(SqlType type)
Verifies if a given SqlType is comparable to this data-type.
override int Compare(ISqlObject x, ISqlObject y)
static SqlNumber ToNumber(String str)
A long string in the system.
override DataObject CastTo(DataObject value, SqlType destType)
Converts the given object value to a SqlType specified.
static bool TryParseTimeStamp(string s, out SqlDateTime value)
override ISqlObject Add(ISqlObject a, ISqlObject b)
static bool TryParse(string s, out SqlNumber value)
SqlDateTime ToTime(String str)
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
static bool TryParseTime(string s, out SqlDateTime value)
override ISqlObject DeserializeObject(Stream stream)
static DataObject Null(SqlType type)
void SetValue(string key, Type type, object value)
static void AssertIsString(SqlTypeCode sqlType)
static readonly SqlLongString Null
override void SerializeObject(Stream stream, ISqlObject obj)
ISqlObject Value
Gets the underlined value that is handled.
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
bool IsNull
Gets a boolean value indicating if the object is NULL.
override Type GetRuntimeType()
static bool IsStringType(SqlTypeCode typeCode)
static readonly SqlNull Value
Defines the contract for a valid SQL Object
override int ColumnSizeOf(ISqlObject obj)
SqlDateTime ToDate(string str)
static readonly string[] DateFormatSql
override bool IsCacheable(ISqlObject value)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
override string ToString()
override bool Equals(SqlType other)
static bool TryParseDate(string s, out SqlDateTime value)
Defines the properties of a specific SQL Type and handles the values compatible.
TextReader GetInput(Encoding encoding)
string DateErrorMessage(string str, SqlTypeCode sqlType, string[] formats)
A unique identifier of an object within a database system, that is composed by a reference to the sto...
static readonly SqlNumber Null
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
SqlBoolean ToBoolean(string s)
override Type GetObjectType()
static readonly string[] TimeFormatSql
SqlBoolean IsLike(ISqlString value, ISqlString pattern)
SqlDateTime ToDateTime(string str)
SqlDateTime ToTimeStamp(String str)
override void GetData(SerializeData data)
override object ConvertTo(ISqlObject obj, Type destType)
static int LexicographicalOrder(ISqlString str1, ISqlString str2)
StringType(SqlTypeCode typeCode, int maxSize, Encoding encoding, CultureInfo locale)
static bool TryParse(string s, out SqlDateTime value)
bool HasValue(string key)
StringType(ObjectData data)
This is a static class that performs the operations to do a pattern search on a given column of a tab...
static bool FullPatternMatch(string pattern, string str, char escapeChar)
Matches a pattern against a string and returns true if it matches or false otherwise.
Deveel.Data.Sql.Objects.SqlString SqlString
override bool CanCastTo(SqlType destType)
Verifies if this type can cast any value to the given SqlType.
static readonly string[] TsFormatSql
string GetString(string key)
override bool Equals(object obj)
override int GetHashCode()