Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions Program/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,18 @@ private class Installment

static void Main(string[] args)
{

var query = new Query("accounts").AsInsert(new
using (var db = SqlLiteQueryFactory())
{
name = "new Account",
currency_id = "USD",
created_at = DateTime.UtcNow,
Value = SqlKata.Expressions.UnsafeLiteral("nextval('hello')", replaceQuotes: false)
});

var compiler = new SqlServerCompiler();
var sql = compiler.Compile(query).Sql;
Console.WriteLine(sql);
var query = db.Query("accounts")
.Where("balance", ">", 0)
.GroupBy("balance")
.Limit(10);

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

using (var db = SqlLiteQueryFactory())
{
var accounts = db.Query("accounts").Get();
Console.WriteLine(accounts.Count());
var exists = query.Clone().Exists();
Console.WriteLine(exists);
}
}

Expand Down Expand Up @@ -81,7 +75,17 @@ private static QueryFactory SqlLiteQueryFactory()

SQLiteConnection.CreateFile("Demo.db");

db.Statement("create table accounts(id integer primary key autoincrement, name varchar, currency_id varchar, created_at datetime);");
db.Statement("create table accounts(id integer primary key autoincrement, name varchar, currency_id varchar, balance decimal, created_at datetime);");
for (var i = 0; i < 10; i++)
{
db.Statement("insert into accounts(name, currency_id, balance, created_at) values(@name, @currency, @balance, @date)", new
{
name = $"Account {i}",
currency = "USD",
balance = 100 * i * 1.1,
date = DateTime.UtcNow,
});
}

}

Expand Down
20 changes: 20 additions & 0 deletions SqlKata.Execution/Query.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ namespace SqlKata.Execution
{
public static class QueryExtensions
{
public static bool Exists(this Query query, IDbTransaction transaction = null, int? timeout = null)
{
return CreateQueryFactory(query).Exists(query, transaction, timeout);
}

public async static Task<bool> ExistsAsync(this Query query, IDbTransaction transaction = null, int? timeout = null)
{
return await CreateQueryFactory(query).ExistsAsync(query, transaction, timeout);
}

public static bool NotExist(this Query query, IDbTransaction transaction = null, int? timeout = null)
{
return !CreateQueryFactory(query).Exists(query, transaction, timeout);
}

public async static Task<bool> NotExistAsync(this Query query, IDbTransaction transaction = null, int? timeout = null)
{
return !(await CreateQueryFactory(query).ExistsAsync(query, transaction, timeout));
}

public static IEnumerable<T> Get<T>(this Query query, IDbTransaction transaction = null, int? timeout = null)
{
return CreateQueryFactory(query).Get<T>(query, transaction, timeout);
Expand Down
24 changes: 24 additions & 0 deletions SqlKata.Execution/QueryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,30 @@ public async Task<IEnumerable<IEnumerable<T>>> GetAsync<T>(

}

public bool Exists(Query query, IDbTransaction transaction = null, int? timeout = null)
{
var clone = query.Clone()
.ClearComponent("select")
.SelectRaw("1 as [Exists]")
.Limit(1);

var rows = Get(clone, transaction, timeout);

return rows.Any();
}

public async Task<bool> ExistsAsync(Query query, IDbTransaction transaction = null, int? timeout = null)
{
var clone = query.Clone()
.ClearComponent("select")
.SelectRaw("1 as [Exists]")
.Limit(1);

var rows = await GetAsync(clone, transaction, timeout);

return rows.Any();
}

public T Aggregate<T>(
Query query,
string aggregateOperation,
Expand Down