DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DataTypeBuilder.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.Collections.Generic;
19 using System.Linq;
20 
21 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql.Parser {
24  static class DataTypeBuilder {
25  public static SqlType Build(ITypeResolver resolver, ISqlNode sqlNode) {
26  var node = sqlNode as DataTypeNode;
27  if (node == null)
28  throw new ArgumentException();
29 
30  var typeName = node.TypeName;
31  var typeMeta = new List<DataTypeMeta>();
32  SqlTypeCode sqlTypeCode;
33 
34  if (node.IsPrimitive) {
35  if (String.Equals(typeName, "LONG VARCHAR")) {
36  sqlTypeCode = SqlTypeCode.LongVarChar;
37  } else if (String.Equals(node.TypeName, "LONG VARBINARY")) {
38  sqlTypeCode = SqlTypeCode.LongVarBinary;
39  } else if (String.Equals(typeName, "INT", StringComparison.OrdinalIgnoreCase)) {
40  sqlTypeCode = SqlTypeCode.Integer;
41  } else {
42  try {
43  sqlTypeCode = (SqlTypeCode) Enum.Parse(typeof (SqlTypeCode), node.TypeName, true);
44  } catch (Exception) {
45  sqlTypeCode = SqlTypeCode.Unknown;
46  }
47  }
48 
49  if (sqlTypeCode == SqlTypeCode.Float ||
50  sqlTypeCode == SqlTypeCode.Real ||
51  sqlTypeCode == SqlTypeCode.Double ||
52  sqlTypeCode == SqlTypeCode.Decimal ||
53  sqlTypeCode == SqlTypeCode.Numeric) {
54  if (node.HasScale)
55  typeMeta.Add(new DataTypeMeta("Scale", node.Scale.ToString()));
56  if (node.HasPrecision)
57  typeMeta.Add(new DataTypeMeta("Precision", node.Precision.ToString()));
58  } else if (sqlTypeCode == SqlTypeCode.Char ||
59  sqlTypeCode == SqlTypeCode.VarChar ||
60  sqlTypeCode == SqlTypeCode.LongVarChar) {
61  if (node.HasSize)
62  typeMeta.Add(new DataTypeMeta("MaxSize", node.Size.ToString()));
63  if (node.HasLocale)
64  typeMeta.Add(new DataTypeMeta("Locale", node.Locale));
65  if (node.HasEncoding)
66  typeMeta.Add(new DataTypeMeta("Encoding", node.Encoding));
67  } else if (sqlTypeCode == SqlTypeCode.Binary ||
68  sqlTypeCode == SqlTypeCode.VarBinary ||
69  sqlTypeCode == SqlTypeCode.LongVarBinary) {
70  if (node.HasSize)
71  typeMeta.Add(new DataTypeMeta("MaxSize", node.Size.ToString()));
72  }
73 
74  // TODO: Support %ROWTYPE and %TYPE
75  } else {
76  sqlTypeCode = SqlTypeCode.Type;
77  }
78 
79  if (String.IsNullOrEmpty(typeName))
80  throw new SqlParseException("Could not determine type name.");
81 
82  DataTypeMeta[] meta = typeMeta.ToArray();
83  if (!node.IsPrimitive && node.Metadata != null)
84  meta = BuildTypeMeta(node.Metadata);
85 
86  var type = TypeResolver.Resolve(sqlTypeCode, typeName, meta, resolver);
87 
88  if (type == null)
89  throw new SqlParseException(String.Format("User defined type {0} could not be resolved.", typeName));
90 
91  return type;
92  }
93 
94  private static DataTypeMeta[] BuildTypeMeta(Dictionary<string, string> metadata) {
95  return metadata.Select(x => new DataTypeMeta(x.Key, x.Value)).ToArray();
96  }
97  }
98 }
A long string in the system.
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
static SqlType Resolve(SqlTypeCode typeCode, string typeName, DataTypeMeta[] metadata, ITypeResolver resolver)
Definition: TypeResolver.cs:21
static DataTypeMeta[] BuildTypeMeta(Dictionary< string, string > metadata)
static SqlType Build(ITypeResolver resolver, ISqlNode sqlNode)
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
Describes the information of a data type as found in a SQL string.
Definition: DataTypeNode.cs:28
An error that occurs when compiling a input string into a SQL object.