Skip to content

Feature/add create table #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6302c9e
Add Support for Create Table
Jun 27, 2023
c2632d0
Create Table Query making is started
Jun 27, 2023
31b7d97
change the type of create table return type
Jun 27, 2023
1959d03
Create Table Class for Query is Added
Jun 27, 2023
0c72c03
Adding sql server compiler for create table
Jun 27, 2023
0b9f44b
sql server compiler is now completed
Jun 27, 2023
762f5d6
Debugging sql server compiler has been started
Jun 27, 2023
2b8b9c9
sql server compiler has been corrected but some small changes needs t…
Jun 27, 2023
5aa0cb4
Correct DbType for creating table in db
Jun 30, 2023
6dea909
Correct and refactor create table
Jun 30, 2023
6fd4137
Create Table in all dbs was ok
Jun 30, 2023
f32afe2
Add Sqlite for creating table
Jun 30, 2023
f479f94
Refactor and fix the type conversion
Jun 30, 2023
f9a5c0f
fix some bugs
Jul 1, 2023
ac56c01
fix bug in creating table in postgresql
Jul 1, 2023
05c882e
oracle create query add ON COMMIT PRESERVE ROWS
Jul 2, 2023
9f85db9
revert the last change
Jul 2, 2023
97d87a4
change the structure of creating table query
AlirezaEiji191379 Jul 3, 2023
c50860e
Create Table Fillers and Formatters
AlirezaEiji191379 Jul 3, 2023
f546968
Add DI to SqlKata
AlirezaEiji191379 Jul 3, 2023
f6e3cdc
change the versions
AlirezaEiji191379 Jul 3, 2023
0a1b8b5
di managing
AlirezaEiji191379 Jul 3, 2023
e2e3ce4
Create Table is now under test
AlirezaEiji191379 Jul 3, 2023
907b2ca
bug fix for oracle
AlirezaEiji191379 Jul 3, 2023
4a99fe0
on commit preserve rows was added success fully for oracle create table
AlirezaEiji191379 Jul 3, 2023
acdccff
add hint and collation for table creation
AlirezaEiji191379 Jul 4, 2023
dd7cbd3
add collate to CreateTableColumn
Jul 4, 2023
e4706a7
create CreateTableAsClause.cs
Jul 4, 2023
fe3247d
Create table as is now completed and under test
AlirezaEiji191379 Jul 4, 2023
298428e
Add DropTable
AlirezaEiji191379 Jul 4, 2023
aaa8583
db column and format was changed
AlirezaEiji191379 Jul 5, 2023
20275f0
correct create table as format
Jul 5, 2023
0fe20e3
Add Select Into Query
Jul 5, 2023
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
149 changes: 59 additions & 90 deletions Program/Program.cs
Original file line number Diff line number Diff line change
@@ -1,114 +1,83 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
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;
using SqlKata.Compilers.Abstractions;
using SqlKata.Compilers.DDLCompiler.CreateTableBuilders.DBSpecificQueries;
using SqlKata.Compilers.Enums;
using SqlKata.Contract.CreateTable;
using SqlKata.Contract.CreateTable.DbTableSpecific;
using SqlKata.DbTypes.DbColumn;
using SqlKata.DbTypes.Enums;

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);
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddKataServices();
var provider = serviceCollection.BuildServiceProvider();
var compilerProvider = provider.GetRequiredService<ICompilerProvider>();
var compiler = compilerProvider.CreateCompiler(DataSource.Postgresql);

//var query = new Query("Users").DropTable();
//var query = new Query("Users").Truncate();
//var query = CreateTable();
//var query = CreateTableAs();

/* var fromClause = new FromClause()
{
Table = "dbo.user"
};

Console.WriteLine(compiler.Wrap(fromClause.Table));*/
var query = new Query("users").Select("Id", "FullName").Into("public.Sample");
Console.WriteLine(compiler.Compile(query));
}

private static void log(Compiler compiler, Query query)
private static Query CreateTableAs()
{
var compiled = compiler.Compile(query);
Console.WriteLine(compiled.ToString());
Console.WriteLine(JsonConvert.SerializeObject(compiled.Bindings));
var selectQuery = new Query("Users").Select("id", "fullname", "age");
var query = new Query("SampleUsers").CreateTableAs(selectQuery, TableType.Temporary,
new OracleDbTableExtensions() { OnCommitPreserveRows = true });
return query;
}

