Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 103c041

Browse files
committed
Add optional schema to DoesTableExist and use in Create and Drop Table API's
1 parent 2670b3c commit 103c041

File tree

14 files changed

+98
-38
lines changed

14 files changed

+98
-38
lines changed

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public override string ToInsertRowStatement(IDbCommand dbCommand, object objWith
233233
}
234234

235235
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
236-
GetQuotedTableName(modelDef), sbColumnNames, sbColumnValues);
236+
GetQuotedTableName(modelDef), sbColumnNames, sbColumnValues);
237237

238238
return sql;
239239
}
@@ -699,18 +699,26 @@ public override string GetQuotedName(string fieldName)
699699
return Quote(fieldName);
700700
}
701701

702-
public override string GetQuotedTableName(ModelDefinition modelDef)
702+
public virtual string GetTableName(string table, string schema = null)
703703
{
704-
if (!modelDef.IsInSchema)
705-
return Quote(NamingStrategy.GetTableName(modelDef.ModelName));
704+
return schema != null
705+
? string.Format("{0}_{1}",
706+
NamingStrategy.GetSchemaName(schema),
707+
NamingStrategy.GetTableName(table))
708+
: NamingStrategy.GetTableName(table);
709+
}
706710

707-
return Quote(string.Format("{0}_{1}", modelDef.Schema,
708-
NamingStrategy.GetTableName(modelDef.ModelName)));
711+
public virtual string GetTableName(ModelDefinition modelDef)
712+
{
713+
return GetTableName(modelDef.ModelName, modelDef.Schema);
709714
}
710715

711-
public override string GetQuotedTableName(string tableName)
716+
public override string GetQuotedTableName(ModelDefinition modelDef)
712717
{
713-
return Quote(NamingStrategy.GetTableName(tableName));
718+
if (!modelDef.IsInSchema)
719+
return Quote(NamingStrategy.GetTableName(modelDef.ModelName));
720+
721+
return Quote(GetTableName(modelDef.ModelName, modelDef.Schema));
714722
}
715723

716724
public override string GetQuotedColumnName(string fieldName)
@@ -730,7 +738,7 @@ public override SqlExpression<T> SqlExpression<T>()
730738
return new FirebirdSqlExpression<T>(this);
731739
}
732740

733-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
741+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
734742
{
735743
if (!QuoteNames & !RESERVED.Contains(tableName.ToUpper()))
736744
{
@@ -773,7 +781,7 @@ public override string ToAddColumnStatement(Type modelType, FieldDefinition fiel
773781
fieldDef.DefaultValue,
774782
fieldDef.CustomFieldDefinition);
775783
return string.Format("ALTER TABLE {0} ADD {1} ;",
776-
GetQuotedTableName(GetModel(modelType).ModelName),
784+
GetQuotedTableName(GetModel(modelType)),
777785
column);
778786
}
779787

@@ -791,7 +799,7 @@ public override string ToAlterColumnStatement(Type modelType, FieldDefinition fi
791799
fieldDef.DefaultValue,
792800
fieldDef.CustomFieldDefinition);
793801
return string.Format("ALTER TABLE {0} ALTER {1} ;",
794-
GetQuotedTableName(GetModel(modelType).ModelName),
802+
GetQuotedTableName(GetModel(modelType)),
795803
column);
796804
}
797805

@@ -800,7 +808,7 @@ public override string ToChangeColumnNameStatement(Type modelType,
800808
string oldColumnName)
801809
{
802810
return string.Format("ALTER TABLE {0} ALTER {1} TO {2} ;",
803-
GetQuotedTableName(GetModel(modelType).ModelName),
811+
GetQuotedTableName(GetModel(modelType)),
804812
GetQuotedColumnName(oldColumnName),
805813
GetQuotedColumnName(fieldDef.FieldName));
806814
}

src/ServiceStack.OrmLite.MySql/MySqlDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public override SqlExpression<T> SqlExpression<T>()
147147
return new MySqlExpression<T>(this);
148148
}
149149

