Skip to content

Commit ca1b9bd

Browse files
committedFeb 7, 2020
add playground hint
1 parent 8dc65ce commit ca1b9bd

15 files changed

+64
-7
lines changed
 

‎advanced.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
Sometimes you need to do some actions only when certain conditions are met, in these cases you can use the `When(condition, whenTrue, whenFalse = null)` method.
66

77
```cs
8+
//:playground
89
var query = db.Query("Transactions");
910

11+
var amount = 100;
12+
1013
query.When(
1114
amount > 0,
1215
q => q.Select("Debit as Amount"),
@@ -18,6 +21,7 @@ query.When(
1821
is the same as
1922

2023
```cs
24+
//:playground
2125
var query = db.Query("Transactions");
2226

2327
if(amount > 0)
@@ -57,6 +61,7 @@ This is helpful when you want to apply some native functions, that are available
5761
### Casting Example
5862

5963
```cs
64+
//:playground
6065
var query = new Query("Posts")
6166
.Select("Id", "Title")
6267
.ForPostgres(q => q.SelectRaw("[Date]::date"))
@@ -87,13 +92,14 @@ Another example is to generate a date series between two given dates, you can us
8792

8893

8994
```cs
95+
//:playground
9096
var now = DateTime.UtcNow;
9197
var format = "yyyy-MM-dd";
9298

9399
DateTime from = now.AddDays(-5).ToString(format),
94100
now.ToString(format);
95101

96-
var rangeQuery = new Query()
102+
var query = new Query()
97103

