Skip to content

Commit

Permalink
organize Program.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-moussawi committed Jun 28, 2019
1 parent 1a2ae58 commit c6b4cba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.pfx
npm-debug.log
Program/node_modules
*.db

# User-specific files
*.suo
Expand Down
Binary file removed Program/Demo.db
Binary file not shown.
67 changes: 52 additions & 15 deletions Program/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
using System.Data;
using Dapper;
using System.Data.SQLite;
using static SqlKata.Query;
using static SqlKata.Expressions;
using System.IO;

namespace Program
{
Expand All @@ -34,34 +35,70 @@ private class Installment
static void Main(string[] args)
{

IDbConnection connection = new SqlConnection(
"Server=tcp:localhost,1433;Initial Catalog=Lite;User ID=sa;Password=P@ssw0rd"
);
var db = SqlLiteQueryFactory();

// SQLiteConnection.CreateFile("Demo.db");
var id = db.Query("accounts").InsertGetId<int>(new
{
name = "new Account",
currency_id = "USD",
created_at = DateTime.UtcNow
});

var id2 = db.Select<int>("insert into accounts(name, currency_id, created_at) values ('account 2','usd','2019-01-01 20:00:00');select last_insert_rowid();");

Console.WriteLine($"last id is: {id}");
Console.WriteLine($"last id2 is: {id2.First()}");

}

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();

// connection = new SQLiteConnection("Data Source=Demo.db");
var connection = new SQLiteConnection("Data Source=Demo.db");

var db = new QueryFactory(connection, new SqlServerCompiler());
var db = new QueryFactory(connection, compiler);

db.Logger = result =>
{
Console.WriteLine(result.ToString());
};

var query = new Query("Users")
.Define("Age", 17)
.Where(q => q.Where("Age", ">", Variable("Age")).OrWhere("Age", "<", Variable("Age")));
if (!File.Exists("Demo.db"))
{
Console.WriteLine("db not exists creating db");

SQLiteConnection.CreateFile("Demo.db");

log(new SqlServerCompiler(), query);
db.Statement("create table accounts(id integer primary key autoincrement, name varchar, currency_id varchar, created_at datetime);");

}

return db;
}

private static void log(Compiler compiler, Query query)
private static QueryFactory SqlServerQueryFactory()
{
var compiled = compiler.Compile(query);
Console.WriteLine(compiled.ToString());
Console.WriteLine(JsonConvert.SerializeObject(compiled.Bindings));
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;
}

}
Expand Down

0 comments on commit c6b4cba

Please sign in to comment.