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

Commit d137ccd

Browse files
committed
Add new GetTableNames/WithRowCounts APIs for SQLite,MSSQL,MySql,PgSQL
1 parent b4e7bca commit d137ccd

File tree

15 files changed

+462
-75
lines changed

15 files changed

+462
-75
lines changed

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,21 +662,30 @@ public override string EscapeWildcards(string value)
662662
.Replace("%", @"^%");
663663
}
664664

665-
public override string GetQuotedName(string fieldName)
665+
public override string GetQuotedName(string name)
666666
{
667-
return Quote(fieldName);
667+
return Quote(name);
668668
}
669669

670670
public override string GetTableName(ModelDefinition modelDef)
671671
{
672672
return GetTableName(modelDef.ModelName, modelDef.Schema);
673673
}
674674

675-
public override string GetTableName(string table, string schema = null)
675+
public override string GetTableName(string table, string schema = null) => GetTableName(table, schema, useStrategy: true);
676+
677+
public override string GetTableName(string table, string schema, bool useStrategy)
676678
{
679+
if (useStrategy)
680+
{
681+
return schema != null
682+
? $"{NamingStrategy.GetSchemaName(schema)}_{NamingStrategy.GetTableName(table)}"
683+
: NamingStrategy.GetTableName(table);
684+
}
685+
677686
return schema != null
678-
? $"{NamingStrategy.GetSchemaName(schema)}_{NamingStrategy.GetTableName(table)}"
679-
: NamingStrategy.GetTableName(table);
687+
? $"{schema}_{table}"
688+
: table;
680689
}
681690

682691
public override string GetQuotedTableName(ModelDefinition modelDef)

src/ServiceStack.OrmLite.MySql/MySqlDialectProviderBase.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,58 @@ public override string GetQuotedValue(object value, Type fieldType)
101101
return base.GetQuotedValue(value, fieldType);
102102
}
103103

104-
public override string GetTableName(string table, string schema = null)
104+
public override string GetTableName(string table, string schema = null) => GetTableName(table, schema, useStrategy: true);
105+
106+
public override string GetTableName(string table, string schema, bool useStrategy)
105107
{
108+
if (useStrategy)
109+
{
110+
return schema != null
111+
? $"{NamingStrategy.GetSchemaName(schema)}_{NamingStrategy.GetTableName(table)}"
112+
: NamingStrategy.GetTableName(table);
113+
}
114+
106115
return schema != null
107-
? string.Format("{0}_{1}",
108-
NamingStrategy.GetSchemaName(schema),
109-
NamingStrategy.GetTableName(table))
110-
: NamingStrategy.GetTableName(table);
116+
? $"{schema}_{table}"
117+
: table;
111118
}
112119

113120
public override string GetQuotedTableName(string tableName, string schema = null)
114121
{
115122
return GetQuotedName(GetTableName(tableName, schema));
116123
}
117124

118-
public override string GetQuotedName(string name)
119-
{
120-
return string.Format("`{0}`", name);
121-
}
125+
public override string GetQuotedName(string name) => $"`{name}`";
122126

123127
public override SqlExpression<T> SqlExpression<T>()
124128
{
125129
return new MySqlExpression<T>(this);
126130
}
127131

132+
public override string GetQuotedName(string name, string schema) => GetQuotedName(name); //schema name is embedded in table name in MySql
133+
134+
public override string ToTableNamesStatement(string schema)
135+
{
136+
return schema == null
137+
? "SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = DATABASE()"
138+
: "SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = DATABASE() AND table_name LIKE {0}".SqlFmt(this, GetTableName("",schema) + "%");
139+
}
140+
141+
/// <summary>
142+
/// Fetch table row counts from information_schema (results are not live)
143+
/// </summary>
144+
public bool UseInformationSchemaStats { get; set; }
145+
146+
public override string ToTableNamesWithRowCountsStatement(string schema)
147+
{
148+
if (!UseInformationSchemaStats)
149+
return null;
150+
151+
return schema == null
152+
? "SELECT table_name, table_rows FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = DATABASE()"
153+
: "SELECT table_name, table_rows FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = DATABASE() AND table_name LIKE {0}".SqlFmt(this, GetTableName("",schema) + "%");
154+
}
155+
128156
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
129157
{
130158
var sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {0} AND TABLE_SCHEMA = {1}"

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,9 @@ private string Quote(string name)
842842
return WillQuote(name) ? string.Format("\"{0}\"", name) : name;
843843
}
844844

845-
public override string GetQuotedName(string fieldName)
845+
public override string GetQuotedName(string name)
846846
{
847-
return Quote(fieldName);
847+
return Quote(name);
848848
}
849849

850850
public override string GetQuotedTableName(ModelDefinition modelDef)

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,27 @@ public override IDbDataParameter CreateParam()
373373
return new NpgsqlParameter();
374374
}
375375

