DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Expressions.ExpressionStringBuilder Class Reference
Inheritance diagram for Deveel.Data.Sql.Expressions.ExpressionStringBuilder:
Deveel.Data.Sql.Expressions.SqlExpressionVisitor

Public Member Functions

 ExpressionStringBuilder ()
 
string ToSqlString (SqlExpression expression)
 
override SqlExpression VisitAssign (SqlAssignExpression assign)
 
override SqlExpression VisitBinary (SqlBinaryExpression binaryEpression)
 
override SqlExpression VisitCast (SqlCastExpression castExpression)
 
override SqlExpression VisitConditional (SqlConditionalExpression conditional)
 
override SqlExpression VisitConstant (SqlConstantExpression constant)
 
override SqlExpression VisitFunctionCall (SqlFunctionCallExpression expression)
 Visits the expression that calls the function defined. More...
 
override SqlExpression VisitQuery (SqlQueryExpression query)
 
override SqlExpression VisitReference (SqlReferenceExpression reference)
 
override SqlExpression VisitTuple (SqlTupleExpression expression)
 
override SqlExpression VisitUnary (SqlUnaryExpression unary)
 
override SqlExpression VisitVariableReference (SqlVariableReferenceExpression reference)
 
- Public Member Functions inherited from Deveel.Data.Sql.Expressions.SqlExpressionVisitor
virtual SqlExpression Visit (SqlExpression expression)
 Visits a given SQL expression. More...
 
virtual SqlExpression[] VisitExpressionList (SqlExpression[] list)
 Visits a list of expressions given. More...
 

Private Member Functions

void PrintQueryColumns (IEnumerable< SelectColumn > selectColumns)
 
void PrintFromClause (FromClause fromClause)
 
string GetUnaryOperatorString (SqlExpressionType unaryType)
 

Static Private Member Functions

static string GetBinaryOperatorString (SqlExpressionType expressionType)
 

Private Attributes

readonly StringBuilder builder
 
bool rootQuery = true
 

Detailed Description

Definition at line 26 of file ExpressionStringBuilder.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Expressions.ExpressionStringBuilder.ExpressionStringBuilder ( )
inline

Definition at line 30 of file ExpressionStringBuilder.cs.

30  {
31  builder = new StringBuilder();
32  }

Member Function Documentation

static string Deveel.Data.Sql.Expressions.ExpressionStringBuilder.GetBinaryOperatorString ( SqlExpressionType  expressionType)
inlinestaticprivate

Definition at line 60 of file ExpressionStringBuilder.cs.

60  {
61  switch (expressionType) {
62  case SqlExpressionType.Add:
63  return "+";
64  case SqlExpressionType.Subtract:
65  return "-";
66  case SqlExpressionType.Divide:
67  return "/";
68  case SqlExpressionType.Multiply:
69  return "*";
70  case SqlExpressionType.Modulo:
71  return "%";
72  case SqlExpressionType.Equal:
73  return "=";
74  case SqlExpressionType.NotEqual:
75  return "<>";
76  case SqlExpressionType.GreaterThan:
77  return ">";
78  case SqlExpressionType.GreaterOrEqualThan:
79  return ">=";
80  case SqlExpressionType.SmallerThan:
81  return "<";
82  case SqlExpressionType.SmallerOrEqualThan:
83  return "<=";
84  case SqlExpressionType.Is:
85  return "IS";
86  case SqlExpressionType.IsNot:
87  return "IS NOT";
88  case SqlExpressionType.Like:
89  return "LIKE";
90  case SqlExpressionType.NotLike:
91  return "NOT LIKE";
92  case SqlExpressionType.AllEqual:
93  return "= ALL";
94  case SqlExpressionType.AllNotEqual:
95  return "<> ALL";
96  case SqlExpressionType.AllGreaterThan:
97  return "> ALL";
98  case SqlExpressionType.AllGreaterOrEqualThan:
99  return ">= ALL";
100  case SqlExpressionType.AllSmallerThan:
101  return "< ALL";
102  case SqlExpressionType.AllSmallerOrEqualThan:
103  return "<= ALL";
104  case SqlExpressionType.AnyEqual:
105  return "= ANY";
106  case SqlExpressionType.AnyNotEqual:
107  return "<> ANY";
108  case SqlExpressionType.AnyGreaterThan:
109  return "> ANY";
110  case SqlExpressionType.AnyGreaterOrEqualThan:
111  return ">= ANY";
112  case SqlExpressionType.AnySmallerThan:
113  return "< ANY";
114  case SqlExpressionType.AnySmallerOrEqualThan:
115  return "<= ANY";
116  case SqlExpressionType.Or:
117  return "OR";
118  case SqlExpressionType.And:
119  return "AND";
120  case SqlExpressionType.XOr:
121  return "XOR";
122  default:
123  throw new NotSupportedException();
124  }
125  }
SqlExpressionType
All the possible type of SqlExpression supported
string Deveel.Data.Sql.Expressions.ExpressionStringBuilder.GetUnaryOperatorString ( SqlExpressionType  unaryType)
inlineprivate

Definition at line 339 of file ExpressionStringBuilder.cs.

