@@ -251,44 +251,30 @@ db.Select<Author>(q => q.Birthday >= new DateTime(agesAgo, 1, 1)
251
251
&& q .Birthday <= new DateTime (agesAgo , 12 , 31 ));
252
252
```
253
253
254
- ** WHERE (("Birthday" >= '1992-01-01 00:00:00.000') AND ("Birthday" <= '1992-12-31 00:00:00.000'))**
255
-
256
254
``` csharp
257
255
db .Select <Author >(q => Sql .In (q .City , " London" , " Madrid" , " Berlin" ));
258
256
```
259
257
260
- ** WHERE "JobCity" In ('London', 'Madrid', 'Berlin')**
261
-
262
258
``` csharp
263
259
db .Select <Author >(q => q .Earnings <= 50 );
264
260
```
265
261
266
- ** WHERE ("Earnings" <= 50)**
267
-
268
262
``` csharp
269
263
db .Select <Author >(q => q .Name .StartsWith (" A" ));
270
264
```
271
265
272
- ** WHERE upper("Name") like 'A%'**
273
-
274
266
``` csharp
275
267
db .Select <Author >(q => q .Name .EndsWith (" garzon" ));
276
268
```
277
269
278
- ** WHERE upper("Name") like '%GARZON'**
279
-
280
270
``` csharp
281
271
db .Select <Author >(q => q .Name .Contains (" Benedict" ));
282
272
```
283
273
284
- ** WHERE upper("Name") like '%BENEDICT%'**
285
-
286
274
``` csharp
287
275
db .Select <Author >(q => q .Rate == 10 && q .City == " Mexico" );
288
276
```
289
277
290
- ** WHERE (("Rate" = 10) AND ("JobCity" = 'Mexico'))**
291
-
292
278
Right now the Expression support can satisfy most simple queries with a strong-typed API.
293
279
For anything more complex (e.g. queries with table joins) you can still easily fall back to raw SQL queries as seen below.
294
280
@@ -300,68 +286,47 @@ OrmLite also includes a number of convenient API's providing DRY, typed data acc
300
286
Person personById = db .SingleById <Person >(1 );
301
287
```
302
288
303
- ** SELECT "Id", "FirstName", "LastName", "Age" FROM "Person" WHERE "Id" = @Id **
304
-
305
289
``` csharp
306
290
Person personByAge = db .Single <Person >(x => x .Age == 42 );
307
291
```
308
292
309
- ** SELECT TOP 1 "Id", "FirstName", "LastName", "Age" FROM "Person" WHERE ("Age" = 42)**
310
-
311
293
``` csharp
312
294
int maxAgeUnder50 = db .Scalar <Person , int >(x => Sql .Max (x .Age ), x => x .Age < 50 );
313
295
```
314
296
315
- ** SELECT Max("Age") FROM "Person" WHERE ("Age" < 50)**
316
-
317
297
``` csharp
318
298
int peopleOver40 = db .Scalar <int >(
319
299
db .From <Person >().Select (Sql .Count (" *" )).Where (q => q .Age > 40 ));
320
300
```
321
301
322
- ** SELECT COUNT(* ) FROM "Person" WHERE ("Age" > 40)**
323
-
324
302
``` csharp
325
303
int peopleUnder50 = db .Count <Person >(x => x .Age < 50 );
326
304
```
327
305
328
- ** SELECT COUNT(* ) FROM "Person" WHERE ("Age" < 50)**
329
-
330
306
``` csharp
331
307
bool has42YearOlds = db .Exists <Person >(new { Age = 42 });
332
308
```
333
309
334
- ** WHERE "Age" = @Age **
335
-
336
310
``` csharp
337
311
List < string > results = db .Column <string >(db .From <Person >().Select (x => x .LastName )
338
312
.Where (q => q .Age == 27 ));
339
313
```
340
314
341
- ** SELECT "LastName" FROM "Person" WHERE ("Age" = 27)**
342
-
343
315
``` csharp
344
316
HashSet < int > results = db .ColumnDistinct <int >(db .From <Person >().Select (x => x .Age )
345
317
.Where (q => q .Age < 50 ));
346
318
```
347
319
348
- ** SELECT "Age" FROM "Person" WHERE ("Age" < 50)**
349
-
350
320
``` csharp
351
321
Dictionary < int ,string > results = db .Dictionary <int , string >(
352
322
db .From <Person >().Select (x => new { x .Id , x .LastName }).Where (x => x .Age < 50 ));
353
323
```
354
324
355
- ** SELECT "Id","LastName" FROM "Person" WHERE ("Age" < 50)**
356
-
357
-
358
325
``` csharp
359
326
Dictionary < int , List < string >> results = db .Lookup <int , string >(
360
327
db .From <Person >().Select (x => new { x .Age , x .LastName }).Where (q => q .Age < 50 ));
361
328
```
362
329
363
- ** SELECT "Age","LastName" FROM "Person" WHERE ("Age" < 50)**
364
-
365
330
### INSERT, UPDATE and DELETEs
366
331
367
332
To see the behaviour of the different APIs, all examples uses this simple model
@@ -384,15 +349,11 @@ is used to filter the update to this specific record:
384
349
``` csharp
385
350
db .Update (new Person { Id = 1 , FirstName = " Jimi" , LastName = " Hendrix" , Age = 27 });
386
351
```
387
- ** UPDATE "Person" SET "FirstName" = 'Jimi',"LastName" = 'Hendrix',"Age" = 27 WHERE "Id" = 1**
388
-
389
352
If you supply your own where expression, it updates every field (inc. Id) but uses your filter instead:
390
353
391
354
``` csharp
392
355
db .Update (new Person { Id = 1 , FirstName = " JJ" }, p => p .LastName == " Hendrix" );
393
356
```
394
- ** UPDATE "Person" SET "Id" = 1,"FirstName" = 'JJ',"LastName" = NULL,"Age" = NULL WHERE ("LastName" = 'Hendrix')**
395
-
396
357
One way to limit the fields which gets updated is to use an ** Anonymous Type** :
397
358
398
359
``` csharp
@@ -405,8 +366,6 @@ Or by using `UpdateNonDefaults` which only updates the non-default values in you
405
366
db .UpdateNonDefaults (new Person { FirstName = " JJ" }, p => p .LastName == " Hendrix" );
406
367
```
407
368
408
- ** UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')**
409
-
410
369
#### UpdateOnly
411
370
412
371
As updating a partial row is a common use-case in Db's, we've added a number of methods for just
@@ -416,35 +375,29 @@ The first expression in an `UpdateOnly` statement is used to specify which field
416
375
``` csharp
417
376
db .UpdateOnly (new Person { FirstName = " JJ" }, p => p .FirstName );
418
377
```
419
- ** UPDATE "Person" SET "FirstName" = 'JJ'**
420
-
421
378
``` csharp
422
379
db .UpdateOnly (new Person { FirstName = " JJ" , Age = 12 },
423
380
onlyFields : p => new { p .FirstName , p .Age });
424
381
```
425
- ** UPDATE "Person" SET "FirstName" = 'JJ', "Age" = 12**
426
-
427
382
When present, the second expression is used as the where filter:
428
383
``` csharp
429
384
db .UpdateOnly (new Person { FirstName = " JJ" },
430
385
onlyFields : p => p .FirstName ,
431
386
where : p => p .LastName == " Hendrix" );
432
387
```
433
- ** UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')**
434
-
435
388
Instead of using the expression filters above you can choose to use an ExpressionVisitor builder which provides more flexibility when you want to programatically construct the update statement:
436
389
437
390
``` csharp
438
391
db .UpdateOnly (new Person { FirstName = " JJ" , LastName = " Hendo" },
439
392
onlyFields : q => q .Update (p => p .FirstName ));
440
393
```
441
- ** UPDATE "Person" SET "FirstName" = 'JJ'**
394
+
395
+ Using a typed SQL Expression:
442
396
443
397
``` csharp
444
398
db .UpdateOnly (new Person { FirstName = " JJ" },
445
399
onlyFields : q => q .Update (p => p .FirstName ).Where (x => x .FirstName == " Jimi" ));
446
400
```
447
- ** UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')**
448
401
449
402
For the ultimate flexibility we also provide un-typed, string-based expressions. Use the ` .SqlFmt() ` extension method escape parameters (inspired by [ massive] ( https://github.com/robconery/massive ) ):
450
403
@@ -458,23 +411,18 @@ Even the Table name can be a string so you perform the same update without requi
458
411
db .UpdateFmt (table : " Person" , set : " FirstName = {0}" .SqlFmt (" JJ" ),
459
412
where : " LastName = {0}" .SqlFmt (" Hendrix" ));
460
413
```
461
- ** UPDATE "Person" SET FirstName = 'JJ' WHERE LastName = 'Hendrix'**
462
-
463
414
### INSERT
464
415
465
416
Insert's are pretty straight forward since in most cases you want to insert every field:
466
417
467
418
``` csharp
468
419
db .Insert (new Person { Id = 1 , FirstName = " Jimi" , LastName = " Hendrix" , Age = 27 });
469
420
```
470
- ** INSERT INTO "Person" ("Id","FirstName","LastName","Age") VALUES (1,'Jimi','Hendrix',27)**
471
-
472
421
But do provide an API that takes an Expression Visitor for the rare cases you don't want to insert every field
473
422
474
423
``` csharp
475
424
db .InsertOnly (new Person { FirstName = " Amy" }, q => q .Insert (p => new {p .FirstName }))
476
425
```
477
- ** INSERT INTO "Person" ("FirstName") VALUES ('Amy')**
478
426
479
427
### DELETE
480
428
@@ -488,8 +436,6 @@ Or an Expression Visitor:
488
436
db .Delete <Person >(q => q .Where (p => p .Age == 27 ));
489
437
```
490
438
491
- ** DELETE FROM "Person" WHERE ("Age" = 27)**
492
-
493
439
As well as un-typed, string-based expressions:
494
440
``` csharp
495
441
db .Delete <Person >(where : " Age = {0}" .SqlFmt (27 ));
@@ -500,8 +446,6 @@ Which also can take a table name so works without requiring a typed **Person** m
500
446
db .Delete (table : " Person" , where : " Age = {0}" .SqlFmt (27 ));
501
447
```
502
448
503
- ** DELETE FROM "Person" WHERE Age = 27**
504
-
505
449
# API Overview
506
450
507
451
The API is minimal, providing basic shortcuts for the primitive SQL statements:
0 commit comments