|
16 | 16 | using System; |
17 | 17 | using System.Collections.Generic; |
18 | 18 | using System.Data; |
19 | | -using System.Data.Common; |
20 | 19 | using System.Globalization; |
21 | 20 | using Index = Migrator.Framework.Index; |
22 | 21 |
|
@@ -64,7 +63,35 @@ protected virtual void CreateConnection(string providerName) |
64 | 63 | collationString = "Latin1_General_CI_AS"; |
65 | 64 | } |
66 | 65 |
|
67 | | - this.Dialect.RegisterProperty(ColumnProperty.CaseSensitive, "COLLATE " + collationString.Replace("_CI_", "_CS_")); |
| 66 | + Dialect.RegisterProperty(ColumnProperty.CaseSensitive, "COLLATE " + collationString.Replace("_CI_", "_CS_")); |
| 67 | + } |
| 68 | + |
| 69 | + public override bool TableExists(string tableName) |
| 70 | + { |
| 71 | + // This is not clean! Usually you should use schema as well as this query will find tables in other tables as well! |
| 72 | + |
| 73 | + using var cmd = CreateCommand(); |
| 74 | + using var reader = ExecuteQuery(cmd, $"SELECT OBJECT_ID('{tableName}', 'U')"); |
| 75 | + |
| 76 | + var result = cmd.ExecuteScalar(); |
| 77 | + |
| 78 | + var tableExists = result != DBNull.Value && result != null; |
| 79 | + |
| 80 | + return tableExists; |
| 81 | + } |
| 82 | + |
| 83 | + public override bool ViewExists(string viewName) |
| 84 | + { |
| 85 | + // This is not clean! Usually you should use schema as well as this query will find views in other tables as well! |
| 86 | + |
| 87 | + using var cmd = CreateCommand(); |
| 88 | + using var reader = ExecuteQuery(cmd, $"SELECT OBJECT_ID('{viewName}', 'V')"); |
| 89 | + |
| 90 | + var result = cmd.ExecuteScalar(); |
| 91 | + |
| 92 | + var viewExists = result != DBNull.Value && result != null; |
| 93 | + |
| 94 | + return viewExists; |
68 | 95 | } |
69 | 96 |
|
70 | 97 | public override bool ConstraintExists(string table, string name) |
@@ -183,50 +210,6 @@ public override void RemoveColumnDefaultValue(string table, string column) |
183 | 210 | } |
184 | 211 | } |
185 | 212 |
|
186 | | - |
187 | | - public override bool TableExists(string table) |
188 | | - { |
189 | | - string schema; |
190 | | - |
191 | | - var firstIndex = table.IndexOf("."); |
192 | | - if (firstIndex >= 0) |
193 | | - { |
194 | | - schema = table.Substring(0, firstIndex).Trim(); |
195 | | - table = table.Substring(firstIndex + 1).Trim(); |
196 | | - } |
197 | | - else |
198 | | - { |
199 | | - schema = _defaultSchema; |
200 | | - } |
201 | | - |
202 | | - schema = schema.StartsWith("[") && schema.EndsWith("]") ? schema.Substring(1, schema.Length - 2) : schema; |
203 | | - table = table.StartsWith("[") && table.EndsWith("]") ? table.Substring(1, table.Length - 2) : table; |
204 | | - |
205 | | - using var cmd = CreateCommand(); |
206 | | - using var reader = base.ExecuteQuery(cmd, string.Format("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{0}' AND TABLE_SCHEMA='{1}'", table, schema)); |
207 | | - return reader.Read(); |
208 | | - } |
209 | | - |
210 | | - public override bool ViewExists(string view) |
211 | | - { |
212 | | - string schema; |
213 | | - |
214 | | - var firstIndex = view.IndexOf("."); |
215 | | - if (firstIndex >= 0) |
216 | | - { |
217 | | - schema = view.Substring(0, firstIndex); |
218 | | - view = view.Substring(firstIndex + 1); |
219 | | - } |
220 | | - else |
221 | | - { |
222 | | - schema = _defaultSchema; |
223 | | - } |
224 | | - |
225 | | - using var cmd = CreateCommand(); |
226 | | - using var reader = base.ExecuteQuery(cmd, string.Format("SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME='{0}' AND TABLE_SCHEMA='{1}'", view, schema)); |
227 | | - return reader.Read(); |
228 | | - } |
229 | | - |
230 | 213 | public override Index[] GetIndexes(string table) |
231 | 214 | { |
232 | 215 | var retVal = new List<Index>(); |
|
0 commit comments