Skip to content
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

Feature/add create table #680

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
change the structure of creating table query
  • Loading branch information
AlirezaEiji191379 committed Jul 3, 2023
commit 97d87a4316d6b1a304c3eaf078b67af937001c29
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
};
}
}
}
10 changes: 10 additions & 0 deletions QueryBuilder/Compilers/DDLCompiler/Abstractions/IColumnCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Text;

namespace SqlKata.Compilers.DDLCompiler.Abstractions
{
internal interface IColumnCompiler
{
void CompileCreateTableColumns(StringBuilder queryString, List<CreateTableColumn> createTableColumnClauses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.Abstractions
{
internal interface ICreateTableQueryCompiler
{
StringBuilder CompileCreateTable(string tableName,TableType tableType,List<CreateTableColumn> createTableColumnClauses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SqlKata.Compilers.DDLCompiler.Abstractions
{
public interface IDDLCompiler
{
SqlResult CompileCreateTable(Query query);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Text;

namespace SqlKata.Compilers.DDLCompiler.Abstractions
{
internal interface IPrimaryKeyCompiler
{
void CompilePrimaryKey(StringBuilder queryString, List<CreateTableColumn> createTableColumnClauses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Text;

namespace SqlKata.Compilers.DDLCompiler.Abstractions
{
public interface IUniqueConstraintCompiler
{
void CompileUniqueConstraints(StringBuilder queryString, List<CreateTableColumn> createTableColumnClauses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Exceptions.CreateTableQuery;

namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.ColumnCompilers
{
internal class ColumnCompiler : IColumnCompiler
{
private readonly ISqlCreateCommandUtil _sqlCommandUtil;

public ColumnCompiler(ISqlCreateCommandUtil sqlCommandUtil)
{
_sqlCommandUtil = sqlCommandUtil;
}
public void CompileCreateTableColumns(StringBuilder queryString, List<CreateTableColumn> createTableColumnClauses)
{
var identityAndAutoIncrementColumns = createTableColumnClauses.Where(x => x.IsIdentity || x.IsAutoIncrement);
if (identityAndAutoIncrementColumns.Count() > 1)
{
throw new AutoIncrementOrIdentityExceededException("table can not have more than one auto increment or identity column");
}
foreach (var columnCluase in createTableColumnClauses)
{
var nullOrNot = columnCluase.IsNullable ? "NULL " : "NOT NULL ";
if (columnCluase.IsIdentity || columnCluase.IsAutoIncrement)
{
queryString.Append($"{columnCluase.ColumnName} {columnCluase.ColumnDbType.GetDBType()} {_sqlCommandUtil.AutoIncrementIdentityCommandGenerator()},\n");
continue;
}
queryString.Append($"{columnCluase.ColumnName} {columnCluase.ColumnDbType.GetDBType()} {nullOrNot},\n");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Text;
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.CreateTableCompilers
{
internal class GeneralCreateTableCompiler : ICreateTableQueryCompiler
{
private readonly ISqlCreateCommandUtil _sqlCommandUtil;
private readonly IColumnCompiler _columnCompiler;
private readonly IPrimaryKeyCompiler _primaryKeyCompiler;
private readonly IUniqueConstraintCompiler _uniqueConstraintCompiler;

public GeneralCreateTableCompiler(ISqlCreateCommandUtil sqlCommandUtil, IColumnCompiler columnCompiler, IPrimaryKeyCompiler primaryKeyCompiler, IUniqueConstraintCompiler uniqueConstraintCompiler)
{
_sqlCommandUtil = sqlCommandUtil;
_columnCompiler = columnCompiler;
_primaryKeyCompiler = primaryKeyCompiler;
_uniqueConstraintCompiler = uniqueConstraintCompiler;
}

public StringBuilder CompileCreateTable(string tableName,TableType tableType,List<CreateTableColumn> createTableColumnClauses)
{
var queryString = new StringBuilder(_sqlCommandUtil.CreateTableCommandGenerator(tableType,tableName));
queryString.Append("(\n");
_columnCompiler.CompileCreateTableColumns(queryString,createTableColumnClauses);
_primaryKeyCompiler.CompilePrimaryKey(queryString,createTableColumnClauses);
_uniqueConstraintCompiler.CompileUniqueConstraints(queryString,createTableColumnClauses);
queryString.Append(")\n");
return queryString;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.CreateCompilers
namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.CreateTableQueryUtils
{
internal class MySqlCreateCommandUtil : ISqlCreateCommandUtil
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.CreateCompilers
namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.CreateTableQueryUtils
{
internal class OracleCreateCommandUtil : ISqlCreateCommandUtil
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.CreateCompilers
namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.CreateTableQueryUtils
{
internal class PostgresqlCreateCommandUtil : ISqlCreateCommandUtil
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.CreateCompilers
namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.CreateTableQueryUtils
{
internal class SqlServerCreateCommandUtil : ISqlCreateCommandUtil
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using SqlKata.Compilers.DDLCompiler.Abstractions;
using SqlKata.Contract.CreateTable;

namespace SqlKata.Compilers.DDLCompiler.CreateCompilers
namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.CreateTableQueryUtils
{
internal class SqliteCreateColmmandUtil : ISqlCreateCommandUtil
internal class SqliteCreateCommandUtil : ISqlCreateCommandUtil
{
public string AutoIncrementIdentityCommandGenerator()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlKata.Compilers.DDLCompiler.Abstractions;

namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.PrimaryKeyCompilers
{
internal class PrimaryKeyCompiler : IPrimaryKeyCompiler
{
public void CompilePrimaryKey(StringBuilder queryString, List<CreateTableColumn> createTableColumnClauses)
{
var primaryKeys = createTableColumnClauses.Where(column => column.IsPrimaryKey);
if (primaryKeys.Any())
queryString.Append(string.Format("PRIMARY KEY ({0}),\n", string.Join(",", primaryKeys.Select(column => column.ColumnName))));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlKata.Compilers.DDLCompiler.Abstractions;

namespace SqlKata.Compilers.DDLCompiler.CreateTableBuilders.UniqueConstraintCompilers
{
public class UniqueConstraintCompiler : IUniqueConstraintCompiler
{
public void CompileUniqueConstraints(StringBuilder queryString, List<CreateTableColumn> createTableColumnClauses)
{
var uniqeColumns = createTableColumnClauses.Where(column => column.IsUnique).ToList();
for (var i = 0; i < uniqeColumns.Count(); i++)
{
queryString.Append($"CONSTRAINT unique_constraint_{i} UNIQUE ({uniqeColumns[i].ColumnName}), \n");
}
}
}
}
38 changes: 38 additions & 0 deletions QueryBuilder/Compilers/DDLCompiler/DDLCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using SqlKata.Clauses;
using SqlKata.Compilers.DDLCompiler.Abstractions;

namespace SqlKata.Compilers.DDLCompiler
{
internal class DDLCompiler
{
private readonly ICreateTableQueryCompiler _createTableQueryCompiler;

public DDLCompiler(ICreateTableQueryCompiler createTableQueryCompiler)
{
_createTableQueryCompiler = createTableQueryCompiler;
}


internal SqlResult CompileCreateTable(Query query)
{
var result = new SqlResult()
{
Query = query.Clone(),
};
var createTableColumnClauses = result.Query.GetComponents<CreateTableColumn>("CreateTableColumn");
var tableName = result.Query.GetOneComponent<FromClause>("from").Table;
var tableType = result.Query.GetOneComponent<TableCluase>("TableType").TableType;
var queryString = _createTableQueryCompiler.CompileCreateTable(tableName,tableType,createTableColumnClauses);
result.RawSql = RefineQueryString(queryString.ToString());
return result;
}

private static string RefineQueryString(string queryString)
{
var lastCommaChar = queryString.LastIndexOf(',');
if(lastCommaChar != -1)
queryString = queryString.Remove(lastCommaChar,1);
return queryString;
}
}
}
71 changes: 0 additions & 71 deletions QueryBuilder/Compilers/DDLCompiler/DbDDLCompiler.cs

This file was deleted.

12 changes: 12 additions & 0 deletions QueryBuilder/Compilers/Enums/DataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SqlKata.Compilers.Enums
{
internal enum DataSource
{
SqlServer,
Oracle,
Postgresql,
MySql,
Firebird,
Sqlite
}
}
5 changes: 1 addition & 4 deletions QueryBuilder/Compilers/MySqlCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using SqlKata.Compilers.DDLCompiler;
using SqlKata.Compilers.DDLCompiler.CreateCompilers;

namespace SqlKata.Compilers
{
public class MySqlCompiler : Compiler
Expand Down Expand Up @@ -51,7 +48,7 @@ public override string CompileLimit(SqlResult ctx)

protected override SqlResult CompileCreateTable(Query query)
{
return new DbDDLCompiler(new MySqlCreateCommandUtil()).CompileCreateTable(query);
return null;
}
}
}
10 changes: 5 additions & 5 deletions QueryBuilder/Compilers/OracleCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using SqlKata.Compilers.DDLCompiler.CreateCompilers;
using SqlKata.Compilers.DDLCompiler;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;

namespace SqlKata.Compilers
{
Expand Down Expand Up @@ -178,7 +174,11 @@ protected override SqlResult CompileRemainingInsertClauses(

protected override SqlResult CompileCreateTable(Query query)
{
return new DbDDLCompiler(new OracleCreateCommandUtil()).CompileCreateTable(query);
return null;
}




}
}
Loading