376+
public override string ToTableNamesStatement(string schema)
377+
{
378+
var sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'";
379+
380+
return schema != null
381+
? sql + " AND table_schema = {0}".SqlFmt(this, schema)
382+
: sql + " AND table_schema = 'public'";
383+
}
384+
385+
/// <summary>
386+
/// Fetch table row counts from pg_stat_user_tables (results are not live)
387+
/// </summary>
388+
public bool UseStatUserTables { get; set; }
389+
390+
public override string ToTableNamesWithRowCountsStatement(string schema)
391+
{
392+
return !UseStatUserTables
393+
? null
394+
: "SELECT relname, n_live_tup FROM pg_stat_user_tables WHERE schemaname = {0}".SqlFmt(this, schema ?? "public");
395+
}
396+
376397
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
377398
{
378399
var sql = !Normalize || ReservedWords.Contains(tableName)
@@ -475,7 +496,7 @@ public override string ToAlterColumnStatement(Type modelType, FieldDefinition fi
475496

476497
public override string GetQuotedTableName(string tableName, string schema = null)
477498
{
478-
return !Normalize || ReservedWords.Contains(tableName) || (schema != null && ReservedWords.Contains(schema))
499+
return !Normalize || ReservedWords.Contains(tableName) || (schema != null && ReservedWords.Contains(schema)) || tableName.Contains(' ')
479500
? base.GetQuotedTableName(tableName, schema)
480501
: schema != null
481502
? schema + "." + tableName
@@ -484,7 +505,7 @@ public override string GetQuotedTableName(string tableName, string schema = null
484505

485506
public override string GetQuotedName(string name)
486507
{
487-
return !Normalize || ReservedWords.Contains(name)
508+
return !Normalize || ReservedWords.Contains(name) || name.Contains(' ')
488509
? base.GetQuotedName(name)
489510
: name;
490511
}

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ public override IDbConnection CreateConnection(string connectionString, Dictiona
102102

103103
public override IDbDataParameter CreateParam() => new SqlParameter();
104104

105+
public override string ToTableNamesStatement(string schema)
106+
{
107+
var sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'";
108+
109+
return schema != null
110+
? sql + " AND TABLE_SCHEMA = {0}".SqlFmt(this, schema)
111+
: sql;
112+
}
113+
114+
public override string ToTableNamesWithRowCountsStatement(string schema)
115+
{
116+
var schemaSql = schema != null ? " AND s.Name = {0}".SqlFmt(this, schema) : "";
117+
118+
var sql = @"SELECT t.NAME, p.rows FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
119+
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
120+
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
121+
WHERE t.is_ms_shipped = 0 " + schemaSql + " GROUP BY t.NAME, p.Rows";
122+
return sql;
123+
}
124+
105125
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
106126
{
107127
var sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {0}"

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,36 @@ public override IDbConnection CreateConnection(string connectionString, Dictiona
144144

145145
protected abstract IDbConnection CreateConnection(string connectionString);
146146

147+
public override string GetQuotedName(string name, string schema) => GetQuotedName(name); //schema name is embedded in table name in MySql
148+
149+
public override string ToTableNamesStatement(string schema)
150+
{
151+
return schema == null
152+
? "SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%'"
153+
: "SELECT name FROM sqlite_master WHERE type ='table' AND name LIKE {0}".SqlFmt(this, GetTableName("",schema) + "%");
154+
}
155+
147156
public override string GetSchemaName(string schema)
148157
{
149158
return schema != null
150159
? NamingStrategy.GetSchemaName(schema).Replace(".", "_")
151160
: NamingStrategy.GetSchemaName(schema);
152161
}
153162

154-
public override string GetTableName(string table, string schema=null)
163+
public override string GetTableName(string table, string schema = null) => GetTableName(table, schema, useStrategy: true);
164+
165+
public override string GetTableName(string table, string schema, bool useStrategy)
155166
{
167+
if (useStrategy)
168+
{
169+
return schema != null
170+
? $"{NamingStrategy.GetSchemaName(schema)}_{NamingStrategy.GetTableName(table)}"
171+
: NamingStrategy.GetTableName(table);
172+
}
173+
156174
return schema != null
157-
? string.Format("{0}_{1}",
158-
NamingStrategy.GetSchemaName(schema),
159-
NamingStrategy.GetTableName(table))
160-
: NamingStrategy.GetTableName(table);
175+
? $"{schema}_{table}"
176+
: table;
161177
}
162178

163179
public override string GetQuotedTableName(string tableName, string schema = null)
@@ -189,8 +205,7 @@ public override bool DoesColumnExist(IDbConnection db, string columnName, string
189205
var columns = db.SqlList<Dictionary<string, object>>(sql);
190206
foreach (var column in columns)
191207
{
192-
object name;
193-
if (column.TryGetValue("name", out name) && name.ToString().EqualsIgnoreCase(columnName))
208+
if (column.TryGetValue("name", out var name) && name.ToString().EqualsIgnoreCase(columnName))
194209
return true;
195210
}
196211
return false;

src/ServiceStack.OrmLite.VistaDB/VistaDbDialectProvider.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,20 @@ public override IDbDataParameter CreateParam()
287287
return new OrmLiteVistaDbParameter(new OrmLiteDataParameter());
288288
}
289289

290-
public override string GetTableName(string table, string schema = null)
290+
public override string GetTableName(string table, string schema = null) => GetTableName(table, schema, useStrategy: true);
291+
292+
public override string GetTableName(string table, string schema, bool useStrategy)
291293
{
294+
if (useStrategy)
295+
{
296+
return schema != null
297+
? $"{NamingStrategy.GetSchemaName(schema)}_{NamingStrategy.GetTableName(table)}"
298+
: NamingStrategy.GetTableName(table);
299+
}
300+
292301
return schema != null
293-
? string.Format("{0}_{1}",
294-
NamingStrategy.GetSchemaName(schema),
295-
NamingStrategy.GetTableName(table))
296-
: NamingStrategy.GetTableName(table);
302+
? $"{schema}_{table}"
303+
: table;
297304
}
298305

299306
public override string GetQuotedTableName(ModelDefinition modelDef)
@@ -308,7 +315,7 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string s
308315
{
309316
var schemaTableName = GetTableName(tableName, schema);
310317
dbCmd.CommandText = "SELECT COUNT(*) FROM [database schema] WHERE typeid = 1 AND name = {0}"
311-
.SqlFmt(schemaTableName);
318+
.SqlFmt(this, schemaTableName);
312319

313320
return dbCmd.LongScalar() > 0;
314321
}

src/ServiceStack.OrmLite/DbScripts.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ public int dbExec(ScriptScopeContext scope, string sql, Dictionary<string, objec
9191
public int dbExec(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
9292
exec(db => db.ExecuteSql(sql, args), scope, options);
9393

94+
public List<string> dbTableNames(ScriptScopeContext scope) => dbTableNames(scope, null, null);
95+
public List<string> dbTableNames(ScriptScopeContext scope, Dictionary<string, object> args) => dbTableNames(scope, args, null);
96+
public List<string> dbTableNames(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
97+
exec(db => db.GetTableNames(args != null && args.TryGetValue("schema", out var oSchema) ? oSchema as string : null), scope, options);
98+
99+
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope) =>
100+
dbTableNamesWithRowCounts(scope, null, null);
101+
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args) =>
102+
dbTableNamesWithRowCounts(scope, args, null);
103+
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
104+
exec(db => db.GetTableNamesWithRowCounts(args != null && args.TryGetValue("schema", out var oSchema) ? oSchema as string : null), scope, options);
105+
94106

95107
public string sqlQuote(string name) => OrmLiteConfig.DialectProvider.GetQuotedName(name);
96108
public string sqlConcat(IEnumerable<object> values) => OrmLiteConfig.DialectProvider.SqlConcat(values);

src/ServiceStack.OrmLite/DbScriptsAsync.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ public Task<object> dbExec(ScriptScopeContext scope, string sql, Dictionary<stri
9393
public Task<object> dbExec(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
9494
exec(db => db.ExecuteSqlAsync(sql, args), scope, options);
9595

96+
public Task<object> dbTableNames(ScriptScopeContext scope) => dbTableNames(scope, null, null);
97+
public Task<object> dbTableNames(ScriptScopeContext scope, Dictionary<string, object> args) => dbTableNames(scope, args, null);
98+
public Task<object> dbTableNames(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
99+
exec(db => db.GetTableNamesAsync(args != null && args.TryGetValue("schema", out var oSchema) ? oSchema as string : null), scope, options);
100+
101+
public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope) =>
102+
dbTableNamesWithRowCounts(scope, null, null);
103+
public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args) =>
104+
dbTableNamesWithRowCounts(scope, args, null);
105+
public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
106+
exec(db => db.GetTableNamesWithRowCountsAsync(args != null && args.TryGetValue("schema", out var oSchema) ? oSchema as string : null), scope, options);
96107

97108
public string sqlQuote(string name) => OrmLiteConfig.DialectProvider.GetQuotedName(name);
98109
public string sqlConcat(IEnumerable<object> values) => OrmLiteConfig.DialectProvider.SqlConcat(values);

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ public interface IOrmLiteDialectProvider
9292

9393
string GetTableName(ModelDefinition modelDef);
9494

95-
string GetTableName(string tableName, string schema = null);
95+
string GetTableName(string table, string schema = null);
96+
string GetTableName(string table, string schema, bool useStrategy);
9697

9798
string GetQuotedTableName(ModelDefinition modelDef);
9899

99100
string GetQuotedTableName(string tableName, string schema=null);
100101

101102
string GetQuotedColumnName(string columnName);
102103

103-
string GetQuotedName(string columnName);
104+
string GetQuotedName(string name);
105+
string GetQuotedName(string name, string schema);
104106

105107
string SanitizeFieldNameForParamName(string fieldName);
106108

@@ -221,6 +223,9 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
221223
string ToInsertStatement<T>(IDbCommand dbCmd, T item, ICollection<string> insertFields = null);
222224
string MergeParamsIntoSql(string sql, IEnumerable<IDbDataParameter> dbParams);
223225

226+
string ToTableNamesStatement(string schema);
227+
string ToTableNamesWithRowCountsStatement(string schema);
228+
224229
string SqlConflict(string sql, string conflictResolution);
225230

226231
string SqlConcat(IEnumerable<object> args);

0 commit comments

Comments
 (0)