forked from sqlkata/querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
114 lines (93 loc) · 3.38 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using SqlKata;
using SqlKata.Compilers;
using SqlKata.Execution;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Linq;
using Newtonsoft.Json;
using Npgsql;
using System.Data;
using Dapper;
using System.Data.SQLite;
using static SqlKata.Expressions;
using System.IO;
namespace Program
{
class Program
{
private class Loan
{
public string Id { get; set; }
public string Name { get; set; }
public List<Installment> Installments { get; set; } = new List<Installment>();
}
private class Installment
{
public string Id { get; set; }
public string LoanId { get; set; }
public int DaysCount { get; set; }
}
static void Main(string[] args)
{
using (var db = SqlLiteQueryFactory())
{
var query = db.Query("accounts")
.Where("balance", ">", 0)
.GroupBy("balance")
.Limit(10);
var accounts = query.Clone().Get();
Console.WriteLine(JsonConvert.SerializeObject(accounts, Formatting.Indented));
var exists = query.Clone().Exists();
Console.WriteLine(exists);
}
}
private static void log(Compiler compiler, Query query)
{
var compiled = compiler.Compile(query);
Console.WriteLine(compiled.ToString());
Console.WriteLine(JsonConvert.SerializeObject(compiled.Bindings));
}
private static QueryFactory SqlLiteQueryFactory()
{
var compiler = new SqliteCompiler();
var connection = new SQLiteConnection("Data Source=Demo.db");
var db = new QueryFactory(connection, compiler);
db.Logger = result =>
{
Console.WriteLine(result.ToString());
};
if (!File.Exists("Demo.db"))
{
Console.WriteLine("db not exists creating db");
SQLiteConnection.CreateFile("Demo.db");
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,
});
}
}
return db;
}
private static QueryFactory SqlServerQueryFactory()
{
var compiler = new PostgresCompiler();
var connection = new SqlConnection(
"Server=tcp:localhost,1433;Initial Catalog=Lite;User ID=sa;Password=P@ssw0rd"
);
var db = new QueryFactory(connection, compiler);
db.Logger = result =>
{
Console.WriteLine(result.ToString());
};
return db;
}
}
}