150-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
150+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
151151
{
152152
//Same as SQL Server apparently?
153153
var sql = ("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES " +

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ public override SqlExpression<T> SqlExpression<T>()
982982
return new OracleSqlExpression<T>(this);
983983
}
984984

985-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
985+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
986986
{
987987
if (!WillQuote(tableName)) tableName = tableName.ToUpper();
988988

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,24 @@ public override SqlExpression<T> SqlExpression<T>()
216216
return new PostgreSqlExpression<T>(this);
217217
}
218218

219-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
219+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
220220
{
221221
var sql = "SELECT COUNT(*) FROM pg_class WHERE relname = {0}"
222222
.SqlFmt(tableName);
223+
223224
var conn = dbCmd.Connection;
224225
if (conn != null)
225226
{
226227
var builder = new NpgsqlConnectionStringBuilder(conn.ConnectionString);
228+
if (schema == null)
229+
schema = builder.SearchPath;
230+
227231
// If a search path (schema) is specified, and there is only one, then assume the CREATE TABLE directive should apply to that schema.
228-
if (!String.IsNullOrEmpty(builder.SearchPath) && !builder.SearchPath.Contains(","))
232+
if (!string.IsNullOrEmpty(schema) && !schema.Contains(","))
229233
sql = "SELECT COUNT(*) FROM pg_class JOIN pg_catalog.pg_namespace n ON n.oid = pg_class.relnamespace WHERE relname = {0} AND nspname = {1}"
230-
.SqlFmt(tableName, builder.SearchPath);
234+
.SqlFmt(tableName, schema);
231235
}
236+
232237
dbCmd.CommandText = sql;
233238
var result = dbCmd.LongScalar();
234239

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ public override SqlExpression<T> SqlExpression<T>()
226226
return new SqlServerExpression<T>(this);
227227
}
228228

229-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
229+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
230230
{
231231
var sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {0}"
232232
.SqlFmt(tableName);
233233

234-
//if (!string.IsNullOrEmpty(schemaName))
235-
// sql += " AND TABLE_SCHEMA = {0}".SqlFmt(schemaName);
234+
if (schema != null)
235+
sql += " AND TABLE_SCHEMA = {0}".SqlFmt(schema);
236236

237237
dbCmd.CommandText = sql;
238238
var result = dbCmd.LongScalar();

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,27 @@ public override IDbConnection CreateConnection(string connectionString, Dictiona
134134

135135
protected abstract IDbConnection CreateConnection(string connectionString);
136136

137+
public virtual string GetTableName(string table, string schema=null)
138+
{
139+
return schema != null
140+
? string.Format("{0}_{1}",
141+
NamingStrategy.GetSchemaName(schema),
142+
NamingStrategy.GetTableName(table))
143+
: NamingStrategy.GetTableName(table);
144+
}
145+
137146
public virtual string GetTableName(ModelDefinition modelDef)
138147
{
139-
var tableName = NamingStrategy.GetTableName(modelDef.ModelName);
140-
return !modelDef.IsInSchema
141-
? tableName
142-
: string.Format("{0}_{1}", modelDef.Schema, tableName);
148+
return GetTableName(modelDef.ModelName, modelDef.Schema);
143149
}
144150

145151
public override string GetQuotedTableName(ModelDefinition modelDef)
146152
{
147153
if (!modelDef.IsInSchema)
148154
return base.GetQuotedTableName(modelDef);
149155

150-
return string.Format("\"{0}_{1}\"", modelDef.Schema, modelDef.ModelName);
156+
return string.Format("\"{0}\"",
157+
GetTableName(modelDef.ModelName, modelDef.Schema));
151158
}
152159

153160
public override object ConvertDbValue(object value, Type type)
@@ -245,10 +252,10 @@ public override SqlExpression<T> SqlExpression<T>()
245252
return new SqliteExpression<T>(this);
246253
}
247254

248-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
255+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
249256
{
250257
var sql = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = {0}"
251-
.SqlFmt(tableName);
258+
.SqlFmt(GetTableName(tableName, schema));
252259

253260
dbCmd.CommandText = sql;
254261
var result = dbCmd.LongScalar();

src/ServiceStack.OrmLite.VistaDB/VistaDbDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public override SqlExpression<T> SqlExpression<T>()
320320
return new VistaDbExpression<T>(this);
321321
}
322322

323-
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
323+
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
324324
{
325325
dbCmd.CommandText = "SELECT COUNT(*) FROM [database schema] WHERE typeid = 1 AND name = {0}"
326326
.SqlFmt(tableName);

src/ServiceStack.OrmLite/INamingStrategy.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ namespace ServiceStack.OrmLite
33
{
44
public interface INamingStrategy
55
{
6+
string GetSchemaName(string name);
7+
string GetSchemaName(ModelDefinition modelDef);
68
string GetTableName(string name);
79
string GetTableName(ModelDefinition modelDef);
810
string GetColumnName(string name);

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ string ToSelectFromProcedureStatement(object fromObjWithProperties,
123123

124124
List<string> SequenceList(Type tableType);
125125

126-
bool DoesTableExist(IDbConnection db, string tableName);
127-
bool DoesTableExist(IDbCommand dbCmd, string tableName);
126+
bool DoesTableExist(IDbConnection db, string tableName, string schema = null);
127+
bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null);
128128

129129
bool DoesSequenceExist(IDbCommand dbCmd, string sequencName);
130130

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,12 +1032,12 @@ public virtual string GetColumnTypeDefinition(Type fieldType)
10321032
return fieldDefinition ?? GetUndefinedColumnDefinition(fieldType, null);
10331033
}
10341034

1035-
public virtual bool DoesTableExist(IDbConnection db, string tableName)
1035+
public virtual bool DoesTableExist(IDbConnection db, string tableName, string schema = null)
10361036
{
1037-
return db.Exec(dbCmd => DoesTableExist(dbCmd, tableName));
1037+
return db.Exec(dbCmd => DoesTableExist(dbCmd, tableName, schema));
10381038
}
10391039

1040-
public virtual bool DoesTableExist(IDbCommand dbCmd, string tableName)
1040+
public virtual bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
10411041
{
10421042
return false;
10431043
}

0 commit comments

Comments
 (0)