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

Commit 7ebe1da

Browse files
committed
Add live flag to specify whether to get slow/realtime or fast/cached rowcounts
1 parent ad159b3 commit 7ebe1da

File tree

9 files changed

+45
-29
lines changed

9 files changed

+45
-29
lines changed

src/ServiceStack.OrmLite.MySql/MySqlDialectProviderBase.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,10 @@ public override string ToTableNamesStatement(string schema)
137137
? "SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = DATABASE()"
138138
: "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) + "%");
139139
}
140-
141-
/// <summary>
142-
/// Fetch table row counts from information_schema (results are not live)
143-
/// </summary>
144-
public bool UseInformationSchemaStats { get; set; }
145140

146-
public override string ToTableNamesWithRowCountsStatement(string schema)
141+
public override string ToTableNamesWithRowCountsStatement(bool live, string schema)
147142
{
148-
if (!UseInformationSchemaStats)
143+
if (live)
149144
return null;
150145

151146
return schema == null

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,10 @@ public override string ToTableNamesStatement(string schema)
381381
? sql + " AND table_schema = {0}".SqlFmt(this, schema)
382382
: sql + " AND table_schema = 'public'";
383383
}
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; }
389384

390-
public override string ToTableNamesWithRowCountsStatement(string schema)
385+
public override string ToTableNamesWithRowCountsStatement(bool live, string schema)
391386
{
392-
return !UseStatUserTables
387+
return live
393388
? null
394389
: "SELECT relname, n_live_tup FROM pg_stat_user_tables WHERE schemaname = {0}".SqlFmt(this, schema ?? "public");
395390
}

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public override string ToTableNamesStatement(string schema)
111111
: sql;
112112
}
113113

114-
public override string ToTableNamesWithRowCountsStatement(string schema)
114+
public override string ToTableNamesWithRowCountsStatement(bool live, string schema)
115115
{
116116
var schemaSql = schema != null ? " AND s.Name = {0}".SqlFmt(this, schema) : "";
117117

src/ServiceStack.OrmLite/DbScripts.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeCon
101101
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args) =>
102102
dbTableNamesWithRowCounts(scope, args, null);
103103
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-
104+
exec(db => args == null
105+
? db.GetTableNamesWithRowCounts()
106+
: db.GetTableNamesWithRowCounts(
107+
live: args.TryGetValue("live", out var oLive) && oLive is bool b && b,
108+
schema: args.TryGetValue("schema", out var oSchema) ? oSchema as string : null),
109+
scope, options);
106110

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

src/ServiceStack.OrmLite/DbScriptsAsync.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope) =>
103103
public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args) =>
104104
dbTableNamesWithRowCounts(scope, args, null);
105105
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);
106+
exec(db => args == null
107+
? db.GetTableNamesWithRowCountsAsync()
108+
: db.GetTableNamesWithRowCountsAsync(
109+
live: args.TryGetValue("live", out var oLive) && oLive is bool b && b,
110+
schema: args.TryGetValue("schema", out var oSchema) ? oSchema as string : null),
111+
scope, options);
107112

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

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,14 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
224224
string MergeParamsIntoSql(string sql, IEnumerable<IDbDataParameter> dbParams);
225225

226226
string ToTableNamesStatement(string schema);
227-
string ToTableNamesWithRowCountsStatement(string schema);
227+
228+
/// <summary>
229+
/// Return table, row count SQL for listing all tables with their row counts
230+
/// </summary>
231+
/// <param name="live">If true returns live current rowc ounts of each table (slower), otherwise returns cached row counts from RDBMS table stats</param>
232+
/// <param name="schema">The table schema if any</param>
233+
/// <returns></returns>
234+
string ToTableNamesWithRowCountsStatement(bool live, string schema);
228235