339  {
340  switch (unaryType) {
341  case SqlExpressionType.UnaryPlus:
342  return "+";
343  case SqlExpressionType.Negate:
344  return "-";
345  case SqlExpressionType.Not:
346  return "NOT";
347  default:
348  throw new NotSupportedException();
349  }
350  }
SqlExpressionType
All the possible type of SqlExpression supported
void Deveel.Data.Sql.Expressions.ExpressionStringBuilder.PrintFromClause ( FromClause  fromClause)
inlineprivate

Definition at line 260 of file ExpressionStringBuilder.cs.

260  {
261  if (fromClause == null || fromClause.IsEmpty)
262  return;
263 
264  builder.Append("FROM ");
265 
266  var tables = fromClause.AllTables.ToList();
267  for (int i = 0; i < tables.Count; i++) {
268  var source = tables[i];
269 
270  JoinPart joinPart = null;
271 
272  if (i > 0) {
273  joinPart = fromClause.GetJoinPart(i - 1);
274  if (joinPart != null) {
275  if (joinPart.JoinType == JoinType.Inner) {
276  builder.Append(" INNER JOIN ");
277  } else if (joinPart.JoinType == JoinType.Right) {
278  builder.Append(" RIGHT OUTER JOIN ");
279  } else if (joinPart.JoinType == JoinType.Left) {
280  builder.Append(" LEFT OUTER JOIN ");
281  } else if (joinPart.JoinType == JoinType.Full) {
282  builder.Append(" FULL OUTER JOINT ");
283  }
284  }
285  }
286 
287  if (source.IsSubQuery) {
288  builder.Append("(");
289  Visit(source.SubQuery);
290  builder.Append(")");
291  } else {
292  builder.Append(source.Name);
293  }
294 
295  if (!String.IsNullOrEmpty(source.Alias)) {
296  builder.Append(" AS ");
297  builder.Append(source.Alias);
298  }
299 
300  if (i < tables.Count - 1) {
301  if (joinPart == null) {
302  builder.Append(", ");
303  } else {
304  builder.Append(" ON ");
305  Visit(joinPart.OnExpression);
306  }
307  }
308  }
309  }
A long string in the system.
JoinType
Enumerates the kind of group join in a selection query.
Definition: JoinType.cs:23
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
void Deveel.Data.Sql.Expressions.ExpressionStringBuilder.PrintQueryColumns ( IEnumerable< SelectColumn selectColumns)
inlineprivate

Definition at line 195 of file ExpressionStringBuilder.cs.

195  {
196  var columns = selectColumns.ToArray();
197  var sz = columns.Length;
198  for (int i = 0; i < sz; i++) {
199  var column = columns[i];
200 
201  if (column.IsGlob) {
202  if (column.IsAll) {
203  builder.Append("*");
204  } else {
205  builder.AppendFormat("{0}.*", column.TableName);
206  }
207  } else {
208  Visit(column.Expression);
209  }
210 
211  if (!String.IsNullOrEmpty(column.Alias))
212  builder.AppendFormat(" AS {0}", column.Alias);
213 
214  if (i < sz - 1)
215  builder.Append(", ");
216  }
217  }
A long string in the system.
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
string Deveel.Data.Sql.Expressions.ExpressionStringBuilder.ToSqlString ( SqlExpression  expression)
inline

Definition at line 34 of file ExpressionStringBuilder.cs.

34  {
35  rootQuery = expression is SqlQueryExpression;
36 
37  Visit(expression);
38  return builder.ToString();
39  }
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitAssign ( SqlAssignExpression  assign)
inlinevirtual

Parameters
assign
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 41 of file ExpressionStringBuilder.cs.

41  {
42  Visit(assign.ReferenceExpression);
43  builder.Append(" = ");
44  Visit(assign.ValueExpression);
45 
46  return assign;
47  }
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitBinary ( SqlBinaryExpression  binaryEpression)
inlinevirtual

Parameters
binaryEpression
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 49 of file ExpressionStringBuilder.cs.

49  {
50  Visit(binaryEpression.Left);
51 
52  var binaryOpString = GetBinaryOperatorString(binaryEpression.ExpressionType);
53  builder.AppendFormat(" {0} ", binaryOpString);
54 
55  Visit(binaryEpression.Right);
56 
57  return binaryEpression;
58  }
static string GetBinaryOperatorString(SqlExpressionType expressionType)
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitCast ( SqlCastExpression  castExpression)
inlinevirtual

Parameters
castExpression
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 127 of file ExpressionStringBuilder.cs.

127  {
128  builder.Append("CAST ");
129  Visit(castExpression.Value);
130  builder.Append(" AS ");
131  builder.Append(castExpression.SqlType);
132 
133  return base.VisitCast(castExpression);
134  }
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitConditional ( SqlConditionalExpression  conditional)
inlinevirtual

Parameters
conditional
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 136 of file ExpressionStringBuilder.cs.

136  {
137  return base.VisitConditional(conditional);
138  }
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitConstant ( SqlConstantExpression  constant)
inlinevirtual

Parameters
constant
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 140 of file ExpressionStringBuilder.cs.

