DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SystemFunctions.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 
19 using Deveel.Data;
20 using Deveel.Data.Sql;
22 using Deveel.Data.Sql.Objects;
23 using Deveel.Data.Sql.Sequences;
24 using Deveel.Data.Sql.Tables;
25 using Deveel.Data.Types;
26 
27 namespace Deveel.Data.Routines {
28  public static class SystemFunctions {
29  static SystemFunctions() {
30  Provider = new SystemFunctionsProvider();
31  }
32 
33  public static FunctionProvider Provider { get; private set; }
34 
35  public static DataObject Or(DataObject ob1, DataObject ob2) {
36  return ob1 != null ? (ob2.IsNull ? ob1 : (!ob1.IsNull ? ob1.Or(ob2) : ob2)) : ob2;
37  }
38 
39  public static DataObject User(IRequest query) {
40  return DataObject.String(query.User().Name);
41  }
42 
43 
44  public static DataObject ToDate(DataObject obj) {
45  return obj.CastTo(PrimitiveTypes.Date());
46  }
47 
48  public static DataObject ToDate(SqlString value) {
49  return ToDate(DataObject.String(value));
50  }
51 
52  public static DataObject ToDateTime(DataObject obj) {
53  return obj.CastTo(PrimitiveTypes.DateTime());
54  }
55 
56  public static DataObject ToDateTime(SqlString value) {
57  return ToDateTime(DataObject.String(value));
58  }
59 
60  public static DataObject ToTimeStamp(DataObject obj) {
61  return obj.CastTo(PrimitiveTypes.TimeStamp());
62  }
63 
64  public static DataObject ToTimeStamp(SqlString value) {
65  return ToTimeStamp(DataObject.String(value));
66  }
67 
68  public static DataObject Cast(DataObject value, SqlType destType) {
69  return value.CastTo(destType);
70  }
71 
72  public static DataObject Cast(IQuery query, DataObject value, SqlString typeString) {
73  var destType = SqlType.Parse(query.Context, typeString.ToString());
74  return Cast(value, destType);
75  }
76 
77  public static DataObject ToNumber(DataObject value) {
78  return value.CastTo(PrimitiveTypes.Numeric());
79  }
80 
81  public static DataObject ToString(DataObject value) {
82  return value.CastTo(PrimitiveTypes.String());
83  }
84 
85  public static DataObject ToBinary(DataObject value) {
86  return value.CastTo(PrimitiveTypes.Binary());
87  }
88 
89  public static DataObject UniqueKey(IRequest query, DataObject tableName) {
90  var tableNameString = (SqlString)tableName.Value;
91  var value = UniqueKey(query, tableNameString);
92  return DataObject.Number(value);
93  }
94 
95  public static SqlNumber UniqueKey(IRequest query, SqlString tableName) {
96  var tableNameString = tableName.ToString();
97  var resolvedName = query.Query.ResolveTableName(tableNameString);
98  return query.Query.GetNextValue(resolvedName);
99  }
100 
101  public static DataObject CurrentValue(IRequest query, DataObject tableName) {
102  var tableNameString = (SqlString)tableName.Value;
103  var value = CurrentValue(query, tableNameString);
104  return DataObject.Number(value);
105  }
106 
107  public static SqlNumber CurrentValue(IRequest query, SqlString tableName) {
108  var tableNameString = tableName.ToString();
109  var resolvedName = query.Query.ResolveTableName(tableNameString);
110  return query.Query.GetCurrentValue(resolvedName);
111  }
112 
113  internal static InvokeResult Iif(InvokeContext context) {
114  var result = DataObject.Null();
115 
116  var evalContext = new EvaluateContext(context.Request, context.VariableResolver, context.GroupResolver);
117 
118  var condition = context.Arguments[0].EvaluateToConstant(evalContext);
119  if (condition.Type is BooleanType) {
120  if (condition.Equals(DataObject.BooleanTrue)) {
121  result = context.Arguments[1].EvaluateToConstant(evalContext);
122  } else if (condition.Equals(DataObject.BooleanFalse)) {
123  result = context.Arguments[2].EvaluateToConstant(evalContext);
124  }
125  }
126 
127  return context.Result(result);
128  }
129 
130  internal static DataObject FRuleConvert(DataObject obj) {
131  if (obj.Type is StringType) {
132  String str = null;
133  if (!obj.IsNull) {
134  str = obj.Value.ToString();
135  }
136  int v;
137  if (str == null || str.Equals("") || str.Equals("NO ACTION")) {
138  v = ImportedKey.NoAction;
139  } else if (str.Equals("CASCADE")) {
140  v = ImportedKey.Cascade;
141  } else if (str.Equals("SET NULL")) {
142  v = ImportedKey.SetNull;
143  } else if (str.Equals("SET DEFAULT")) {
145  } else if (str.Equals("RESTRICT")) {
146  v = ImportedKey.Restrict;
147  } else {
148  throw new InvalidOperationException("Unrecognised foreign key rule: " + str);
149  }
150 
151  // Return the correct enumeration
152  return DataObject.Integer(v);
153  }
154  if (obj.Type is NumericType) {
155  var code = ((SqlNumber)obj.AsBigInt().Value).ToInt32();
156  string v;
157  if (code == (int)ForeignKeyAction.Cascade) {
158  v = "CASCADE";
159  } else if (code == (int)ForeignKeyAction.NoAction) {
160  v = "NO ACTION";
161  } else if (code == (int)ForeignKeyAction.SetDefault) {
162  v = "SET DEFAULT";
163  } else if (code == (int)ForeignKeyAction.SetNull) {
164  v = "SET NULL";
165  } else {
166  throw new InvalidOperationException("Unrecognised foreign key rule: " + code);
167  }
168 
169  return DataObject.String(v);
170  }
171 
172  throw new InvalidOperationException("Unsupported type in function argument");
173  }
174  }
175 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static readonly DataObject BooleanFalse
The representation of a BOOLEAN false as DataObject
Definition: DataObject.cs:44
bool IsNull
Gets a value that indicates if this object is materialized as null.
Definition: DataObject.cs:91
static DataObject Integer(int value)
Definition: DataObject.cs:576
static DataObject ToDate(SqlString value)
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
A long string in the system.
static SqlNumber CurrentValue(IRequest query, SqlString tableName)
static DataObject ToDate(DataObject obj)
DataObject CastTo(SqlType destType)
Converts this object to the given SqlType.
Definition: DataObject.cs:488
static DataObject Cast(IQuery query, DataObject value, SqlString typeString)
static InvokeResult Iif(InvokeContext context)
static DataObject Number(SqlNumber value)
Definition: DataObject.cs:552
static DataObject Null(SqlType type)
Definition: DataObject.cs:630
static DataObject CurrentValue(IRequest query, DataObject tableName)
SqlExpression[] Arguments
Gets the array of expressions forming the arguments of the execution.
static BinaryType Binary(int maxSize)
static DataObject UniqueKey(IRequest query, DataObject tableName)
static DataObject Or(DataObject ob1, DataObject ob2)
static SqlNumber UniqueKey(IRequest query, SqlString tableName)
static readonly DataObject BooleanTrue
The representation of a BOOLEAN true as DataObject
Definition: DataObject.cs:39
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject ToBinary(DataObject value)
DataObject AsBigInt()
Definition: DataObject.cs:524
static DataObject ToString(DataObject value)
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
static DataObject ToNumber(DataObject value)
static DataObject Cast(DataObject value, SqlType destType)
static NumericType Numeric()
static DataObject ToTimeStamp(SqlString value)
Represents the result of the execution of a routine.
Definition: InvokeResult.cs:25
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
static DataObject ToTimeStamp(DataObject obj)
static DataObject User(IRequest query)
static DataObject ToDateTime(SqlString value)
string ToString(Encoding encoding)
Definition: SqlString.cs:182
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
InvokeResult Result(DataObject value)
Encapsulates the elements needed to evaluate an SqlExpression
static DataObject FRuleConvert(DataObject obj)
static DataObject ToDateTime(DataObject obj)
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27
new IQueryContext Context
Definition: IQuery.cs:21
DataObject Or(DataObject other)
Definition: DataObject.cs:428
static SqlType Parse(string s)
Parses a SQL formatted string that defines a data-type into a constructed SqlType object equivalent...
Definition: SqlType.cs:321