public void PutDropMaterialized(SqlScriptCompiler cmp) { if (_materializedName == null) return; cmp.Dumper.DropTable(new TableInfo(null) { FullName = _materializedName }, false); cmp.EndCommand(); }
public void RunRound2Reverted(SqlScriptCompiler cmp, bool useTransaction) { if (!_dbsh.LifetimeHandler.CreateDelete) { return; } RunCore(cmp, useTransaction, RunCoreRound2Reverted, 2); }
public void PutMaterialize(SqlScriptCompiler cmp) { if (_materializedName == null) return; cmp.PutSmallTitleComment($"Materialize of entity {SqlAlias}"); cmp.StartTimeMeasure("OP"); cmp.GenCommandSql(_materializeSelect); cmp.PutLogMessage(null, LogOperationType.Materialize, $"@rows rows of {SqlAlias} materialized", "OP"); }
private void RunCore(SqlScriptCompiler cmp, bool useTransaction, Action<SqlScriptCompiler> doRun, int round) { cmp.PutMainTitleComment($"Synchronize entity {SqlAlias} (table {TargetTable}) - round {round}"); cmp.StartTimeMeasure("TABLE"); cmp.PutBeginTryCatch(this); cmp.Put("&>"); doRun(cmp); cmp.Put("&<"); cmp.PutEndTryCatch(this, useTransaction); cmp.PutLogMessage(this, LogOperationType.TableSynchronized, $"table synchronized - round {round}", "TABLE"); }
public void RunRound1(SqlScriptCompiler cmp, bool useTransaction) { if ( !_dbsh.LifetimeHandler.CreateMarkRelived && !_dbsh.LifetimeHandler.CreateMarkDeleted && !_dbsh.LifetimeHandler.CreateMarkUpdated && !_dbsh.LifetimeHandler.CreateInsert && !_dbsh.LifetimeHandler.CreateUpdate ) { return; } RunCore(cmp, useTransaction, RunCoreRound1, 1); }
private void RunCoreRound1(SqlScriptCompiler cmp) { if (_dbsh.LifetimeHandler.CreateMarkRelived) { var update = CompileMarkRelived(); if (update != null) { cmp.StartTimeMeasure("OP"); cmp.PutSmallTitleComment("MARK RELIVED"); cmp.GenCommandSql(update); cmp.EndCommand(); cmp.PutLogMessage(this, LogOperationType.MarkRelived, "@rows rows marked as relived", "OP"); } } if (_dbsh.LifetimeHandler.CreateMarkDeleted) { var update = CompileMarkDeleted(); if (update != null) { cmp.StartTimeMeasure("OP"); cmp.PutSmallTitleComment("MARK DELETED"); cmp.GenCommandSql(update); cmp.PutLogMessage(this, LogOperationType.MarkDeleted, "@rows rows marked as deleted", "OP"); } } if (_dbsh.LifetimeHandler.CreateMarkUpdated) { var update = CompileMarkUpdated(); if (update != null) { cmp.StartTimeMeasure("OP"); cmp.PutSmallTitleComment("MARK UPDATED"); cmp.GenCommandSql(update); cmp.PutLogMessage(this, LogOperationType.MarkUpdated, "@rows rows marked as updated", "OP"); } } if (_dbsh.LifetimeHandler.CreateInsert) { var insert = CompileInsert(); if (insert != null) { cmp.StartTimeMeasure("OP"); cmp.PutSmallTitleComment("INSERT"); bool isIdentity = Structure != null && Structure.Columns.Any(x => x.AutoIncrement && insert.TargetColumns.Contains(x.Name)); if (isIdentity) cmp.GenCommandSql(dmp => dmp.AllowIdentityInsert(insert.TargetTable, true)); cmp.GenCommandSql(insert); cmp.PutLogMessage(this, LogOperationType.Insert, "@rows rows inserted", "OP"); if (isIdentity) cmp.GenCommandSql(dmp => dmp.AllowIdentityInsert(insert.TargetTable, false)); } } if (_dbsh.LifetimeHandler.CreateUpdate) { var update = CompileUpdate(); if (update != null) { cmp.StartTimeMeasure("OP"); cmp.PutSmallTitleComment("UPDATE"); cmp.GenCommandSql(update); cmp.PutLogMessage(this, LogOperationType.Update, "@rows rows updated", "OP"); } } }
private void RunCoreRound2Reverted(SqlScriptCompiler cmp) { if (_dbsh.LifetimeHandler.CreateDelete) { var delete = CompileDelete(); if (delete != null) { cmp.StartTimeMeasure("OP"); cmp.PutSmallTitleComment("DELETE"); cmp.GenCommandSql(delete); cmp.PutLogMessage(this, LogOperationType.Delete, "@rows rows deleted", "OP"); } } }
public string GenerateScript(IDatabaseFactory factory, IShellContext context, bool useTransaction, List<ParameterModel> pars, Dictionary<string, string> parValues) { var cmp = new SqlScriptCompiler(factory, this, context, null); cmp.PutScriptProlog(useTransaction, pars, parValues); DumpScript(cmp, useTransaction); return cmp.GetCompiledSql(); }
public string GenerateCreateProcedure(IDatabaseFactory factory, NameWithSchema name, IShellContext context, bool useTransaction, bool overwriteExisting, List<ParameterModel> pars, string editorInfo) { if (overwriteExisting) { string sqlCore = GenerateCreateProcedureCore(factory, name, context, useTransaction, "", pars, editorInfo); var cmp = new SqlScriptCompiler(factory, this, context, name.ToString()); cmp.CreateOrAlterProcedure(name, sqlCore); return cmp.GetCompiledSql(); } else { return GenerateCreateProcedureCore(factory, name, context, useTransaction, "create", pars, editorInfo); } }
private string GenerateCreateProcedureCore(IDatabaseFactory factory, NameWithSchema name, IShellContext context, bool useTransaction, string createKeyword, List<ParameterModel> pars, string editorInfo) { var cmp = new SqlScriptCompiler(factory, this, context, name.ToString()); cmp.PutProcedureHeader(name, useTransaction, createKeyword, pars); DumpScript(cmp, useTransaction); cmp.PutProcedureFooter(); cmp.PutEditorInfo(editorInfo); return cmp.GetCompiledSql(); }
public void Run(DbConnection conn, IDatabaseFactory factory, IShellContext context, bool useTransaction, List<ParameterModel> pars, Dictionary<string, string> parValues) { FillExternalSources(conn, factory, context); var cmp = new SqlScriptCompiler(factory, this, context, null); cmp.PutScriptProlog(useTransaction, pars, parValues); DumpScript(cmp, useTransaction); ExecuteScript(conn, cmp.GetCompiledSql()); FreeExternalSources(conn, factory, context); }
private void DumpScript(SqlScriptCompiler cmp, bool useTransaction) { cmp.PutCommonProlog(useTransaction, _model.SqlPrologBeforeBeginTransaction, _model.SqlPrologAfterBeginTransaction); foreach (var source in EnumSources()) { source.PutMaterialize(cmp); } foreach (var ent in Entities) { if (!ent.IncludeInSync) continue; ent.RunRound1(cmp, useTransaction); } var reverted = new List<TargetEntitySqlModel>(Entities); reverted.Reverse(); foreach (var ent in reverted) { if (!ent.IncludeInSync) continue; ent.RunRound2Reverted(cmp, useTransaction); } foreach (var source in EnumSources()) { source.PutDropMaterialized(cmp); } cmp.PutCommonEpilog(useTransaction, _model.SqlEpilogBeforeCommitTransaction, _model.SqlEpilogAfterCommitTransaction); }