[463047]: System.Data.SQLite.Linq / SQL Generation / DmlSqlGenerator.cs  Maximize  Restore  History

Download this file

718 lines (619 with data), 25.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//---------------------------------------------------------------------
// <copyright file="DmlSqlGenerator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//---------------------------------------------------------------------
namespace System.Data.SQLite
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees;
using System.Data.Common.Utils;
using System.Data.Mapping.Update.Internal;
/// <summary>
/// Class generating SQL for a DML command tree.
/// </summary>
internal static class DmlSqlGenerator
{
private static readonly int s_commandTextBuilderInitialCapacity = 256;
internal static string GenerateUpdateSql(DbUpdateCommandTree tree, out List<DbParameter> parameters)
{
StringBuilder commandText = new StringBuilder(s_commandTextBuilderInitialCapacity);
ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, null != tree.Returning, "UpdateFunction");
// update [schemaName].[tableName]
commandText.Append("UPDATE ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
// set c1 = ..., c2 = ..., ...
bool first = true;
commandText.Append("SET ");
foreach (DbSetClause setClause in tree.SetClauses)
{
if (first) { first = false; }
else { commandText.Append(", "); }
setClause.Property.Accept(translator);
commandText.Append(" = ");
setClause.Value.Accept(translator);
}
if (first)
{
// If first is still true, it indicates there were no set
// clauses. Introduce a fake set clause so that:
// - we acquire the appropriate locks
// - server-gen columns (e.g. timestamp) get recomputed
//
// We use the following pattern:
//
// update Foo
// set @i = 0
// where ...
DbParameter parameter = translator.CreateParameter(default(Int32), DbType.Int32);
commandText.Append(parameter.ParameterName);
commandText.Append(" = 0");
}
commandText.AppendLine();
// where c1 = ..., c2 = ...
commandText.Append("WHERE ");
tree.Predicate.Accept(translator);
commandText.AppendLine();
// generate returning sql
GenerateReturningSql(commandText, tree, translator, tree.Returning);
parameters = translator.Parameters;
return commandText.ToString();
}
internal static string GenerateDeleteSql(DbDeleteCommandTree tree, out List<DbParameter> parameters)
{
StringBuilder commandText = new StringBuilder(s_commandTextBuilderInitialCapacity);
ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, false, "DeleteFunction");
// delete [schemaName].[tableName]
commandText.Append("DELETE FROM ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
// where c1 = ... AND c2 = ...
commandText.Append("WHERE ");
tree.Predicate.Accept(translator);
parameters = translator.Parameters;
commandText.AppendLine(";");
return commandText.ToString();
}
internal static string GenerateInsertSql(DbInsertCommandTree tree, out List<DbParameter> parameters)
{
StringBuilder commandText = new StringBuilder(s_commandTextBuilderInitialCapacity);
ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, null != tree.Returning, "InsertFunction");
// insert [schemaName].[tableName]
commandText.Append("INSERT INTO ");
tree.Target.Expression.Accept(translator);
if (tree.SetClauses.Count > 0)
{
// (c1, c2, c3, ...)
commandText.Append("(");
bool first = true;
foreach (DbSetClause setClause in tree.SetClauses)
{
if (first) { first = false; }
else { commandText.Append(", "); }
setClause.Property.Accept(translator);
}
commandText.AppendLine(")");
// values c1, c2, ...
first = true;
commandText.Append(" VALUES (");
foreach (DbSetClause setClause in tree.SetClauses)
{
if (first) { first = false; }
else { commandText.Append(", "); }
setClause.Value.Accept(translator);
translator.RegisterMemberValue(setClause.Property, setClause.Value);
}
commandText.AppendLine(");");
}
else // No columns specified. Insert an empty row containing default values by inserting null into the rowid
{
commandText.AppendLine(" DEFAULT VALUES;");
}
// generate returning sql
GenerateReturningSql(commandText, tree, translator, tree.Returning);
parameters = translator.Parameters;
return commandText.ToString();
}
// Generates T-SQL describing a member
// Requires: member must belong to an entity type (a safe requirement for DML
// SQL gen, where we only access table columns)
private static string GenerateMemberTSql(EdmMember member)
{
return SqlGenerator.QuoteIdentifier(member.Name);
}
/// <summary>
/// Generates SQL fragment returning server-generated values.
/// Requires: translator knows about member values so that we can figure out
/// how to construct the key predicate.
/// <code>
/// Sample SQL:
///
/// select IdentityValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and IdentityValue = scope_identity()
///
/// or
///
/// select TimestamptValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and Id = 1
///
/// Note that we filter on rowcount to ensure no rows are returned if no rows were modified.
/// </code>
/// </summary>
/// <param name="commandText">Builder containing command text</param>
/// <param name="tree">Modification command tree</param>
/// <param name="translator">Translator used to produce DML SQL statement
/// for the tree</param>
/// <param name="returning">Returning expression. If null, the method returns
/// immediately without producing a SELECT statement.</param>
private static void GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree,
ExpressionTranslator translator, DbExpression returning)
{
// Nothing to do if there is no Returning expression
if (null == returning) { return; }
// select
commandText.Append("SELECT ");
returning.Accept(translator);
commandText.AppendLine();
// from
commandText.Append("FROM ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
// where
commandText.Append("WHERE last_rows_affected() > 0");
EntitySetBase table = ((DbScanExpression)tree.Target.Expression).Target;
bool identity = false;
foreach (EdmMember keyMember in table.ElementType.KeyMembers)
{
commandText.Append(" AND ");
commandText.Append(GenerateMemberTSql(keyMember));
commandText.Append(" = ");
// retrieve member value sql. the translator remembers member values
// as it constructs the DML statement (which precedes the "returning"
// SQL)
DbParameter value;
if (translator.MemberValues.TryGetValue(keyMember, out value))
{
commandText.Append(value.ParameterName);
}
else
{
// if no value is registered for the key member, it means it is an identity
// which can be retrieved using the scope_identity() function
if (identity)
{
// there can be only one server generated key
throw new NotSupportedException(string.Format("Server generated keys are only supported for identity columns. More than one key column is marked as server generated in table '{0}'.", table.Name));
}
commandText.AppendLine("last_insert_rowid();");
identity = true;
}
}
}
/// <summary>
/// Lightweight expression translator for DML expression trees, which have constrained
/// scope and support.
/// </summary>
private class ExpressionTranslator : DbExpressionVisitor
{
/// <summary>
/// Initialize a new expression translator populating the given string builder
/// with command text. Command text builder and command tree must not be null.
/// </summary>
/// <param name="commandText">Command text with which to populate commands</param>
/// <param name="commandTree">Command tree generating SQL</param>
/// <param name="preserveMemberValues">Indicates whether the translator should preserve
/// member values while compiling t-SQL (only needed for server generation)</param>
internal ExpressionTranslator(StringBuilder commandText, DbModificationCommandTree commandTree,
bool preserveMemberValues, string kind)
{
Debug.Assert(null != commandText);
Debug.Assert(null != commandTree);
_kind = kind;
_commandText = commandText;
_commandTree = commandTree;
_parameters = new List<DbParameter>();
_memberValues = preserveMemberValues ? new Dictionary<EdmMember, DbParameter>() :
null;
}
private readonly StringBuilder _commandText;
private readonly DbModificationCommandTree _commandTree;
private readonly List<DbParameter> _parameters;
private readonly Dictionary<EdmMember, DbParameter> _memberValues;
private int parameterNameCount = 0;
private string _kind;
internal List<DbParameter> Parameters { get { return _parameters; } }
internal Dictionary<EdmMember, DbParameter> MemberValues { get { return _memberValues; } }
// generate parameter (name based on parameter ordinal)
internal SQLiteParameter CreateParameter(object value, TypeUsage type)
{
PrimitiveTypeKind primitiveType = MetadataHelpers.GetPrimitiveTypeKind(type);
DbType dbType = MetadataHelpers.GetDbType(primitiveType);
return CreateParameter(value, dbType);
}
// Creates a new parameter for a value in this expression translator
internal SQLiteParameter CreateParameter(object value, DbType dbType)
{
string parameterName = string.Concat("@p", parameterNameCount.ToString(CultureInfo.InvariantCulture));
parameterNameCount++;
SQLiteParameter parameter = new SQLiteParameter(parameterName, value);
parameter.DbType = dbType;
_parameters.Add(parameter);
return parameter;
}
#region Basics
public override void Visit(DbApplyExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Input);
if (expression.Apply != null)
{
VisitExpression(expression.Apply.Expression);
}
VisitExpressionBindingPost(expression.Input);
}
public override void Visit(DbArithmeticExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionList(expression.Arguments);
}
public override void Visit(DbCaseExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionList(expression.When);
VisitExpressionList(expression.Then);
VisitExpression(expression.Else);
}
public override void Visit(DbCastExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbCrossJoinExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
foreach (DbExpressionBinding binding in expression.Inputs)
{
VisitExpressionBindingPre(binding);
}
foreach (DbExpressionBinding binding2 in expression.Inputs)
{
VisitExpressionBindingPost(binding2);
}
}
public override void Visit(DbDerefExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbDistinctExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbElementExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbEntityRefExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbExceptExpression expression)
{
VisitBinary(expression);
}
protected virtual void VisitBinary(DbBinaryExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
this.VisitExpression(expression.Left);
this.VisitExpression(expression.Right);
}
public override void Visit(DbExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
throw new NotSupportedException("DbExpression");
}
public override void Visit(DbFilterExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Input);
VisitExpression(expression.Predicate);
VisitExpressionBindingPost(expression.Input);
}
public override void Visit(DbFunctionExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionList(expression.Arguments);
//if (expression.IsLambda)
//{
// VisitLambdaFunctionPre(expression.Function, expression.LambdaBody);
// VisitExpression(expression.LambdaBody);
// VisitLambdaFunctionPost(expression.Function, expression.LambdaBody);
//}
}
public override void Visit(DbGroupByExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitGroupExpressionBindingPre(expression.Input);
VisitExpressionList(expression.Keys);
VisitGroupExpressionBindingMid(expression.Input);
VisitAggregateList(expression.Aggregates);
VisitGroupExpressionBindingPost(expression.Input);
}
public override void Visit(DbIntersectExpression expression)
{
VisitBinary(expression);
}
public override void Visit(DbIsEmptyExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbIsOfExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbJoinExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Left);
VisitExpressionBindingPre(expression.Right);
VisitExpression(expression.JoinCondition);
VisitExpressionBindingPost(expression.Left);
VisitExpressionBindingPost(expression.Right);
}
public override void Visit(DbLikeExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpression(expression.Argument);
VisitExpression(expression.Pattern);
VisitExpression(expression.Escape);
}
public override void Visit(DbLimitExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpression(expression.Argument);
VisitExpression(expression.Limit);
}
public override void Visit(DbOfTypeExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbParameterReferenceExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
}
public override void Visit(DbProjectExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Input);
VisitExpression(expression.Projection);
VisitExpressionBindingPost(expression.Input);
}
public override void Visit(DbQuantifierExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Input);
VisitExpression(expression.Predicate);
VisitExpressionBindingPost(expression.Input);
}
public override void Visit(DbRefExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbRefKeyExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbRelationshipNavigationExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpression(expression.NavigationSource);
}
public override void Visit(DbSkipExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Input);
foreach (DbSortClause clause in expression.SortOrder)
{
VisitExpression(clause.Expression);
}
VisitExpressionBindingPost(expression.Input);
VisitExpression(expression.Count);
}
public override void Visit(DbSortExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpressionBindingPre(expression.Input);
for (int i = 0; i < expression.SortOrder.Count; i++)
{
VisitExpression(expression.SortOrder[i].Expression);
}
VisitExpressionBindingPost(expression.Input);
}
public override void Visit(DbTreatExpression expression)
{
VisitUnaryExpression(expression);
}
public override void Visit(DbUnionAllExpression expression)
{
VisitBinary(expression);
}
public override void Visit(DbVariableReferenceExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
}
public virtual void VisitAggregate(DbAggregate aggregate)
{
if (aggregate == null) throw new ArgumentException("aggregate");
VisitExpressionList(aggregate.Arguments);
}
public virtual void VisitAggregateList(IList<DbAggregate> aggregates)
{
if (aggregates == null) throw new ArgumentException("aggregates");
for (int i = 0; i < aggregates.Count; i++)
{
VisitAggregate(aggregates[i]);
}
}
public virtual void VisitExpression(DbExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
expression.Accept(this);
}
protected virtual void VisitExpressionBindingPost(DbExpressionBinding binding)
{
}
protected virtual void VisitExpressionBindingPre(DbExpressionBinding binding)
{
if (binding == null) throw new ArgumentException("binding");
VisitExpression(binding.Expression);
}
public virtual void VisitExpressionList(IList<DbExpression> expressionList)
{
if (expressionList == null) throw new ArgumentException("expressionList");
for (int i = 0; i < expressionList.Count; i++)
{
VisitExpression(expressionList[i]);
}
}
protected virtual void VisitGroupExpressionBindingMid(DbGroupExpressionBinding binding)
{
}
protected virtual void VisitGroupExpressionBindingPost(DbGroupExpressionBinding binding)
{
}
protected virtual void VisitGroupExpressionBindingPre(DbGroupExpressionBinding binding)
{
if (binding == null) throw new ArgumentException("binding");
VisitExpression(binding.Expression);
}
protected virtual void VisitLambdaFunctionPost(EdmFunction function, DbExpression body)
{
}
protected virtual void VisitLambdaFunctionPre(EdmFunction function, DbExpression body)
{
if (function == null) throw new ArgumentException("function");
if (body == null) throw new ArgumentException("body");
}
//internal virtual void VisitRelatedEntityReference(DbRelatedEntityRef relatedEntityRef)
//{
// VisitExpression(relatedEntityRef.TargetEntityReference);
//}
//internal virtual void VisitRelatedEntityReferenceList(IList<DbRelatedEntityRef> relatedEntityReferences)
//{
// for (int i = 0; i < relatedEntityReferences.Count; i++)
// {
// VisitRelatedEntityReference(relatedEntityReferences[i]);
// }
//}
protected virtual void VisitUnaryExpression(DbUnaryExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
VisitExpression(expression.Argument);
}
#endregion
public override void Visit(DbAndExpression expression)
{
VisitBinary(expression, " AND ");
}
public override void Visit(DbOrExpression expression)
{
VisitBinary(expression, " OR ");
}
public override void Visit(DbComparisonExpression expression)
{
Debug.Assert(expression.ExpressionKind == DbExpressionKind.Equals,
"only equals comparison expressions are produced in DML command trees in V1");
VisitBinary(expression, " = ");
RegisterMemberValue(expression.Left, expression.Right);
}
/// <summary>
/// Call this method to register a property value pair so the translator "remembers"
/// the values for members of the row being modified. These values can then be used
/// to form a predicate for server-generation (based on the key of the row)
/// </summary>
/// <param name="propertyExpression">DbExpression containing the column reference (property expression).</param>
/// <param name="value">DbExpression containing the value of the column.</param>
internal void RegisterMemberValue(DbExpression propertyExpression, DbExpression value)
{
if (null != _memberValues)
{
// register the value for this property
Debug.Assert(propertyExpression.ExpressionKind == DbExpressionKind.Property,
"DML predicates and setters must be of the form property = value");
// get name of left property
EdmMember property = ((DbPropertyExpression)propertyExpression).Property;
// don't track null values
if (value.ExpressionKind != DbExpressionKind.Null)
{
Debug.Assert(value.ExpressionKind == DbExpressionKind.Constant,
"value must either constant or null");
// retrieve the last parameter added (which describes the parameter)
_memberValues[property] = _parameters[_parameters.Count - 1];
}
}
}
public override void Visit(DbIsNullExpression expression)
{
expression.Argument.Accept(this);
_commandText.Append(" IS NULL");
}
public override void Visit(DbNotExpression expression)
{
_commandText.Append("NOT (");
expression.Accept(this);
_commandText.Append(")");
}
public override void Visit(DbConstantExpression expression)
{
SQLiteParameter parameter = CreateParameter(expression.Value, expression.ResultType);
_commandText.Append(parameter.ParameterName);
}
public override void Visit(DbScanExpression expression)
{
string definingQuery = MetadataHelpers.TryGetValueForMetadataProperty<string>(expression.Target, "DefiningQuery");
if (definingQuery != null)
{
throw new NotSupportedException(String.Format("Unable to update the EntitySet '{0}' because it has a DefiningQuery and no <{1}> element exists in the <ModificationFunctionMapping> element to support the current operation.", expression.Target.Name, _kind));
}
_commandText.Append(SqlGenerator.GetTargetTSql(expression.Target));
}
public override void Visit(DbPropertyExpression expression)
{
_commandText.Append(GenerateMemberTSql(expression.Property));
}
public override void Visit(DbNullExpression expression)
{
_commandText.Append("NULL");
}
public override void Visit(DbNewInstanceExpression expression)
{
// assumes all arguments are self-describing (no need to use aliases
// because no renames are ever used in the projection)
bool first = true;
foreach (DbExpression argument in expression.Arguments)
{
if (first) { first = false; }
else { _commandText.Append(", "); }
argument.Accept(this);
}
}
private void VisitBinary(DbBinaryExpression expression, string separator)
{
_commandText.Append("(");
expression.Left.Accept(this);
_commandText.Append(separator);
expression.Right.Accept(this);
_commandText.Append(")");
}
}
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks