This repository was archived by the owner on Dec 24, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDbScripts.cs
More file actions
312 lines (236 loc) · 16.3 KB
/
Copy pathDbScripts.cs
File metadata and controls
312 lines (236 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ServiceStack.Data;
using ServiceStack.Script;
namespace ServiceStack.OrmLite
{
[Obsolete("Use DbScriptsAsync")]
public class DbScripts : ScriptMethods
{
private const string DbInfo = "__dbinfo"; // Keywords.DbInfo
private const string DbConnection = "__dbconnection"; // useDb global
private IDbConnectionFactory dbFactory;
public IDbConnectionFactory DbFactory
{
get => dbFactory ??= Context.Container.Resolve<IDbConnectionFactory>();
set => dbFactory = value;
}
public IDbConnection OpenDbConnection(ScriptScopeContext scope, Dictionary<string, object> options)
{
var dbConn = OpenDbConnectionFromOptions(options);
if (dbConn != null)
return dbConn;
if (scope.PageResult != null)
{
if (scope.PageResult.Args.TryGetValue(DbInfo, out var oDbInfo) && oDbInfo is ConnectionInfo dbInfo)
return DbFactory.OpenDbConnection(dbInfo);
if (scope.PageResult.Args.TryGetValue(DbConnection, out var oDbConn) && oDbConn is Dictionary<string, object> globalDbConn)
return OpenDbConnectionFromOptions(globalDbConn);
}
return DbFactory.OpenDbConnection();
}
T dialect<T>(ScriptScopeContext scope, Func<IOrmLiteDialectProvider, T> fn)
{
if (scope.PageResult != null)
{
if (scope.PageResult.Args.TryGetValue(DbInfo, out var oDbInfo) && oDbInfo is ConnectionInfo dbInfo)
return fn(DbFactory.GetDialectProvider(dbInfo));
if (scope.PageResult.Args.TryGetValue(DbConnection, out var oDbConn) && oDbConn is Dictionary<string, object> useDb)
return fn(DbFactory.GetDialectProvider(
providerName:useDb.GetValueOrDefault("providerName")?.ToString(),
namedConnection:useDb.GetValueOrDefault("namedConnection")?.ToString()));
}
return fn(OrmLiteConfig.DialectProvider);
}
public IgnoreResult useDb(ScriptScopeContext scope, Dictionary<string, object> dbConnOptions)
{
if (dbConnOptions == null)
{
scope.PageResult.Args.Remove(DbConnection);
}
else
{
if (!dbConnOptions.ContainsKey("connectionString") && !dbConnOptions.ContainsKey("namedConnection"))
throw new NotSupportedException(nameof(useDb) + " requires either 'connectionString' or 'namedConnection' property");
scope.PageResult.Args[DbConnection] = dbConnOptions;
}
return IgnoreResult.Value;
}
private IDbConnection OpenDbConnectionFromOptions(Dictionary<string, object> options)
{
if (options != null)
{
if (options.TryGetValue("connectionString", out var connectionString))
{
return options.TryGetValue("providerName", out var providerName)
? DbFactory.OpenDbConnectionString((string) connectionString, (string) providerName)
: DbFactory.OpenDbConnectionString((string) connectionString);
}
if (options.TryGetValue("namedConnection", out var namedConnection))
{
return DbFactory.OpenDbConnection((string) namedConnection);
}
}
return null;
}
T exec<T>(Func<IDbConnection, T> fn, ScriptScopeContext scope, object options)
{
try
{
using var db = OpenDbConnection(scope, options as Dictionary<string, object>);
return fn(db);
}
catch (Exception ex)
{
throw new StopFilterExecutionException(scope, options, ex);
}
}
public object dbSelect(ScriptScopeContext scope, string sql) =>
exec(db => db.SqlList<Dictionary<string, object>>(sql), scope, null);
public object dbSelect(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.SqlList<Dictionary<string, object>>(sql, args), scope, null);
public object dbSelect(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.SqlList<Dictionary<string, object>>(sql, args), scope, options);
public object dbSingle(ScriptScopeContext scope, string sql) =>
exec(db => db.Single<Dictionary<string, object>>(sql), scope, null);
public object dbSingle(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.Single<Dictionary<string, object>>(sql, args), scope, null);
public object dbSingle(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.Single<Dictionary<string, object>>(sql, args), scope, options);
public object dbScalar(ScriptScopeContext scope, string sql) =>
exec(db => db.Scalar<object>(sql), scope, null);
public object dbScalar(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.Scalar<object>(sql, args), scope, null);
public object dbScalar(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.Scalar<object>(sql, args), scope, options);
public long dbCount(ScriptScopeContext scope, string sql) =>
exec(db => db.RowCount(sql), scope, null);
public long dbCount(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.RowCount(sql, args), scope, null);
public long dbCount(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.RowCount(sql, args), scope, options);
public bool dbExists(ScriptScopeContext scope, string sql) =>
dbScalar(scope, sql) != null;
public bool dbExists(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
dbScalar(scope, sql, args) != null;
public bool dbExists(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
dbScalar(scope, sql, args, options) != null;
public int dbExec(ScriptScopeContext scope, string sql) =>
exec(db => db.ExecuteSql(sql), scope, null);
public int dbExec(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.ExecuteSql(sql, args), scope, null);
public int dbExec(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.ExecuteSql(sql, args), scope, options);
public List<string> dbNamedConnections() => OrmLiteConnectionFactory.NamedConnections.Keys.ToList();
public List<string> dbTableNames(ScriptScopeContext scope) => dbTableNames(scope, null, null);
public List<string> dbTableNames(ScriptScopeContext scope, Dictionary<string, object> args) => dbTableNames(scope, args, null);
public List<string> dbTableNames(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
exec(db => db.GetTableNames(args != null && args.TryGetValue("schema", out var oSchema) ? oSchema as string : null), scope, options);
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope) =>
dbTableNamesWithRowCounts(scope, null, null);
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args) =>
dbTableNamesWithRowCounts(scope, args, null);
public List<KeyValuePair<string, long>> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
exec(db => args == null
? db.GetTableNamesWithRowCounts()
: db.GetTableNamesWithRowCounts(
live: args.TryGetValue("live", out var oLive) && oLive is bool b && b,
schema: args.TryGetValue("schema", out var oSchema) ? oSchema as string : null),
scope, options);
public string[] dbColumnNames(ScriptScopeContext scope, string tableName) => dbColumnNames(scope, tableName, null);
public string[] dbColumnNames(ScriptScopeContext scope, string tableName, object options) =>
dbColumns(scope, tableName, options).Select(x => x.ColumnName).ToArray();
public ColumnSchema[] dbColumns(ScriptScopeContext scope, string tableName) => dbColumns(scope, tableName, null);
public ColumnSchema[] dbColumns(ScriptScopeContext scope, string tableName, object options) =>
exec(db => db.GetTableColumns($"SELECT * FROM {sqlQuote(scope,tableName)}"), scope, options);
public ColumnSchema[] dbDesc(ScriptScopeContext scope, string sql) => dbDesc(scope, sql, null);
public ColumnSchema[] dbDesc(ScriptScopeContext scope, string sql, object options) => exec(db => db.GetTableColumns(sql), scope, options);
public string sqlQuote(ScriptScopeContext scope, string name) => dialect(scope, d => d.GetQuotedName(name));
public string sqlConcat(ScriptScopeContext scope, IEnumerable<object> values) => dialect(scope, d => d.SqlConcat(values));
public string sqlCurrency(ScriptScopeContext scope, string fieldOrValue) => dialect(scope, d => d.SqlCurrency(fieldOrValue));
public string sqlCurrency(ScriptScopeContext scope, string fieldOrValue, string symbol) =>
dialect(scope, d => d.SqlCurrency(fieldOrValue, symbol));
public string sqlCast(ScriptScopeContext scope, object fieldOrValue, string castAs) =>
dialect(scope, d => d.SqlCast(fieldOrValue, castAs));
public string sqlBool(ScriptScopeContext scope, bool value) => dialect(scope, d => d.SqlBool(value));
public string sqlTrue(ScriptScopeContext scope) => dialect(scope, d => d.SqlBool(true));
public string sqlFalse(ScriptScopeContext scope) => dialect(scope, d => d.SqlBool(false));
public string sqlLimit(ScriptScopeContext scope, int? offset, int? limit) =>
dialect(scope, d => padCondition(d.SqlLimit(offset, limit)));
public string sqlLimit(ScriptScopeContext scope, int? limit) =>
dialect(scope, d => padCondition(d.SqlLimit(null, limit)));
public string sqlSkip(ScriptScopeContext scope, int? offset) =>
dialect(scope, d => padCondition(d.SqlLimit(offset, null)));
public string sqlTake(ScriptScopeContext scope, int? limit) =>
dialect(scope, d => padCondition(d.SqlLimit(null, limit)));
public string sqlOrderByFields(ScriptScopeContext scope, string orderBy) =>
dialect(scope, d => OrmLiteUtils.OrderByFields(d, orderBy));
public string ormliteVar(ScriptScopeContext scope, string name) =>
dialect(scope, d => d.Variables.TryGetValue(name, out var value) ? value : null);
public string sqlVerifyFragment(string sql) => sql.SqlVerifyFragment();
public bool isUnsafeSql(string sql) => OrmLiteUtils.isUnsafeSql(sql, OrmLiteUtils.VerifySqlRegEx);
public bool isUnsafeSqlFragment(string sql) => OrmLiteUtils.isUnsafeSql(sql, OrmLiteUtils.VerifyFragmentRegEx);
private string padCondition(string text) => string.IsNullOrEmpty(text) ? "" : " " + text;
}
public partial class DbScriptsAsync
{
private DbScripts sync;
private DbScripts Sync => sync ??= new DbScripts { Context = Context, Pages = Pages };
public object dbSelectSync(ScriptScopeContext scope, string sql) => Sync.dbSelect(scope, sql);
public object dbSelectSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
Sync.dbSelect(scope, sql, args);
public object dbSelectSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
Sync.dbSelect(scope, sql, args, options);
public object dbSingleSync(ScriptScopeContext scope, string sql) =>
Sync.dbSingle(scope, sql);
public object dbSingleSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
Sync.dbSingle(scope, sql, args);
public object dbSingleSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
Sync.dbSingle(scope, sql, args, options);
public object dbScalarSync(ScriptScopeContext scope, string sql) =>
Sync.dbScalar(scope, sql);
public object dbScalarSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
Sync.dbScalar(scope, sql, args);
public object dbScalarSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
Sync.dbScalar(scope, sql, args, options);
public long dbCountSync(ScriptScopeContext scope, string sql) =>
Sync.dbCount(scope, sql);
public long dbCountSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
Sync.dbCount(scope, sql, args);
public long dbCountSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
Sync.dbCount(scope, sql, args, options);
public bool dbExistsSync(ScriptScopeContext scope, string sql) =>
Sync.dbExists(scope, sql);
public bool dbExistsSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
Sync.dbExists(scope, sql, args);
public bool dbExistsSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
Sync.dbExists(scope, sql, args, options);
public int dbExecSync(ScriptScopeContext scope, string sql) =>
Sync.dbExec(scope, sql);
public int dbExecSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args) =>
Sync.dbExec(scope, sql, args);
public int dbExecSync(ScriptScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
Sync.dbExec(scope, sql, args, options);
public List<string> dbTableNamesSync(ScriptScopeContext scope) => dbTableNamesSync(scope, null, null);
public List<string> dbTableNamesSync(ScriptScopeContext scope, Dictionary<string, object> args) => dbTableNamesSync(scope, args, null);
public List<string> dbTableNamesSync(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
Sync.dbTableNames(scope, args, options);
public List<KeyValuePair<string, long>> dbTableNamesWithRowCountsSync(ScriptScopeContext scope) =>
dbTableNamesWithRowCountsSync(scope, null, null);
public List<KeyValuePair<string, long>> dbTableNamesWithRowCountsSync(ScriptScopeContext scope, Dictionary<string, object> args) =>
dbTableNamesWithRowCountsSync(scope, args, null);
public List<KeyValuePair<string, long>> dbTableNamesWithRowCountsSync(ScriptScopeContext scope, Dictionary<string, object> args, object options) =>
Sync.dbTableNamesWithRowCounts(scope, args, options);
public string[] dbColumnNamesSync(ScriptScopeContext scope, string tableName) => dbColumnNamesSync(scope, tableName, null);
public string[] dbColumnNamesSync(ScriptScopeContext scope, string tableName, object options) =>
dbColumnsSync(scope, tableName, options).Select(x => x.ColumnName).ToArray();
public ColumnSchema[] dbColumnsSync(ScriptScopeContext scope, string tableName) => dbColumnsSync(scope, tableName, null);
public ColumnSchema[] dbColumnsSync(ScriptScopeContext scope, string tableName, object options) =>
Sync.dbColumns(scope, tableName, options);
public ColumnSchema[] dbDescSync(ScriptScopeContext scope, string sql) => dbDescSync(scope, sql, null);
public ColumnSchema[] dbDescSync(ScriptScopeContext scope, string sql, object options) =>
Sync.dbDesc(scope, sql, options);
}
}