private bool RunActionAsSql(DbDiffAction action) { string sql = action.GenerateSql(m_dstDb); if (SqlConfirmForm.Run(sql)) { m_dstDb.Connection.Invoke(() => { var con = m_dstDb.Connection.SystemConnection; var tran = con.BeginTransaction(); var sqlo = new ConnectionSqlOutputStream(con, tran, m_dstDb.Dialect); try { con.ExecuteNonQueries(sql, m_dstDb.Connection.Dialect, tran, null); } catch { tran.Rollback(); throw; } tran.Commit(); }); //var dmp = m_dstDb.Dialect.CreateDumper(sqlo, new SqlFormatProperties()); //dmp.TargetDb = m_dstDb; //m_dstDb.Connection.Invoke(() => action.GenerateScript(dmp)); return(true); } return(false); }
public static void RunScript(this DbConnection conn, IServiceProvider serviceProvider, Action <ISqlDumper> script, DbTransaction trans = null) { ConnectionSqlOutputStream sqlo = new ConnectionSqlOutputStream(conn, trans, GenericDialect.InternalInstance); var factory = conn.GetFactory(serviceProvider); ISqlDumper fmt = factory.CreateDumper(sqlo, SqlFormatProperties.Default); script(fmt); }
protected void RunSqlScript(Action <ISqlDumper> dmpFunc) { using (var conn = OpenConnection()) { var sqlo = new ConnectionSqlOutputStream(conn, null, DatabaseFactory.CreateDialect()); var dmp = DatabaseFactory.CreateDumper(sqlo, new SqlFormatProperties()); dmpFunc(dmp); } }
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)); } }