Example #1
0
        private void FreeExternalSources(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 };
                dmp.DropTable(tbl, false);
            }
        }
Example #2
0
        public ICdlWriter CreateWriter(TableInfo inputRowFormat, CopyTableTargetOptions options, IShellContext context, DataFormatSettings sourceDataFormat)
        {
            var connection = GetConnectionProvider(context);
            using (var conn = connection.Connect())
            {
                var db = new DatabaseInfo();
                db.LinkedInfo = LinkedInfo;
                var tbl = inputRowFormat.CloneTable(db);
                tbl.FullName = GetFullName(context);
                foreach (var col in tbl.Columns) col.AutoIncrement = false;
                tbl.ForeignKeys.Clear();
                if (tbl.PrimaryKey != null) tbl.PrimaryKey.ConstraintName = null;
                tbl.AfterLoadLink();

                if (IdentityColumn != null)
                {
                    var col = new ColumnInfo(tbl);
                    col.Name = IdentityColumn;
                    col.DataType = "int";
                    col.AutoIncrement = true;
                    col.NotNull = true;
                    var pk = new PrimaryKeyInfo(tbl);
                    pk.Columns.Add(new ColumnReference {RefColumn = col});
                    pk.ConstraintName = "PK_" + tbl.Name;
                    tbl.PrimaryKey = pk;
                    tbl.Columns.Insert(0, col);
                }

                //var sw = new StringWriter();
                var so = new ConnectionSqlOutputStream(conn, null, connection.Factory.CreateDialect());
                var dmp = connection.Factory.CreateDumper(so, new SqlFormatProperties());
                if (DropIfExists) dmp.DropTable(tbl, true);

                bool useExistingTable = false;
                if (UseIfExists)
                {
                    var ts = context.GetDatabaseStructure(connection.ProviderString);
                    useExistingTable = ts.FindTableLike(tbl.FullName.Schema, tbl.FullName.Name) != null;
                }

                if (!useExistingTable)
                {
                    tbl.Columns.ForEach(x => x.EnsureDataType(connection.Factory.CreateSqlTypeProvider()));
                    dmp.CreateTable(tbl);
                }
                //using (var cmd = conn.CreateCommand())
                //{
                //    cmd.CommandText = sw.ToString();
                //    cmd.ExecuteNonQuery();
                //}

                return new TableWriter(context, connection, GetFullName(context), inputRowFormat, options, useExistingTable ? null : tbl, LinkedInfo, sourceDataFormat);
            }
        }
Example #3
0
        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}");
                    }
                }
            }
        }