229236
string SqlConflict(string sql, string conflictResolution);
230237

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ protected virtual string ToDropColumnStatement(Type modelType, string columnName
16421642

16431643
public virtual string ToTableNamesStatement(string schema) => throw new NotSupportedException();
16441644

1645-
public virtual string ToTableNamesWithRowCountsStatement(string schema) => null; //returning null Fallsback to slow UNION N+1 COUNT(*) op
1645+
public virtual string ToTableNamesWithRowCountsStatement(bool live, string schema) => null; //returning null Fallsback to slow UNION N+1 COUNT(*) op
16461646

16471647
public virtual string SqlConflict(string sql, string conflictResolution) => sql; //NOOP
16481648

src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,11 @@ public static SqlExpression<T> From<T>(this IDbConnection dbConn, TableOptions t
110110
public static Task<List<string>> GetTableNamesAsync(this IDbConnection db) => GetTableNamesAsync(db, null);
111111
public static Task<List<string>> GetTableNamesAsync(this IDbConnection db, string schema) => db.ColumnAsync<string>(db.GetDialectProvider().ToTableNamesStatement(schema));
112112

113-
public static List<KeyValuePair<string,long>> GetTableNamesWithRowCounts(this IDbConnection db) => GetTableNamesWithRowCounts(db, null);
114-
public static List<KeyValuePair<string,long>> GetTableNamesWithRowCounts(this IDbConnection db, string schema)
113+
public static List<KeyValuePair<string,long>> GetTableNamesWithRowCounts(this IDbConnection db, bool live=false, string schema=null)
115114
{
116115
List<KeyValuePair<string, long>> GetResults()
117116
{
118-
var sql = db.GetDialectProvider().ToTableNamesWithRowCountsStatement(schema);
117+
var sql = db.GetDialectProvider().ToTableNamesWithRowCountsStatement(live, schema);
119118
if (sql != null)
120119
return db.KeyValuePairs<string, long>(sql);
121120

@@ -128,12 +127,11 @@ List<KeyValuePair<string, long>> GetResults()
128127
return results;
129128
}
130129

131-
public static Task<List<KeyValuePair<string,long>>> GetTableNamesWithRowCountsAsync(this IDbConnection db) => GetTableNamesWithRowCountsAsync(db, null);
132-
public static async Task<List<KeyValuePair<string,long>>> GetTableNamesWithRowCountsAsync(this IDbConnection db, string schema)
130+
public static async Task<List<KeyValuePair<string,long>>> GetTableNamesWithRowCountsAsync(this IDbConnection db, bool live = false, string schema = null)
133131
{
134132
Task<List<KeyValuePair<string, long>>> GetResultsAsync()
135133
{
136-
var sql = db.GetDialectProvider().ToTableNamesWithRowCountsStatement(schema);
134+
var sql = db.GetDialectProvider().ToTableNamesWithRowCountsStatement(live, schema);
137135
if (sql != null)
138136
return db.KeyValuePairsAsync<string, long>(sql);
139137

tests/ServiceStack.OrmLite.Tests/MetaDataTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void Can_get_GetTableNamesWithRowCounts()
8989
3.Times(i => db.Insert(new Table1 {Id = i + 1, Field1 = $"Field{i+1}"}) );
9090
1.Times(i => db.Insert(new Table2 {Id = i + 1, Field2 = $"Field{i+1}"}) );
9191

92-
var tableNames = db.GetTableNamesWithRowCounts();
92+
var tableNames = db.GetTableNamesWithRowCounts(live:true);
9393
tableNames.TextDump().Print();
9494
Assert.That(tableNames.Count, Is.GreaterThan(0));
9595

@@ -100,6 +100,10 @@ public void Can_get_GetTableNamesWithRowCounts()
100100
Assert.That(table2Pos, Is.GreaterThanOrEqualTo(0));
101101

102102
Assert.That(table1Pos < table2Pos); //is sorted desc
103+
104+
tableNames = db.GetTableNamesWithRowCounts(live:false);
105+
Assert.That(tableNames.Any(x => x.Key.EqualsIgnoreCase(nameof(Table1))));
106+
Assert.That(tableNames.Any(x => x.Key.EqualsIgnoreCase(nameof(Table2))));
103107
}
104108
}
105109

@@ -114,7 +118,7 @@ public async Task Can_get_GetTableNamesWithRowCounts_Async()
114118
3.Times(i => db.Insert(new Table1 {Id = i + 1, Field1 = $"Field{i+1}"}) );
115119
1.Times(i => db.Insert(new Table2 {Id = i + 1, Field2 = $"Field{i+1}"}) );
116120

117-
var tableNames = await db.GetTableNamesWithRowCountsAsync();
121+
var tableNames = await db.GetTableNamesWithRowCountsAsync(live:true);
118122
tableNames.TextDump().Print();
119123
Assert.That(tableNames.Count, Is.GreaterThan(0));
120124

@@ -125,6 +129,10 @@ public async Task Can_get_GetTableNamesWithRowCounts_Async()
125129
Assert.That(table2Pos, Is.GreaterThanOrEqualTo(0));
126130

127131
Assert.That(table1Pos < table2Pos); //is sorted desc
132+
133+
tableNames = await db.GetTableNamesWithRowCountsAsync(live:false);
134+
Assert.That(tableNames.Any(x => x.Key.EqualsIgnoreCase(nameof(Table1))));
135+
Assert.That(tableNames.Any(x => x.Key.EqualsIgnoreCase(nameof(Table2))));
128136
}
129137
}
130138

@@ -140,7 +148,7 @@ public void Can_get_GetTableNamesWithRowCounts_in_Schema()
140148
3.Times(i => db.Insert(new Schematable1 {Id = i + 1, Field1 = $"Field{i+1}"}) );
141149
1.Times(i => db.Insert(new Schematable2 {Id = i + 1, Field2 = $"Field{i+1}"}) );
142150

143-
var tableNames = db.GetTableNamesWithRowCounts(schema);
151+
var tableNames = db.GetTableNamesWithRowCounts(live:true,schema:schema);
144152
tableNames.TextDump().Print();
145153
Assert.That(tableNames.Count, Is.GreaterThan(0));
146154

@@ -151,6 +159,10 @@ public void Can_get_GetTableNamesWithRowCounts_in_Schema()
151159
Assert.That(table2Pos, Is.GreaterThanOrEqualTo(0));
152160

153161
Assert.That(table1Pos < table2Pos); //is sorted desc
162+
163+
tableNames = db.GetTableNamesWithRowCounts(live:true,schema:schema);
164+
Assert.That(tableNames.Any(x => x.Key.IndexOf(nameof(Schematable1), StringComparison.OrdinalIgnoreCase) >= 0));
165+
Assert.That(tableNames.Any(x => x.Key.IndexOf(nameof(Schematable2), StringComparison.OrdinalIgnoreCase) >= 0));
154166
}
155167
}
156168
}

0 commit comments

Comments
 (0)