private static QueryFactory SqlLiteQueryFactory()
private static Query CreateTable()
{
var compiler = new SqliteCompiler();

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

var db = new QueryFactory(connection, compiler);

db.Logger = result =>
var query = new Query("Users").CreateTable(new List<TableColumnDefinitionDto>()
{
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++)
new()
{
ColumnName = "id",
ColumnDbType = new OracleDBColumn()
{
OracleDbType = OracleDbType.Int32
},
IsAutoIncrement = true,
IsNullable = false,
IsPrimaryKey = true,
IsUnique = false,
},
new()
{
db.Statement("insert into accounts(name, currency_id, balance, created_at) values(@name, @currency, @balance, @date)", new
ColumnName = "FullName",
ColumnDbType = new OracleDBColumn()
{
name = $"Account {i}",
currency = "USD",
balance = 100 * i * 1.1,
date = DateTime.UtcNow,
});
OracleDbType = OracleDbType.Varchar2,
Length = 30,
//Collation = "Arabiv_Ci_100_Ai"
},
IsAutoIncrement = false,
IsNullable = false,
IsPrimaryKey = false,
IsUnique = true,
}

}

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;
}, TableType.Temporary);
return query;
}

}
}
3 changes: 2 additions & 1 deletion Program/Program.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
<PackageReference Include="Npgsql" Version="4.0.4" />
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
<PackageReference Include="System.Data.SQLite" Version="1.0.109.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions QueryBuilder.Tests/Infrastructure/TestCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public virtual MethodInfo Call_FindCompilerMethodInfo(Type clauseType, string me
{
return FindCompilerMethodInfo(clauseType, methodName);
}

protected override SqlResult CompileCreateTableAs(Query query)
{
throw new NotImplementedException();
}

protected override SqlResult CompileCreateTable(Query query)
{
throw new NotImplementedException();
}
}

class TestSqlServerCompiler : SqlServerCompiler
Expand Down
33 changes: 32 additions & 1 deletion QueryBuilder/Clauses/ColumnClause.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using SqlKata.DbTypes.DbColumn;

namespace SqlKata
{
public abstract class AbstractColumn : AbstractClause
Expand Down Expand Up @@ -30,6 +32,35 @@ public override AbstractClause Clone()
}
}

public class CreateTableColumn : AbstractClause
{
public string ColumnName { get; set; }
public BaseDBColumn ColumnDbType { get; set; }
public bool IsNullable { get; set; }
public bool IsPrimaryKey { get; set; }
public bool IsUnique { get; set; }
public bool IsIdentity { get; set; }
public bool IsAutoIncrement { get; set; }
public string Collate { get; set; }
public override AbstractClause Clone()
{
return new CreateTableColumn
{
Component= Component,
ColumnDbType= ColumnDbType,
ColumnName= ColumnName,
IsAutoIncrement= IsAutoIncrement,
IsPrimaryKey= IsPrimaryKey,
IsUnique= IsUnique,
IsIdentity= IsIdentity,
IsNullable= IsNullable,
Collate = Collate
};
}
}



/// <summary>
/// Represents column clause calculated using query.
/// </summary>
Expand Down Expand Up @@ -85,7 +116,7 @@ public override AbstractClause Clone()
public class AggregatedColumn : AbstractColumn
{
/// <summary>
/// Gets or sets the a query that used to filter the data,
/// Gets or sets the a query that used to filter the data,
/// the compiler will consider only the `Where` clause.
/// </summary>
/// <value>
Expand Down
14 changes: 14 additions & 0 deletions QueryBuilder/Clauses/CreateTableAsClause.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace SqlKata.Clauses
{
public class CreateTableAsClause : AbstractClause
{
public Query SelectQuery { get; set; }
public override AbstractClause Clone()
{
return new CreateTableAsClause
{
SelectQuery = SelectQuery.Clone()
};
}
}
}
24 changes: 24 additions & 0 deletions QueryBuilder/Clauses/CreateTableQueryExtensionClause.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using SqlKata.Contract.CreateTable.DbTableSpecific;

namespace SqlKata.Clauses
{
/// <summary>
/// this clause is for other queries that are supported by specific dbs. for example in oracle we have ON COMMIT PRESERVER ROWS for creating temp table
/// and we do not have this option in other databases like mysql!
/// this can be inherited and used for other dbs.
/// </summary>
public class CreateTableQueryExtensionClause : AbstractClause
{
public CreateDbTableExtension CreateDbTableExtension { get; set; }

public override AbstractClause Clone()
{
return new CreateTableQueryExtensionClause()
{
Component = Component,
Engine = Engine,
CreateDbTableExtension = CreateDbTableExtension
};
}
}
}
16 changes: 16 additions & 0 deletions QueryBuilder/Clauses/IntoClause.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace SqlKata.Clauses
{
public class IntoClause : AbstractClause
{
public string TableName { get; set; }

public override AbstractClause Clone()
{
return new IntoClause()
{
TableName = TableName,
Component = Component
};
}
}
}
17 changes: 17 additions & 0 deletions QueryBuilder/Clauses/TableCluase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using SqlKata.Contract.CreateTable;

namespace SqlKata.Clauses
{
public class TableCluase : AbstractClause
{
public TableType TableType { get; set; }
public override AbstractClause Clone()
{
return new TableCluase()
{
TableType = TableType,
Component = Component
};
}
}
}
10 changes: 10 additions & 0 deletions QueryBuilder/Compilers/Abstractions/ICompilerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SqlKata.Compilers.Enums;

namespace SqlKata.Compilers.Abstractions
{
internal interface ICompilerFactory
{
DataSource DataSource { get; }
Compiler CreateCompiler();
}
}
9 changes: 9 additions & 0 deletions QueryBuilder/Compilers/Abstractions/ICompilerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SqlKata.Compilers.Enums;

namespace SqlKata.Compilers.Abstractions
{
public interface ICompilerProvider
{
Compiler CreateCompiler(DataSource dataSource);
}
}
Loading