Skip to content

Commit 3c0c875

Browse files
Merge pull request #390 from sqlkata/feature/add-exists
add Exists and NotExists
2 parents 0406270 + b9535ee commit 3c0c875

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

Program/Program.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,18 @@ private class Installment
3434

3535
static void Main(string[] args)
3636
{
37-
38-
var query = new Query("accounts").AsInsert(new
37+
using (var db = SqlLiteQueryFactory())
3938
{
40-
name = "new Account",
41-
currency_id = "USD",
42-
created_at = DateTime.UtcNow,
43-
Value = SqlKata.Expressions.UnsafeLiteral("nextval('hello')", replaceQuotes: false)
44-
});
45-
46-
var compiler = new SqlServerCompiler();
47-
var sql = compiler.Compile(query).Sql;
48-
Console.WriteLine(sql);
39+
var query = db.Query("accounts")
40+
.Where("balance", ">", 0)
41+
.GroupBy("balance")
42+
.Limit(10);
4943

44+
var accounts = query.Clone().Get();
45+
Console.WriteLine(JsonConvert.SerializeObject(accounts, Formatting.Indented));
5046

51-
using (var db = SqlLiteQueryFactory())
52-
{
53-
var accounts = db.Query("accounts").Get();
54-
Console.WriteLine(accounts.Count());
47+
var exists = query.Clone().Exists();
48+
Console.WriteLine(exists);
5549
}
5650
}
5751

@@ -81,7 +75,17 @@ private static QueryFactory SqlLiteQueryFactory()
8175

8276
SQLiteConnection.CreateFile("Demo.db");
8377

84-
db.Statement("create table accounts(id integer primary key autoincrement, name varchar, currency_id varchar, created_at datetime);");
78+
db.Statement("create table accounts(id integer primary key autoincrement, name varchar, currency_id varchar, balance decimal, created_at datetime);");
79+
for (var i = 0; i < 10; i++)
80+
{
81+
db.Statement("insert into accounts(name, currency_id, balance, created_at) values(@name, @currency, @balance, @date)", new
82+
{
83+
name = $"Account {i}",
84+
currency = "USD",
85+
balance = 100 * i * 1.1,
86+
date = DateTime.UtcNow,
87+
});
88+
}
8589

8690
}
8791

SqlKata.Execution/Query.Extensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ namespace SqlKata.Execution
77
{
88
public static class QueryExtensions
99
{
10+
public static bool Exists(this Query query, IDbTransaction transaction = null, int? timeout = null)
11+
{
12+
return CreateQueryFactory(query).Exists(query, transaction, timeout);
13+
}
14+
15+
public async static Task<bool> ExistsAsync(this Query query, IDbTransaction transaction = null, int? timeout = null)
16+
{
17+
return await CreateQueryFactory(query).ExistsAsync(query, transaction, timeout);
18+
}
19+
20+
public static bool NotExist(this Query query, IDbTransaction transaction = null, int? timeout = null)
21+
{
22+
return !CreateQueryFactory(query).Exists(query, transaction, timeout);
23+
}
24+
25+
public async static Task<bool> NotExistAsync(this Query query, IDbTransaction transaction = null, int? timeout = null)
26+
{
27+
return !(await CreateQueryFactory(query).ExistsAsync(query, transaction, timeout));
28+
}
29+
1030
public static IEnumerable<T> Get<T>(this Query query, IDbTransaction transaction = null, int? timeout = null)
1131
{
1232
return CreateQueryFactory(query).Get<T>(query, transaction, timeout);

SqlKata.Execution/QueryFactory.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,30 @@ public async Task<IEnumerable<IEnumerable<T>>> GetAsync<T>(
342342

343343
}
344344

345+
public bool Exists(Query query, IDbTransaction transaction = null, int? timeout = null)
346+
{
347+
var clone = query.Clone()
348+
.ClearComponent("select")
349+
.SelectRaw("1 as [Exists]")
350+
.Limit(1);
351+
352+
var rows = Get(clone, transaction, timeout);
353+
354+
return rows.Any();
355+
}
356+
357+
public async Task<bool> ExistsAsync(Query query, IDbTransaction transaction = null, int? timeout = null)
358+
{
359+
var clone = query.Clone()
360+
.ClearComponent("select")
361+
.SelectRaw("1 as [Exists]")
362+
.Limit(1);
363+
364+
var rows = await GetAsync(clone, transaction, timeout);
365+
366+
return rows.Any();
367+
}
368+
345369
public T Aggregate<T>(
346370
Query query,
347371
string aggregateOperation,

0 commit comments

Comments
 (0)