DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DataTypeNode.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.Sql.Objects;
22 using Deveel.Data.Types;
23 
24 namespace Deveel.Data.Sql.Parser {
32  internal DataTypeNode() {
33  IsPrimitive = true;
34  Metadata = new Dictionary<string, string>();
35  }
36 
42  public bool IsPrimitive { get; private set; }
43 
47  public string TypeName { get; private set; }
48 
56  public int Size { get; private set; }
57 
63  public bool HasSize { get; private set; }
64 
74  public int Scale { get; private set; }
75 
81  public bool HasScale { get; private set; }
82 
88  public int Precision { get; private set; }
89 
94  public bool HasPrecision { get; private set; }
95 
102  public string Locale { get; private set; }
103 
109  public bool HasLocale { get; private set; }
110 
111  public string Encoding { get; private set; }
112 
113  public bool HasEncoding { get; private set; }
114 
115  public Dictionary<string, string> Metadata { get; private set; }
116 
117  protected override ISqlNode OnChildNode(ISqlNode node) {
118  if (node.NodeName == "identity_type") {
119  TypeName = "IDENTITY";
120  } else if (node.NodeName == "boolean_type") {
121  GetBooleanType(node);
122  } else if (node.NodeName == "decimal_type") {
123  GetNumberType(node);
124  } else if (node.NodeName == "character_type" ||
125  node.NodeName == "binary_type") {
126  GetSizedType(node);
127  } else if (node.NodeName == "date_type" ||
128  node.NodeName == "integer_type" ||
129  node.NodeName == "float_type") {
130  GetSimpleType(node);
131  } else if (node.NodeName == "interval_type") {
132  } else if (node.NodeName == "user_type") {
133  GetUserType(node);
134  } else if (node.NodeName == "row_type") {
135 
136  }
137 
138  return base.OnChildNode(node);
139  }
140 
141  private void GetBooleanType(ISqlNode node) {
142  IsPrimitive = true;
143  TypeName = "BOOLEAN";
144  }
145 
146  private void GetUserType(ISqlNode node) {
147  IsPrimitive = false;
148  TypeName = node.FindNode<ObjectNameNode>().Name;
149  var optMetaList = node.FindByName("user_type_meta_opt");
150  if (optMetaList != null) {
151  GetMeta(optMetaList.ChildNodes.FirstOrDefault());
152  }
153  }
154 
155  private void GetMeta(ISqlNode node) {
156  if (node == null)
157  return;
158 
159  var metas = node.FindNodes<DataTypeMetaNode>();
160  foreach (var meta in metas) {
161  Metadata[meta.Name] = meta.Value;
162  }
163  }
164 
165  private void GetSimpleType(ISqlNode node) {
166  var type = node.ChildNodes.First();
167  TypeName = type.NodeName;
168  }
169 
170  private void GetSizedType(ISqlNode node) {
171  foreach (var childNode in node.ChildNodes) {
172  if (childNode.NodeName == "long_varchar") {
173  TypeName = "LONG VARCHAR";
174  } else if (childNode is SqlKeyNode) {
175  TypeName = ((SqlKeyNode) childNode).Text;
176  } else if (childNode.NodeName == "datatype_size") {
177  GetDataSize(childNode);
178  } else if (childNode.NodeName == "locale_opt") {
179  GetLocale(childNode);
180  } else if (childNode.NodeName == "encoding_opt") {
181  GetEncoding(childNode);
182  }
183  }
184  }
185 
186  private void GetLocale(ISqlNode node) {
187  foreach (var childNode in node.ChildNodes) {
188  if (childNode is StringLiteralNode) {
189  Locale = ((StringLiteralNode) childNode).Value;
190  HasLocale = true;
191  }
192  }
193  }
194 
195  private void GetEncoding(ISqlNode node) {
196  foreach (var childNode in node.ChildNodes) {
197  if (childNode is StringLiteralNode) {
198  Encoding = ((StringLiteralNode) childNode).Value;
199  HasEncoding = true;
200  }
201  }
202  }
203 
204  private void GetDataSize(ISqlNode node) {
205  foreach (var childNode in node.ChildNodes) {
206  if (childNode is IntegerLiteralNode) {
207  Size = (int) ((IntegerLiteralNode) childNode).Value;
208  HasSize = true;
209  }
210  }
211  }
212 
213  private void GetNumberType(ISqlNode node) {
214  foreach (var childNode in node.ChildNodes) {
215  if (childNode is SqlKeyNode) {
216  TypeName = ((SqlKeyNode) childNode).Text;
217  } else if (childNode.NodeName == "number_precision") {
218  GetNumberPrecision(childNode);
219  }
220  }
221 
222  }
223 
224  private void GetNumberPrecision(ISqlNode node) {
225  foreach (var childNode in node.ChildNodes) {
226  if (!HasScale) {
227  Scale = (int) ((IntegerLiteralNode) childNode).Value;
228  HasScale = true;
229  } else {
230  Precision = (int) ((IntegerLiteralNode) childNode).Value;
231  HasPrecision = true;
232  }
233  }
234  }
235  }
236 }
override ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
Represents a keyword found during the compilation of a source text.
Definition: SqlKeyNode.cs:25
void GetBooleanType(ISqlNode node)
Represents a composed name for an object within the system.
string NodeName
Gets the name of the node analyzed from the parser.
Definition: ISqlNode.cs:29
A node containing a constant literal string passed within an SQL command.
IEnumerable< ISqlNode > ChildNodes
Gets a read-only enumeration of the children nodes, if any.
Definition: ISqlNode.cs:39
DataTypeNode()
Constructs an empty DataTypeNode.
Definition: DataTypeNode.cs:32
void GetNumberPrecision(ISqlNode node)
Describes the information of a data type as found in a SQL string.
Definition: DataTypeNode.cs:28
The default implementation of ISqlNode, that is a node in the text analysis parsing of SQL commands...
Definition: SqlNode.cs:32
Encapsulates a number that is any falling in the group of integers.