DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Variable.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;
21 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql.Variables {
24  public sealed class Variable : IDbObject, IEquatable<Variable> {
25  public Variable(VariableInfo variableInfo) {
26  if (variableInfo == null)
27  throw new ArgumentNullException("variableInfo");
28 
29  VariableInfo = variableInfo;
30  }
31 
32  public VariableInfo VariableInfo { get; private set; }
33 
34  public string Name {
35  get { return VariableInfo.VariableName; }
36  }
37 
38  public SqlType Type {
39  get { return VariableInfo.Type; }
40  }
41 
42  public SqlExpression Expression { get; private set; }
43 
44  public bool ValueFromExpression {
45  get { return Expression != null; }
46  }
47 
48  public DataObject Value { get; private set; }
49 
50  public bool IsConstant {
51  get { return VariableInfo.IsConstant; }
52  }
53 
54  public bool IsNotNull {
55  get { return VariableInfo.IsNotNull; }
56  }
57 
59  get { return new ObjectName(VariableInfo.VariableName); }
60  }
61 
63  get { return DbObjectType.Variable; }
64  }
65 
66  public bool Equals(Variable other) {
67  if (other == null)
68  return false;
69 
70  if (!Name.Equals(other.Name) ||
71  !Type.Equals(other.Type))
72  return false;
73 
74  if (ValueFromExpression &&
75  other.ValueFromExpression) {
76  // TODO: Determine if the two expressions are equal!
77  return true;
78  }
79 
80  return Value.Equals(other.Value);
81  }
82 
83  public void SetValue(IQuery context, SqlExpression expression) {
84  if (expression == null)
85  throw new ArgumentNullException("expression");
86 
87  if (IsConstant)
88  throw new InvalidOperationException();
89 
90  if (!expression.IsConstant())
91  throw new ArgumentException();
92 
93  Expression = expression;
94 
95  var exp = expression.Evaluate(context, null);
96  if (exp.ExpressionType != SqlExpressionType.Constant)
97  throw new InvalidOperationException();
98 
99  var value = ((SqlConstantExpression) exp).Value;
100  SetValue(value);
101  }
102 
103  public void SetValue(DataObject value) {
104  if (IsConstant)
105  throw new InvalidOperationException();
106 
107  if (!IsNotNull && value.IsNull)
108  throw new ArgumentException();
109 
110  if (!Type.Equals(value.Type)) {
111  if (!value.Type.CanCastTo(Type))
112  throw new ArgumentException();
113 
114  value = value.CastTo(Type);
115  }
116 
117  Value = value;
118  }
119  }
120 }
bool IsNull
Gets a value that indicates if this object is materialized as null.
Definition: DataObject.cs:91
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
DataObject CastTo(SqlType destType)
Converts this object to the given SqlType.
Definition: DataObject.cs:488
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
bool Equals(Variable other)
Definition: Variable.cs:66
ObjectName FullName
Gets the fully qualified name of the object used to resolve it uniquely within the database...
Definition: IDbObject.cs:30
Describes the name of an object within a database.
Definition: ObjectName.cs:44
Variable(VariableInfo variableInfo)
Definition: Variable.cs:25
SqlExpressionType
All the possible type of SqlExpression supported
A user-defined TYPE that holds complex objects in a database column.
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
DbObjectType ObjectType
Gets the type of database object that the implementation is for
Definition: IDbObject.cs:35
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
An expression that holds a constant value.
void SetValue(IQuery context, SqlExpression expression)
Definition: Variable.cs:83
virtual bool CanCastTo(SqlType destType)
Verifies if this type can cast any value to the given SqlType.
Definition: SqlType.cs:153
Defines the base class for instances that represent SQL expression tree nodes.
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
void SetValue(DataObject value)
Definition: Variable.cs:103