static int GetCount(string tableName, DbConnection connection) { try { return connection.ExecuteScalar<int>("SELECT COUNT(*) FROM [" + tableName.Replace("]", "]]") + "]"); } catch { return -1; } }
private void FillExternalSources(DbConnection conn, IDatabaseFactory factory, IShellContext context) { if (!_externalSources.Any()) return; var sw = new StringWriter(); var so = new ConnectionSqlOutputStream(conn, null, factory.CreateDialect()); var dmp = factory.CreateDumper(so, new SqlFormatProperties()); foreach (var exSource in _externalSources) { var tbl = new TableInfo(null) { FullName = exSource.ExternalDataName }; foreach (var col in exSource.Dbsh.Columns) { tbl.Columns.Add(new ColumnInfo(tbl) { Name = col.Name, DataType = col.DataType ?? "nvarchar(500)", CommonType = DbTypeBase.ParseType(col.DataType ?? "nvarchar(500)"), }); } dmp.CreateTable(tbl); var filterModel = FilterJoinSqlModel.Create(exSource, SourceGraphModel, factory); var copyTable = new DbShell.Core.CopyTable { Source = filterModel?.DataSource ?? exSource.Dbsh.DataSource, Target = new DbShell.Core.Table { Name = exSource.ExternalDataName.Name, StructureOverride = tbl, }, AllowBulkCopy = _model.AllowBulkCopy, }; var runnable = (IRunnable)copyTable; runnable.Run(context); if (exSource.Dbsh.OnExternalFilledAssertion != null) { string value = conn.ExecuteScalar(exSource.Dbsh.OnExternalFilledAssertion.Replace("${TABLE}", exSource.ExternalDataName.Name))?.ToString(); if (value != exSource.Dbsh.OnExternalFilledRequiredValue) { throw new Exception($"DBSH-00000 OnExternalFilledAssertion failed, source={exSource.SqlAlias}, required={exSource.Dbsh.OnExternalFilledRequiredValue}, actual={value}, query={exSource.Dbsh.OnExternalFilledAssertion}"); } } } }