98104
.ForPostgres(q =>
99105

‎combine.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ the above methods accept either an instance of `Query` or a labmda expression
99

1010

1111
```cs
12+
//:playground
1213
var phones = new Query("Phones");
1314
var laptops = new Query("Laptops");
1415

@@ -23,6 +24,7 @@ var mobiles = laptops.Union(phones);
2324
Or by using the labmda overload
2425

2526
```cs
27+
//:playground
2628
var mobiles = new Query("Laptops").ExceptAll(q => q.From("OldLaptops"));
2729
```
2830

@@ -35,6 +37,7 @@ var mobiles = new Query("Laptops").ExceptAll(q => q.From("OldLaptops"));
3537
You can always use the `CombineRaw` method to append raw expressions
3638

3739
```cs
40+
//:playground
3841
var mobiles = new Query("Laptops").CombineRaw("union all select * from OldLaptops");
3942
```
4043

@@ -46,6 +49,7 @@ Off course you can use the table identifier characters `[` and `]` to instruct S
4649

4750

4851
```cs
52+
//:playground
4953
var mobiles = new Query("Laptops").CombineRaw("union all select * from [OldLaptops]");
5054
```
5155

‎compilers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Currently, SqlKata query builder supports natively the following compilers **Sql
1010
Theoretically the output of different compilers should be similar, this is true for the 80% of the cases, however in some edge cases the output can be very different, for instance take a look how the `Limit` and `Offset` clause get compiled in each compiler
1111

1212
```cs
13+
//:playground
1314
new Query("Posts").Limit(10).Offset(20);
1415
```
1516

‎cte.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ In Sql, CTE is represented as a `with` clause.
1515
To add a CTE to your query simply use the `With` method.
1616

1717
```cs
18+
//:playground
1819
var activePosts = new Query("Comments")
1920
.Select("PostId")
2021
.SelectRaw("count(1) as Count")
@@ -38,6 +39,7 @@ SELECT [Posts].*, [ActivePosts].[Count] FROM [Posts] INNER JOIN [ActivePosts] ON
3839
You can use the `WithRaw` method if you want to pass a raw Sql Expression.
3940

4041
```cs
42+
//:playground
4143
var query = new Query("Posts")
4244
.WithRaw("ActivePosts", "select PostId, count(1) as count from Comments having count(1) > ?", new [] {50}) // now you can consider ActivePosts as a regular table in the database
4345
.Join("ActivePosts", "ActivePosts.PostId", "Posts.Id")

‎from.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
The `Query` constructor takes an optional parameter to set the `from` clause
55

66
```cs
7+
//:playground
78
new Query("Posts");
89
```
910

1011
Or you can use the `From` method to set it
1112

1213
```cs
14+
//:playground
1315
new Query().From("Posts");
1416
```
1517

@@ -21,6 +23,7 @@ SELECT * FROM [Posts]
2123
To alias the table you should use the `as` syntax
2224

2325
```cs
26+
//:playground
2427
new Query("Posts as p")
2528
```
2629

@@ -33,6 +36,7 @@ SELECT * FROM [Posts] AS [p]
3336
You can select from a sub query by passing a `Query` instance to the `From` method or you can use the `Lambda` function overload.
3437

3538
```cs
39+
//:playground
3640
var fewMonthsAgo = DateTime.UtcNow.AddMonths(-6);
3741
var oldPostsQuery = new Query("Posts").Where("Date", "<", fewMonthsAgo).As("old");
3842

@@ -47,6 +51,7 @@ SELECT * FROM (SELECT * FROM [Posts] WHERE [Date] < '2017-06-01 6:31:26') AS [ol
4751
You can rewrite the same query by using the `Lambda` function overload
4852

4953
```cs
54+
//:playground
5055
new Query().From(q =>
5156
q.From("Posts").Where("Date", "<", fewMonthsAgo).As("old")
5257
).OrderByDesc("Date");

‎group.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## GroupBy
44

55
```cs
6+
//:playground
67
var query = new Query("Comments")
78
.Select("PostId")
89
.SelectRaw("count(1) as count")
@@ -16,6 +17,7 @@ SELECT [PostId], count(1) as count FROM [Comments] GROUP BY [PostId]
1617
## GroupByRaw
1718

1819
```cs
20+
//:playground
1921
var query = new Query("Companies")
2022
.Select("Profit")
2123
.SelectRaw("COUNT(*) as count")

‎having.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Having
44

55
```cs
6+
//:playground
67
var commentsCount = new Query("Comments").Select("PostId").SelectRaw("count(1) as Count").GroupBy("PostId");
78

89
var query = new Query().From(commentsCount).Having("Count", ">", 100);
@@ -17,6 +18,7 @@ SELECT * FROM (SELECT [PostId], count(1) as Count FROM [Comments] GROUP BY [Post
1718
## HavingRaw
1819

1920
```cs
21+
//:playground
2022
var query = new Query("Comments").Select("PostId").SelectRaw("count(1) as Count").GroupBy("PostId").HavingRaw("count(1) > 50");
2123
```
2224

‎join.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
To apply an inner join use the `Join` method
66

77
```cs
8+
//:playground
89
var query = new Query("Posts").Join("Authors", "Authors.Id", "Posts.AuthorId");
910
```
1011

@@ -17,12 +18,14 @@ SELECT * FROM [Posts] INNER JOIN [Authors] ON [Authors].[Id] = [Posts].[AuthorId
1718
The 4th parameter is optional and default to `=`, pass any other operator to override the join operator.
1819

1920
```cs
21+
//:playground
2022
var query = new Query("Posts").Join("Comments", "Comments.Date", "Posts.Date", ">");
2123
```
2224

2325
## Join with a Sub Query
2426

2527
```cs
28+
//:playground
2629
var topComments = new Query("Comments").OrderByDesc("Likes").Limit(10);
2730

2831
var posts = new Query("Posts").LeftJoin(
@@ -44,7 +47,8 @@ SELECT * FROM [Posts] LEFT JOIN (
4447
In some advanced cases you may need to apply some constraints on the join clause.
4548

4649
```cs
47-
var comments = new Query("comments").LeftJoin("Posts", j =>
50+
//:playground
51+
var comments = new Query("comments").LeftJoin("Posts", j =>
4852
j.On("Posts.Id", "Comments.Id").WhereNotNull("Comments.AuthorId")
4953
);
5054
```

‎limit.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
`Limit` and `Offset` allows you to limit the number of results returned from the database, this method is highly correlated with the `OrderBy` and `OrderByDesc` methods.
44

55
```cs
6+
//:playground
67
var latestPosts = new Query("Posts").OrderByDesc("Date").Limit(10)
78
```
89

@@ -27,6 +28,7 @@ SELECT * FROM `Posts` ORDER BY `Date` DESC LIMIT 10
2728
if you want to skip some records, use the `Offset` method.
2829

2930
```cs
31+
//:playground
3032
var latestPosts = new Query("Posts").OrderByDesc("Date").Limit(10).Offset(5);
3133
```
3234

@@ -55,6 +57,7 @@ SELECT * FROM `Posts` ORDER BY `Date` DESC LIMIT 10 OFFSET 5
5557
You can use the `ForPage` method to easily paginate your data.
5658

5759
```cs
60+
//:playground
5861
var posts = new Query("Posts").OrderByDesc("Date").ForPage(2);
5962
```
6063

@@ -64,6 +67,7 @@ By default this method will return `15` rows per page, you can override this val
6467
6568

6669
```cs
70+
//:playground
6771
var posts = new Query("Posts").OrderByDesc("Date").ForPage(3, 50);
6872
```
6973

‎order.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## OrderBy
44

55
```cs
6+
//:playground
67
var query = new Query("Comments").OrderBy("Date").OrderByDesc("Name");
78
```
89

@@ -15,6 +16,7 @@ SELECT * FROM [Comments] ORDER BY [Date], [Name] DESC
1516
## OrderByRaw
1617

1718
```cs
19+
//:playground
1820
var query = new Query("Comments").OrderByRaw("[Likes] DESC NULLS LAST")
1921
```
2022

‎select.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Select a single or many columns
55

66
```cs
7+
//:playground
78
new Query("Posts").Select("Id", "Title", "CreatedAt as Date");
89
```
910

@@ -17,9 +18,10 @@ SELECT [Id], [Title], [CreatedAt] AS [Date] FROM [Posts]
1718
Select from a sub query
1819

1920
```cs
20-
var countQuery = new Query("Comments").WhereColumns("Comments.PostId", "Posts.Id").AsCount();
21+
//:playground
22+
var countQuery = new Query("Comments").WhereColumns("Comments.PostId", "=", "Posts.Id").AsCount();
2123

22-
new Query("Posts").Select("Id").Select(countQuery, "CommentsCount");
24+
var query = new Query("Posts").Select("Id").Select(countQuery, "CommentsCount");
2325
```
2426

2527
```sql
@@ -30,6 +32,7 @@ SELECT [Id], (SELECT COUNT(*) AS [count] FROM [Comments] WHERE [Comments].[PostI
3032
Your friend when you need the full freedom
3133

3234
```cs
35+
//:playground
3336
new Query("Posts").Select("Id").SelectRaw("count(1) over(partition by AuthorId) as PostsByAuthor")
3437
```
3538

@@ -42,6 +45,7 @@ You can wrap your identifier inside `[` and `]` so they get recognized by SqlKat
4245

4346

4447
```cs
48+
//:playground
4549
new Query("Posts").Select("Id").SelectRaw("count(1) over(partition by [AuthorId]) as [PostsByAuthor]")
4650
```
4751

@@ -69,6 +73,7 @@ SELECT `Id`, count(1) over(partition by `AuthorId`) as `PostsByAuthor` FROM `Pos
6973
Starting **v1.1.2**, you can use the Braces Expansions feature, to select multiple columns at the same time. This will allow you to write the same query in a more compact way.
7074

7175
```cs
76+
//:playground
7277
new Query("Users")
7378
.Join("Profiles", "Profiles.UserId", "Users.Id")
7479
.Select("Users.{Id, Name, LastName}", "Profiles.{GithubUrl, Website}")
@@ -77,6 +82,7 @@ new Query("Users")
7782
Same as writing
7883

7984
```cs
85+
//:playground
8086
new Query("Users")
8187
.Join("Profiles", "Profiles.UserId", "Users.Id")
8288
.Select("Users.Id", "Users.Name", "Users.LastName", "Profiles.GithubUrl", "Profiles.Website")

‎update.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
## Insert
66
```cs
7+
//:playground
78
var query = new Query("Books").AsInsert(new {
89
Title = "Toyota Kata",
910
CreatedAt = new DateTime(2009, 8, 4),
@@ -23,6 +24,7 @@ INSERT INTO [Books] ([Name], [CreatedAt], [Author]) VALUES ('Toyota Kata', '2009
2324
you can use the insert many overload to insert multiple records
2425

2526
```cs
27+
//:playground
2628
var cols = new [] {"Name", "Price"};
2729

2830
var data = new [] {
@@ -44,6 +46,7 @@ INSERT INTO [Products] ([Name], [Price]) VALUES ("A", 1000), ("B", 2000), ("C",
4446
You can also insert records for the result of another Select Query.
4547

4648
```cs
49+
//:playground
4750
var cols = new [] { "Id", "Name", "Address" };
4851
new Query("ActiveUsers").AsInsert(cols, new Query("Users").Where("Active", 1));
4952
```
@@ -55,6 +58,7 @@ INSERT INTO [ActiveUsers] ([Id], [Name], [Address]) SELECT * FROM [Users] WHERE
5558
## Update
5659

5760
```cs
61+
//:playground
5862
var query = new Query("Posts").WhereNull("AuthorId").AsUpdate(new {
5963
AuthorId = 10
6064
});
@@ -67,6 +71,7 @@ UPDATE [Posts] SET [AuthorId] = 10 WHERE [AuthorId] IS NULL
6771
## Delete
6872

6973
```cs
74+
//:playground
7075
var query = new Query("Posts").Where("Date", ">", DateTime.UtcNow.AddDays(-30)).AsDelete();
7176
```
7277

‎where-date.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ SELECT * FROM `Posts` WHERE TIME(`CreatedAt`) > '16:30'
5757
For example to get the posts created in the first of February.
5858

5959
```cs
60+
//:playground
6061
new Query("Posts").WhereDatePart("day", "CreatedAt", 1).WhereDatePart("month", "CreatedAt", 2);
6162
```
6263

‎where-string.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ To override this behavior you can pass a boolean `true` to the last parameter `c
99

1010

1111
```cs
12+
//:playground
1213
new Query("Posts").WhereEnds("Title", "Book")
1314
```
1415

0 commit comments

Comments
 (0)