Skip to content

Commit d4ae6cb

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
Updated TableExists and ViewExists
1 parent 259ef1d commit d4ae6cb

File tree

2 files changed

+31
-64
lines changed

2 files changed

+31
-64
lines changed

src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Data;
19-
using System.Data.Common;
2019
using System.Globalization;
2120
using Index = Migrator.Framework.Index;
2221

@@ -64,7 +63,35 @@ protected virtual void CreateConnection(string providerName)
6463
collationString = "Latin1_General_CI_AS";
6564
}
6665

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;
6895
}
6996

7097
public override bool ConstraintExists(string table, string name)
@@ -183,50 +210,6 @@ public override void RemoveColumnDefaultValue(string table, string column)
183210
}
184211
}
185212

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-
230213
public override Index[] GetIndexes(string table)
231214
{
232215
var retVal = new List<Index>();

src/Migrator/Providers/TransformationProvider.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -558,28 +558,12 @@ public virtual void RemoveColumnDefaultValue(string table, string column)
558558

559559
public virtual bool TableExists(string table)
560560
{
561-
try
562-
{
563-
ExecuteNonQuery("SELECT COUNT(*) FROM " + table);
564-
return true;
565-
}
566-
catch (Exception)
567-
{
568-
return false;
569-
}
561+
throw new NotImplementedException();
570562
}
571563

572564
public virtual bool ViewExists(string view)
573565
{
574-
try
575-
{
576-
ExecuteNonQuery("SELECT COUNT(*) FROM " + view);
577-
return true;
578-
}
579-
catch (Exception)
580-
{
581-
return false;
582-
}
566+
throw new NotImplementedException();
583567
}
584568

585569
public virtual void SwitchDatabase(string databaseName)

0 commit comments

Comments
 (0)