Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 1132290

Browse files
committed
Remove inline-SQL which are no longer applicable now that OrmLite uses parameterized queries by default
1 parent c1487d2 commit 1132290

File tree

1 file changed

+2
-58
lines changed

1 file changed

+2
-58
lines changed

README.md

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -251,44 +251,30 @@ db.Select<Author>(q => q.Birthday >= new DateTime(agesAgo, 1, 1)
251251
&& q.Birthday <= new DateTime(agesAgo, 12, 31));
252252
```
253253

254-
**WHERE (("Birthday" >= '1992-01-01 00:00:00.000') AND ("Birthday" <= '1992-12-31 00:00:00.000'))**
255-
256254
```csharp
257255
db.Select<Author>(q => Sql.In(q.City, "London", "Madrid", "Berlin"));
258256
```
259257

260-
**WHERE "JobCity" In ('London', 'Madrid', 'Berlin')**
261-
262258
```csharp
263259
db.Select<Author>(q => q.Earnings <= 50);
264260
```
265261

266-
**WHERE ("Earnings" <= 50)**
267-
268262
```csharp
269263
db.Select<Author>(q => q.Name.StartsWith("A"));
270264
```
271265

272-
**WHERE upper("Name") like 'A%'**
273-
274266
```csharp
275267
db.Select<Author>(q => q.Name.EndsWith("garzon"));
276268
```
277269

278-
**WHERE upper("Name") like '%GARZON'**
279-
280270
```csharp
281271
db.Select<Author>(q => q.Name.Contains("Benedict"));
282272
```
283273

284-
**WHERE upper("Name") like '%BENEDICT%'**
285-
286274
```csharp
287275
db.Select<Author>(q => q.Rate == 10 && q.City == "Mexico");
288276
```
289277

290-
**WHERE (("Rate" = 10) AND ("JobCity" = 'Mexico'))**
291-
292278
Right now the Expression support can satisfy most simple queries with a strong-typed API.
293279
For anything more complex (e.g. queries with table joins) you can still easily fall back to raw SQL queries as seen below.
294280

@@ -300,68 +286,47 @@ OrmLite also includes a number of convenient API's providing DRY, typed data acc
300286
Person personById = db.SingleById<Person>(1);
301287
```
302288

303-
**SELECT "Id", "FirstName", "LastName", "Age" FROM "Person" WHERE "Id" = @Id**
304-
305289
```csharp
306290
Person personByAge = db.Single<Person>(x => x.Age == 42);
307291
```
308292

309-
**SELECT TOP 1 "Id", "FirstName", "LastName", "Age" FROM "Person" WHERE ("Age" = 42)**
310-
311293
```csharp
312294
int maxAgeUnder50 = db.Scalar<Person, int>(x => Sql.Max(x.Age), x => x.Age < 50);
313295
```
314296

315-
**SELECT Max("Age") FROM "Person" WHERE ("Age" < 50)**
316-
317297
```csharp
318298
int peopleOver40 = db.Scalar<int>(
319299
db.From<Person>().Select(Sql.Count("*")).Where(q => q.Age > 40));
320300
```
321301

322-
**SELECT COUNT(*) FROM "Person" WHERE ("Age" > 40)**
323-
324302
```csharp
325303
int peopleUnder50 = db.Count<Person>(x => x.Age < 50);
326304
```
327305

328-
**SELECT COUNT(*) FROM "Person" WHERE ("Age" < 50)**
329-
330306
```csharp
331307
bool has42YearOlds = db.Exists<Person>(new { Age = 42 });
332308
```
333309

334-
**WHERE "Age" = @Age**
335-
336310
```csharp
337311
List<string> results = db.Column<string>(db.From<Person>().Select(x => x.LastName)
338312
.Where(q => q.Age == 27));
339313
```
340314

341-
**SELECT "LastName" FROM "Person" WHERE ("Age" = 27)**
342-
343315
```csharp
344316
HashSet<int> results = db.ColumnDistinct<int>(db.From<Person>().Select(x => x.Age)
345317
.Where(q => q.Age < 50));
346318
```
347319

348-
**SELECT "Age" FROM "Person" WHERE ("Age" < 50)**
349-
350320
```csharp
351321
Dictionary<int,string> results = db.Dictionary<int, string>(
352322
db.From<Person>().Select(x => new { x.Id, x.LastName }).Where(x => x.Age < 50));
353323
```
354324

355-
**SELECT "Id","LastName" FROM "Person" WHERE ("Age" < 50)**
356-
357-
358325
```csharp
359326
Dictionary<int, List<string>> results = db.Lookup<int, string>(
360327
db.From<Person>().Select(x => new { x.Age, x.LastName }).Where(q => q.Age < 50));
361328
```
362329

