1
1
using System . Data ;
2
2
using System . Data . SqlClient ;
3
- using System . Text ;
4
3
5
4
namespace Serilog . Sinks . MSSqlServer . Platform
6
5
{
7
- internal class SqlTableCreator
6
+ internal class SqlTableCreator : ISqlTableCreator
8
7
{
9
- private readonly string _connectionString ;
10
- private readonly string _schemaName ;
11
- private readonly string _tableName ;
12
- private readonly DataTable _dataTable ;
13
- private readonly ColumnOptions _columnOptions ;
8
+ private readonly ISqlCreateTableWriter _sqlCreateTableWriter ;
14
9
15
- public SqlTableCreator ( string connectionString , string schemaName , string tableName , DataTable dataTable , ColumnOptions columnOptions )
10
+ public SqlTableCreator ( ISqlCreateTableWriter sqlCreateTableWriter )
16
11
{
17
- _connectionString = connectionString ;
18
- _schemaName = schemaName ;
19
- _tableName = tableName ;
20
- _dataTable = dataTable ;
21
- _columnOptions = columnOptions ;
12
+ _sqlCreateTableWriter = sqlCreateTableWriter ;
22
13
}
23
14
24
- public int CreateTable ( )
15
+ public int CreateTable ( string connectionString , string schemaName , string tableName , DataTable dataTable , ColumnOptions columnOptions )
25
16
{
26
- using ( var conn = new SqlConnection ( _connectionString ) )
17
+ using ( var conn = new SqlConnection ( connectionString ) )
27
18
{
28
- string sql = GetSqlFromDataTable ( ) ;
19
+ string sql = _sqlCreateTableWriter . GetSqlFromDataTable ( schemaName , tableName , dataTable , columnOptions ) ;
29
20
using ( SqlCommand cmd = new SqlCommand ( sql , conn ) )
30
21
{
31
22
conn . Open ( ) ;
@@ -34,84 +25,5 @@ public int CreateTable()
34
25
35
26
}
36
27
}
37
-
38
- private string GetSqlFromDataTable ( )
39
- {
40
- var sql = new StringBuilder ( ) ;
41
- var ix = new StringBuilder ( ) ;
42
- int indexCount = 1 ;
43
-
44
- // start schema check and DDL (wrap in EXEC to make a separate batch)
45
- sql . AppendLine ( $ "IF(NOT EXISTS(SELECT * FROM sys.schemas WHERE name = '{ _schemaName } '))") ;
46
- sql . AppendLine ( "BEGIN" ) ;
47
- sql . AppendLine ( $ "EXEC('CREATE SCHEMA [{ _schemaName } ] AUTHORIZATION [dbo]')") ;
48
- sql . AppendLine ( "END" ) ;
49
-
50
- // start table-creatin batch and DDL
51
- sql . AppendLine ( $ "IF NOT EXISTS (SELECT s.name, t.name FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE s.name = '{ _schemaName } ' AND t.name = '{ _tableName } ')") ;
52
- sql . AppendLine ( "BEGIN" ) ;
53
- sql . AppendLine ( $ "CREATE TABLE [{ _schemaName } ].[{ _tableName } ] ( ") ;
54
-
55
- // build column list
56
- int i = 1 ;
57
- foreach ( DataColumn column in _dataTable . Columns )
58
- {
59
- var common = ( SqlColumn ) column . ExtendedProperties [ "SqlColumn" ] ;
60
-
61
- sql . Append ( GetColumnDDL ( common ) ) ;
62
- if ( _dataTable . Columns . Count > i ++ ) sql . Append ( "," ) ;
63
- sql . AppendLine ( ) ;
64
-
65
- // collect non-PK indexes for separate output after the table DDL
66
- if ( common != null && common . NonClusteredIndex && common != _columnOptions . PrimaryKey )
67
- ix . AppendLine ( $ "CREATE NONCLUSTERED INDEX [IX{ indexCount ++ } _{ _tableName } ] ON [{ _schemaName } ].[{ _tableName } ] ([{ common . ColumnName } ]);") ;
68
- }
69
-
70
- // primary key constraint at the end of the table DDL
71
- if ( _columnOptions . PrimaryKey != null )
72
- {
73
- var clustering = ( _columnOptions . PrimaryKey . NonClusteredIndex ? "NON" : string . Empty ) ;
74
- sql . AppendLine ( $ " CONSTRAINT [PK_{ _tableName } ] PRIMARY KEY { clustering } CLUSTERED ([{ _columnOptions . PrimaryKey . ColumnName } ])") ;
75
- }
76
-
77
- // end of CREATE TABLE
78
- sql . AppendLine ( ");" ) ;
79
-
80
- // CCI is output separately after table DDL
81
- if ( _columnOptions . ClusteredColumnstoreIndex )
82
- sql . AppendLine ( $ "CREATE CLUSTERED COLUMNSTORE INDEX [CCI_{ _tableName } ] ON [{ _schemaName } ].[{ _tableName } ]") ;
83
-
84
- // output any extra non-clustered indexes
85
- sql . Append ( ix ) ;
86
-
87
- // end of batch
88
- sql . AppendLine ( "END" ) ;
89
-
90
- return sql . ToString ( ) ;
91
- }
92
-
93
- // Examples of possible output:
94
- // [Id] BIGINT IDENTITY(1,1) NOT NULL
95
- // [Message] VARCHAR(1024) NULL
96
- private string GetColumnDDL ( SqlColumn column )
97
- {
98
- var sb = new StringBuilder ( ) ;
99
-
100
- sb . Append ( $ "[{ column . ColumnName } ] ") ;
101
-
102
- sb . Append ( column . DataType . ToString ( ) . ToUpperInvariant ( ) ) ;
103
-
104
- if ( SqlDataTypes . DataLengthRequired . Contains ( column . DataType ) )
105
- sb . Append ( "(" ) . Append ( column . DataLength == - 1 ? "MAX" : column . DataLength . ToString ( ) ) . Append ( ")" ) ;
106
-
107
- if ( column . StandardColumnIdentifier == StandardColumn . Id )
108
- sb . Append ( " IDENTITY(1,1)" ) ;
109
-
110
- sb . Append ( column . AllowNull ? " NULL" : " NOT NULL" ) ;
111
-
112
- return sb . ToString ( ) ;
113
- }
114
-
115
28
}
116
-
117
29
}
0 commit comments