DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
PrimitiveTypes.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Globalization;
19 using System.Text;
20 
21 using Deveel.Data.Sql;
22 
23 namespace Deveel.Data.Types {
29  public static class PrimitiveTypes {
30  public static BooleanType Boolean() {
31  return Boolean(SqlTypeCode.Boolean);
32  }
33 
34  public static BooleanType Boolean(SqlTypeCode sqlType) {
35  return new BooleanType(sqlType);
36  }
37 
38  public static BooleanType Bit() {
39  return Boolean(SqlTypeCode.Bit);
40  }
41 
42  public static StringType String() {
43  return String(SqlTypeCode.String);
44  }
45 
46  public static StringType String(SqlTypeCode sqlType) {
47  return String(sqlType, StringType.DefaultMaxSize);
48  }
49 
50  public static StringType String(SqlTypeCode sqlType, int maxSize) {
51  return String(sqlType, maxSize, (CultureInfo) null);
52  }
53 
54  public static StringType String(SqlTypeCode sqlType, CultureInfo locale) {
55  return String(sqlType, StringType.DefaultMaxSize, locale);
56  }
57 
58  public static StringType String(SqlTypeCode sqlType, int maxSize, CultureInfo locale) {
59  return String(sqlType, maxSize, Encoding.Unicode, locale);
60  }
61 
62  public static StringType String(SqlTypeCode sqlType, Encoding encoding) {
63  return String(sqlType, StringType.DefaultMaxSize, encoding);
64  }
65 
66  public static StringType String(SqlTypeCode sqlType, int maxSize, Encoding encoding) {
67  return String(sqlType, maxSize, encoding, null);
68  }
69 
70  public static StringType String(SqlTypeCode sqlType, Encoding encoding, CultureInfo locale) {
71  return String(sqlType, StringType.DefaultMaxSize, encoding, locale);
72  }
73 
74  public static StringType String(SqlTypeCode sqlType, int maxSize, Encoding encoding, CultureInfo locale) {
75  return new StringType(sqlType, maxSize, encoding, locale);
76  }
77 
78  public static NumericType Numeric() {
79  return Numeric(-1);
80  }
81 
82  public static NumericType Numeric(int size) {
83  return Numeric(size, 0);
84  }
85 
86  public static NumericType Numeric(int size, byte scale) {
87  return Numeric(SqlTypeCode.Numeric, size, scale);
88  }
89 
90  public static NumericType Numeric(SqlTypeCode sqlType) {
91  return Numeric(sqlType, -1);
92  }
93 
94  public static NumericType Numeric(SqlTypeCode sqlType, int size) {
95  return Numeric(sqlType, size, 0);
96  }
97 
98  public static NumericType Numeric(SqlTypeCode sqlType, int size, byte scale) {
99  return new NumericType(sqlType, size, scale);
100  }
101 
102  public static NumericType TinyInt(int size) {
103  return Numeric(SqlTypeCode.TinyInt, size);
104  }
105 
106  public static NumericType TinyInt() {
107  return TinyInt(-1);
108  }
109 
110  public static NumericType Integer() {
111  return Integer(-1);
112  }
113 
114  public static NumericType Integer(int size) {
115  return Numeric(SqlTypeCode.Integer, size);
116  }
117 
118  public static SqlType BigInt() {
119  return BigInt(-1);
120  }
121 
122  public static SqlType BigInt(int size) {
123  return Numeric(SqlTypeCode.BigInt);
124  }
125 
126  public static NullType Null() {
127  return Null(SqlTypeCode.Null);
128  }
129 
130  public static NullType Null(SqlTypeCode sqlType) {
131  return new NullType(sqlType);
132  }
133 
134  public static DateType DateTime() {
135  return DateTime(SqlTypeCode.DateTime);
136  }
137 
138  public static DateType DateTime(SqlTypeCode sqlType) {
139  return new DateType(sqlType);
140  }
141 
142  public static DateType Date() {
143  return DateTime(SqlTypeCode.Date);
144  }
145 
146  public static DateType TimeStamp() {
147  return DateTime(SqlTypeCode.TimeStamp);
148  }
149 
150  public static DateType Time() {
151  return DateTime(SqlTypeCode.Time);
152  }
153 
154  public static RowType RowType(ObjectName tableName) {
155  return new RowType(tableName);
156  }
157 
158  public static ColumnType ColumnType(ObjectName columnName) {
159  return new ColumnType(columnName);
160  }
161 
162  public static BinaryType Binary(int maxSize) {
163  return Binary(SqlTypeCode.Binary, maxSize);
164  }
165 
166  public static BinaryType Binary() {
167  return Binary(SqlTypeCode.Binary);
168  }
169 
170  public static BinaryType Binary(SqlTypeCode sqlType) {
171  return Binary(sqlType, -1);
172  }
173 
174  public static BinaryType Binary(SqlTypeCode sqlType, int maxSize) {
175  return new BinaryType(sqlType, maxSize);
176  }
177 
178  public static IntervalType Interval(SqlTypeCode sqlType) {
179  return new IntervalType(sqlType);
180  }
181 
182  public static bool IsPrimitive(SqlTypeCode sqlType) {
183  if (sqlType == SqlTypeCode.Unknown ||
184  sqlType == SqlTypeCode.Type ||
185  sqlType == SqlTypeCode.QueryPlan ||
186  sqlType == SqlTypeCode.Object)
187  return false;
188 
189  return true;
190  }
191 
192  public static bool IsPrimitive(string name) {
193  if (System.String.IsNullOrEmpty(name))
194  return false;
195 
196  if (System.String.Equals("long varchar", name, StringComparison.OrdinalIgnoreCase))
197  name = "longvarchar";
198  if (System.String.Equals("long varbinary", name, StringComparison.OrdinalIgnoreCase))
199  name = "longvarbinary";
200 
201  if (name.EndsWith("%TYPE", StringComparison.OrdinalIgnoreCase) ||
202  name.EndsWith("%ROWTYPE", StringComparison.OrdinalIgnoreCase))
203  return true;
204 
205  if (name.Equals("NUMERIC", StringComparison.OrdinalIgnoreCase) ||
206  name.Equals("STRING", StringComparison.OrdinalIgnoreCase) ||
207  name.Equals("DATE", StringComparison.OrdinalIgnoreCase) ||
208  name.Equals("NULL", StringComparison.OrdinalIgnoreCase) ||
209  name.Equals("BOOLEAN", StringComparison.OrdinalIgnoreCase) ||
210  name.Equals("BINARY", StringComparison.OrdinalIgnoreCase))
211  return true;
212 
213  if (name.Equals("BIT", StringComparison.OrdinalIgnoreCase) ||
214  name.Equals("BOOLEAN", StringComparison.OrdinalIgnoreCase))
215  return true;
216 
217  if (name.Equals("TINYINT", StringComparison.OrdinalIgnoreCase) ||
218  name.Equals("SMALLINT", StringComparison.OrdinalIgnoreCase) ||
219  name.Equals("INTEGER", StringComparison.OrdinalIgnoreCase) ||
220  name.Equals("INT", StringComparison.OrdinalIgnoreCase) ||
221  name.Equals("BIGINT", StringComparison.OrdinalIgnoreCase) ||
222  name.Equals("REAL", StringComparison.OrdinalIgnoreCase) ||
223  name.Equals("FLOAT", StringComparison.OrdinalIgnoreCase) ||
224  name.Equals("DOUBLE", StringComparison.OrdinalIgnoreCase) ||
225  name.Equals("DECIMAL", StringComparison.OrdinalIgnoreCase))
226  return true;
227 
228  if (name.Equals("DATE", StringComparison.OrdinalIgnoreCase) ||
229  name.Equals("TIME", StringComparison.OrdinalIgnoreCase) ||
230  name.Equals("TIMESTAMP", StringComparison.OrdinalIgnoreCase))
231  return true;
232 
233  if (name.Equals("YEAR TO MONTH", StringComparison.OrdinalIgnoreCase) ||
234  name.Equals("DAY TO SECOND", StringComparison.OrdinalIgnoreCase) ||
235  name.Equals("INTERVAL", StringComparison.OrdinalIgnoreCase))
236  return true;
237 
238  if (name.Equals("CHAR", StringComparison.OrdinalIgnoreCase) ||
239  name.Equals("VARCHAR", StringComparison.OrdinalIgnoreCase) ||
240  name.Equals("LONGVARCHAR", StringComparison.OrdinalIgnoreCase) ||
241  name.Equals("CLOB", StringComparison.OrdinalIgnoreCase))
242  return true;
243 
244  if (name.Equals("BINARY", StringComparison.OrdinalIgnoreCase) ||
245  name.Equals("VARBINARY", StringComparison.OrdinalIgnoreCase) ||
246  name.Equals("LONGVARBINARY", StringComparison.OrdinalIgnoreCase) ||
247  name.Equals("BLOB", StringComparison.OrdinalIgnoreCase))
248  return true;
249 
250  return false;
251  }
252 
253  public static SqlType Resolve(string typeName, params DataTypeMeta[] args) {
254  if (!IsPrimitive(typeName))
255  return Null();
256 
257  if (System.String.Equals("long varchar", typeName, StringComparison.OrdinalIgnoreCase))
258  typeName = "longvarchar";
259  if (System.String.Equals("long varbinary", typeName, StringComparison.OrdinalIgnoreCase))
260  typeName = "longvarbinary";
261 
262  SqlTypeCode typeCode;
263 
264  try {
265  typeCode = (SqlTypeCode) Enum.Parse(typeof (SqlTypeCode), typeName, true);
266  } catch (Exception) {
267  throw new ArgumentException(System.String.Format("The name {0} is not a valid SQL type.", typeName));
268  }
269 
270  return Resolve(typeCode, typeName, args);
271  }
272 
273  public static SqlTypeCode ResolveTypeCode(string typeName) {
274  if (System.String.Equals("long varchar", typeName, StringComparison.OrdinalIgnoreCase))
275  typeName = "longvarchar";
276  if (System.String.Equals("long varbinary", typeName, StringComparison.OrdinalIgnoreCase))
277  typeName = "longvarbinary";
278 
279  SqlTypeCode typeCode;
280 
281  try {
282  typeCode = (SqlTypeCode)Enum.Parse(typeof(SqlTypeCode), typeName, true);
283  } catch (Exception) {
284  throw new ArgumentException(System.String.Format("The name {0} is not a valid SQL type.", typeName));
285  }
286 
287  if (!IsPrimitive(typeCode))
288  return SqlTypeCode.Unknown;
289 
290  return typeCode;
291  }
292 
293  public static SqlType Resolve(SqlTypeCode sqlType, string typeName, params DataTypeMeta[] args) {
294  return Resolve(new TypeResolveContext(sqlType, typeName, args));
295  }
296 
297  public static SqlType Resolve(TypeResolveContext context) {
298  if (!context.IsPrimitive)
299  return null;
300 
301  var sqlType = context.TypeCode;
302 
303  if (sqlType == SqlTypeCode.BigInt ||
304  sqlType == SqlTypeCode.Boolean)
305  return Boolean(sqlType);
306 
307  if (sqlType == SqlTypeCode.Numeric ||
308  sqlType == SqlTypeCode.TinyInt ||
309  sqlType == SqlTypeCode.SmallInt ||
310  sqlType == SqlTypeCode.Integer ||
311  sqlType == SqlTypeCode.BigInt ||
312  sqlType == SqlTypeCode.Real ||
313  sqlType == SqlTypeCode.Double ||
314  sqlType == SqlTypeCode.Float ||
315  sqlType == SqlTypeCode.Decimal) {
316  if (!context.HasAnyMeta)
317  return Numeric(sqlType);
318 
319  var precisionMeta = context.GetMeta("Precision");
320  var scaleMeta = context.GetMeta("Scale");
321 
322  if (precisionMeta == null)
323  return Numeric(sqlType);
324 
325  if (scaleMeta == null)
326  return Numeric(sqlType, precisionMeta.ToInt32());
327 
328  return Numeric(sqlType, precisionMeta.ToInt32(), (byte) scaleMeta.ToInt32());
329  }
330 
331  if (sqlType == SqlTypeCode.Char ||
332  sqlType == SqlTypeCode.VarChar ||
333  sqlType == SqlTypeCode.LongVarChar ||
334  sqlType == SqlTypeCode.String ||
335  sqlType == SqlTypeCode.Clob) {
336  if (!context.HasAnyMeta)
337  return String(sqlType);
338 
339  var maxSizeMeta = context.GetMeta("MaxSize");
340  var localeMeta = context.GetMeta("Locale");
341  var encodingMeta = context.GetMeta("Encoding");
342 
343  int maxSize = StringType.DefaultMaxSize;
344  CultureInfo locale = null;
345  var encoding = Encoding.Unicode;
346 
347  if (maxSizeMeta != null)
348  maxSize = maxSizeMeta.ToInt32();
349  if (localeMeta != null)
350  locale = new CultureInfo(localeMeta.Value);
351  if (encodingMeta != null)
352  encoding = Encoding.GetEncoding(encodingMeta.Value);
353 
354  return new StringType(sqlType, maxSize, encoding, locale);
355  }
356 
357  if (sqlType == SqlTypeCode.Binary ||
358  sqlType == SqlTypeCode.VarBinary ||
359  sqlType == SqlTypeCode.LongVarBinary ||
360  sqlType == SqlTypeCode.Blob) {
361  if (!context.HasAnyMeta)
362  return Binary(sqlType);
363 
364  var maxSize = BinaryType.DefaultMaxSize;
365  var maxSizeMeta = context.GetMeta("MaxSize");
366  if (maxSizeMeta != null)
367  maxSize = maxSizeMeta.ToInt32();
368 
369  return Binary(sqlType, maxSize);
370  }
371 
372  if (sqlType == SqlTypeCode.Date ||
373  sqlType == SqlTypeCode.Time ||
374  sqlType == SqlTypeCode.TimeStamp ||
375  sqlType == SqlTypeCode.DateTime)
376  return DateTime(sqlType);
377 
378  if (sqlType == SqlTypeCode.Null)
379  return Null(sqlType);
380 
381  if (sqlType == SqlTypeCode.RowType) {
382  if (!context.HasAnyMeta)
383  throw new ArgumentException("Invalid number of arguments for %ROWTYPE type");
384 
385  var tableNameMeta = context.GetMeta("TableName");
386  if (tableNameMeta == null)
387  throw new ArgumentException();
388 
389  var tableName = ObjectName.Parse(tableNameMeta.Value);
390  return RowType(tableName);
391  }
392 
393  if (sqlType == SqlTypeCode.ColumnType) {
394  if (!context.HasAnyMeta)
395  throw new ArgumentException("Invalid number of arguments for %TYPE type");
396 
397  var columnNameMeta = context.GetMeta("ColumnName");
398  if (columnNameMeta == null)
399  throw new ArgumentException();
400 
401  var columnName = ObjectName.Parse(columnNameMeta.Value);
402  return ColumnType(columnName);
403  }
404 
405  throw new ArgumentException(System.String.Format("The SQL type {0} is not primitive.", sqlType));
406  }
407 
408  public static SqlType FromType(Type type) {
409  if (type == typeof(bool))
410  return Boolean();
411  if (type == typeof(string))
412  return String();
413  if (type == typeof (byte))
414  return Bit();
415  if (type == typeof (short))
416  return TinyInt();
417 
418  throw new NotSupportedException();
419  }
420  }
421 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static StringType String(SqlTypeCode sqlType)
static StringType String(SqlTypeCode sqlType, Encoding encoding)
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
static NumericType Numeric(SqlTypeCode sqlType, int size)
static bool IsPrimitive(string name)
A long string in the system.
static SqlType Resolve(string typeName, params DataTypeMeta[] args)
static SqlType Resolve(TypeResolveContext context)
static BooleanType Boolean()
static DateType DateTime(SqlTypeCode sqlType)
A 8-bytes long integer type.
static BooleanType Boolean(SqlTypeCode sqlType)
static BinaryType Binary(int maxSize)
static StringType String(SqlTypeCode sqlType, CultureInfo locale)
An integer value that can span from 0 to 255.
static ColumnType ColumnType(ObjectName columnName)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
A generic numeric type. This is the main type used by the system to represent numbers.
static NumericType Numeric(int size, byte scale)
static NumericType Numeric(int size)
static StringType String(SqlTypeCode sqlType, int maxSize, Encoding encoding)
static StringType String(SqlTypeCode sqlType, int maxSize)
static StringType String(SqlTypeCode sqlType, int maxSize, CultureInfo locale)
static NumericType Numeric()
static IntervalType Interval(SqlTypeCode sqlType)
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
static RowType RowType(ObjectName tableName)
static NumericType Numeric(SqlTypeCode sqlType)
A single byte that can have only 1 and 0 as values.
static SqlTypeCode ResolveTypeCode(string typeName)
static BinaryType Binary(SqlTypeCode sqlType)
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
static SqlType BigInt(int size)
static StringType String(SqlTypeCode sqlType, Encoding encoding, CultureInfo locale)
static NumericType TinyInt(int size)
static SqlType Resolve(SqlTypeCode sqlType, string typeName, params DataTypeMeta[] args)
A data type that represents the NULL value of a given SQL data type.
Definition: NullType.cs:29
static bool IsPrimitive(SqlTypeCode sqlType)
static NullType Null(SqlTypeCode sqlType)
static BinaryType Binary(SqlTypeCode sqlType, int maxSize)
static StringType String(SqlTypeCode sqlType, int maxSize, Encoding encoding, CultureInfo locale)
static SqlType FromType(Type type)
static NumericType Integer(int size)
static NumericType Numeric(SqlTypeCode sqlType, int size, byte scale)