363-
**SELECT "Age","LastName" FROM "Person" WHERE ("Age" < 50)**
364-
365330
### INSERT, UPDATE and DELETEs
366331

367332
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:
384349
```csharp
385350
db.Update(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27});
386351
```
387-
**UPDATE "Person" SET "FirstName" = 'Jimi',"LastName" = 'Hendrix',"Age" = 27 WHERE "Id" = 1**
388-
389352
If you supply your own where expression, it updates every field (inc. Id) but uses your filter instead:
390353

391354
```csharp
392355
db.Update(new Person { Id = 1, FirstName = "JJ" }, p => p.LastName == "Hendrix");
393356
```
394-
**UPDATE "Person" SET "Id" = 1,"FirstName" = 'JJ',"LastName" = NULL,"Age" = NULL WHERE ("LastName" = 'Hendrix')**
395-
396357
One way to limit the fields which gets updated is to use an **Anonymous Type**:
397358

398359
```csharp
@@ -405,8 +366,6 @@ Or by using `UpdateNonDefaults` which only updates the non-default values in you
405366
db.UpdateNonDefaults(new Person { FirstName = "JJ" }, p => p.LastName == "Hendrix");
406367
```
407368

408-
**UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')**
409-
410369
#### UpdateOnly
411370

412371
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
416375
```csharp
417376
db.UpdateOnly(new Person { FirstName = "JJ" }, p => p.FirstName);
418377
```
419-
**UPDATE "Person" SET "FirstName" = 'JJ'**
420-
421378
```csharp
422379
db.UpdateOnly(new Person { FirstName = "JJ", Age = 12 },
423380
onlyFields: p => new { p.FirstName, p.Age });
424381
```
425-
**UPDATE "Person" SET "FirstName" = 'JJ', "Age" = 12**
426-
427382
When present, the second expression is used as the where filter:
428383
```csharp
429384
db.UpdateOnly(new Person { FirstName = "JJ" },
430385
onlyFields: p => p.FirstName,
431386
where: p => p.LastName == "Hendrix");
432387
```
433-
**UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')**
434-
435388
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:
436389

437390
```csharp
438391
db.UpdateOnly(new Person { FirstName = "JJ", LastName = "Hendo" },
439392
onlyFields: q => q.Update(p => p.FirstName));
440393
```
441-
**UPDATE "Person" SET "FirstName" = 'JJ'**
394+
395+
Using a typed SQL Expression:
442396

443397
```csharp
444398
db.UpdateOnly(new Person { FirstName = "JJ" },
445399
onlyFields: q => q.Update(p => p.FirstName).Where(x => x.FirstName == "Jimi"));
446400
```
447-
**UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')**
448401

449402
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)):
450403

@@ -458,23 +411,18 @@ Even the Table name can be a string so you perform the same update without requi
458411
db.UpdateFmt(table: "Person", set: "FirstName = {0}".SqlFmt("JJ"),
459412
where: "LastName = {0}".SqlFmt("Hendrix"));
460413
```
461-
**UPDATE "Person" SET FirstName = 'JJ' WHERE LastName = 'Hendrix'**
462-
463414
### INSERT
464415

465416
Insert's are pretty straight forward since in most cases you want to insert every field:
466417

467418
```csharp
468419
db.Insert(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 });
469420
```
470-
**INSERT INTO "Person" ("Id","FirstName","LastName","Age") VALUES (1,'Jimi','Hendrix',27)**
471-
472421
But do provide an API that takes an Expression Visitor for the rare cases you don't want to insert every field
473422

474423
```csharp
475424
db.InsertOnly(new Person { FirstName = "Amy" }, q => q.Insert(p => new {p.FirstName}))
476425
```
477-
**INSERT INTO "Person" ("FirstName") VALUES ('Amy')**
478426

479427
### DELETE
480428

@@ -488,8 +436,6 @@ Or an Expression Visitor:
488436
db.Delete<Person>(q => q.Where(p => p.Age == 27));
489437
```
490438

491-
**DELETE FROM "Person" WHERE ("Age" = 27)**
492-
493439
As well as un-typed, string-based expressions:
494440
```csharp
495441
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
500446
db.Delete(table: "Person", where: "Age = {0}".SqlFmt(27));
501447
```
502448

503-
**DELETE FROM "Person" WHERE Age = 27**
504-
505449
# API Overview
506450

507451
The API is minimal, providing basic shortcuts for the primitive SQL statements:

0 commit comments

Comments
 (0)