140  {
141  var value = constant.Value;
142  if (value.Type is QueryType) {
143  // TODO: convert to sql string also a QUERY PLAN
144  builder.Append("({QUERY})");
145  } else if (value.Type is ArrayType) {
146  var array = (SqlArray) value.Value;
147  if (array.IsNull) {
148  builder.Append("NULL");
149  } else {
150  builder.Append("(");
151  var sz = array.Length;
152  for (int i = 0; i < sz; i++) {
153  Visit(array[i]);
154 
155  if (i < sz - 1)
156  builder.Append(", ");
157  }
158  builder.Append(")");
159  }
160  } else if (value.Type is NullType) {
161  builder.Append("NULL");
162  } else if (value.Type.IsPrimitive) {
163  if (value.IsNull) {
164  builder.Append("NULL");
165  } else {
166  builder.Append(value.Value);
167  }
168  }
169 
170  return constant;
171  }
A data type that represents the NULL value of a given SQL data type.
Definition: NullType.cs:29
An object that provides methods for accessing a finite collection of SQL expressions.
Definition: SqlArray.cs:28
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitFunctionCall ( SqlFunctionCallExpression  expression)
inlinevirtual

Visits the expression that calls the function defined.

Parameters
expressionThe SqlFunctionCallExpression to visit.
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 173 of file ExpressionStringBuilder.cs.

173  {
174  builder.Append(expression.FunctioName);
175  builder.Append("(");
176 
177  if (expression.Arguments != null &&
178  expression.Arguments.Length > 0) {
179  var args = expression.Arguments;
180  var argc = args.Length;
181 
182  for (int i = 0; i < argc; i++) {
183  Visit(args[i]);
184 
185  if (i < argc - 1)
186  builder.Append(", ");
187  }
188  }
189 
190  builder.Append(")");
191 
192  return expression;
193  }
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitQuery ( SqlQueryExpression  query)
inlinevirtual

Parameters
query
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 219 of file ExpressionStringBuilder.cs.

219  {
220  if (!rootQuery)
221  builder.Append("(");
222 
223  builder.Append("SELECT ");
224  if (query.Distinct)
225  builder.Append("DISTINCT ");
226 
227  PrintQueryColumns(query.SelectColumns);
228  builder.Append(" ");
229 
230  PrintFromClause(query.FromClause);
231 
232  if (query.WhereExpression != null) {
233  builder.Append(" WHERE ");
234  Visit(query.WhereExpression);
235  }
236 
237  if (query.GroupBy != null) {
238  builder.Append(" GROUP BY ");
239  VisitExpressionList(query.GroupBy.ToArray());
240 
241  if (query.HavingExpression != null) {
242  builder.Append(" HVAING ");
243  Visit(query.HavingExpression);
244  }
245  }
246 
247  if (query.GroupMax != null) {
248  builder.Append(" GROUP MAX ");
249  builder.Append(query.GroupMax.FullName);
250  }
251 
252  // TODO: COMPOSITE ...
253 
254  if (!rootQuery)
255  builder.Append(")");
256 
257  return query;
258  }
virtual SqlExpression[] VisitExpressionList(SqlExpression[] list)
Visits a list of expressions given.
void PrintQueryColumns(IEnumerable< SelectColumn > selectColumns)
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitReference ( SqlReferenceExpression  reference)
inlinevirtual

Parameters
reference
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 311 of file ExpressionStringBuilder.cs.

311  {
312  builder.Append(reference.ReferenceName);
313  return reference;
314  }
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitTuple ( SqlTupleExpression  expression)
inlinevirtual

Parameters
expression
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 316 of file ExpressionStringBuilder.cs.

316  {
317  builder.Append("(");
318 
319  var sz = expression.Expressions.Length;
320  for (int i = 0; i < sz; i++) {
321  Visit(expression.Expressions[i]);
322  if (i < sz - 1)
323  builder.Append(", ");
324  }
325 
326  builder.Append(")");
327  return expression;
328  }
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitUnary ( SqlUnaryExpression  unary)
inlinevirtual

Parameters
unary
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 330 of file ExpressionStringBuilder.cs.

330  {
331  var unaryOpString = GetUnaryOperatorString(unary.ExpressionType);
332  builder.Append(unaryOpString);
333  builder.Append(" ");
334  Visit(unary.Operand);
335 
336  return unary;
337  }
string GetUnaryOperatorString(SqlExpressionType unaryType)
virtual SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionStringBuilder.VisitVariableReference ( SqlVariableReferenceExpression  reference)
inlinevirtual

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 352 of file ExpressionStringBuilder.cs.

352  {
353  builder.AppendFormat(":{0}", reference.VariableName);
354  return reference;
355  }

Member Data Documentation

readonly StringBuilder Deveel.Data.Sql.Expressions.ExpressionStringBuilder.builder
private

Definition at line 27 of file ExpressionStringBuilder.cs.

bool Deveel.Data.Sql.Expressions.ExpressionStringBuilder.rootQuery = true
private

Definition at line 28 of file ExpressionStringBuilder.cs.


The documentation for this class was generated from the following file: