forked from sqlkata/querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (87 loc) · 3.06 KB
/
Program.cs
File metadata and controls
110 lines (87 loc) · 3.06 KB
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
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)
{
var query = new Query("accounts").AsInsert(new
{
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);
using (var db = SqlLiteQueryFactory())
{
var accounts = db.Query("accounts").Get();
Console.WriteLine(accounts.Count());
}
}
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, created_at datetime);");
}
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;
}
}
}