|
5 | 5 |
|
6 | 6 | namespace Serilog.Sinks.MSSqlServer
|
7 | 7 | {
|
8 |
| - internal class SqlTableCreator |
9 |
| - { |
10 |
| - private readonly string _connectionString; |
11 |
| - private string _tableName; |
12 |
| - private string _schemaName; |
13 |
| - |
14 |
| - #region Constructor |
15 |
| - |
16 |
| - public SqlTableCreator(string connectionString, string schemaName) |
17 |
| - { |
18 |
| - _schemaName = schemaName; |
19 |
| - _connectionString = connectionString; |
20 |
| - } |
21 |
| - |
22 |
| - #endregion |
23 |
| - |
24 |
| - #region Instance Methods |
25 |
| - public int CreateTable(DataTable table) |
26 |
| - { |
27 |
| - if (table == null) return 0; |
28 |
| - |
29 |
| - if (string.IsNullOrWhiteSpace(table.TableName) || string.IsNullOrWhiteSpace(_connectionString)) return 0; |
30 |
| - |
31 |
| - _tableName = table.TableName; |
32 |
| - using (var conn = new SqlConnection(_connectionString)) |
33 |
| - { |
34 |
| - string sql = GetSqlFromDataTable(_tableName, table, _schemaName); |
35 |
| - using (SqlCommand cmd = new SqlCommand(sql, conn)) |
36 |
| - { |
37 |
| - conn.Open(); |
38 |
| - return cmd.ExecuteNonQuery(); |
39 |
| - } |
40 |
| - |
41 |
| - } |
42 |
| - } |
43 |
| - #endregion |
44 |
| - |
45 |
| - #region Static Methods |
46 |
| - |
47 |
| - private static string GetSqlFromDataTable(string tableName, DataTable table, string schema) |
48 |
| - { |
49 |
| - StringBuilder sql = new StringBuilder(); |
50 |
| - sql.AppendFormat("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 = '{0}' AND t.name = '{1}')", schema, tableName); |
51 |
| - sql.AppendLine(" BEGIN"); |
52 |
| - sql.AppendFormat(" CREATE TABLE [{0}].[{1}] ( ", schema, tableName); |
53 |
| - |
54 |
| - // columns |
55 |
| - int numOfColumns = table.Columns.Count; |
56 |
| - int i = 1; |
57 |
| - foreach (DataColumn column in table.Columns) |
58 |
| - { |
59 |
| - sql.AppendFormat("[{0}] {1}", column.ColumnName, SqlGetType(column)); |
60 |
| - if (column.ColumnName.ToUpper().Equals("ID") || column.AutoIncrement) |
61 |
| - sql.Append(" IDENTITY(1,1) "); |
62 |
| - if (numOfColumns > i) |
63 |
| - sql.AppendFormat(", "); |
64 |
| - i++; |
65 |
| - } |
66 |
| - |
67 |
| - // primary keys |
68 |
| - if (table.PrimaryKey.Length > 0) |
69 |
| - { |
70 |
| - sql.AppendFormat(" CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED (", tableName); |
71 |
| - |
72 |
| - int numOfKeys = table.PrimaryKey.Length; |
73 |
| - i = 1; |
74 |
| - foreach (DataColumn column in table.PrimaryKey) |
75 |
| - { |
76 |
| - sql.AppendFormat("[{0}]", column.ColumnName); |
77 |
| - if (numOfKeys > i) |
78 |
| - sql.AppendFormat(", "); |
79 |
| - |
80 |
| - i++; |
81 |
| - } |
82 |
| - sql.Append("))"); |
83 |
| - } |
84 |
| - sql.AppendLine(" END"); |
85 |
| - return sql.ToString(); |
86 |
| - } |
87 |
| - |
88 |
| - // Return T-SQL data type definition, based on schema definition for a column |
89 |
| - private static string SqlGetType(object type, int columnSize, int numericPrecision, int numericScale, |
90 |
| - bool allowDbNull) |
91 |
| - { |
92 |
| - string sqlType; |
93 |
| - |
94 |
| - switch (type.ToString()) |
95 |
| - { |
96 |
| - case "System.Boolean": |
97 |
| - sqlType = "BIT"; |
98 |
| - break; |
| 8 | + internal class SqlTableCreator |
| 9 | + { |
| 10 | + private readonly string _connectionString; |
| 11 | + private string _tableName; |
| 12 | + private string _schemaName; |
| 13 | + |
| 14 | + #region Constructor |
| 15 | + |
| 16 | + public SqlTableCreator(string connectionString, string schemaName) |
| 17 | + { |
| 18 | + _schemaName = schemaName; |
| 19 | + _connectionString = connectionString; |
| 20 | + } |
| 21 | + |
| 22 | + #endregion |
| 23 | + |
| 24 | + #region Instance Methods |
| 25 | + public int CreateTable(DataTable table) |
| 26 | + { |
| 27 | + if (table == null) return 0; |
| 28 | + |
| 29 | + if (string.IsNullOrWhiteSpace(table.TableName) || string.IsNullOrWhiteSpace(_connectionString)) return 0; |
| 30 | + |
| 31 | + _tableName = table.TableName; |
| 32 | + using (var conn = new SqlConnection(_connectionString)) |
| 33 | + { |
| 34 | + string sql = GetSqlFromDataTable(_tableName, table, _schemaName); |
| 35 | + using (SqlCommand cmd = new SqlCommand(sql, conn)) |
| 36 | + { |
| 37 | + conn.Open(); |
| 38 | + return cmd.ExecuteNonQuery(); |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + } |
| 43 | + #endregion |
| 44 | + |
| 45 | + #region Static Methods |
| 46 | + |
| 47 | + private static string GetSqlFromDataTable(string tableName, DataTable table, string schema) |
| 48 | + { |
| 49 | + StringBuilder sql = new StringBuilder(); |
| 50 | + sql.AppendFormat("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 = '{0}' AND t.name = '{1}')", schema, tableName); |
| 51 | + sql.AppendLine(" BEGIN"); |
| 52 | + sql.AppendFormat(" CREATE TABLE [{0}].[{1}] ( ", schema, tableName); |
| 53 | + |
| 54 | + // columns |
| 55 | + int numOfColumns = table.Columns.Count; |
| 56 | + int i = 1; |
| 57 | + foreach (DataColumn column in table.Columns) |
| 58 | + { |
| 59 | + sql.AppendFormat("[{0}] {1}", column.ColumnName, SqlGetType(column)); |
| 60 | + if (column.ColumnName.ToUpper().Equals("ID") || column.AutoIncrement) |
| 61 | + sql.Append(" IDENTITY(1,1) "); |
| 62 | + if (numOfColumns > i) |
| 63 | + sql.AppendFormat(", "); |
| 64 | + i++; |
| 65 | + } |
| 66 | + |
| 67 | + // primary keys |
| 68 | + if (table.PrimaryKey.Length > 0) |
| 69 | + { |
| 70 | + sql.AppendFormat(" CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED (", tableName); |
| 71 | + |
| 72 | + int numOfKeys = table.PrimaryKey.Length; |
| 73 | + i = 1; |
| 74 | + foreach (DataColumn column in table.PrimaryKey) |
| 75 | + { |
| 76 | + sql.AppendFormat("[{0}]", column.ColumnName); |
| 77 | + if (numOfKeys > i) |
| 78 | + sql.AppendFormat(", "); |
| 79 | + |
| 80 | + i++; |
| 81 | + } |
| 82 | + sql.Append("))"); |
| 83 | + } |
| 84 | + sql.AppendLine(" END"); |
| 85 | + return sql.ToString(); |
| 86 | + } |
| 87 | + |
| 88 | + // Return T-SQL data type definition, based on schema definition for a column |
| 89 | + private static string SqlGetType(object type, int columnSize, int numericPrecision, int numericScale, |
| 90 | + bool allowDbNull) |
| 91 | + { |
| 92 | + string sqlType; |
| 93 | + |
| 94 | + switch (type.ToString()) |
| 95 | + { |
| 96 | + case "System.Boolean": |
| 97 | + sqlType = "BIT"; |
| 98 | + break; |
99 | 99 |
|
100 | 100 | case "System.Byte":
|
101 | 101 | sqlType = "TINYINT";
|
102 | 102 | break;
|
103 | 103 |
|
104 | 104 | case "System.String":
|
105 |
| - sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")"; |
106 |
| - break; |
107 |
| - |
108 |
| - case "System.Decimal": |
109 |
| - if (numericScale > 0) |
110 |
| - sqlType = "REAL"; |
111 |
| - else if (numericPrecision > 10) |
112 |
| - sqlType = "BIGINT"; |
113 |
| - else |
114 |
| - sqlType = "INT"; |
115 |
| - break; |
116 |
| - |
117 |
| - case "System.Double": |
118 |
| - case "System.Single": |
119 |
| - sqlType = "REAL"; |
120 |
| - break; |
121 |
| - |
122 |
| - case "System.Int64": |
123 |
| - sqlType = "BIGINT"; |
124 |
| - break; |
125 |
| - |
126 |
| - case "System.Int16": |
127 |
| - case "System.Int32": |
128 |
| - sqlType = "INT"; |
129 |
| - break; |
130 |
| - |
131 |
| - case "System.DateTime": |
132 |
| - sqlType = "DATETIME"; |
133 |
| - break; |
134 |
| - |
135 |
| - case "System.Guid": |
136 |
| - sqlType = "UNIQUEIDENTIFIER"; |
137 |
| - break; |
138 |
| - |
139 |
| - default: |
140 |
| - throw new Exception(string.Format("{0} not implemented.", type)); |
141 |
| - } |
142 |
| - |
143 |
| - sqlType += " " + (allowDbNull ? "NULL" : "NOT NULL"); |
144 |
| - |
145 |
| - return sqlType; |
146 |
| - } |
147 |
| - |
148 |
| - // Overload based on DataColumn from DataTable type |
149 |
| - private static string SqlGetType(DataColumn column) |
150 |
| - { |
151 |
| - return SqlGetType(column.DataType, column.MaxLength, 10, 2, column.AllowDBNull); |
152 |
| - } |
153 |
| - |
154 |
| - #endregion |
155 |
| - } |
| 105 | + sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")"; |
| 106 | + break; |
| 107 | + |
| 108 | + case "System.Decimal": |
| 109 | + if (numericScale > 0) |
| 110 | + sqlType = "REAL"; |
| 111 | + else if (numericPrecision > 10) |
| 112 | + sqlType = "BIGINT"; |
| 113 | + else |
| 114 | + sqlType = "INT"; |
| 115 | + break; |
| 116 | + |
| 117 | + case "System.Double": |
| 118 | + case "System.Single": |
| 119 | + sqlType = "REAL"; |
| 120 | + break; |
| 121 | + |
| 122 | + case "System.Int64": |
| 123 | + sqlType = "BIGINT"; |
| 124 | + break; |
| 125 | + |
| 126 | + case "System.Int16": |
| 127 | + case "System.Int32": |
| 128 | + sqlType = "INT"; |
| 129 | + break; |
| 130 | + |
| 131 | + case "System.DateTime": |
| 132 | + sqlType = "DATETIME"; |
| 133 | + break; |
| 134 | + |
| 135 | + case "System.Guid": |
| 136 | + sqlType = "UNIQUEIDENTIFIER"; |
| 137 | + break; |
| 138 | + |
| 139 | + default: |
| 140 | + throw new Exception(string.Format("{0} not implemented.", type)); |
| 141 | + } |
| 142 | + |
| 143 | + sqlType += " " + (allowDbNull ? "NULL" : "NOT NULL"); |
| 144 | + |
| 145 | + return sqlType; |
| 146 | + } |
| 147 | + |
| 148 | + // Overload based on DataColumn from DataTable type |
| 149 | + private static string SqlGetType(DataColumn column) |
| 150 | + { |
| 151 | + return SqlGetType(column.DataType, column.MaxLength, 10, 2, column.AllowDBNull); |
| 152 | + } |
| 153 | + |
| 154 | + #endregion |
| 155 | + } |
156 | 156 | }
|
0 commit comments