├── .gitignore
├── DbExpressions.Tests
├── App.config
├── DbExpressionFactoryTests.cs
├── DbExpressionSyntaxTests.cs
├── DbExpressions.Tests.csproj
├── DbExpressions.Tests.csproj.vspscc
├── MySqlExpressionSyntaxTests.cs
├── OracleExpressionSyntaxTests.cs
├── Properties
│ └── AssemblyInfo.cs
├── SQLiteExpressionSyntaxTests.cs
├── SqlExpressionSyntaxTests.cs
└── StringExtensions.cs
├── DbExpressions.sln
├── DbExpressions.vsmdi
├── DbExpressions.vssscc
├── DbExpressions
├── Configuration
│ ├── DbExpressionSettings.cs
│ ├── QueryTranslatorElement.cs
│ └── QueryTranslatorElementCollection.cs
├── DbExpressionFactory.cs
├── DbExpressions.csproj
├── DbExpressions.csproj.vspscc
├── DbGroupByExpression.cs
├── DbQueryTranslatorFactory.cs
├── DbTranslateResult.cs
├── Enums
│ ├── DbAggregateFunctionExpressionType.cs
│ ├── DbBinaryExpressionType.cs
│ ├── DbDateTimeFunctionExpressionType.cs
│ ├── DbExpressionType.cs
│ ├── DbFunctionExpressionType.cs
│ ├── DbJoinExpressionType.cs
│ ├── DbMathematicalFunctionExpressionType.cs
│ ├── DbOrderByExpressionType.cs
│ ├── DbQueryType.cs
│ ├── DbStringFunctionExpressionType.cs
│ └── DbUnaryExpressionType.cs
├── Expressions
│ ├── DbAggregateFunctionExpression.cs
│ ├── DbAliasExpression.cs
│ ├── DbBatchExpression.cs
│ ├── DbBinaryExpression.cs
│ ├── DbColumnExpression.cs
│ ├── DbConcatExpression.cs
│ ├── DbConditionalExpression.cs
│ ├── DbConstantExpression.cs
│ ├── DbDateTimeFunctionExpression.cs
│ ├── DbDeleteExpression.cs
│ ├── DbExistsExpression.cs
│ ├── DbExpression.cs
│ ├── DbFunctionExpression.cs
│ ├── DbInExpression.cs
│ ├── DbInsertExpression.cs
│ ├── DbJoinExpression.cs
│ ├── DbListExpression.cs
│ ├── DbMathematicalFunctionExpression.cs
│ ├── DbOrderByExpression.cs
│ ├── DbPrefixExpression.cs
│ ├── DbQuery.cs
│ ├── DbQueryExpression.cs
│ ├── DbSelectExpression.cs
│ ├── DbSqlExpression.cs
│ ├── DbStringFunctionExpression.cs
│ ├── DbTableExpression.cs
│ ├── DbUnaryExpression.cs
│ └── DbUpdateExpression.cs
├── Extensions
│ ├── DbDeleteQueryExtensions.cs
│ ├── DbExpressionExtensions.cs
│ ├── DbInsertQueryExtensions.cs
│ ├── DbQueryExtensions.cs
│ ├── DbSelectQueryExtensions.cs
│ ├── DbUpdateQueryExtensions.cs
│ └── StringBuilderExtensions.cs
├── Properties
│ └── AssemblyInfo.cs
├── Settings.StyleCop
└── Visitors
│ ├── DbExpressionFinder.cs
│ ├── DbExpressionReplacer.cs
│ ├── DbExpressionVisitor.cs
│ └── Translators
│ ├── Concrete
│ ├── MySqlQueryTranslator.cs
│ ├── OracleQueryTranslator.cs
│ ├── SQLiteQueryTranslator.cs
│ └── SqlQueryTranslator.cs
│ └── DbQueryTranslator.cs
├── LICENSE
├── Local.testsettings
├── README
└── TraceAndTestImpact.testsettings
/.gitignore:
--------------------------------------------------------------------------------
1 | [Oo]bj/
2 | [Bb]in/
3 | _ReSharper*
4 | /TestResults
5 | *.suo
6 | *.user
7 | *.nupkg
8 | *.cs_
9 | *.vsp
10 | *.psess
11 | StyleCopViolations.xml
12 | StyleCop.Cache
13 | .vs/*
--------------------------------------------------------------------------------
/DbExpressions.Tests/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/DbExpressions.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 |
7 |
8 | 2.0
9 | {F53DA910-FC47-4909-81EC-643E8BA9D51E}
10 | Library
11 | Properties
12 | DbExpressions.Tests
13 | DbExpressions.Tests
14 | v4.0
15 | 512
16 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | true
28 | full
29 | false
30 | bin\Debug\
31 | DEBUG;TRACE
32 | prompt
33 | 4
34 |
35 |
36 | pdbonly
37 | true
38 | bin\Release\
39 | TRACE
40 | prompt
41 | 4
42 |
43 |
44 |
45 |
46 |
47 | 3.5
48 |
49 |
50 |
51 |
52 |
53 | False
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | {EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}
69 | DbExpressions
70 |
71 |
72 |
73 |
74 |
75 |
76 |
83 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/DbExpressions.Tests.csproj.vspscc:
--------------------------------------------------------------------------------
1 | ""
2 | {
3 | "FILE_VERSION" = "9237"
4 | "ENLISTMENT_CHOICE" = "NEVER"
5 | "PROJECT_FILE_RELATIVE_PATH" = ""
6 | "NUMBER_OF_EXCLUDED_FILES" = "0"
7 | "ORIGINAL_PROJECT_FILE_PATH" = ""
8 | "NUMBER_OF_NESTED_PROJECTS" = "0"
9 | "SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER"
10 | }
11 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/MySqlExpressionSyntaxTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 |
3 | namespace DbExpressions.Tests
4 | {
5 | //[TestClass]
6 | public class MySqlExpressionSyntaxTests : DbExpressionSyntaxTests
7 | {
8 | protected override DbQueryTranslator QueryTranslator
9 | {
10 | get { return DbQueryTranslatorFactory.GetQueryTranslator("MySql.Data.MySqlClient"); }
11 | }
12 |
13 | public override string AndExpressionSyntax
14 | {
15 | get { return "(?p0 AND ?p1)"; }
16 | }
17 |
18 | public override string AddExpressionSyntax
19 | {
20 | get { return "(?p0 + ?p1)"; }
21 | }
22 |
23 | public override string AliasExpressionSyntax
24 | {
25 | get { return "`SomeTable` AS t0"; }
26 | }
27 |
28 | public override string AssignExpressionSyntax
29 | {
30 | get { return "`SomeColumn` = ?p0"; }
31 | }
32 |
33 | public override string AssignNullExpressionSyntax
34 | {
35 | get { return "`SomeColumn` = NULL"; }
36 | }
37 |
38 | public override string AvgExpressionSyntax
39 | {
40 | get { return "AVG(`SomeColumn`)"; }
41 | }
42 |
43 | public override string ColumnExpressionSyntax
44 | {
45 | get { return "`SomeColumn`"; }
46 | }
47 |
48 | public override string ConcatExpressionSyntax
49 | {
50 | get { return "`SomeTable` `SomeOtherTable`"; }
51 | }
52 |
53 | public override string ConditionalExpressionSyntax
54 | {
55 | get { return "CASE WHEN (`SomeColumn` = ?p0) THEN ?p1 ELSE ?p2 END"; }
56 | }
57 |
58 | public override string ConstantExpressionSyntax
59 | {
60 | get { return "?p0"; }
61 | }
62 |
63 | public override string CountExpressionSyntax
64 | {
65 | get { return "COUNT(`SomeColumn`)"; }
66 | }
67 |
68 | public override string DivideExpressionSyntax
69 | {
70 | get { return "(?p0 / ?p1)"; }
71 | }
72 |
73 | public override string EqualExpressionSyntax
74 | {
75 | get { return "(`SomeColumn` = ?p0)"; }
76 | }
77 |
78 | public override string ExistsExpressionSyntax
79 | {
80 | get { return "EXISTS(SELECT `SomeColumn` FROM `SomeTable`)"; }
81 | }
82 |
83 | public override string GreaterThanExpressionSyntax
84 | {
85 | get { return "(`SomeColumn` > ?p0)"; }
86 | }
87 |
88 | public override string GreaterThanOrEqualExpressionSyntax
89 | {
90 | get { return "(`SomeColumn` >= ?p0)"; }
91 | }
92 |
93 | public override string InValueRangeExpressionSyntax
94 | {
95 | get { return "`SomeColumn` IN(?p0,?p1)"; }
96 | }
97 |
98 | public override string InSubQueryExpressionSyntax
99 | {
100 | get { return "`SomeColumn` IN(SELECT `SomeOtherColumn` FROM `SomeOtherTable`)"; }
101 | }
102 |
103 | public override string InnerJoinExpressionSyntax
104 | {
105 | get { return "INNER JOIN `SomeTable` ON (`SomeColumn` = `SomeOtherColumn`)"; }
106 | }
107 |
108 | public override string LeftOuterJoinExpressionSyntax
109 | {
110 | get { return "LEFT OUTER JOIN `SomeTable` ON (`SomeColumn` = `SomeOtherColumn`)"; }
111 | }
112 |
113 | public override string RightOuterJoinExpressionSyntax
114 | {
115 | get { return "RIGHT OUTER JOIN `SomeTable` ON (`SomeColumn` = `SomeOtherColumn`)"; }
116 | }
117 |
118 | public override string LengthExpressionSyntax
119 | {
120 | get { return "LENGTH(`SomeColumn`)"; }
121 | }
122 |
123 | public override string LessThanExpressionSyntax
124 | {
125 | get { return "(`SomeColumn` < ?p0)"; }
126 | }
127 |
128 | public override string LessThanOrEqualExpressionSyntax
129 | {
130 | get { return "(`SomeColumn` <= ?p0)"; }
131 | }
132 |
133 | public override string ListExpressionSyntax
134 | {
135 | get { return "`SomeColumn`,`SomeOtherColumn`"; }
136 | }
137 |
138 | public override string BatchExpressionSyntax
139 | {
140 | get { return "`SomeColumn`;`SomeOtherColumn`;"; }
141 | }
142 |
143 | public override string MaxExpressionSyntax
144 | {
145 | get { return "MAX(`SomeColumn`)"; }
146 | }
147 |
148 | public override string MinExpressionSyntax
149 | {
150 | get { return "MIN(`SomeColumn`)"; }
151 | }
152 |
153 | public override string MultiplyExpressionSyntax
154 | {
155 | get { return "(?p0 * ?p1)"; }
156 | }
157 |
158 | public override string SubstractExpressionSyntax
159 | {
160 | get { return "(?p0 - ?p1)"; }
161 | }
162 |
163 | public override string NotExpressionSyntax
164 | {
165 | get { return "NOT (`SomeColumn` = ?p0)"; }
166 | }
167 |
168 | public override string DistinctExpressionSyntax
169 | {
170 | get { return "SELECT DISTINCT `SomeColumn`"; }
171 | }
172 |
173 | public override string DistinctWithTopExpressionSyntax
174 | {
175 | get { return "SELECT DISTINCT `SomeColumn` LIMIT ?p0"; }
176 | }
177 |
178 | public override string NotEqualExpressionSyntax
179 | {
180 | get { return "(`SomeColumn` <> ?p0)"; }
181 | }
182 |
183 | public override string OrExpressionSyntax
184 | {
185 | get { return "(?p0 OR ?p1)"; }
186 | }
187 |
188 |
189 | public override string OrderByAscendingExpressionSyntax
190 | {
191 | get { return "`SomeColumn` ASC"; }
192 | }
193 |
194 | public override string OrderByDescendingExpressionSyntax
195 | {
196 | get { return "`SomeColumn` DESC"; }
197 | }
198 |
199 | public override string PrefixExpressionSyntax
200 | {
201 | get { return "`t0`.`SomeColumn`"; }
202 | }
203 |
204 | public override string ReplaceExpressionSyntax
205 | {
206 | get { return "REPLACE(`SomeColumn`,?p0,?p1)"; }
207 | }
208 |
209 | public override string ReverseExpressionSyntax
210 | {
211 | get { return "REVERSE(`SomeColumn`)"; }
212 | }
213 |
214 | public override string SoundExExpressionSyntax
215 | {
216 | get { return "SOUNDEX(`SomeColumn`)"; }
217 | }
218 |
219 | public override string SumExpressionSyntax
220 | {
221 | get { return "SUM(`SomeColumn`)"; }
222 | }
223 |
224 | public override string TableExpressionSyntax
225 | {
226 | get { return "`SomeTable`"; }
227 | }
228 |
229 | public override string ToLowerExpressionSyntax
230 | {
231 | get { return "LOWER(`SomeColumn`)"; }
232 | }
233 |
234 | public override string ToUpperExpressionSyntax
235 | {
236 | get { return "UPPER(`SomeColumn`)"; }
237 | }
238 |
239 | public override string TrimExpressionSyntax
240 | {
241 | get { return "TRIM(`SomeColumn`)"; }
242 | }
243 |
244 | public override string TrimStartExpressionSyntax
245 | {
246 | get { return "LTRIM(`SomeColumn`)"; }
247 | }
248 |
249 | public override string TrimEndExpressionSyntax
250 | {
251 | get { return "RTRIM(`SomeColumn`)"; }
252 | }
253 |
254 | public override string SubStringExpressionSyntax
255 | {
256 | get { return "SUBSTRING(`SomeColumn`,?p0,?p1)"; }
257 | }
258 |
259 | public override string DateExpressionSyntax
260 | {
261 | get { return "DATE(`SomeColumn`)"; }
262 | }
263 |
264 | public override string YearExpressionSyntax
265 | {
266 | get { return "EXTRACT(YEAR FROM `SomeColumn`)"; }
267 | }
268 |
269 | public override string MonthExpressionSyntax
270 | {
271 | get { return "EXTRACT(MONTH FROM `SomeColumn`)"; }
272 | }
273 |
274 | public override string HourExpressionSyntax
275 | {
276 | get { return "EXTRACT(HOUR FROM `SomeColumn`)"; }
277 | }
278 |
279 | public override string MinuteExpressionSyntax
280 | {
281 | get { return "EXTRACT(MINUTE FROM `SomeColumn`)"; }
282 | }
283 |
284 | public override string SecondExpressionSyntax
285 | {
286 | get { return "EXTRACT(SECOND FROM `SomeColumn`)"; }
287 | }
288 |
289 | public override string MillisecondExpressionSyntax
290 | {
291 | get { return "(EXTRACT(MICROSECOND FROM `SomeColumn`) DIV 1000)"; }
292 | }
293 |
294 | public override string DayOfYearExpressionSyntax
295 | {
296 | get { return "DAYOFYEAR(`SomeColumn`)"; }
297 | }
298 |
299 | public override string DayOfMonthExpressionSyntax
300 | {
301 | get { return "EXTRACT(DAY FROM `SomeColumn`)"; }
302 | }
303 |
304 | public override string DayOfWeekExpressionSyntax
305 | {
306 | get { return "WEEKDAY(`SomeColumn`)"; }
307 | }
308 |
309 | public override string AddYearsExpressionSyntax
310 | {
311 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 YEAR)"; }
312 | }
313 |
314 | public override string AddMonthsExpressionSyntax
315 | {
316 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 MONTH)"; }
317 | }
318 |
319 | public override string AddDaysExpressionSyntax
320 | {
321 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 DAY)"; }
322 | }
323 |
324 | public override string AddHoursExpressionSyntax
325 | {
326 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 HOUR)"; }
327 | }
328 |
329 | public override string AddMinutesExpressionSyntax
330 | {
331 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 MINUTE)"; }
332 | }
333 |
334 | public override string AddSecondsExpressionSyntax
335 | {
336 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 SECOND)"; }
337 | }
338 |
339 | public override string AddMilliSecondsExpressionSyntax
340 | {
341 | get { return "DATE_ADD(`SomeColumn`, INTERVAL ?p0 * 1000 MICROSECOND)"; }
342 | }
343 |
344 | public override string NowExpressionSyntax
345 | {
346 | get { return "CURRENT_TIMESTAMP()"; }
347 | }
348 |
349 | public override string ToDayExpressionSyntax
350 | {
351 | get { return "CURRENT_DATE()"; }
352 | }
353 |
354 | public override string UpdateExpressionSyntax
355 | {
356 | get { return "UPDATE `SomeTable` SET `SomeColumn` = ?p0 WHERE (`SomeColumn` = ?p1)"; }
357 | }
358 |
359 | public override string UpdateExpressionWithAliasedTargetSyntax
360 | {
361 | get { return "UPDATE `SomeTable` AS t0 SET `SomeColumn` = ?p0 WHERE (`SomeColumn` = ?p1)"; }
362 | }
363 |
364 | public override string UpdateExpressionWithFromClauseSyntax
365 | {
366 | get { return "UPDATE `SomeTable` AS t0 INNER JOIN `SomeOtherTable` AS t1 ON (`t0`.`Id` = `t1`.`Id`) SET `SomeColumn` = ?p0 WHERE (`t0`.`SomeColumn` = ?p1)"; }
367 | }
368 |
369 |
370 | public override string InsertExpressionSyntax
371 | {
372 | get { return "INSERT INTO `SomeTable` (`SomeColumn`) VALUES(?p0)"; }
373 | }
374 |
375 | public override string DeleteExpressionSyntax
376 | {
377 | get { return "DELETE FROM `SomeTable` WHERE (`SomeColumn` = ?p0)"; }
378 | }
379 |
380 | public override string DeleteExpressionWithAliasedTargetSyntax
381 | {
382 | get { return "DELETE t0 FROM `SomeTable` AS t0 WHERE (`t0`.`SomeColumn` = ?p0)"; }
383 | }
384 |
385 | public override string SelectExpressionSyntax
386 | {
387 | get { return "SELECT `SomeColumn` FROM `SomeTable`"; }
388 | }
389 |
390 | public override string SelectExpressionWithTakeSyntax
391 | {
392 | get { return "SELECT `SomeColumn` FROM `SomeTable` LIMIT ?p0"; }
393 | }
394 |
395 | public override string SelectExpressionWithSkipSyntax
396 | {
397 | get { return "SELECT `SomeColumn` FROM `SomeTable` LIMIT ?p0 OFFSET 18446744073709551615"; }
398 | }
399 |
400 | public override string SelectExpressionWithSkipAndTakeSyntax
401 | {
402 | get { return "SELECT `SomeColumn` FROM `SomeTable` LIMIT ?p0 OFFSET ?p1"; }
403 | }
404 |
405 | public override string SelectExpressionWithOrderBySyntax
406 | {
407 | get { return "SELECT `SomeColumn` FROM `SomeTable` ORDER BY `SomeColumn` ASC"; }
408 | }
409 |
410 | public override string SelectExpressionWithGroupBySyntax
411 | {
412 | get { return "SELECT `SomeColumn` FROM `SomeTable` GROUP BY `SomeColumn`"; }
413 | }
414 |
415 | public override string SelectExpressionWithGroupByHavingSyntax
416 | {
417 | get { return "SELECT `SomeColumn` FROM `SomeTable` GROUP BY `SomeColumn` HAVING (`SomeColumn` > ?p0)"; }
418 | }
419 |
420 | public override string AbsExpressionSyntax
421 | {
422 | get { return "ABS(`SomeColumn`)"; }
423 | }
424 |
425 | public override string AcosExpressionSyntax
426 | {
427 | get { return "ACOS(`SomeColumn`)"; }
428 | }
429 |
430 | public override string AsinExpressionSyntax
431 | {
432 | get { return "ASIN(`SomeColumn`)"; }
433 | }
434 |
435 | public override string AtanExpressionSyntax
436 | {
437 | get { return "ATAN(`SomeColumn`)"; }
438 | }
439 |
440 | public override string Atan2ExpressionSyntax
441 | {
442 | get { return "ATAN2(`SomeColumn`)"; }
443 | }
444 |
445 | public override string CeilingExpressionSyntax
446 | {
447 | get { return "CEILING(`SomeColumn`)"; }
448 | }
449 |
450 | public override string CosExpressionSyntax
451 | {
452 | get { return "COS(`SomeColumn`)"; }
453 | }
454 |
455 | public override string CotExpressionSyntax
456 | {
457 | get { return "COT(`SomeColumn`)"; }
458 | }
459 |
460 | public override string DegressExpressionSyntax
461 | {
462 | get { return "DEGREES(`SomeColumn`)"; }
463 | }
464 |
465 | public override string ExpExpressionSyntax
466 | {
467 | get { return "EXP(`SomeColumn`)"; }
468 | }
469 |
470 | public override string FloorExpressionSyntax
471 | {
472 | get { return "FLOOR(`SomeColumn`)"; }
473 | }
474 |
475 | public override string LogExpressionSyntax
476 | {
477 | get { return "LOG(`SomeColumn`)"; }
478 | }
479 |
480 | public override string Log10ExpressionSyntax
481 | {
482 | get { return "LOG10(`SomeColumn`)"; }
483 | }
484 |
485 | public override string PIExpressionSyntax
486 | {
487 | get { return "PI()"; }
488 | }
489 |
490 | public override string PowerExpressionSyntax
491 | {
492 | get { return "POWER(`SomeColumn`,?p0)"; }
493 | }
494 |
495 | public override string RadiansExpressionSyntax
496 | {
497 | get { return "RADIANS(`SomeColumn`)"; }
498 | }
499 |
500 | public override string RandExpressionSyntax
501 | {
502 | get { return "RAND()"; }
503 | }
504 |
505 | public override string RandWithSeedExpressionSyntax
506 | {
507 | get { return "RAND(?p0)"; }
508 | }
509 |
510 | public override string RoundExpressionSyntax
511 | {
512 | get { return "ROUND(`SomeColumn`,?p0)"; }
513 | }
514 |
515 | public override string SignExpressionSyntax
516 | {
517 | get { return "SIGN(`SomeColumn`)"; }
518 | }
519 |
520 | public override string SinExpressionSyntax
521 | {
522 | get { return "SIN(`SomeColumn`)"; }
523 | }
524 |
525 | public override string SqrtExpressionSyntax
526 | {
527 | get { return "SQRT(`SomeColumn`)"; }
528 | }
529 |
530 | public override string SquareExpressionSyntax
531 | {
532 | get { return "POWER(`SomeColumn`,?p0)"; }
533 | }
534 |
535 | public override string TanExpressionSyntax
536 | {
537 | get { return "TAN(`SomeColumn`)"; }
538 | }
539 |
540 | public override string NullCheckEqualsExpressionSyntax
541 | {
542 | get { return "(`SomeColumn` IS NULL)"; }
543 | }
544 |
545 | public override string NullCheckNotEqualsExpressionSyntax
546 | {
547 | get { return "(`SomeColumn` IS NOT NULL)"; }
548 | }
549 | }
550 | }
551 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/OracleExpressionSyntaxTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
6 |
7 | namespace DbExpressions.Tests
8 | {
9 | [TestClass]
10 | public class OracleExpressionSyntaxTests : DbExpressionSyntaxTests
11 | {
12 |
13 |
14 | protected override DbQueryTranslator QueryTranslator
15 | {
16 | get { return DbQueryTranslatorFactory.GetQueryTranslator("Oracle.DataAccess.Client"); }
17 | }
18 |
19 | public override string AndExpressionSyntax
20 | {
21 | get { throw new NotImplementedException(); }
22 | }
23 |
24 | public override string AddExpressionSyntax
25 | {
26 | get { return "(:?p0 + :?p1)"; }
27 | }
28 |
29 | public override string AliasExpressionSyntax
30 | {
31 | get { throw new NotImplementedException(); }
32 | }
33 |
34 | public override string AssignExpressionSyntax
35 | {
36 | get { throw new NotImplementedException(); }
37 | }
38 |
39 | public override string AssignNullExpressionSyntax
40 | {
41 | get { return "\"SomeColumn\" = NULL"; }
42 | }
43 |
44 | public override string AvgExpressionSyntax
45 | {
46 | get { return "AVG(\"SomeColumn\")"; }
47 | }
48 |
49 | public override string ColumnExpressionSyntax
50 | {
51 | get { return "\"SomeColumn\""; }
52 | }
53 |
54 | public override string ConcatExpressionSyntax
55 | {
56 | get { return "\"SomeTable\" \"SomeOtherTable\""; }
57 | }
58 |
59 | public override string ConditionalExpressionSyntax
60 | {
61 | get { throw new NotImplementedException(); }
62 | }
63 |
64 | public override string ConstantExpressionSyntax
65 | {
66 | get { throw new NotImplementedException(); }
67 | }
68 |
69 | public override string CountExpressionSyntax
70 | {
71 | get { return "COUNT(\"SomeColumn\")"; }
72 | }
73 |
74 | public override string DivideExpressionSyntax
75 | {
76 | get { throw new NotImplementedException(); }
77 | }
78 |
79 | public override string EqualExpressionSyntax
80 | {
81 | get { return "(\"SomeColumn\" = :?p0)"; }
82 | }
83 |
84 | public override string ExistsExpressionSyntax
85 | {
86 | get { throw new NotImplementedException(); }
87 | }
88 |
89 | public override string GreaterThanExpressionSyntax
90 | {
91 | get { return "(\"SomeColumn\" > :?p0)"; }
92 | }
93 |
94 | public override string GreaterThanOrEqualExpressionSyntax
95 | {
96 | get { throw new NotImplementedException(); }
97 | }
98 |
99 | public override string InValueRangeExpressionSyntax
100 | {
101 | get { return "\"SomeColumn\" IN(:?p0,:?p1)"; }
102 | }
103 |
104 | public override string InSubQueryExpressionSyntax
105 | {
106 | get { return "\"SomeColumn\" IN(SELECT \"SomeOtherColumn\" FROM \"SomeOtherTable\")"; }
107 | }
108 |
109 | public override string InnerJoinExpressionSyntax
110 | {
111 | get { return "INNER JOIN \"SomeTable\" ON (\"SomeColumn\" = \"SomeOtherColumn\")"; }
112 | }
113 |
114 | public override string LeftOuterJoinExpressionSyntax
115 | {
116 | get { return "LEFT OUTER JOIN \"SomeTable\" ON (\"SomeColumn\" = \"SomeOtherColumn\")"; }
117 | }
118 |
119 | public override string RightOuterJoinExpressionSyntax
120 | {
121 | get { throw new NotImplementedException(); }
122 | }
123 |
124 | public override string LengthExpressionSyntax
125 | {
126 | get { return "LENGTH(\"SomeColumn\")"; }
127 | }
128 |
129 | public override string LessThanExpressionSyntax
130 | {
131 | get { return "(\"SomeColumn\" < :?p0)"; }
132 | }
133 |
134 | public override string LessThanOrEqualExpressionSyntax
135 | {
136 | get { throw new NotImplementedException(); }
137 | }
138 |
139 | public override string ListExpressionSyntax
140 | {
141 | get { return "\"SomeColumn\",\"SomeOtherColumn\""; }
142 | }
143 |
144 | public override string BatchExpressionSyntax
145 | {
146 | get { return "begin \"SomeColumn\";\"SomeOtherColumn\"; end;"; }
147 | }
148 |
149 | public override string MaxExpressionSyntax
150 | {
151 | get { return "MAX(\"SomeColumn\")"; }
152 | }
153 |
154 | public override string MinExpressionSyntax
155 | {
156 | get { return "MIN(\"SomeColumn\")"; }
157 | }
158 |
159 | public override string MultiplyExpressionSyntax
160 | {
161 | get { throw new NotImplementedException(); }
162 | }
163 |
164 | public override string SubstractExpressionSyntax
165 | {
166 | get { return "(:?p0 - :?p1)"; }
167 | }
168 |
169 | public override string NotExpressionSyntax
170 | {
171 | get { return "NOT (\"SomeColumn\" = :?p0)"; }
172 | }
173 |
174 | public override string DistinctExpressionSyntax
175 | {
176 | get { return "SELECT DISTINCT \"SomeColumn\""; }
177 | }
178 |
179 | public override string DistinctWithTopExpressionSyntax
180 | {
181 | get { return "SELECT DISTINCT \"SomeColumn\" WHERE ROWNUM <= :?p0"; }
182 | }
183 |
184 | public override string NotEqualExpressionSyntax
185 | {
186 | get { throw new NotImplementedException(); }
187 | }
188 |
189 | public override string OrExpressionSyntax
190 | {
191 | get { return "(:?p0 OR :?p1)"; }
192 | }
193 |
194 | public override string OrderByAscendingExpressionSyntax
195 | {
196 | get { return "\"SomeColumn\" ASC"; }
197 | }
198 |
199 | public override string OrderByDescendingExpressionSyntax
200 | {
201 | get { throw new NotImplementedException(); }
202 | }
203 |
204 | public override string PrefixExpressionSyntax
205 | {
206 | get { return "\"t0\".\"SomeColumn\""; }
207 | }
208 |
209 | public override string ReplaceExpressionSyntax
210 | {
211 | get { return "REPLACE(\"SomeColumn\",:?p0,:?p1)"; }
212 | }
213 |
214 | public override string ReverseExpressionSyntax
215 | {
216 | get { throw new NotImplementedException(); }
217 | }
218 |
219 | public override string SoundExExpressionSyntax
220 | {
221 | get { throw new NotImplementedException(); }
222 | }
223 |
224 | public override string SumExpressionSyntax
225 | {
226 | get { return "SUM(\"SomeColumn\")"; }
227 | }
228 |
229 | public override string TableExpressionSyntax
230 | {
231 | get { throw new NotImplementedException(); }
232 | }
233 |
234 | public override string ToLowerExpressionSyntax
235 | {
236 | get { throw new NotImplementedException(); }
237 | }
238 |
239 | public override string ToUpperExpressionSyntax
240 | {
241 | get { throw new NotImplementedException(); }
242 | }
243 |
244 | public override string TrimExpressionSyntax
245 | {
246 | get { return "TRIM(\"SomeColumn\")"; }
247 | }
248 |
249 | public override string TrimStartExpressionSyntax
250 | {
251 | get { return "LTRIM(\"SomeColumn\")"; }
252 | }
253 |
254 | public override string TrimEndExpressionSyntax
255 | {
256 | get { return "RTRIM(\"SomeColumn\")"; }
257 | }
258 |
259 | public override string SubStringExpressionSyntax
260 | {
261 | get { return "SUBSTR(\"SomeColumn\",:?p0,:?p1)"; }
262 | }
263 |
264 | public override string DateExpressionSyntax
265 | {
266 | get { return "TRUNC(\"SomeColumn\")"; }
267 | }
268 |
269 | public override string YearExpressionSyntax
270 | {
271 | get { return "EXTRACT(YEAR FROM \"SomeColumn\")"; }
272 | }
273 |
274 | public override string MonthExpressionSyntax
275 | {
276 | get { throw new NotImplementedException(); }
277 | }
278 |
279 | public override string HourExpressionSyntax
280 | {
281 | get { throw new NotImplementedException(); }
282 | }
283 |
284 | public override string MinuteExpressionSyntax
285 | {
286 | get { return "EXTRACT(MINUTE FROM \"SomeColumn\")"; }
287 | }
288 |
289 | public override string SecondExpressionSyntax
290 | {
291 | get { return "ROUND(EXTRACT(SECOND FROM \"SomeColumn\"))"; }
292 | }
293 |
294 | public override string MillisecondExpressionSyntax
295 | {
296 | get { return "TO_CHAR(\"SomeColumn\",'FF3')"; }
297 | }
298 |
299 | public override string DayOfYearExpressionSyntax
300 | {
301 | get { throw new NotImplementedException(); }
302 | }
303 |
304 | public override string DayOfMonthExpressionSyntax
305 | {
306 | get { return "EXTRACT(DAY FROM \"SomeColumn\")"; }
307 | }
308 |
309 | public override string DayOfWeekExpressionSyntax
310 | {
311 | get { return "TO_CHAR(\"SomeColumn\" ,'D')"; }
312 | }
313 |
314 | public override string AddYearsExpressionSyntax
315 | {
316 | get { throw new NotImplementedException(); }
317 | }
318 |
319 | public override string AddMonthsExpressionSyntax
320 | {
321 | get { throw new NotImplementedException(); }
322 | }
323 |
324 | public override string AddDaysExpressionSyntax
325 | {
326 | get { return "\"SomeColumn\" + :?p0"; }
327 | }
328 |
329 | public override string AddHoursExpressionSyntax
330 | {
331 | get { return "\"SomeColumn\" + (:?p0/24)"; }
332 | }
333 |
334 | public override string AddMinutesExpressionSyntax
335 | {
336 | get { throw new NotImplementedException(); }
337 | }
338 |
339 | public override string AddSecondsExpressionSyntax
340 | {
341 | get { throw new NotImplementedException(); }
342 | }
343 |
344 | public override string AddMilliSecondsExpressionSyntax
345 | {
346 | get { return "\"SomeColumn\" + NUMTODSINTERVAL(:?p0 / 1000, 'SECOND')"; }
347 | }
348 |
349 | public override string NowExpressionSyntax
350 | {
351 | get { throw new NotImplementedException(); }
352 | }
353 |
354 | public override string ToDayExpressionSyntax
355 | {
356 | get { return "TRUNC(CURRENT_DATE)"; }
357 | }
358 |
359 | public override string UpdateExpressionSyntax
360 | {
361 | get { return "UPDATE \"SomeTable\" SET \"SomeColumn\" = :?p0 WHERE (\"SomeColumn\" = :?p1)"; }
362 | }
363 |
364 | public override string UpdateExpressionWithAliasedTargetSyntax
365 | {
366 | get { return "UPDATE \"SomeTable\" t0 SET \"SomeColumn\" = :?p0 WHERE (\"SomeColumn\" = :?p1)"; }
367 | }
368 |
369 | public override string UpdateExpressionWithFromClauseSyntax
370 | {
371 | get { return "UPDATE \"SomeTable\" t0 INNER JOIN \"SomeOtherTable\" t1 ON (\"t0\".\"Id\" = \"t1\".\"Id\") SET \"SomeColumn\" = :?p0 WHERE (\"t0\".\"SomeColumn\" = :?p1)"; }
372 | }
373 |
374 | public override string InsertExpressionSyntax
375 | {
376 | get { return "INSERT INTO \"SomeTable\" (\"SomeColumn\") VALUES(:?p0)"; }
377 | }
378 |
379 | public override string DeleteExpressionSyntax
380 | {
381 | get { throw new NotImplementedException(); }
382 | }
383 |
384 | public override string DeleteExpressionWithAliasedTargetSyntax
385 | {
386 | get { return "DELETE FROM \"SomeTable\" t0 WHERE (\"t0\".\"SomeColumn\" = :?p0)"; }
387 | }
388 |
389 | public override string SelectExpressionSyntax
390 | {
391 | get { return "SELECT \"SomeColumn\" FROM \"SomeTable\""; }
392 | }
393 |
394 | public override string SelectExpressionWithTakeSyntax
395 | {
396 | get { return "SELECT \"SomeColumn\" FROM \"SomeTable\" WHERE ROWNUM <= :?p0"; }
397 | }
398 |
399 | public override string SelectExpressionWithSkipSyntax
400 | {
401 | get { return "SELECT \"SomeColumn\" FROM \"SomeTable\" WHERE ROWNUM > :?p0"; }
402 | }
403 |
404 | public override string SelectExpressionWithSkipAndTakeSyntax
405 | {
406 | get { throw new NotImplementedException(); }
407 | }
408 |
409 | public override string SelectExpressionWithOrderBySyntax
410 | {
411 | get { throw new NotImplementedException(); }
412 | }
413 |
414 | public override string SelectExpressionWithGroupBySyntax
415 | {
416 | get { throw new NotImplementedException(); }
417 | }
418 |
419 | public override string SelectExpressionWithGroupByHavingSyntax
420 | {
421 | get { return "SELECT \"SomeColumn\" FROM \"SomeTable\" GROUP BY \"SomeColumn\" HAVING (\"SomeColumn\" > :?p0)"; }
422 | }
423 |
424 | public override string AbsExpressionSyntax
425 | {
426 | get { return "ABS(\"SomeColumn\")"; }
427 | }
428 |
429 | public override string AcosExpressionSyntax
430 | {
431 | get { throw new NotImplementedException(); }
432 | }
433 |
434 | public override string AsinExpressionSyntax
435 | {
436 | get { return "ASIN(\"SomeColumn\")"; }
437 | }
438 |
439 | public override string AtanExpressionSyntax
440 | {
441 | get { return "ATAN(\"SomeColumn\")"; }
442 | }
443 |
444 | public override string Atan2ExpressionSyntax
445 | {
446 | get { return "ATAN2(\"SomeColumn\",:?p0)"; }
447 | }
448 |
449 | public override string CeilingExpressionSyntax
450 | {
451 | get { return "CEIL(\"SomeColumn\")"; }
452 | }
453 |
454 | public override string CosExpressionSyntax
455 | {
456 | get { return "COS(\"SomeColumn\")"; }
457 | }
458 |
459 | public override string CotExpressionSyntax
460 | {
461 | get { return "COSH(\"SomeColumn\")"; }
462 | }
463 |
464 | public override string DegressExpressionSyntax
465 | {
466 | get { throw new NotImplementedException(); }
467 | }
468 |
469 | public override string ExpExpressionSyntax
470 | {
471 | get { throw new NotImplementedException(); }
472 | }
473 |
474 | public override string FloorExpressionSyntax
475 | {
476 | get { throw new NotImplementedException(); }
477 | }
478 |
479 | public override string LogExpressionSyntax
480 | {
481 | get { throw new NotImplementedException(); }
482 | }
483 |
484 | public override string Log10ExpressionSyntax
485 | {
486 | get { return "LOG(10,\"SomeColumn\")"; }
487 | }
488 |
489 | public override string PIExpressionSyntax
490 | {
491 | get { return "ACOS(-1)"; }
492 | }
493 |
494 | public override string PowerExpressionSyntax
495 | {
496 | get { return "POWER(\"SomeColumn\",:?p0)"; }
497 | }
498 |
499 | public override string RadiansExpressionSyntax
500 | {
501 | get { throw new NotImplementedException(); }
502 | }
503 |
504 | public override string RandExpressionSyntax
505 | {
506 | get { return "dbms_random.value"; }
507 | }
508 |
509 | public override string RandWithSeedExpressionSyntax
510 | {
511 | get { throw new NotImplementedException(); }
512 | }
513 |
514 | public override string RoundExpressionSyntax
515 | {
516 | get { return "ROUND(\"SomeColumn\",:?p0)"; }
517 | }
518 |
519 | public override string SignExpressionSyntax
520 | {
521 | get { throw new NotImplementedException(); }
522 | }
523 |
524 | public override string SinExpressionSyntax
525 | {
526 | get { return "SIN(\"SomeColumn\")"; }
527 | }
528 |
529 | public override string SqrtExpressionSyntax
530 | {
531 | get { return "SQRT(\"SomeColumn\")"; }
532 | }
533 |
534 | public override string SquareExpressionSyntax
535 | {
536 | get { return "POWER(\"SomeColumn\",:?p0)"; }
537 | }
538 |
539 | public override string TanExpressionSyntax
540 | {
541 | get { throw new NotImplementedException(); }
542 | }
543 |
544 | public override string NullCheckEqualsExpressionSyntax
545 | {
546 | get { return "(\"SomeColumn\" IS NULL)"; }
547 | }
548 |
549 | public override string NullCheckNotEqualsExpressionSyntax
550 | {
551 | get { return "(\"SomeColumn\" IS NOT NULL)"; }
552 | }
553 | }
554 | }
555 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 | using System.Security;
5 |
6 | // General Information about an assembly is controlled through the following
7 | // set of attributes. Change these attribute values to modify the information
8 | // associated with an assembly.
9 | [assembly: AssemblyTitle("DbExpressions.Tests")]
10 | [assembly: AssemblyDescription("")]
11 | [assembly: AssemblyConfiguration("")]
12 | [assembly: AssemblyCompany("Microsoft")]
13 | [assembly: AssemblyProduct("DbExpressions.Tests")]
14 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
15 | [assembly: AssemblyTrademark("")]
16 | [assembly: AssemblyCulture("")]
17 |
18 | // Setting ComVisible to false makes the types in this assembly not visible
19 | // to COM components. If you need to access a type in this assembly from
20 | // COM, set the ComVisible attribute to true on that type.
21 | [assembly: ComVisible(false)]
22 |
23 | // The following GUID is for the ID of the typelib if this project is exposed to COM
24 | [assembly: Guid("49e0a954-8ce6-4a67-bd3b-3658116ee6b1")]
25 |
26 | // Version information for an assembly consists of the following four values:
27 | //
28 | // Major Version
29 | // Minor Version
30 | // Build Number
31 | // Revision
32 | //
33 | // You can specify all the values or you can default the Build and Revision Numbers
34 | // by using the '*' as shown below:
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/SQLiteExpressionSyntaxTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
6 |
7 | namespace DbExpressions.Tests
8 | {
9 | //[TestClass]
10 | public class SQLiteExpressionSyntaxTests : DbExpressionSyntaxTests
11 | {
12 | protected override DbQueryTranslator QueryTranslator
13 | {
14 | get { return DbQueryTranslatorFactory.GetQueryTranslator("System.Data.SQLite"); }
15 | }
16 |
17 | public override string AndExpressionSyntax
18 | {
19 | get { return "(@p0 AND @p1)"; }
20 | }
21 |
22 | public override string AddExpressionSyntax
23 | {
24 | get { return "(@p0 + @p1)"; }
25 | }
26 |
27 | public override string AliasExpressionSyntax
28 | {
29 | get { return "[SomeTable] AS t0"; }
30 | }
31 |
32 | public override string AssignExpressionSyntax
33 | {
34 | get { return "[SomeColumn] = @p0"; }
35 | }
36 |
37 | public override string AssignNullExpressionSyntax
38 | {
39 | get { return "[SomeColumn] = NULL"; }
40 | }
41 |
42 | public override string AvgExpressionSyntax
43 | {
44 | get { return "AVG([SomeColumn])"; }
45 | }
46 |
47 | public override string ColumnExpressionSyntax
48 | {
49 | get { return "[SomeColumn]"; }
50 | }
51 |
52 | public override string ConcatExpressionSyntax
53 | {
54 | get { return "[SomeTable] [SomeOtherTable]"; }
55 | }
56 |
57 | public override string ConditionalExpressionSyntax
58 | {
59 | get { return "CASE WHEN ([SomeColumn] = @p0) THEN @p1 ELSE @p2 END"; }
60 | }
61 |
62 | public override string ConstantExpressionSyntax
63 | {
64 | get { return "@p0"; }
65 | }
66 |
67 | public override string CountExpressionSyntax
68 | {
69 | get { return "COUNT([SomeColumn])"; }
70 | }
71 |
72 | public override string DivideExpressionSyntax
73 | {
74 | get { return "(@p0 / @p1)"; }
75 | }
76 |
77 | public override string EqualExpressionSyntax
78 | {
79 | get { return "([SomeColumn] = @p0)"; }
80 | }
81 |
82 | public override string ExistsExpressionSyntax
83 | {
84 | get { return "EXISTS(SELECT [SomeColumn] FROM [SomeTable])"; }
85 | }
86 |
87 | public override string GreaterThanExpressionSyntax
88 | {
89 | get { return "([SomeColumn] > @p0)"; }
90 | }
91 |
92 | public override string GreaterThanOrEqualExpressionSyntax
93 | {
94 | get { return "([SomeColumn] >= @p0)"; }
95 | }
96 |
97 | public override string InValueRangeExpressionSyntax
98 | {
99 | get { return "[SomeColumn] IN(@p0,@p1)"; }
100 | }
101 |
102 | public override string InSubQueryExpressionSyntax
103 | {
104 | get { return "[SomeColumn] IN(SELECT [SomeOtherColumn] FROM [SomeOtherTable])"; }
105 | }
106 |
107 | public override string InnerJoinExpressionSyntax
108 | {
109 | get { return "INNER JOIN [SomeTable] ON ([SomeColumn] = [SomeOtherColumn])"; }
110 | }
111 |
112 | public override string LeftOuterJoinExpressionSyntax
113 | {
114 | get { return "LEFT OUTER JOIN [SomeTable] ON ([SomeColumn] = [SomeOtherColumn])"; }
115 | }
116 |
117 | public override string RightOuterJoinExpressionSyntax
118 | {
119 | get { return "N/A"; }
120 | }
121 |
122 | public override string LengthExpressionSyntax
123 | {
124 | get { return "LENGTH([SomeColumn])"; }
125 | }
126 |
127 | public override string LessThanExpressionSyntax
128 | {
129 | get { return "([SomeColumn] < @p0)"; }
130 | }
131 |
132 | public override string LessThanOrEqualExpressionSyntax
133 | {
134 | get { return "([SomeColumn] <= @p0)"; }
135 | }
136 |
137 | public override string ListExpressionSyntax
138 | {
139 | get { return "[SomeColumn],[SomeOtherColumn]"; }
140 | }
141 |
142 | public override string BatchExpressionSyntax
143 | {
144 | get { return "[SomeColumn];[SomeOtherColumn];"; }
145 | }
146 |
147 | public override string MaxExpressionSyntax
148 | {
149 | get { return "MAX([SomeColumn])"; }
150 | }
151 |
152 | public override string MinExpressionSyntax
153 | {
154 | get { return "MIN([SomeColumn])"; }
155 | }
156 |
157 | public override string MultiplyExpressionSyntax
158 | {
159 | get { return "(@p0 * @p1)"; }
160 | }
161 |
162 | public override string SubstractExpressionSyntax
163 | {
164 | get { return "(@p0 - @p1)"; }
165 | }
166 |
167 | public override string NotExpressionSyntax
168 | {
169 | get { return "NOT ([SomeColumn] = @p0)"; }
170 | }
171 |
172 | public override string DistinctExpressionSyntax
173 | {
174 | get { return "SELECT DISTINCT [SomeColumn]"; }
175 | }
176 |
177 | public override string DistinctWithTopExpressionSyntax
178 | {
179 | get { return "SELECT DISTINCT [SomeColumn] LIMIT @p0"; }
180 | }
181 |
182 | public override string NotEqualExpressionSyntax
183 | {
184 | get { return "([SomeColumn] <> @p0)"; }
185 | }
186 |
187 | public override string OrExpressionSyntax
188 | {
189 | get { return "(@p0 OR @p1)"; }
190 | }
191 |
192 | public override string OrderByAscendingExpressionSyntax
193 | {
194 | get { return "[SomeColumn] ASC"; }
195 | }
196 |
197 | public override string OrderByDescendingExpressionSyntax
198 | {
199 | get { return "[SomeColumn] DESC"; }
200 | }
201 |
202 | public override string PrefixExpressionSyntax
203 | {
204 | get { return "[t0].[SomeColumn]"; }
205 | }
206 |
207 | public override string ReplaceExpressionSyntax
208 | {
209 | get { return "REPLACE([SomeColumn],@p0,@p1)"; }
210 | }
211 |
212 | public override string ReverseExpressionSyntax
213 | {
214 | get { return "REVERSE([SomeColumn])"; }
215 | }
216 |
217 | public override string SoundExExpressionSyntax
218 | {
219 | get { return "SOUNDEX([SomeColumn])"; }
220 | }
221 |
222 | public override string SumExpressionSyntax
223 | {
224 | get { return "SUM([SomeColumn])"; }
225 | }
226 |
227 | public override string TableExpressionSyntax
228 | {
229 | get { return "[SomeTable]"; }
230 | }
231 |
232 | public override string ToLowerExpressionSyntax
233 | {
234 | get { return "LOWER([SomeColumn])"; }
235 | }
236 |
237 | public override string ToUpperExpressionSyntax
238 | {
239 | get { return "UPPER([SomeColumn])"; }
240 | }
241 |
242 | public override string TrimExpressionSyntax
243 | {
244 | get { return "TRIM([SomeColumn])"; }
245 | }
246 |
247 | public override string TrimStartExpressionSyntax
248 | {
249 | get { return "LTRIM([SomeColumn])"; }
250 | }
251 |
252 | public override string TrimEndExpressionSyntax
253 | {
254 | get { return "RTRIM([SomeColumn])"; }
255 | }
256 |
257 | public override string SubStringExpressionSyntax
258 | {
259 | get { return "SUBSTR([SomeColumn],@p0,@p1)"; }
260 | }
261 |
262 | public override string DateExpressionSyntax
263 | {
264 | get { return "DATE([SomeColumn])"; }
265 | }
266 |
267 | public override string YearExpressionSyntax
268 | {
269 | get { return "STRFTIME('%Y',[SomeColumn])"; }
270 | }
271 |
272 | public override string MonthExpressionSyntax
273 | {
274 | get { return "STRFTIME('%m',[SomeColumn])"; }
275 | }
276 |
277 | public override string HourExpressionSyntax
278 | {
279 | get { return "STRFTIME('%H',[SomeColumn])"; }
280 | }
281 |
282 | public override string MinuteExpressionSyntax
283 | {
284 | get { return "STRFTIME('%M',[SomeColumn])"; }
285 | }
286 |
287 | public override string SecondExpressionSyntax
288 | {
289 | get { return "STRFTIME('%S',[SomeColumn])"; }
290 | }
291 |
292 | public override string MillisecondExpressionSyntax
293 | {
294 | get { return "N/A"; }
295 | }
296 |
297 | public override string DayOfYearExpressionSyntax
298 | {
299 | get { return "STRFTIME('%j',[SomeColumn])"; }
300 | }
301 |
302 | public override string DayOfMonthExpressionSyntax
303 | {
304 | get { return "STRFTIME('%d',[SomeColumn])"; }
305 | }
306 |
307 | public override string DayOfWeekExpressionSyntax
308 | {
309 | get { return "STRFTIME('%w',[SomeColumn])"; }
310 | }
311 |
312 | public override string AddYearsExpressionSyntax
313 | {
314 | get { return "DATETIME([SomeColumn], '+@p0 year')"; }
315 | }
316 |
317 | public override string AddMonthsExpressionSyntax
318 | {
319 | get { return "DATETIME([SomeColumn], '+@p0 month')"; }
320 | }
321 |
322 | public override string AddDaysExpressionSyntax
323 | {
324 | get { return "DATETIME([SomeColumn], '+@p0 day')"; }
325 | }
326 |
327 | public override string AddHoursExpressionSyntax
328 | {
329 | get { return "DATETIME([SomeColumn], '+@p0 hour')"; }
330 | }
331 |
332 | public override string AddMinutesExpressionSyntax
333 | {
334 | get { return "DATETIME([SomeColumn], '+@p0 minute')"; }
335 | }
336 |
337 | public override string AddSecondsExpressionSyntax
338 | {
339 | get { return "DATETIME([SomeColumn], '+@p0 second')"; }
340 | }
341 |
342 | public override string AddMilliSecondsExpressionSyntax
343 | {
344 | get { return "N/A"; }
345 | }
346 |
347 | public override string NowExpressionSyntax
348 | {
349 | get { return "DATETIME('now','localtime')"; }
350 | }
351 |
352 | public override string ToDayExpressionSyntax
353 | {
354 | get { return "DATE('now','localtime')"; }
355 | }
356 |
357 | public override string UpdateExpressionSyntax
358 | {
359 | get { return "UPDATE [SomeTable] SET [SomeColumn] = @p0 WHERE ([SomeColumn] = @p1)"; }
360 | }
361 |
362 | public override string UpdateExpressionWithAliasedTargetSyntax
363 | {
364 | get { return "N/A"; }
365 | }
366 |
367 | public override string UpdateExpressionWithFromClauseSyntax
368 | {
369 | get { return "N/A"; }
370 | }
371 |
372 | public override string InsertExpressionSyntax
373 | {
374 | get { return "INSERT INTO [SomeTable] ([SomeColumn]) VALUES(@p0)"; }
375 | }
376 |
377 | public override string DeleteExpressionSyntax
378 | {
379 | get { return "DELETE FROM [SomeTable] WHERE ([SomeColumn] = @p0)"; }
380 | }
381 |
382 | public override string DeleteExpressionWithAliasedTargetSyntax
383 | {
384 | get { return "N/A"; }
385 | }
386 |
387 | public override string SelectExpressionSyntax
388 | {
389 | get { return "SELECT [SomeColumn] FROM [SomeTable]"; }
390 | }
391 |
392 | public override string SelectExpressionWithTakeSyntax
393 | {
394 | get { return "SELECT [SomeColumn] FROM [SomeTable] LIMIT @p0"; }
395 | }
396 |
397 | public override string SelectExpressionWithSkipSyntax
398 | {
399 | get { return "SELECT [SomeColumn] FROM [SomeTable] LIMIT -1 OFFSET @p0"; }
400 | }
401 |
402 | public override string SelectExpressionWithSkipAndTakeSyntax
403 | {
404 | get { return "SELECT [SomeColumn] FROM [SomeTable] LIMIT @p0 OFFSET @p1"; }
405 | }
406 |
407 | public override string SelectExpressionWithOrderBySyntax
408 | {
409 | get { return "SELECT [SomeColumn] FROM [SomeTable] ORDER BY [SomeColumn] ASC"; }
410 | }
411 |
412 | public override string SelectExpressionWithGroupBySyntax
413 | {
414 | get { return "SELECT [SomeColumn] FROM [SomeTable] GROUP BY [SomeColumn]"; }
415 | }
416 |
417 | public override string SelectExpressionWithGroupByHavingSyntax
418 | {
419 | get { return "SELECT [SomeColumn] FROM [SomeTable] GROUP BY [SomeColumn] HAVING ([SomeColumn] > @p0)"; }
420 | }
421 |
422 | public override string AbsExpressionSyntax
423 | {
424 | get { return "ABS([SomeColumn])"; }
425 | }
426 |
427 | public override string AcosExpressionSyntax
428 | {
429 | get { return "ACOS([SomeColumn])"; }
430 | }
431 |
432 | public override string AsinExpressionSyntax
433 | {
434 | get { return "ASIN([SomeColumn])"; }
435 | }
436 |
437 | public override string AtanExpressionSyntax
438 | {
439 | get { return "ATAN([SomeColumn])"; }
440 | }
441 |
442 | public override string Atan2ExpressionSyntax
443 | {
444 | get { return "ATAN2([SomeColumn])"; }
445 | }
446 |
447 | public override string CeilingExpressionSyntax
448 | {
449 | get { return "CEILING([SomeColumn])"; }
450 | }
451 |
452 | public override string CosExpressionSyntax
453 | {
454 | get { return "COS([SomeColumn])"; }
455 | }
456 |
457 | public override string CotExpressionSyntax
458 | {
459 | get { return "COT([SomeColumn])"; }
460 | }
461 |
462 | public override string DegressExpressionSyntax
463 | {
464 | get { return "DEGREES([SomeColumn])"; }
465 | }
466 |
467 | public override string ExpExpressionSyntax
468 | {
469 | get { return "EXP([SomeColumn])"; }
470 | }
471 |
472 | public override string FloorExpressionSyntax
473 | {
474 | get { return "FLOOR([SomeColumn])"; }
475 | }
476 |
477 | public override string LogExpressionSyntax
478 | {
479 | get { return "LOG([SomeColumn])"; }
480 | }
481 |
482 | public override string Log10ExpressionSyntax
483 | {
484 | get { return "LOG10([SomeColumn])"; }
485 | }
486 |
487 | public override string PIExpressionSyntax
488 | {
489 | get { return "PI()"; }
490 | }
491 |
492 | public override string PowerExpressionSyntax
493 | {
494 | get { return "POWER([SomeColumn],@p0)"; }
495 | }
496 |
497 | public override string RadiansExpressionSyntax
498 | {
499 | get { return "RADIANS([SomeColumn])"; }
500 | }
501 |
502 | public override string RandExpressionSyntax
503 | {
504 | get { return "RANDOM()"; }
505 | }
506 |
507 | public override string RandWithSeedExpressionSyntax
508 | {
509 | get { throw new NotImplementedException(); }
510 | }
511 |
512 | public override string RoundExpressionSyntax
513 | {
514 | get { return "ROUND([SomeColumn],@p0)"; }
515 | }
516 |
517 | public override string SignExpressionSyntax
518 | {
519 | get { return "SIGN([SomeColumn])"; }
520 | }
521 |
522 | public override string SinExpressionSyntax
523 | {
524 | get { return "SIN([SomeColumn])"; }
525 | }
526 |
527 | public override string SqrtExpressionSyntax
528 | {
529 | get { return "SQRT([SomeColumn])"; }
530 | }
531 |
532 | public override string SquareExpressionSyntax
533 | {
534 | get { return "SQUARE([SomeColumn])"; }
535 | }
536 |
537 | public override string TanExpressionSyntax
538 | {
539 | get { return "TAN([SomeColumn])"; }
540 | }
541 |
542 | public override string NullCheckEqualsExpressionSyntax
543 | {
544 | get { return "([SomeColumn] IS NULL)"; }
545 | }
546 |
547 | public override string NullCheckNotEqualsExpressionSyntax
548 | {
549 | get { return "([SomeColumn] IS NOT NULL)"; }
550 | }
551 | }
552 | }
553 |
--------------------------------------------------------------------------------
/DbExpressions.Tests/StringExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions.Tests
4 | {
5 | public static class StringExtensions
6 | {
7 | public static string Clean(this string target)
8 | {
9 | return target.Replace(Environment.NewLine, "").Replace("\t", "");
10 | }
11 |
12 | ///
13 | /// Strips of the formatting (new line and tabs)
14 | ///
15 | /// The target string.
16 | ///
17 | public static string Strip(this string target)
18 | {
19 | return target.Replace(Environment.NewLine, "").Replace("\t", "");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/DbExpressions.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbExpressions", "DbExpressions\DbExpressions.csproj", "{EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbExpressions.Tests", "DbExpressions.Tests\DbExpressions.Tests.csproj", "{F53DA910-FC47-4909-81EC-643E8BA9D51E}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8311E6C3-7550-4BC8-9BDF-338F64F2F1C7}"
9 | ProjectSection(SolutionItems) = preProject
10 | DbExpressions.vsmdi = DbExpressions.vsmdi
11 | Local.testsettings = Local.testsettings
12 | TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
13 | EndProjectSection
14 | EndProject
15 | Global
16 | GlobalSection(TestCaseManagementSettings) = postSolution
17 | CategoryFile = DbExpressions.vsmdi
18 | EndGlobalSection
19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
20 | Debug|Any CPU = Debug|Any CPU
21 | Release|Any CPU = Release|Any CPU
22 | EndGlobalSection
23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
24 | {EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25 | {EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
26 | {EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
27 | {EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}.Release|Any CPU.Build.0 = Release|Any CPU
28 | {F53DA910-FC47-4909-81EC-643E8BA9D51E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29 | {F53DA910-FC47-4909-81EC-643E8BA9D51E}.Debug|Any CPU.Build.0 = Debug|Any CPU
30 | {F53DA910-FC47-4909-81EC-643E8BA9D51E}.Release|Any CPU.ActiveCfg = Release|Any CPU
31 | {F53DA910-FC47-4909-81EC-643E8BA9D51E}.Release|Any CPU.Build.0 = Release|Any CPU
32 | EndGlobalSection
33 | GlobalSection(SolutionProperties) = preSolution
34 | HideSolutionNode = FALSE
35 | EndGlobalSection
36 | EndGlobal
37 |
--------------------------------------------------------------------------------
/DbExpressions.vsmdi:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/DbExpressions.vssscc:
--------------------------------------------------------------------------------
1 | ""
2 | {
3 | "FILE_VERSION" = "9237"
4 | "ENLISTMENT_CHOICE" = "NEVER"
5 | "PROJECT_FILE_RELATIVE_PATH" = ""
6 | "NUMBER_OF_EXCLUDED_FILES" = "0"
7 | "ORIGINAL_PROJECT_FILE_PATH" = ""
8 | "NUMBER_OF_NESTED_PROJECTS" = "0"
9 | "SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROJECT"
10 | }
11 |
--------------------------------------------------------------------------------
/DbExpressions/Configuration/DbExpressionSettings.cs:
--------------------------------------------------------------------------------
1 | using System.Configuration;
2 |
3 | namespace DbExpressions.Configuration
4 | {
5 | ///
6 | /// Configures the dbExpression section.
7 | ///
8 | public class DbExpressionSettings : ConfigurationSection
9 | {
10 | ///
11 | /// Gets or sets the name of the default provider.
12 | ///
13 | [ConfigurationProperty("defaultProvider",DefaultValue = "System.Data.SqlClient")]
14 | public string DefaultProvider
15 | {
16 | get { return (string)this["defaultProvider"]; }
17 |
18 | set { this["defaultProvider"] = value; }
19 | }
20 |
21 | ///
22 | /// Gets a list of instances.
23 | ///
24 | [ConfigurationProperty("queryTranslators", IsDefaultCollection = true)]
25 | public QueryTranslatorElementCollection QueryTranslators
26 | {
27 | get { return this["queryTranslators"] as QueryTranslatorElementCollection; }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/DbExpressions/Configuration/QueryTranslatorElement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using System.Configuration;
4 |
5 | namespace DbExpressions.Configuration
6 | {
7 | ///
8 | /// Represents a configuration element in a configuration file.
9 | ///
10 | public class QueryTranslatorElement : ConfigurationElement
11 | {
12 | ///
13 | /// Gets or sets the provider invariant name.
14 | ///
15 | [ConfigurationProperty("providerName")]
16 | public string ProviderName
17 | {
18 | get { return (string)this["providerName"]; }
19 | set { this["providerName"] = value; }
20 | }
21 |
22 | ///
23 | /// Gets or sets the query translator type
24 | ///
25 | [TypeConverter(typeof(TypeNameConverter))]
26 | [ConfigurationProperty("type")]
27 | public Type Type
28 | {
29 | get { return (Type)this["type"]; }
30 | set { this["type"] = value; }
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/DbExpressions/Configuration/QueryTranslatorElementCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Configuration;
2 |
3 | namespace DbExpressions.Configuration
4 | {
5 | ///
6 | /// Represents a list of instances.
7 | ///
8 | public class QueryTranslatorElementCollection : ConfigurationElementCollection
9 | {
10 | ///
11 | /// When overridden in a derived class, creates a new .
12 | ///
13 | ///
14 | /// A new .
15 | ///
16 | protected override ConfigurationElement CreateNewElement()
17 | {
18 | return new QueryTranslatorElement();
19 | }
20 |
21 | ///
22 | /// Gets the element key for a specified configuration element when overridden in a derived class.
23 | ///
24 | ///
25 | /// An that acts as the key for the specified .
26 | ///
27 | /// The to return the key for.
28 | protected override object GetElementKey(ConfigurationElement element)
29 | {
30 | return ((QueryTranslatorElement)element).ProviderName;
31 | }
32 |
33 | ///
34 | /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
35 | ///
36 | ///
37 | /// The name of the collection; otherwise, an empty string. The default is an empty string.
38 | ///
39 | protected override string ElementName
40 | {
41 | get { return "queryTranslator"; }
42 | }
43 |
44 | ///
45 | /// Gets the type of the .
46 | ///
47 | ///
48 | /// The of this collection.
49 | ///
50 | public override ConfigurationElementCollectionType CollectionType
51 | {
52 | get { return ConfigurationElementCollectionType.BasicMap; }
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/DbExpressions/DbExpressions.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.30703
7 | 2.0
8 | {EFB8519C-78AA-48CC-AF0B-7AFE126BE9A1}
9 | Library
10 | Properties
11 | DbExpressions
12 | DbExpressions
13 | v4.0
14 | 512
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | true
26 | full
27 | false
28 | bin\Debug\
29 | DEBUG;TRACE
30 | prompt
31 | 4
32 | bin\Debug\DbExpressions.XML
33 | false
34 | BasicCorrectnessRules.ruleset
35 |
36 |
37 | pdbonly
38 | true
39 | bin\Release\
40 | TRACE
41 | prompt
42 | 4
43 | bin\Release\DbExpressions.XML
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 |
127 |
--------------------------------------------------------------------------------
/DbExpressions/DbExpressions.csproj.vspscc:
--------------------------------------------------------------------------------
1 | ""
2 | {
3 | "FILE_VERSION" = "9237"
4 | "ENLISTMENT_CHOICE" = "NEVER"
5 | "PROJECT_FILE_RELATIVE_PATH" = ""
6 | "NUMBER_OF_EXCLUDED_FILES" = "0"
7 | "ORIGINAL_PROJECT_FILE_PATH" = ""
8 | "NUMBER_OF_NESTED_PROJECTS" = "0"
9 | "SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER"
10 | }
11 |
--------------------------------------------------------------------------------
/DbExpressions/DbGroupByExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents the 'GROUP BY' clause of the query.
5 | ///
6 | public class DbGroupByExpression : DbExpression
7 | {
8 |
9 | ///
10 | /// Initializes a new instance of the class.
11 | ///
12 | internal DbGroupByExpression() {}
13 |
14 | ///
15 | /// Gets the target used to create the group.
16 | ///
17 | public DbExpression TargetExpression { get; internal set; }
18 |
19 | ///
20 | /// Gets the that represents a search condition for a group.
21 | ///
22 | public DbExpression HavingExpression { get; internal set; }
23 |
24 | ///
25 | /// Gets the of the .
26 | ///
27 | public override DbExpressionType ExpressionType
28 | {
29 | get { return DbExpressionType.GroupBy; }
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/DbExpressions/DbQueryTranslatorFactory.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Configuration;
4 | using System.Data.Common;
5 | using System.Reflection;
6 | using DbExpressions.Configuration;
7 | using System.Linq;
8 |
9 | namespace DbExpressions
10 | {
11 | ///
12 | /// Factory class that creates instances.
13 | ///
14 | public static class DbQueryTranslatorFactory
15 | {
16 | private static readonly ConcurrentDictionary QueryTranslatorTypes
17 | = new ConcurrentDictionary();
18 |
19 | private static string _defaultProviderName;
20 |
21 | static DbQueryTranslatorFactory()
22 | {
23 | RegisterBuiltinTranslators();
24 | _defaultProviderName = "System.Data.SqlClient";
25 | RegisterConfiguredTranslators();
26 | }
27 |
28 | private static void RegisterConfiguredTranslators()
29 | {
30 | DbExpressionSettings dbExpressionSettings;
31 | try
32 | {
33 | dbExpressionSettings =
34 | (DbExpressionSettings)ConfigurationManager.GetSection("dbExpressions");
35 | }
36 | catch (Exception ex)
37 | {
38 |
39 | throw;
40 | }
41 |
42 | if (dbExpressionSettings == null)
43 | return;
44 | _defaultProviderName = dbExpressionSettings.DefaultProvider;
45 | foreach (var translatorElement in dbExpressionSettings.QueryTranslators)
46 | {
47 | var queryTranslatorElement = (QueryTranslatorElement) translatorElement;
48 | RegisterQueryTranslator(queryTranslatorElement.ProviderName,queryTranslatorElement.Type);
49 | }
50 | }
51 |
52 | private static void RegisterBuiltinTranslators()
53 | {
54 | RegisterQueryTranslator("System.Data.SqlClient",typeof(SqlQueryTranslator));
55 | RegisterQueryTranslator("MySql.Data.MySqlClient", typeof(MySqlQueryTranslator));
56 | RegisterQueryTranslator("System.Data.SQLite", typeof(SQLiteQueryTranslator));
57 | RegisterQueryTranslator("Oracle.DataAccess.Client", typeof(OracleQueryTranslator));
58 | }
59 |
60 | ///
61 | /// Gets a new instance.
62 | ///
63 | /// The invariant provider name.
64 | ///
65 | public static DbQueryTranslator GetQueryTranslator(string invariantProviderName)
66 | {
67 | Type translatorType;
68 | QueryTranslatorTypes.TryGetValue(invariantProviderName, out translatorType);
69 | if (translatorType == null)
70 | throw new ArgumentOutOfRangeException("invariantProviderName",
71 | string.Format("No query translator found for the target provider : {0}",invariantProviderName));
72 | return CreateInstance(invariantProviderName, translatorType);
73 |
74 | }
75 |
76 | ///
77 | /// Gets a new instance according to the configured default provider name.
78 | ///
79 | ///
80 | /// The default value is "System.Data.SqlClient", but this can be overridden in app/web.config.
81 | /// The default provider is also used to translate an expression in the method.
82 | ///
83 | ///
84 | ///
85 | public static DbQueryTranslator GetQueryTranslator()
86 | {
87 | return GetQueryTranslator(_defaultProviderName);
88 | }
89 |
90 | ///
91 | /// Registers a new type for the target provider.
92 | ///
93 | /// The invarient name of the provider.
94 | /// The concrete implementation of that targets this provider.
95 | public static void RegisterQueryTranslator(string providerInvariantName, Type queryTranslatorType)
96 | {
97 | QueryTranslatorTypes.AddOrUpdate(providerInvariantName,
98 | queryTranslatorType, (key, oldValue) => queryTranslatorType);
99 | }
100 |
101 | ///
102 | /// Specifies the name of the default provider that will be used if otherwise not specified.
103 | /// This will also be used in the ToString() override to provide a textual representation of the query during debugging.
104 | ///
105 | ///
106 | public static void SetDefaultProvider(string providerInvariantName)
107 | {
108 | _defaultProviderName = providerInvariantName;
109 | }
110 |
111 |
112 |
113 | private static DbQueryTranslator CreateInstance(string providerInvariantName, Type type)
114 | {
115 | var providerFactory = CreateProviderFactory(providerInvariantName);
116 | return (DbQueryTranslator)Activator.CreateInstance(type, new object[] {providerFactory});
117 | }
118 |
119 | private static DbProviderFactory CreateProviderFactory(string providerInvariantName)
120 | {
121 | return DbProviderFactories.GetFactory(providerInvariantName);
122 | }
123 |
124 |
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/DbExpressions/DbTranslateResult.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Data;
3 | using System.Data.Common;
4 |
5 | namespace DbExpressions
6 | {
7 | ///
8 | /// Represents the result of translating a tree.
9 | ///
10 | public class DbTranslateResult
11 | {
12 | private readonly DbProviderFactory _dbProviderFactory;
13 |
14 | ///
15 | /// Initializes a new instance of the class.
16 | ///
17 | /// The generated SQL statement.
18 | /// A of
19 | /// instances that are referenced in the query.
20 | /// The instance.
21 | internal DbTranslateResult(string sql, IEnumerable parameters, DbProviderFactory dbProviderFactory)
22 | {
23 | _dbProviderFactory = dbProviderFactory;
24 | Sql = sql;
25 | Parameters = parameters;
26 | }
27 |
28 | ///
29 | /// Gets or sets a that contains the
30 | /// instances that has been created for constant expressions.
31 | ///
32 | public IEnumerable Parameters { get; private set; }
33 |
34 | ///
35 | /// Gets the Sql statement that has been created from a tree.
36 | ///
37 | public string Sql { get; private set; }
38 |
39 |
40 | ///
41 | /// Creates a new instance based on this .
42 | ///
43 | ///
44 | public IDbCommand CreateCommand()
45 | {
46 | var command = _dbProviderFactory.CreateCommand();
47 | if (command != null)
48 | {
49 | command.CommandText = Sql;
50 | foreach (var dataParameter in Parameters)
51 | {
52 | command.Parameters.Add(dataParameter);
53 | }
54 | return command;
55 | }
56 | return null;
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbAggregateFunctionExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Specifies the type of .
5 | ///
6 | public enum DbAggregateFunctionExpressionType
7 | {
8 | ///
9 | /// A node that represents returning the number of items in the expression.
10 | ///
11 | Count,
12 |
13 | ///
14 | /// Returns the minimum value in the expression.
15 | ///
16 | Min,
17 |
18 | ///
19 | /// Returns the maximum value in the expression.
20 | ///
21 | Max,
22 |
23 | ///
24 | /// A node that represents returning the average of the values in the expression.
25 | ///
26 | Avg,
27 |
28 | ///
29 | /// A node that represents returning the sum of all values in the expression.
30 | ///
31 | Sum
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbBinaryExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Specifies the type of .
5 | ///
6 | public enum DbBinaryExpressionType
7 | {
8 | ///
9 | /// A node that represents a logical AND operation.
10 | ///
11 | And,
12 |
13 | ///
14 | /// A node that represents a logical OR operation.
15 | ///
16 | Or,
17 |
18 | ///
19 | /// A node that represents an equality comparison.
20 | ///
21 | Equal,
22 |
23 | ///
24 | /// A node that represents an inequality comparison.
25 | ///
26 | NotEqual,
27 |
28 | ///
29 | /// A node that represents a "greater than" comparison.
30 | ///
31 | GreaterThan,
32 |
33 | ///
34 | /// A node that represents a "greater than or equal" comparison.
35 | ///
36 | GreaterThanOrEqual,
37 |
38 | ///
39 | /// A node that represents a "less than" comparison.
40 | ///
41 | LessThan,
42 |
43 | ///
44 | /// A node that represents a "less than or equal" comparison.
45 | ///
46 | LessThanOrEqual,
47 |
48 | ///
49 | /// A node that represents arithmetic addition.
50 | ///
51 | Add,
52 |
53 | ///
54 | /// A node that represents arithmetic subtraction.
55 | ///
56 | Subtract,
57 |
58 | ///
59 | /// A node that represents arithmetic multiplication.
60 | ///
61 | Multiply,
62 |
63 | ///
64 | /// A node that represents arithmetic division.
65 | ///
66 | Divide,
67 |
68 | ///
69 | /// A node that represents a column or variable assignment
70 | ///
71 | Assignment
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbDateTimeFunctionExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Describes the node types that represents calling date/time functions.
5 | ///
6 | public enum DbDateTimeFunctionExpressionType
7 | {
8 | ///
9 | /// A node that represents adding the specified number of years to a date/time.
10 | ///
11 | AddYears,
12 |
13 | ///
14 | /// A node that represents adding the specified number of months to a date/time.
15 | ///
16 | AddMonths,
17 |
18 | ///
19 | /// A node that represents adding the specified number of days to a date/time.
20 | ///
21 | AddDays,
22 |
23 | ///
24 | /// A node that represents adding the specified number of hours to a date/time.
25 | ///
26 | AddHours,
27 |
28 | ///
29 | /// A node that represents adding the specified number of minutes to a date/time.
30 | ///
31 | AddMinutes,
32 |
33 | ///
34 | /// A node that represents adding the specified number of seconds to a date/time.
35 | ///
36 | AddSeconds,
37 |
38 | ///
39 | /// A node that represents adding the specified number of milliseconds to a date/time.
40 | ///
41 | AddMilliseconds,
42 |
43 | ///
44 | /// A node that represents returning the date component of a date/time value.
45 | ///
46 | Date,
47 |
48 | ///
49 | /// A node that represents returning the day of the month.
50 | ///
51 | DayOfMonth,
52 |
53 | ///
54 | /// A node that represents returning the day of the week.
55 | ///
56 | DayOfWeek,
57 |
58 | ///
59 | /// A node that represents returning the day of the year.
60 | ///
61 | DayOfYear,
62 |
63 | ///
64 | /// A node that represents returning the year component of a date/time value.
65 | ///
66 | Year,
67 |
68 | ///
69 | /// A node that represents returning the month component of a date/time value.
70 | ///
71 | Month,
72 |
73 | ///
74 | /// A node that represents returning the hour component of a date/time value.
75 | ///
76 | Hour,
77 |
78 | ///
79 | /// A node that represents returning the millisecond component of a date/time value.
80 | ///
81 | MilliSecond,
82 |
83 | ///
84 | /// A node that represents returning the minute component of a date/time value.
85 | ///
86 | Minute,
87 |
88 | ///
89 | /// A node that represents returning the current date and time.
90 | ///
91 | Now,
92 |
93 | ///
94 | /// A node that represents returning the second component of a date/time value.
95 | ///
96 | Second,
97 |
98 | ///
99 | /// A node that represents returning the current date.
100 | ///
101 | ToDay
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Describes the node types for the nodes of an tree.
5 | ///
6 | ///
7 | public enum DbExpressionType
8 | {
9 | ///
10 | /// A node that represents a ,
11 | /// .
12 | ///
13 | Binary,
14 |
15 | ///
16 | /// A node that represents a list of instances.
17 | ///
18 | List,
19 |
20 | ///
21 | /// A node that represents the execution of multiple statements as one batch.
22 | ///
23 | Batch,
24 |
25 | ///
26 | /// A node that represents calling a function.
27 | ///
28 | Function,
29 |
30 | ///
31 | /// A node that represents a that has a constant value.
32 | ///
33 | Constant,
34 |
35 | ///
36 | /// A node the represents the 'SELECT' clause of the query.
37 | ///
38 | Select,
39 |
40 | ///
41 | /// A node the represents the 'INSERT' clause of the query.
42 | ///
43 | Insert,
44 |
45 | ///
46 | /// A node the represents the 'UPDATE' clause of the query.
47 | ///
48 | Update,
49 |
50 | ///
51 | /// A node the represents the 'DELETE' clause of the query.
52 | ///
53 | Delete,
54 |
55 | ///
56 | /// A node that represents a .
57 | ///
58 | Query,
59 |
60 | ///
61 | /// A node that represents an element in the 'ORDER BY' clause of the query.
62 | ///
63 | OrderBy,
64 |
65 | ///
66 | /// A node the represents the 'GROUP BY' clause of the query.
67 | ///
68 | GroupBy,
69 |
70 | ///
71 | /// A node that represents a table reference in the query.
72 | ///
73 | Table,
74 |
75 | ///
76 | /// A node that represents a column reference in the query.
77 | ///
78 | Column,
79 |
80 | ///
81 | /// A node that represents a 'JOIN' in the query.
82 | ///
83 | Join,
84 |
85 | ///
86 | /// A node that represents an 'EXISTS' subquery.
87 | ///
88 | Exists,
89 |
90 | ///
91 | /// A node that represents an alised .
92 | ///
93 | Alias,
94 |
95 | ///
96 | /// A node that represents a prefix.
97 | ///
98 | Prefix,
99 |
100 | ///
101 | /// A node the represents a .
102 | ///
103 | Unary,
104 |
105 | ///
106 | /// A node that represents a .
107 | ///
108 | Conditional,
109 |
110 | ///
111 | /// A node that represents a .
112 | ///
113 | Concat,
114 |
115 | ///
116 | /// A node that represents a .
117 | ///
118 | In,
119 |
120 | ///
121 | /// A node that represents a sql fragment.
122 | ///
123 | Sql
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbFunctionExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Describes the node types for the nodes of an tree.
5 | ///
6 | ///
7 | public enum DbFunctionExpressionType
8 | {
9 | ///
10 | /// A node that represents a string function.
11 | ///
12 | String,
13 |
14 | ///
15 | /// A node that represents a datetime function.
16 | ///
17 | DateTime,
18 |
19 | ///
20 | /// A node that represents an aggregate function.
21 | ///
22 | Aggregate,
23 |
24 | ///
25 | /// A node that represents a mathematical function.
26 | ///
27 | Mathematical
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbJoinExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Specifies the type of .
5 | ///
6 | public enum DbJoinExpressionType
7 | {
8 | ///
9 | /// Specifies all matching pairs of rows are returned.
10 | ///
11 | InnerJoin,
12 | ///
13 | /// Specifies that all rows from the left table
14 | /// not meeting the join condition are included in the result set,
15 | /// and output columns from the other table are set to NULL
16 | /// in addition to all rows returned by the inner join.
17 | ///
18 | LeftOuterJoin,
19 |
20 | ///
21 | /// Specifies all rows from the right table
22 | /// not meeting the join condition are included in the result set,
23 | /// and output columns that correspond to the other table are set to NULL,
24 | /// in addition to all rows returned by the inner join.
25 | ///
26 | RightOuterJoin
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbMathematicalFunctionExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Describes the node types that represents calling mathematical functions.
5 | ///
6 | public enum DbMathematicalFunctionExpressionType
7 | {
8 | ///
9 | /// A node that represents returning the absolute (positive) value of the specified numeric expression.
10 | ///
11 | Abs,
12 |
13 | ///
14 | /// A node that represents returning the angle, in radians, whose cosine is the specified float expression; also called arccosine.
15 | ///
16 | Acos,
17 |
18 | ///
19 | /// A node that represents returning the angle, in radians, whose sine is the specified float expression. This is also called arcsine.
20 | ///
21 | Asin,
22 |
23 | ///
24 | /// A node that represents returning the angle in radians whose tangent is a specified float expression. This is also called arctangent.
25 | ///
26 | Atan,
27 |
28 | ///
29 | /// A node that represents returning the angle, in radians, between the positive x-axis and the ray from the origin to the point (y, x),
30 | /// where x and y are the values of the two specified float expressions.
31 | ///
32 | Atan2,
33 |
34 | ///
35 | /// A node that represents returning the smallest integer greater than, or equal to, the specified numeric expression.
36 | ///
37 | Ceiling,
38 |
39 | ///
40 | /// A node that represents returning the trigonometric cosine of the specified angle, in radians, in the specified expression.
41 | ///
42 | Cos,
43 |
44 | ///
45 | /// A node that represents returning the trigonometric cotangent of the specified angle, in radians, in the specified float expression.
46 | ///
47 | Cot,
48 |
49 | ///
50 | /// A node that represents returning the corresponding angle in degrees for an angle specified in radians.
51 | ///
52 | Degrees,
53 |
54 | ///
55 | /// A node that represents returning the exponential value of the specified float expression.
56 | ///
57 | Exp,
58 |
59 | ///
60 | /// A node that represents returning the largest integer less than or equal to the specified numeric expression.
61 | ///
62 | Floor,
63 |
64 | ///
65 | /// A node that represents returning the natural logarithm of the specified float expression.
66 | ///
67 | Log,
68 |
69 | ///
70 | /// A node that represents returning the base-10 logarithm of the specified float expression.
71 | ///
72 | Log10,
73 |
74 | ///
75 | /// A node that represents returning the constant value of PI.
76 | ///
77 | PI,
78 |
79 | ///
80 | /// A node that represents returning the value of the specified expression to the specified power.
81 | ///
82 | Power,
83 |
84 | ///
85 | /// A node that represents returning the radians of the specified numeric expression.
86 | ///
87 | Radians,
88 |
89 | ///
90 | /// A node that represents returning a pseudo-random float value from 0 through 1
91 | ///
92 | Rand,
93 |
94 | ///
95 | /// A node that represents returning a pseudo-random float value from 0 through 1 using a seed value.
96 | ///
97 | RandSeed,
98 |
99 | ///
100 | /// A node that represents returning a numeric value, rounded to the specified length or precision.
101 | ///
102 | Round,
103 |
104 | ///
105 | /// A node that represents returning the positive (+1), zero (0), or negative (-1) sign of the specified expression.
106 | ///
107 | Sign,
108 |
109 | ///
110 | /// A node that represents returning the trigonometric sine of the specified angle, in radians, and in an approximate numeric, float, expression.
111 | ///
112 | Sin,
113 |
114 | ///
115 | /// A node that represents returning the square root of the specified float value.
116 | ///
117 | Sqrt,
118 |
119 | ///
120 | /// A node that represents returning the square of the specified float value.
121 | ///
122 | Square,
123 |
124 | ///
125 | /// A node that represents returning the tangent of the input expression.
126 | ///
127 | Tan
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbOrderByExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Describes the possible sort directions for an .
5 | ///
6 | public enum DbOrderByExpressionType
7 | {
8 | ///
9 | /// Sorts the element in an ascending order.
10 | ///
11 | Ascending,
12 |
13 | ///
14 | /// Sorts the element in an descending order.
15 | ///
16 | Descending
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbQueryType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Specifies the type of .
5 | ///
6 | public enum DbQueryType
7 | {
8 | ///
9 | /// A node that represents a 'SELECT' query.
10 | ///
11 | Select,
12 | ///
13 | /// A node that represents a 'UPDATE' query.
14 | ///
15 | Update,
16 | ///
17 | /// A node that represents a 'INSERT' query.
18 | ///
19 | Insert,
20 |
21 | ///
22 | /// A node that represents a 'DELETE' query.
23 | ///
24 | Delete,
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbStringFunctionExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Describes the node types that represents calling string functions.
5 | ///
6 | public enum DbStringFunctionExpressionType
7 | {
8 | ///
9 | /// A node that represents changing a string to uppercase.
10 | ///
11 | ToUpper,
12 |
13 | ///
14 | /// A node that represents changing a string to lowercase.
15 | ///
16 | ToLower,
17 |
18 | ///
19 | /// A node that represents returning the length of a string.
20 | ///
21 | Length,
22 |
23 | ///
24 | /// A node that represents removing all leading and trailing spaces from a string.
25 | ///
26 | Trim,
27 |
28 | ///
29 | /// A node that represents removing all leading spaces from a string.
30 | ///
31 | TrimStart,
32 |
33 | ///
34 | /// A node that represents removing all trailing spaces from a string.
35 | ///
36 | TrimEnd,
37 |
38 | ///
39 | /// A node that represents replacing
40 | /// all occurrences of a specified string value with another string value.
41 | ///
42 | Replace,
43 |
44 | ///
45 | /// A node that represents reversing a string value.
46 | ///
47 | Reverse,
48 |
49 | ///
50 | /// A node that represents returning a four-character code to evaluate the similarity
51 | /// of two strings.
52 | ///
53 | SoundEx,
54 |
55 | ///
56 | /// A node that represents retriving a substring from a string.
57 | ///
58 | SubString
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/DbExpressions/Enums/DbUnaryExpressionType.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Specifies the type of .
5 | ///
6 | public enum DbUnaryExpressionType
7 | {
8 | ///
9 | /// A node that represents negating the result of a boolean
10 | ///
11 | Not,
12 |
13 | ///
14 | /// A node that represents a conversion operation.
15 | ///
16 | Cast,
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbAggregateFunctionExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents calling an aggregate function in the target DBMS.
5 | ///
6 | public class DbAggregateFunctionExpression : DbFunctionExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The of the .
12 | /// The arguments used when calling the function.
13 | internal DbAggregateFunctionExpression(DbAggregateFunctionExpressionType aggregateFunctionExpressionType, DbExpression[] arguments)
14 | : base(DbFunctionExpressionType.Aggregate, arguments)
15 | {
16 | AggregateFunctionExpressionType = aggregateFunctionExpressionType;
17 | }
18 |
19 | ///
20 | /// Gets the for this .
21 | ///
22 | public DbAggregateFunctionExpressionType AggregateFunctionExpressionType { get; private set; }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbAliasExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents an aliased
5 | ///
6 | public class DbAliasExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The target to be aliased.
12 | /// the alias to be used to reference the
13 | internal DbAliasExpression(DbExpression target, string alias)
14 | {
15 | Target = target;
16 | Alias = alias;
17 | }
18 |
19 | ///
20 | /// Gets the target to be aliased.
21 | ///
22 | public DbExpression Target { get; private set; }
23 |
24 | ///
25 | /// Gets the alias to be used to reference the
26 | ///
27 | public string Alias { get; private set; }
28 |
29 | ///
30 | /// Gets the of the .
31 | ///
32 | ///
33 | public override DbExpressionType ExpressionType
34 | {
35 | get { return DbExpressionType.Alias; }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbBatchExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// Represents a batch of instances.
7 | ///
8 | public class DbBatchExpression : DbListExpression
9 | {
10 | ///
11 | /// Initializes a new instance of the class.
12 | ///
13 | internal DbBatchExpression()
14 | {
15 | }
16 |
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | /// A that
21 | /// contains instances to be copied to the new list.
22 | internal DbBatchExpression(IEnumerable dbExpressions) : base(dbExpressions)
23 | {
24 | }
25 |
26 | ///
27 | /// Gets the of the .
28 | ///
29 | ///
30 | public override DbExpressionType ExpressionType
31 | {
32 | get { return DbExpressionType.Batch; }
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbBinaryExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a that has a binary operator.
5 | ///
6 | public class DbBinaryExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The that this represents.
12 | /// The left expression.
13 | /// The right expression.
14 | internal DbBinaryExpression(DbBinaryExpressionType binaryExpressionType, DbExpression leftExpression, DbExpression rightExpression)
15 | {
16 | BinaryExpressionType = binaryExpressionType;
17 | LeftExpression = leftExpression;
18 | RightExpression = rightExpression;
19 | }
20 |
21 | ///
22 | /// Gets the left operand of the binary operation.
23 | ///
24 | public DbExpression LeftExpression { get; private set; }
25 |
26 | ///
27 | /// Gets the right operand of the binary operation.
28 | ///
29 | public DbExpression RightExpression { get; private set; }
30 |
31 | ///
32 | /// Gets the of the .
33 | ///
34 | public override DbExpressionType ExpressionType
35 | {
36 | get { return DbExpressionType.Binary; }
37 | }
38 |
39 | ///
40 | /// Gets the that this represents.
41 | ///
42 | public DbBinaryExpressionType BinaryExpressionType { get; private set; }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbColumnExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a column reference in the query.
5 | ///
6 | public class DbColumnExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The name of the column.
12 | internal DbColumnExpression(string columnName)
13 | {
14 | ColumnName = columnName;
15 | }
16 |
17 | ///
18 | /// Gets the of the .
19 | ///
20 | public override DbExpressionType ExpressionType
21 | {
22 | get { return DbExpressionType.Column; }
23 | }
24 |
25 | ///
26 | /// Gets the name of the column.
27 | ///
28 | public string ColumnName { get; private set; }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbConcatExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a concatenate operation between two instances.
5 | ///
6 | public class DbConcatExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | internal DbConcatExpression()
12 | {
13 | }
14 |
15 | ///
16 | /// Gets the left operand of the concatenate operation.
17 | ///
18 | public DbExpression LeftExpression { get; internal set; }
19 |
20 | ///
21 | /// Gets the right operand of the concatenate operation.
22 | ///
23 | public DbExpression RightExpression { get; internal set; }
24 |
25 | ///
26 | /// Gets the of the .
27 | ///
28 | ///
29 | public override DbExpressionType ExpressionType
30 | {
31 | get { return DbExpressionType.Concat; }
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbConditionalExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a that has a conditional operator.
5 | ///
6 | public class DbConditionalExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | internal DbConditionalExpression()
12 | {
13 | }
14 |
15 | ///
16 | /// Gets the that is executed if the evaluates to true.
17 | ///
18 | public DbExpression IfTrue { get; internal set; }
19 |
20 | ///
21 | /// Gets the that is executed if the evaluates to false.
22 | ///
23 | public DbExpression IfFalse { get; internal set; }
24 |
25 | ///
26 | /// Gets the that is evaluated.
27 | ///
28 | public DbExpression Condition { get; internal set; }
29 |
30 | ///
31 | /// Gets the of the .
32 | ///
33 | ///
34 | public override DbExpressionType ExpressionType
35 | {
36 | get { return DbExpressionType.Conditional; }
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbConstantExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a that has a constant value.
5 | ///
6 | public class DbConstantExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The value of the constant expression.
12 | internal DbConstantExpression(object value)
13 | {
14 | Value = value;
15 | }
16 |
17 | ///
18 | /// Gets the value of the constant expression.
19 | ///
20 | public object Value { get; private set; }
21 |
22 | ///
23 | /// Gets the of the .
24 | ///
25 | public override DbExpressionType ExpressionType
26 | {
27 | get { return DbExpressionType.Constant; }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbDateTimeFunctionExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents calling a built in date/time function in the target DBMS.
5 | ///
6 | public class DbDateTimeFunctionExpression : DbFunctionExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The of the .
12 | /// The arguments used when calling the function.
13 | internal DbDateTimeFunctionExpression(DbDateTimeFunctionExpressionType dateTimeFunctionExpressionType, DbExpression[] arguments)
14 | : base(DbFunctionExpressionType.DateTime, arguments)
15 | {
16 | DateTimeFunctionExpressionType = dateTimeFunctionExpressionType;
17 | }
18 |
19 | ///
20 | /// Gets the for this .
21 | ///
22 | public DbDateTimeFunctionExpressionType DateTimeFunctionExpressionType { get; private set; }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbDeleteExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a 'DELETE' database query.
5 | ///
6 | public class DbDeleteExpression : DbQueryExpression
7 | {
8 | ///
9 | /// Gets the of the .
10 | ///
11 | ///
12 | public override DbExpressionType ExpressionType
13 | {
14 | get { return DbExpressionType.Delete; }
15 | }
16 |
17 | ///
18 | /// Gets or sets the target that represents the database table to delete from.
19 | ///
20 | public DbExpression Target { get; set; }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbExistsExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a subquery to test for existence of rows.
5 | ///
6 | public class DbExistsExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The that is used as the sub query.
12 | internal DbExistsExpression(DbSelectExpression subSelectExpression)
13 | {
14 | SubSelectExpression = subSelectExpression;
15 | }
16 |
17 | ///
18 | /// Gets the that is used as the sub query.
19 | ///
20 | public DbSelectExpression SubSelectExpression { get; private set; }
21 |
22 | ///
23 | /// Gets the of the .
24 | ///
25 | ///
26 | public override DbExpressionType ExpressionType
27 | {
28 | get { return DbExpressionType.Exists; }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbExpression.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// Provides the base class from which the classes that represent expression tree nodes are derived.
7 | ///
8 | public abstract class DbExpression
9 | {
10 | private static readonly DbExpressionFactory ExpressionFactory = new DbExpressionFactory();
11 |
12 | [ThreadStatic]
13 | private static DbQueryTranslator dbQueryTranslator;
14 |
15 | ///
16 | /// Gets the of the .
17 | ///
18 | public abstract DbExpressionType ExpressionType { get; }
19 |
20 | #region Operator Overloads
21 |
22 | ///
23 | /// Implements the operator ==.
24 | ///
25 | /// The left expression.
26 | /// The right expression.
27 | /// A instance.
28 | public static DbExpression operator ==(DbExpression leftExpression, DbExpression rightExpression)
29 | {
30 | return ExpressionFactory.Equal(
31 | leftExpression.IsNull() ? ExpressionFactory.Constant(null) : leftExpression,
32 | rightExpression.IsNull() ? ExpressionFactory.Constant(null) : rightExpression);
33 | }
34 |
35 | ///
36 | /// Implements the operator ==.
37 | ///
38 | /// The left expression.
39 | /// The value to be compared with .
40 | /// A instance.
41 | public static DbExpression operator ==(DbExpression leftExpression, object value)
42 | {
43 | return ExpressionFactory.Equal(leftExpression, ExpressionFactory.Constant(value));
44 | }
45 |
46 | ///
47 | /// Implements the operator ==.
48 | ///
49 | /// The value to be compared with .
50 | /// The right expression.
51 | /// A instance.
52 | public static DbExpression operator ==(object value, DbExpression rightExpression)
53 | {
54 | return ExpressionFactory.Equal(ExpressionFactory.Constant(value), rightExpression);
55 | }
56 |
57 | ///
58 | /// Implements the operator !=.
59 | ///
60 | /// The left expression.
61 | /// The right expression.
62 | /// A instance.
63 | public static DbExpression operator !=(DbExpression leftExpression, DbExpression rightExpression)
64 | {
65 | return ExpressionFactory.NotEqual(
66 | leftExpression.IsNull() ? ExpressionFactory.Constant(null) : leftExpression,
67 | rightExpression.IsNull() ? ExpressionFactory.Constant(null) : rightExpression);
68 | }
69 |
70 | ///
71 | /// Implements the operator !=.
72 | ///
73 | /// The value to be compared with .
74 | /// The right expression.
75 | /// A instance.
76 | public static DbExpression operator !=(object value, DbExpression rightExpression)
77 | {
78 | return ExpressionFactory.NotEqual(ExpressionFactory.Constant(value), rightExpression);
79 | }
80 |
81 | ///
82 | /// Implements the operator !=.
83 | ///
84 | /// The left expression.
85 | /// The value to be compared with .
86 | /// A instance.
87 | public static DbExpression operator !=(DbExpression leftExpression, object value)
88 | {
89 | return ExpressionFactory.NotEqual(leftExpression, ExpressionFactory.Constant(value));
90 | }
91 |
92 | ///
93 | /// Implements the operator >.
94 | ///
95 | /// The left expression.
96 | /// The right expression.
97 | /// A instance.
98 | public static DbExpression operator >(DbExpression leftExpression, DbExpression rightExpression)
99 | {
100 | return ExpressionFactory.GreaterThan(leftExpression, rightExpression);
101 | }
102 |
103 | ///
104 | /// Implements the less than operator.
105 | ///
106 | /// The left expression.
107 | /// The right expression.
108 | /// A instance.
109 | public static DbExpression operator <(DbExpression leftExpression, DbExpression rightExpression)
110 | {
111 | return ExpressionFactory.LessThan(leftExpression, rightExpression);
112 | }
113 |
114 | ///
115 | /// Implements the operator >.
116 | ///
117 | /// The left expression.
118 | /// The value to be compared with .
119 | /// A instance.
120 | public static DbExpression operator >(DbExpression leftExpression, object value)
121 | {
122 | return ExpressionFactory.GreaterThan(leftExpression, ExpressionFactory.Constant(value));
123 | }
124 |
125 | ///
126 | /// Implements the less than operator.
127 | ///
128 | /// The left expression.
129 | /// The value to be compared with .
130 | /// A instance.
131 | public static DbExpression operator <(DbExpression leftExpression, object value)
132 | {
133 | return ExpressionFactory.LessThan(leftExpression, ExpressionFactory.Constant(value));
134 | }
135 |
136 | ///
137 | /// Implements the less than operator.
138 | ///
139 | /// The value to be compared with .
140 | /// The right expression.
141 | /// A instance.
142 | public static DbExpression operator <(object value, DbExpression rightExpression)
143 | {
144 | return ExpressionFactory.LessThan(ExpressionFactory.Constant(value), rightExpression);
145 | }
146 |
147 | ///
148 | /// Implements the greater than operator.
149 | ///
150 | /// The value to be compared with .
151 | /// The right expression.
152 | /// A instance.
153 | public static DbExpression operator >(object value, DbExpression rightExpression)
154 | {
155 | return ExpressionFactory.GreaterThan(ExpressionFactory.Constant(value), rightExpression);
156 | }
157 |
158 | ///
159 | /// Implements the greater than or equal operator.
160 | ///
161 | /// The left expression.
162 | /// The right expression.
163 | /// A instance.
164 | public static DbExpression operator >=(DbExpression leftExpression, DbExpression rightExpression)
165 | {
166 | return ExpressionFactory.GreaterThanOrEqual(leftExpression, rightExpression);
167 | }
168 |
169 | ///
170 | /// Implements the less than or equal operator.
171 | ///
172 | /// The left expression.
173 | /// The right expression.
174 | /// A instance.
175 | public static DbExpression operator <=(DbExpression leftExpression, DbExpression rightExpression)
176 | {
177 | return ExpressionFactory.LessThanOrEqual(leftExpression, rightExpression);
178 | }
179 |
180 | ///
181 | /// Implements the greater or equal operator.
182 | ///
183 | /// The left expression.
184 | /// The value to be compared with .
185 | /// A instance.
186 | public static DbExpression operator >=(DbExpression leftExpression, object value)
187 | {
188 | return ExpressionFactory.GreaterThanOrEqual(leftExpression, ExpressionFactory.Constant(value));
189 | }
190 |
191 | ///
192 | /// Implements the less than or equal operator.
193 | ///
194 | /// The left expression.
195 | /// The value to be compared with .
196 | /// A instance.
197 | public static DbExpression operator <=(DbExpression leftExpression, object value)
198 | {
199 | return ExpressionFactory.LessThanOrEqual(leftExpression, ExpressionFactory.Constant(value));
200 | }
201 |
202 | ///
203 | /// Implements the less than or equal operator.
204 | ///
205 | /// The value to be compared with .
206 | /// The right expression.
207 | /// A instance.
208 | public static DbExpression operator <=(object value, DbExpression rightExpression)
209 | {
210 | return ExpressionFactory.LessThanOrEqual(ExpressionFactory.Constant(value), rightExpression);
211 | }
212 |
213 | ///
214 | /// Implements the operator >=.
215 | ///
216 | /// The value to be compared with .
217 | /// The right expression.
218 | /// A instance.
219 | public static DbExpression operator >=(object value, DbExpression rightExpression)
220 | {
221 | return ExpressionFactory.GreaterThanOrEqual(ExpressionFactory.Constant(value), rightExpression);
222 | }
223 |
224 | ///
225 | /// Implements the operator &.
226 | ///
227 | /// The left expression.
228 | /// The right expression.
229 | /// The result of the operator.
230 | public static DbExpression operator &(DbExpression leftExpression, DbExpression rightExpression)
231 | {
232 | return ExpressionFactory.And(leftExpression, rightExpression);
233 | }
234 |
235 | ///
236 | /// Implements the operator |;.
237 | ///
238 | /// The left expression.
239 | /// The right expression.
240 | /// The result of the operator.
241 | public static DbExpression operator |(DbExpression leftExpression, DbExpression rightExpression)
242 | {
243 | return ExpressionFactory.Or(leftExpression, rightExpression);
244 | }
245 |
246 | ///
247 | /// Determines whether the specified is equal to the current .
248 | ///
249 | ///
250 | /// true if the specified is equal to the current ; otherwise, false.
251 | ///
252 | /// The to compare with the current . 2
253 | public override bool Equals(object obj)
254 | {
255 | return obj.ToString() == ToString();
256 | }
257 |
258 | ///
259 | /// Serves as a hash function for a particular type.
260 | ///
261 | ///
262 | /// A hash code for the current .
263 | ///
264 | /// 2
265 | public override int GetHashCode()
266 | {
267 | return ToString().GetHashCode();
268 | }
269 |
270 | #endregion
271 |
272 | ///
273 | /// Returns a that represents this instance.
274 | ///
275 | ///
276 | /// A that represents this instance.
277 | ///
278 | public override string ToString()
279 | {
280 | InitializeQueryTranslator();
281 | return dbQueryTranslator.Translate(this).Sql;
282 | }
283 |
284 | private static void InitializeQueryTranslator()
285 | {
286 | if (dbQueryTranslator == null)
287 | dbQueryTranslator = DbQueryTranslatorFactory.GetQueryTranslator();
288 | }
289 | }
290 | }
291 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbFunctionExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents calling a built in function in the target DBMS.
5 | ///
6 | public class DbFunctionExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The of the .
12 | /// The arguments used when calling the function.
13 | internal DbFunctionExpression(DbFunctionExpressionType functionExpressionType, DbExpression[] arguments)
14 | {
15 | FunctionExpressionType = functionExpressionType;
16 | Arguments = arguments;
17 | }
18 |
19 | ///
20 | /// Gets the for this .
21 | ///
22 | public DbFunctionExpressionType FunctionExpressionType { get; private set; }
23 |
24 | ///
25 | /// Gets the arguments used to execute the method.
26 | ///
27 | public DbExpression[] Arguments { get; private set; }
28 |
29 | ///
30 | /// Gets the of the .
31 | ///
32 | public override DbExpressionType ExpressionType
33 | {
34 | get { return DbExpressionType.Function; }
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbInExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents determining if a given value matches any value in a sub query or a list.
5 | ///
6 | public class DbInExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The target .
12 | /// The that either represents a sub query or a list of values.
13 | public DbInExpression(DbExpression target, DbExpression expression)
14 | {
15 | Target = target;
16 | Expression = expression;
17 | }
18 |
19 | ///
20 | /// Gets the target .
21 | ///
22 | public DbExpression Target { get; private set; }
23 |
24 | ///
25 | /// Gets the that either represents a sub query or a list of values.
26 | ///
27 | public DbExpression Expression { get; private set; }
28 |
29 | ///
30 | /// Gets the of the .
31 | ///
32 | ///
33 | public override DbExpressionType ExpressionType
34 | {
35 | get { return DbExpressionType.In; }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbInsertExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents an 'INSERT' database query.
5 | ///
6 | public class DbInsertExpression : DbQueryExpression
7 | {
8 | ///
9 | /// Gets the of the .
10 | ///
11 | ///
12 | public override DbExpressionType ExpressionType
13 | {
14 | get { return DbExpressionType.Insert; }
15 | }
16 |
17 | ///
18 | /// Gets or sets the target that represents the database table to insert into.
19 | ///
20 | public DbExpression Target { get; set; }
21 |
22 | ///
23 | /// Gets or sets the that represents the target columns for the insert statement.
24 | ///
25 | public DbExpression TargetColumns { get; set; }
26 |
27 | ///
28 | /// Gets or sets the that represents a value or a list of values to be inserted.
29 | ///
30 | public DbExpression Values { get; set; }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbJoinExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a 'JOIN' in the query.
5 | ///
6 | public class DbJoinExpression : DbExpression
7 | {
8 |
9 | ///
10 | /// Initializes a new instance of the class.
11 | ///
12 | /// The that specifies the type of to create.
13 | /// The join target .
14 | /// The join condition.
15 | internal DbJoinExpression(DbJoinExpressionType joinExpressionType, DbExpression target, DbExpression condition)
16 | {
17 | JoinExpressionType = joinExpressionType;
18 | Target = target;
19 | Condition = condition;
20 | }
21 |
22 | ///
23 | /// Gets the of the .
24 | ///
25 | ///
26 | public override DbExpressionType ExpressionType
27 | {
28 | get { return DbExpressionType.Join; }
29 | }
30 |
31 | ///
32 | /// Specifies the type of .
33 | ///
34 | public DbJoinExpressionType JoinExpressionType{ get; private set; }
35 |
36 | ///
37 | /// Gets or sets the target
38 | ///
39 | public DbExpression Target { get; private set; }
40 |
41 | ///
42 | /// Specifies the condition on which the join is based
43 | ///
44 | public DbExpression Condition { get; private set; }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbListExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | namespace DbExpressions
5 | {
6 | ///
7 | /// Represents a list of instances.
8 | ///
9 | public class DbListExpression : DbExpression, IEnumerable
10 | {
11 | private readonly List _dbExpressions = new List();
12 |
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | internal DbListExpression()
17 | {
18 | }
19 |
20 | ///
21 | /// Initializes a new instance of the class.
22 | ///
23 | /// A that
24 | /// contains instances to be copied to the new list.
25 | internal DbListExpression(IEnumerable dbExpressions)
26 | {
27 | _dbExpressions.AddRange(dbExpressions);
28 | }
29 |
30 |
31 | ///
32 | /// Gets the of the .
33 | ///
34 | ///
35 | public override DbExpressionType ExpressionType
36 | {
37 | get { return DbExpressionType.List; }
38 | }
39 |
40 | ///
41 | /// Returns an enumerator that iterates through the collection.
42 | ///
43 | ///
44 | /// A that can be used to iterate through the collection.
45 | ///
46 | public IEnumerator GetEnumerator()
47 | {
48 | return _dbExpressions.GetEnumerator();
49 | }
50 |
51 | ///
52 | /// Returns an enumerator that iterates through a collection.
53 | ///
54 | ///
55 | /// An object that can be used to iterate through the collection.
56 | ///
57 | IEnumerator IEnumerable.GetEnumerator()
58 | {
59 | return GetEnumerator();
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbMathematicalFunctionExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents calling a built in mathematical function in the target DBMS.
5 | ///
6 | public class DbMathematicalFunctionExpression : DbFunctionExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The of the .
12 | /// The arguments used when calling the function.
13 | internal DbMathematicalFunctionExpression(DbMathematicalFunctionExpressionType mathematicalFunctionExpressionType, DbExpression[] arguments)
14 | : base(DbFunctionExpressionType.Mathematical, arguments)
15 | {
16 | MathematicalFunctionExpressionType = mathematicalFunctionExpressionType;
17 | }
18 |
19 | ///
20 | /// Gets the for this .
21 | ///
22 | public DbMathematicalFunctionExpressionType MathematicalFunctionExpressionType { get; private set; }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbOrderByExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents an element in the 'ORDER BY' clause of the query.
5 | ///
6 | public class DbOrderByExpression : DbExpression
7 | {
8 | ///
9 | /// Gets or sets the that represents an element in the 'ORDER BY' clause.
10 | ///
11 | public DbExpression Expression { get; set; }
12 |
13 | ///
14 | /// Gets or sets the that specifies the sort order
15 | /// used by this .
16 | ///
17 | public DbOrderByExpressionType OrderByExpressionType { get; set; }
18 |
19 | ///
20 | /// Gets the of the .
21 | ///
22 | public override DbExpressionType ExpressionType
23 | {
24 | get { return DbExpressionType.OrderBy; }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbPrefixExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a prefixed
5 | ///
6 | public class DbPrefixExpression : DbExpression
7 | {
8 |
9 | ///
10 | /// Initializes a new instance of the class.
11 | ///
12 | /// The target.
13 | /// the prefix to be used to reference the
14 | internal DbPrefixExpression(DbExpression target, string prefix)
15 | {
16 | Target = target;
17 | Prefix = prefix;
18 | }
19 |
20 | ///
21 | /// Gets the target to be prefixed.
22 | ///
23 | public DbExpression Target { get; private set; }
24 |
25 |
26 | ///
27 | /// Gets the prefix to be used to reference the
28 | ///
29 | public string Prefix { get; private set; }
30 |
31 | ///
32 | /// Gets the of the .
33 | ///
34 | ///
35 | public override DbExpressionType ExpressionType
36 | {
37 | get { return DbExpressionType.Prefix; }
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbQuery.cs:
--------------------------------------------------------------------------------
1 | using System.Data;
2 |
3 | namespace DbExpressions
4 | {
5 |
6 | ///
7 | /// Provides the common interface for implementing
8 | ///
9 | public abstract class DbQuery : DbExpression
10 | {
11 | ///
12 | /// Returns the that the implementation operates on.
13 | ///
14 | ///
15 | /// This method may not be needed when proper covariance is added to the .Net framework.
16 | ///
17 | ///
18 | internal abstract DbQueryExpression GetQueryExpression();
19 |
20 | }
21 |
22 | ///
23 | /// Provides the common interface for implementing
24 | ///
25 | ///
26 | public abstract class DbQuery :
27 | DbQuery where TQueryExpression:DbQueryExpression, new()
28 | {
29 |
30 | ///
31 | /// Initializes a new instance of the class.
32 | ///
33 | internal DbQuery()
34 | {
35 | QueryExpression = new TQueryExpression();
36 | }
37 |
38 | ///
39 | /// Gets the of the .
40 | ///
41 | ///
42 | public override DbExpressionType ExpressionType
43 | {
44 | get { return DbExpressionType.Query; }
45 | }
46 |
47 | ///
48 | /// Gets or sets the that this operates on.
49 | ///
50 | internal TQueryExpression QueryExpression { get; set; }
51 |
52 |
53 | internal override DbQueryExpression GetQueryExpression()
54 | {
55 | return QueryExpression;
56 | }
57 |
58 | ///
59 | /// Translates the query into an executable instance using the default provider.
60 | ///
61 | ///
62 | public IDbCommand Translate()
63 | {
64 | var translator = DbQueryTranslatorFactory.GetQueryTranslator();
65 | return translator.Translate(this).CreateCommand();
66 | }
67 |
68 | ///
69 | /// Translates the query into an executable instance.
70 | ///
71 | /// The name of the provider used translate the query.
72 | ///
73 | public IDbCommand Translate(string providerName)
74 | {
75 | var translator = DbQueryTranslatorFactory.GetQueryTranslator(providerName);
76 | return translator.Translate(this).CreateCommand();
77 | }
78 | }
79 |
80 | ///
81 | /// Represents a "SELECT" query.
82 | ///
83 | public class DbSelectQuery : DbQuery
84 | {
85 |
86 | }
87 |
88 | ///
89 | /// Represents an "UPDATE" query.
90 | ///
91 | public class DbUpdateQuery : DbQuery
92 | {
93 |
94 | }
95 |
96 | ///
97 | /// Represents an "UPDATE" query.
98 | ///
99 | public class DbInsertQuery : DbQuery
100 | {
101 |
102 | }
103 |
104 | ///
105 | /// Represents a "DELETE" query.
106 | ///
107 | public class DbDeleteQuery : DbQuery
108 | {
109 |
110 | }
111 |
112 | }
113 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbQueryExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Provides the base class from which the classes that represents database queries are derived.
5 | ///
6 | public abstract class DbQueryExpression : DbExpression
7 | {
8 | ///
9 | /// Gets or sets the that represents the 'FROM' clause of the query.
10 | ///
11 | public DbExpression FromExpression { get; set; }
12 |
13 | ///
14 | /// Gets or sets the that represents the 'WHERE' clause of the query.
15 | ///
16 | public DbExpression WhereExpression { get; set; }
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbSelectExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a 'SELECT' database query.
5 | ///
6 | public class DbSelectExpression : DbQueryExpression
7 | {
8 | ///
9 | /// Gets or sets the that represents the projection of the query.
10 | ///
11 | public DbExpression ProjectionExpression { get; set; }
12 |
13 | ///
14 | /// Gets or sets the that represents the 'GROUP BY' clause of the query.
15 | ///
16 | public DbExpression GroupByExpression { get; set; }
17 |
18 | ///
19 | /// Gets or sets the that represents the 'HAVING' clause of the query.
20 | ///
21 | public DbExpression HavingExpression { get; set; }
22 |
23 | ///
24 | /// Gets or sets the that represents the 'ORDER BY' clause of the query.
25 | ///
26 | public DbExpression OrderByExpression { get; set; }
27 |
28 | ///
29 | /// Gets or sets the that is used to limit the number of rows returned by the query.
30 | ///
31 | public DbExpression TakeExpression { get; set; }
32 |
33 | ///
34 | /// Gets or sets the that is used to bypass a number of rows in the result set.
35 | ///
36 | public DbExpression SkipExpression { get; set; }
37 |
38 | ///
39 | /// Gets or sets a value that indicate if this should be treated as a sub query.
40 | ///
41 | public bool IsSubQuery { get; set; }
42 |
43 | ///
44 | /// Gets or sets a value that indicate if this represents a distinct selection of rows.
45 | ///
46 | public bool IsDistinct { get; set; }
47 |
48 | ///
49 | /// Gets the of the .
50 | ///
51 | public override DbExpressionType ExpressionType
52 | {
53 | get { return DbExpressionType.Select; }
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbSqlExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents the SQL fragment created when translating a
5 | ///
6 | public class DbSqlExpression : DbExpression
7 | {
8 | ///
9 | /// Initializes a new instance of the class.
10 | ///
11 | /// The SQL.
12 | public DbSqlExpression(string sql)
13 | {
14 | Sql = sql;
15 | }
16 |
17 | ///
18 | /// Gets the of the .
19 | ///
20 | ///
21 | public override DbExpressionType ExpressionType
22 | {
23 | get { return DbExpressionType.Sql; }
24 | }
25 |
26 | ///
27 | /// Gets the SQL that this represents.
28 | ///
29 | public string Sql { get; private set; }
30 |
31 |
32 | ///
33 | /// Returns a that represents this instance.
34 | ///
35 | ///
36 | /// A that represents this instance.
37 | ///
38 | public override string ToString()
39 | {
40 | return Sql;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbStringFunctionExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents calling a built in string function in the target DBMS.
5 | ///
6 | public class DbStringFunctionExpression : DbFunctionExpression
7 | {
8 |
9 | ///
10 | /// Initializes a new instance of the class.
11 | ///
12 | /// The of the .
13 | /// The arguments used when calling the function.
14 | internal DbStringFunctionExpression(DbStringFunctionExpressionType stringFunctionExpressionType, DbExpression[] arguments) : base(DbFunctionExpressionType.String, arguments)
15 | {
16 | StringFunctionExpressionType = stringFunctionExpressionType;
17 | }
18 |
19 | ///
20 | /// Gets the for this .
21 | ///
22 | public DbStringFunctionExpressionType StringFunctionExpressionType { get; private set; }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbTableExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents a table reference in the query.
5 | ///
6 | public class DbTableExpression : DbExpression
7 | {
8 |
9 | ///
10 | /// Initializes a new instance of the class.
11 | ///
12 | /// The name of the table.
13 | internal DbTableExpression(string tableName)
14 | {
15 | TableName = tableName;
16 | }
17 |
18 | ///
19 | /// Gets the of the .
20 | ///
21 | ///
22 | public override DbExpressionType ExpressionType
23 | {
24 | get { return DbExpressionType.Table; }
25 | }
26 |
27 | ///
28 | /// Gets the name of the table.
29 | ///
30 | public string TableName { get; private set; }
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbUnaryExpression.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// Represents a that has a unary operator.
7 | ///
8 | public class DbUnaryExpression : DbExpression
9 | {
10 |
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// The that this represents.
15 | /// The that represents the unary operand.
16 | internal DbUnaryExpression(DbUnaryExpressionType unaryExpressionType, DbExpression operand)
17 | :this(unaryExpressionType,operand,null) {}
18 |
19 | ///
20 | /// Initializes a new instance of the class.
21 | ///
22 | /// The that this represents.
23 | /// The that represents the unary operand.
24 | /// The that specifies the type to convert to.
25 | internal DbUnaryExpression(DbUnaryExpressionType unaryExpressionType, DbExpression operand, Type targetType)
26 | {
27 | UnaryExpressionType = unaryExpressionType;
28 | Operand = operand;
29 | TargetType = targetType;
30 | }
31 |
32 |
33 | ///
34 | /// Gets the that represents the unary operand.
35 | ///
36 | public DbExpression Operand { get; private set; }
37 |
38 | ///
39 | /// Gets the that this represents.
40 | ///
41 | public DbUnaryExpressionType UnaryExpressionType { get; private set;}
42 |
43 |
44 | ///
45 | /// Gets the that specifies the type to be converted to
46 | ///
47 | public Type TargetType { get; private set; }
48 |
49 |
50 | ///
51 | /// Gets the of the .
52 | ///
53 | ///
54 | public override DbExpressionType ExpressionType
55 | {
56 | get { return DbExpressionType.Unary; }
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/DbExpressions/Expressions/DbUpdateExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DbExpressions
2 | {
3 | ///
4 | /// Represents an 'UPDATE' database query.
5 | ///
6 | public class DbUpdateExpression : DbQueryExpression
7 | {
8 | ///
9 | /// Gets the of the .
10 | ///
11 | ///
12 | public override DbExpressionType ExpressionType
13 | {
14 | get { return DbExpressionType.Update; }
15 | }
16 |
17 | ///
18 | /// Gets or sets the target that represents the database table to be updated.
19 | ///
20 | public DbExpression Target { get; set; }
21 |
22 | ///
23 | /// Gets or sets the that represents assigning values
24 | /// to columns or variables.
25 | ///
26 | public DbExpression SetExpression { get; set; }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/DbDeleteQueryExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// Provides the fluent interface that targets a 'DELETE' query.
7 | ///
8 | public static class DbDeleteQueryExtensions
9 | {
10 | private static readonly DbExpressionFactory DbExpressionFactory = new DbExpressionFactory();
11 |
12 | ///
13 | /// Creates a that is used to delete data from the database.
14 | ///
15 | /// The target .
16 | /// A used to
17 | /// specify the that represents the target table or view.
18 | /// A instance.
19 | public static DbDeleteQuery Delete(this DbDeleteQuery dbDeleteQuery, Func targetSelector)
20 | {
21 | return Delete(dbDeleteQuery, targetSelector(DbExpressionFactory));
22 | }
23 |
24 | ///
25 | /// Creates a that is used to delete data from the database.
26 | ///
27 | /// The target .
28 | /// The that represents the target table or view.
29 | /// A instance.
30 | public static DbDeleteQuery Delete(this DbDeleteQuery dbDeleteQuery, DbExpression target)
31 | {
32 | dbDeleteQuery.QueryExpression.Target = target;
33 | return dbDeleteQuery;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/DbExpressionExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace DbExpressions
5 | {
6 | ///
7 | /// Contains extension methods that targets the type.
8 | ///
9 | public static class DbExpressionExtensions
10 | {
11 | private static readonly DbExpressionFactory ExpressionFactory = new DbExpressionFactory();
12 |
13 | ///
14 | /// Determines whether the specified is null.
15 | ///
16 | /// The target .
17 | ///
18 | /// true if the specified is null; otherwise, false.
19 | ///
20 | ///
21 | /// This method is needed because of the operator overloading between instances.
22 | ///
23 | public static bool IsNull(this DbExpression dbExpression)
24 | {
25 | return ((object)dbExpression) == null;
26 | }
27 |
28 | ///
29 | /// Returns a list of instances that matches the .
30 | ///
31 | /// The type of to search for.
32 | /// The that represents the sub tree for which to start searching.
33 | /// The used to filter the result.
34 | /// A list of instances that matches the given predicate.
35 | public static IEnumerable Find(this DbExpression expression, Func predicate) where TDbExpression : DbExpression
36 | {
37 | var finder = new DbExpressionFinder();
38 | return finder.Find(expression, predicate);
39 | }
40 |
41 | ///
42 | /// Searches for expressions using the given and replaces matches with
43 | /// the result from the delegate.
44 | ///
45 | /// The type of to search for.
46 | /// The that represents the sub tree
47 | /// for which to start searching.
48 | /// The used to filter the result.
49 | /// The used to specify the replacement expression.
50 | /// The modified tree.
51 | public static DbExpression Replace(this DbExpression dbExpression, Func predicate, Func replaceWith) where TDbExpression : DbExpression
52 | {
53 | var replacer = new DbExpressionReplacer();
54 | return replacer.Replace(dbExpression, predicate, replaceWith);
55 | }
56 |
57 | ///
58 | /// Creates a new for the target .
59 | ///
60 | /// The to be aliased.
61 | /// The alias.
62 | /// A instance.
63 | public static DbAliasExpression As(this DbExpression dbExpression, string alias)
64 | {
65 | return ExpressionFactory.Alias(dbExpression, alias);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/DbInsertQueryExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace DbExpressions
5 | {
6 | ///
7 | /// Provides the fluent interface that targets an 'INSERT' query.
8 | ///
9 | public static class DbInsertQueryExtensions
10 | {
11 | private static readonly DbExpressionFactory DbExpressionFactory = new DbExpressionFactory();
12 |
13 | ///
14 | /// Creates a that is used to insert data into the database.
15 | ///
16 | /// The target .
17 | /// A used to
18 | /// specify the that represents the target table or view.
19 | /// A instance.
20 | public static DbInsertQuery Insert(this DbInsertQuery dbInsertQuery, Func targetSelector)
21 | {
22 | return Insert(dbInsertQuery, targetSelector(DbExpressionFactory));
23 | }
24 |
25 | ///
26 | /// Creates a that is used to insert data into the database.
27 | ///
28 | /// The target .
29 | /// The that represents the target table or view.
30 | /// A instance.
31 | public static DbInsertQuery Insert(this DbInsertQuery dbInsertQuery, DbExpression target)
32 | {
33 | dbInsertQuery.QueryExpression.Target = target;
34 | return dbInsertQuery;
35 | }
36 |
37 | ///
38 | /// Specifies the target columns.
39 | ///
40 | /// The target .
41 | /// A function used to specify the target columns.
42 | /// A instance.
43 | public static DbInsertQuery Columns(this DbInsertQuery dbInsertQuery, params Func[] columnSelector)
44 | {
45 | return Columns(dbInsertQuery, DbExpressionFactory.List(columnSelector.Select(e => e(DbExpressionFactory))));
46 | }
47 |
48 | ///
49 | /// Specifies the target columns.
50 | ///
51 | /// The target .
52 | /// The that represents the target columns.
53 | /// A instance.
54 | public static DbInsertQuery Columns(this DbInsertQuery dbInsertQuery, DbExpression columnsExpression)
55 | {
56 | dbInsertQuery.QueryExpression.TargetColumns = columnsExpression;
57 | return dbInsertQuery;
58 | }
59 |
60 | ///
61 | /// Specifies the values to be inserted.
62 | ///
63 | /// The target .
64 | /// A used to specify the values to be inserted.
65 | /// A instance.
66 | public static DbQuery Values(this DbQuery dbInsertQuery, params Func[] valueSelector)
67 | {
68 | return Values(dbInsertQuery, DbExpressionFactory.List(valueSelector.Select(e => e(DbExpressionFactory))));
69 | }
70 |
71 | ///
72 | /// Specifies the values to be inserted.
73 | ///
74 | /// The target .
75 | /// The that represents the values to be inserted.
76 | /// A instance.
77 | public static DbQuery Values(this DbQuery dbInsertQuery, DbExpression valuesExpression)
78 | {
79 | dbInsertQuery.QueryExpression.Values = valuesExpression;
80 | return dbInsertQuery;
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/DbQueryExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// Represents the fluent interface that applies to all query types (SELECT,INSERT, UPDATE AND DELETE)
7 | ///
8 | public static class DbQueryExtensions
9 | {
10 |
11 | private static readonly DbExpressionFactory DbExpressionFactory = new DbExpressionFactory();
12 |
13 |
14 | ///
15 | /// Specifies the tables, views, derived tables, and joined tables used in DELETE, SELECT, and UPDATE statements.
16 | ///
17 | /// The target
18 | /// A to specify the .
19 | ///
20 | public static DbQuery From(this DbQuery query, Func expressionSelector)
21 | where TQueryExpression : DbQueryExpression, new()
22 | {
23 | return From(query,expressionSelector(DbExpressionFactory));
24 | }
25 |
26 | ///
27 | /// Specifies the tables, views, derived tables, and joined tables used in DELETE, SELECT, and UPDATE statements.
28 | ///
29 | /// The target
30 | /// A that represents the from clause.
31 | ///
32 | public static DbQuery From(this DbQuery query, DbExpression expression)
33 | where TQueryExpression : DbQueryExpression, new()
34 | {
35 | var dbExpression = expression;
36 | if (!query.QueryExpression.FromExpression.IsNull())
37 | if (dbExpression.ExpressionType == DbExpressionType.Join)
38 | dbExpression = DbExpressionFactory.Concat(query.QueryExpression.FromExpression, dbExpression);
39 | else
40 | dbExpression = DbExpressionFactory.List(new[] {query.QueryExpression.FromExpression, expression });
41 | query.QueryExpression.FromExpression = dbExpression;
42 | return query;
43 | }
44 |
45 |
46 | ///
47 | /// Creates a between the current 'FROM' clause and the expression returned by the functor.
48 | ///
49 | /// Specifies the type of join expression.
50 | /// The target
51 | /// The target join expression.
52 | /// A that specifies the join condition.
53 | ///
54 | public static DbQuery Join(this DbQuery query,
55 | DbJoinExpressionType joinExpressionType, Func target,
56 | Func condition) where TQueryExpression : DbQueryExpression, new()
57 | {
58 | return Join(query,joinExpressionType, target(DbExpressionFactory), condition(DbExpressionFactory));
59 | }
60 |
61 | ///
62 | /// Creates a between the current 'FROM' clause and the expression returned by the functor.
63 | ///
64 | /// Specifies the type of join expression.
65 | /// The target
66 | /// The target join expression.
67 | /// A that specifies the join condition.
68 | ///
69 | public static DbQuery Join(this DbQuery query,
70 | DbJoinExpressionType joinExpressionType, DbExpression target,
71 | DbExpression condition) where TQueryExpression : DbQueryExpression, new()
72 | {
73 |
74 | var dbExpression = (DbExpression)DbExpressionFactory.MakeJoin(joinExpressionType, target, condition);
75 | if (!query.QueryExpression.FromExpression.IsNull())
76 | dbExpression = DbExpressionFactory.Concat(query.QueryExpression.FromExpression, dbExpression);
77 | query.QueryExpression.FromExpression = dbExpression;
78 | return query;
79 | }
80 |
81 |
82 | ///
83 | /// Creates an 'INNER JOIN' between the current 'FROM' clause and the expression returned by the functor.
84 | ///
85 | /// The target
86 | /// The target join expression.
87 | /// A that specifies the join condition.
88 | ///
89 | public static DbQuery InnerJoin(this DbQuery query,
90 | Func target, Func condition)
91 | where TQueryExpression : DbQueryExpression, new()
92 | {
93 | return Join(query,DbJoinExpressionType.InnerJoin, target, condition);
94 | }
95 |
96 | ///
97 | /// Creates an 'INNER JOIN' between the current 'FROM' clause and the expression returned by the functor.
98 | ///
99 | /// The target
100 | /// The target join expression.
101 | /// A that specifies the join condition.
102 | ///
103 | public static DbQuery InnerJoin(this DbQuery query,
104 | DbExpression target, DbExpression condition)
105 | where TQueryExpression : DbQueryExpression, new()
106 | {
107 | return Join(query, DbJoinExpressionType.InnerJoin, target, condition);
108 | }
109 |
110 | ///
111 | /// Creates an 'LEFT OUTER JOIN' between the current 'FROM' clause and the expression returned by the functor.
112 | ///
113 | /// The target
114 | /// The target join expression.
115 | /// A that specifies the join condition.
116 | ///
117 | public static DbQuery LeftOuterJoin(this DbQuery query,
118 | Func target, Func condition)
119 | where TQueryExpression : DbQueryExpression, new()
120 | {
121 | return Join(query,DbJoinExpressionType.LeftOuterJoin, target, condition);
122 | }
123 |
124 | ///
125 | /// Creates an 'LEFT OUTER JOIN' between the current 'FROM' clause and the expression returned by the functor.
126 | ///
127 | /// The target
128 | /// The target join expression.
129 | /// A that specifies the join condition.
130 | ///
131 | public static DbQuery LeftOuterJoin(this DbQuery query,
132 | DbExpression target, DbExpression condition)
133 | where TQueryExpression : DbQueryExpression, new()
134 | {
135 | return Join(query, DbJoinExpressionType.LeftOuterJoin, target, condition);
136 | }
137 |
138 | ///
139 | /// Creates an 'RIGHT OUTER JOIN' between the current 'FROM' clause and the expression returned by the functor.
140 | ///
141 | /// The target
142 | /// The target join expression.
143 | /// A that specifies the join condition.
144 | ///
145 | public static DbQuery RightOuterJoin(this DbQuery query,
146 | Func target, Func condition)
147 | where TQueryExpression : DbQueryExpression, new()
148 | {
149 | return Join(query,DbJoinExpressionType.RightOuterJoin, target, condition);
150 | }
151 |
152 |
153 | ///
154 | /// Creates an 'RIGHT OUTER JOIN' between the current 'FROM' clause and the expression returned by the functor.
155 | ///
156 | /// The target
157 | /// The target join expression.
158 | /// A that specifies the join condition.
159 | ///
160 | public static DbQuery RightOuterJoin(this DbQuery query,
161 | DbExpression target, DbExpression condition)
162 | where TQueryExpression : DbQueryExpression, new()
163 | {
164 | return Join(query, DbJoinExpressionType.RightOuterJoin, target, condition);
165 | }
166 |
167 | ///
168 | /// Specifies the search condition for the rows returned by the query.
169 | ///
170 | /// The target
171 | /// A to specify the .
172 | ///
173 | public static DbQuery Where(this DbQuery query,
174 | Func expressionSelector)
175 | where TQueryExpression : DbQueryExpression, new()
176 | {
177 | return Where(query,expressionSelector(DbExpressionFactory));
178 | }
179 |
180 | ///
181 | /// Specifies the search condition for the rows returned by the query.
182 | ///
183 | /// The target
184 | /// The that makes up the search condition.
185 | ///
186 | public static DbQuery Where(this DbQuery query,DbExpression dbExpression)
187 | where TQueryExpression : DbQueryExpression, new()
188 | {
189 | if (!query.QueryExpression.WhereExpression.IsNull())
190 | dbExpression = DbExpressionFactory.And(query.QueryExpression.WhereExpression, dbExpression);
191 | query.QueryExpression.WhereExpression = dbExpression;
192 | return query;
193 | }
194 |
195 |
196 | }
197 | }
198 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/DbSelectQueryExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace DbExpressions
5 | {
6 | ///
7 | /// Provides the fluent interface that targets a 'SELECT' query.
8 | ///
9 | public static class DbSelectQueryExtensions
10 | {
11 |
12 | private static readonly DbExpressionFactory DbExpressionFactory = new DbExpressionFactory();
13 |
14 | ///
15 | /// Specifies the projecton of the query.
16 | ///
17 | /// The target
18 | /// A used to specify the that represents the projection.
19 | ///
20 | public static DbSelectQuery Select(this DbSelectQuery dbSelectQuery, params Func[] expressionSelector)
21 | {
22 | return Select(dbSelectQuery, DbExpressionFactory.List(expressionSelector.Select(e => e(DbExpressionFactory))));
23 | }
24 |
25 | ///
26 | /// Specifies the projecton of the query.
27 | ///
28 | /// The target
29 | /// A used to specify the that represents the projection.
30 | ///
31 | public static DbSelectQuery SelectDistinct(this DbSelectQuery dbSelectQuery, params Func[] expressionSelector)
32 | {
33 | return SelectDistinct(dbSelectQuery, DbExpressionFactory.List(expressionSelector.Select(e => e(DbExpressionFactory))));
34 | }
35 |
36 | ///
37 | /// Specifies the projection of the query.
38 | ///
39 | /// The target
40 | /// The that represents the projection.
41 | ///
42 | public static DbSelectQuery Select(this DbSelectQuery dbSelectQuery, DbExpression expression)
43 | {
44 | dbSelectQuery.QueryExpression.ProjectionExpression = expression;
45 | return dbSelectQuery;
46 | }
47 |
48 | ///
49 | /// Specifies the projection of the query.
50 | ///
51 | /// The target
52 | /// The that represents the projection.
53 | ///
54 | public static DbSelectQuery SelectDistinct(this DbSelectQuery dbSelectQuery, DbExpression expression)
55 | {
56 | dbSelectQuery.QueryExpression.ProjectionExpression = expression;
57 | dbSelectQuery.QueryExpression.IsDistinct = true;
58 | return dbSelectQuery;
59 | }
60 |
61 |
62 |
63 | ///
64 | /// Creates a that represents ordering the result set.
65 | ///
66 | /// The target .
67 | /// A function used to specify an element in the 'ORDER BY' clause.
68 | /// Specifies the sort order direction.
69 | ///
70 | public static DbQuery OrderBy(this DbQuery dbSelectQuery, Func expressionSelector, DbOrderByExpressionType orderByExpressionType)
71 | {
72 | var dbExpression = (DbExpression)DbExpressionFactory.MakeOrderBy(orderByExpressionType, expressionSelector(DbExpressionFactory));
73 | if (!dbSelectQuery.QueryExpression.OrderByExpression.IsNull())
74 | dbExpression = DbExpressionFactory.List(new[] { dbSelectQuery.QueryExpression.OrderByExpression, dbExpression });
75 | dbSelectQuery.QueryExpression.OrderByExpression = dbExpression;
76 | return dbSelectQuery;
77 | }
78 |
79 | ///
80 | /// Creates a new that represents an ascending ordering of the result set.
81 | ///
82 | /// The target .
83 | /// A function used to specify an element in the 'ORDER BY' clause.
84 | public static DbQuery OrderByAscending(this DbQuery dbSelectQuery, Func expressionSelector)
85 | {
86 | return OrderBy(dbSelectQuery, expressionSelector, DbOrderByExpressionType.Ascending);
87 | }
88 |
89 | ///
90 | /// Creates a new that represents an descending ordering of the result set.
91 | ///
92 | /// The target .
93 | /// A function used to specify an element in the 'ORDER BY' clause.
94 | public static DbQuery OrderByDescending(this DbQuery dbSelectQuery, Func expressionSelector)
95 | {
96 | return OrderBy(dbSelectQuery, expressionSelector, DbOrderByExpressionType.Ascending);
97 | }
98 |
99 | ///
100 | /// Limits the numbers of rows returned by the query.
101 | ///
102 | /// The target .
103 | /// A function used to create a that the number of rows to return.
104 | ///
105 | public static DbQuery Take(this DbQuery dbSelectQuery, Func expressionSelector)
106 | {
107 | dbSelectQuery.QueryExpression.TakeExpression = expressionSelector(DbExpressionFactory);
108 | return dbSelectQuery;
109 | }
110 |
111 | ///
112 | /// Limits the numbers of rows returned by the query.
113 | ///
114 | /// The target .
115 | /// The number of rows to return.
116 | ///
117 | public static DbQuery Take(this DbQuery dbSelectQuery, int count)
118 | {
119 | dbSelectQuery.QueryExpression.TakeExpression = DbExpressionFactory.Constant(count);
120 | return dbSelectQuery;
121 |
122 | }
123 |
124 | ///
125 | /// Bypasses a specified number of rows in the result set.
126 | ///
127 | /// The target .
128 | /// A function used to create a that the number of rows to bypass.
129 | ///
130 | public static DbQuery Skip(this DbQuery dbSelectQuery, Func expressionSelector)
131 | {
132 | dbSelectQuery.QueryExpression.SkipExpression = expressionSelector(DbExpressionFactory);
133 | return dbSelectQuery;
134 | }
135 |
136 |
137 | ///
138 | /// Bypasses a specified number of rows in the result set.
139 | ///
140 | /// The target .
141 | /// The number of rows to bypass in the result set
142 | ///
143 | public static DbQuery Skip(this DbQuery dbSelectQuery, int count)
144 | {
145 | dbSelectQuery.QueryExpression.SkipExpression = DbExpressionFactory.Constant(count);
146 | return dbSelectQuery;
147 | }
148 |
149 | ///
150 | /// Collects data across multiple records and group the results by one or more columns.
151 | ///
152 | /// The target .
153 | /// A function used to specify an element in the 'GROUP BY' clause.
154 | ///
155 | public static DbQuery GroupBy(this DbQuery dbSelectQuery, Func expressionSelector)
156 | {
157 | var dbExpression = expressionSelector(DbExpressionFactory);
158 | if (!dbSelectQuery.QueryExpression.GroupByExpression.IsNull())
159 | dbExpression = DbExpressionFactory.List(new[] { dbSelectQuery.QueryExpression.GroupByExpression, dbExpression });
160 | dbSelectQuery.QueryExpression.GroupByExpression = dbExpression;
161 | return dbSelectQuery;
162 | }
163 |
164 | ///
165 | /// Specifies a search condition for a group or an aggregate.
166 | ///
167 | /// The target .
168 | /// A function used to specify the 'HAVING' expression.
169 | ///
170 | public static DbQuery Having(this DbQuery dbSelectQuery, Func expressionSelector)
171 | {
172 | var dbExpression = expressionSelector(DbExpressionFactory);
173 | if (!dbSelectQuery.QueryExpression.HavingExpression.IsNull())
174 | dbExpression = DbExpressionFactory.List(new[] { dbSelectQuery.QueryExpression.HavingExpression, dbExpression });
175 | dbSelectQuery.QueryExpression.HavingExpression = dbExpression;
176 | return dbSelectQuery;
177 | }
178 |
179 |
180 |
181 |
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/DbUpdateQueryExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// Provides the fluent interface that targets an 'UPDATE' query.
7 | ///
8 | public static class DbUpdateQueryExtensions
9 | {
10 |
11 | private static readonly DbExpressionFactory DbExpressionFactory = new DbExpressionFactory();
12 |
13 |
14 | ///
15 | /// Specifies the target table to update.
16 | ///
17 | /// The target
18 | /// The that represents the target table or view.
19 | ///
20 | public static DbUpdateQuery Update(this DbUpdateQuery dbUpdateQuery, DbExpression target)
21 | {
22 | dbUpdateQuery.QueryExpression.Target = target;
23 | return dbUpdateQuery;
24 | }
25 |
26 | ///
27 | /// Specifies the target table to update.
28 | ///
29 | /// The target
30 | /// A used to
31 | /// specify the that represents the target table or view.
32 | ///
33 | public static DbUpdateQuery Update(this DbUpdateQuery dbUpdateQuery, Func targetSelector)
34 | {
35 | return Update(dbUpdateQuery, targetSelector(DbExpressionFactory));
36 | }
37 |
38 | ///
39 | /// Specifies the column to be updated.
40 | ///
41 | /// The target .
42 | /// A function used to specify the column to be updated.
43 | /// A function used to specify the new value.
44 | ///
45 | public static DbQuery Set(this DbQuery dbUpdateQuery,
46 | Func columnSelector, Func valueSelector)
47 | {
48 | return Set(dbUpdateQuery,columnSelector(DbExpressionFactory), valueSelector(DbExpressionFactory));
49 | }
50 |
51 | ///
52 | /// Specifies the column to be updated.
53 | ///
54 | /// The target .
55 | /// A function used to specify the column to be updated.
56 | /// The new value.
57 | ///
58 | public static DbQuery Set(this DbQuery dbUpdateQuery, Func columnSelector, object value)
59 | {
60 | return Set(dbUpdateQuery,columnSelector(DbExpressionFactory), DbExpressionFactory.Constant(value));
61 | }
62 |
63 | ///
64 | /// Specifies the column to be updated.
65 | ///
66 | /// The target .
67 | /// The that represents the target table or view.
68 | /// The that represents the new value.
69 | ///
70 | public static DbQuery Set(this DbQuery dbUpdateQuery, DbExpression target, DbExpression valueExpression)
71 | {
72 | var dbExpression = (DbExpression)DbExpressionFactory.Assign(target, valueExpression);
73 | if (!dbUpdateQuery.QueryExpression.SetExpression.IsNull())
74 | dbExpression = DbExpressionFactory.List(new[] { dbUpdateQuery.QueryExpression.SetExpression, dbExpression });
75 | dbUpdateQuery.QueryExpression.SetExpression = dbExpression;
76 | return dbUpdateQuery;
77 | }
78 |
79 |
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/DbExpressions/Extensions/StringBuilderExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace DbExpressions
7 | {
8 | ///
9 | /// Extends the class.
10 | ///
11 | public static class StringBuilderExtensions
12 | {
13 | ///
14 | /// Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance.
15 | /// Each format item is replaced by the string representation of a corresponding object argument.
16 | ///
17 | /// The target instance.
18 | /// The indent level to use
19 | /// A composite format string.
20 | /// An array of objects to format.
21 | public static void AppendFormat(this StringBuilder stringBuilder, int indentLevel, string format, params object[] arguments)
22 | {
23 | for (int i = 0; i < indentLevel; i++)
24 | {
25 | stringBuilder.Append("\t");
26 | }
27 | stringBuilder.AppendFormat(format, arguments);
28 | }
29 |
30 | ///
31 | /// Appends the default line terminator, or a copy of a specified string and the default line terminator, to the end of this instance.
32 | ///
33 | /// The target instance.
34 | /// The indent level to use
35 | /// A composite format string.
36 | /// An array of objects to format.
37 | public static void AppendLine(this StringBuilder stringBuilder, int indentLevel, string format, params object[] arguments)
38 | {
39 | AppendFormat(stringBuilder, indentLevel, format, arguments);
40 | stringBuilder.AppendLine();
41 | }
42 |
43 | ///
44 | /// Appends the default line terminator, or a copy of a specified string and the default line terminator, to the end of this instance.
45 | ///
46 | /// The target instance.
47 | /// The indent level to use
48 | ///
49 | public static void AppendLine(this StringBuilder stringBuilder, int indentLevel, string value)
50 | {
51 | for (int i = 0; i < indentLevel; i++)
52 | {
53 | stringBuilder.Append("\t");
54 | }
55 | stringBuilder.AppendLine(value);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/DbExpressions/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 | using System.Security;
5 |
6 | // General Information about an assembly is controlled through the following
7 | // set of attributes. Change these attribute values to modify the information
8 | // associated with an assembly.
9 | [assembly: AssemblyTitle("DbExpressions")]
10 | [assembly: AssemblyDescription("")]
11 | [assembly: AssemblyConfiguration("")]
12 | [assembly: AssemblyCompany("Microsoft")]
13 | [assembly: AssemblyProduct("DbExpressions")]
14 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
15 | [assembly: AssemblyTrademark("")]
16 | [assembly: AssemblyCulture("")]
17 |
18 | // Setting ComVisible to false makes the types in this assembly not visible
19 | // to COM components. If you need to access a type in this assembly from
20 | // COM, set the ComVisible attribute to true on that type.
21 | [assembly: ComVisible(false)]
22 |
23 | // The following GUID is for the ID of the typelib if this project is exposed to COM
24 | [assembly: Guid("37a44f70-1ee6-47dd-8e09-420d88a861a2")]
25 |
26 | // Version information for an assembly consists of the following four values:
27 | //
28 | // Major Version
29 | // Minor Version
30 | // Build Number
31 | // Revision
32 | //
33 | // You can specify all the values or you can default the Build and Revision Numbers
34 | // by using the '*' as shown below:
35 | // [assembly: AssemblyVersion("1.0.*")]
36 | [assembly: AssemblyVersion("1.0.0.4")]
37 | [assembly: AssemblyFileVersion("1.0.0.4")]
38 | //Need this because know bug in Configuration Manager
39 | //http://social.msdn.microsoft.com/Forums/en-US/clr/thread/1e14f665-10a3-426b-a75d-4e66354c5522
40 |
41 | [assembly: AllowPartiallyTrustedCallers()]
42 |
--------------------------------------------------------------------------------
/DbExpressions/Settings.StyleCop:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | False
8 |
9 |
10 |
11 |
12 | False
13 |
14 |
15 |
16 |
17 | False
18 |
19 |
20 |
21 |
22 | False
23 |
24 |
25 |
26 |
27 | False
28 |
29 |
30 |
31 |
32 | False
33 |
34 |
35 |
36 |
37 | False
38 |
39 |
40 |
41 |
42 | False
43 |
44 |
45 |
46 |
47 | True
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | False
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 | False
68 |
69 |
70 |
71 |
72 | False
73 |
74 |
75 |
76 |
77 |
78 | db
79 | Db
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 | False
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 | False
98 |
99 |
100 |
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/DbExpressions/Visitors/DbExpressionFinder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace DbExpressions
5 | {
6 | ///
7 | /// A class used to search for instances.
8 | ///
9 | /// The type of to search for.
10 | public class DbExpressionFinder : DbExpressionVisitor where TDbExpression : DbExpression
11 | {
12 | private readonly IList _result = new List();
13 | private Func _predicate;
14 |
15 | ///
16 | /// Returns a list of instances that matches the .
17 | ///
18 | /// The that represents the sub tree for which to start searching.
19 | /// The used to filter the result.
20 | /// A list of instances that matches the given predicate.
21 | public IEnumerable Find(DbExpression expression, Func predicate)
22 | {
23 | _result.Clear();
24 | _predicate = predicate;
25 | Visit(expression);
26 | return _result;
27 | }
28 |
29 | ///
30 | /// Visits each node of the tree checks
31 | /// if the current expression matches the predicate.
32 | ///
33 | /// The currently being visited.
34 | /// A instance.
35 | public override DbExpression Visit(DbExpression dbExpression)
36 | {
37 | if (!dbExpression.IsNull() && dbExpression.GetType() == typeof(TDbExpression))
38 | {
39 | if (_predicate((TDbExpression)dbExpression))
40 | _result.Add((TDbExpression)dbExpression);
41 | }
42 |
43 | return base.Visit(dbExpression);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/DbExpressions/Visitors/DbExpressionReplacer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DbExpressions
4 | {
5 | ///
6 | /// A class that is capable of doing a find and replace in an tree.
7 | ///
8 | /// The type of to find and replace.
9 | public class DbExpressionReplacer : DbExpressionVisitor where TDbExpression : DbExpression
10 | {
11 | private Func _replaceWith;
12 | private Func _predicate;
13 |
14 | ///
15 | /// Searches for expressions using the given and
16 | /// replaces matches with the result from the delegate.
17 | ///
18 | /// The that
19 | /// represents the sub tree for which to start searching.
20 | /// The used to filter the result.
21 | /// The
22 | /// used to specify the replacement expression.
23 | /// The modified tree.
24 | public DbExpression Replace(DbExpression expression, Func predicate, Func replaceWith)
25 | {
26 | _replaceWith = replaceWith;
27 | _predicate = predicate;
28 | return Visit(expression);
29 | }
30 |
31 | ///
32 | /// Visits each node of the tree checks
33 | /// if the current expression matches the predicate. If a match is found
34 | /// the expression will be replaced.
35 | ///
36 | /// The currently being visited.
37 | /// The modified tree.
38 | public override DbExpression Visit(DbExpression expression)
39 | {
40 | if (!expression.IsNull() && expression is TDbExpression)
41 | if (_predicate((TDbExpression)expression))
42 | return _replaceWith((TDbExpression)expression);
43 | return base.Visit(expression);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/DbExpressions/Visitors/Translators/DbQueryTranslator.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Data;
3 | using System.Data.Common;
4 |
5 | namespace DbExpressions
6 | {
7 | ///
8 | /// Provides a base class for database-specific implementations.
9 | ///
10 | public abstract class DbQueryTranslator : DbExpressionVisitor
11 | {
12 |
13 | private readonly DbCommandBuilder _commandBuilder;
14 | private readonly DbProviderFactory _providerFactory;
15 |
16 |
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | ///
21 | protected DbQueryTranslator(DbProviderFactory providerFactory)
22 | {
23 | _providerFactory = providerFactory;
24 | _commandBuilder = _providerFactory.CreateCommandBuilder();
25 | Parameters = new List();
26 | }
27 |
28 | ///
29 | /// Translates the into a instance.
30 | ///
31 | /// The to translate.
32 | ///
33 | public virtual DbTranslateResult Translate(DbExpression dbExpression)
34 | {
35 | Parameters.Clear();
36 | var sqlExpression = Visit(dbExpression);
37 | var translateResult = new DbTranslateResult(((DbSqlExpression)sqlExpression).Sql,Parameters,_providerFactory);
38 | return translateResult;
39 | }
40 |
41 | ///
42 | /// Gets a list of instances.
43 | ///
44 | protected IList Parameters { get; private set; }
45 |
46 | ///
47 | /// Quotes the .
48 | ///
49 | /// The original unquoted identifier.
50 | ///
51 | protected virtual string QuoteIdentifier(string unquotedIdentifier)
52 | {
53 | return _commandBuilder.QuoteIdentifier(unquotedIdentifier);
54 | }
55 |
56 | ///
57 | /// Creates a new used in the result of the query translation.
58 | ///
59 | /// The name of the parameter.
60 | /// The parameter value.
61 | protected virtual void CreateParameter(string name, object value)
62 | {
63 | var parameter = _providerFactory.CreateParameter();
64 | parameter.ParameterName = name;
65 | parameter.Value = value;
66 | Parameters.Add(parameter);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Greeng0
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Local.testsettings:
--------------------------------------------------------------------------------
1 |
2 |
3 | These are default test settings for a local test run.
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/seesharper/DbExpressions/3d49ef2bde395191cdc65573da42df04df91f3c5/README
--------------------------------------------------------------------------------
/TraceAndTestImpact.testsettings:
--------------------------------------------------------------------------------
1 |
2 |
3 | These are test settings for Trace and Test Impact.
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------