protected virtual void Translate(CopyContext Ctx, DataRow R) { DataTable T = R.Table; foreach (DataColumn C in T.Columns) { try { object O; if (myContext.IsDefined(C.ColumnName)) { //I traduttori privati prevalgono su quelli pubblici e non risentono del codice esterno O = myContext.Translate(C.ColumnName, R[C.ColumnName]); } else { O = Ctx.Translate(GetExternalCodeForField(C.ColumnName), R[C.ColumnName]); } if (O == null && AllowNoTransl.Contains(C.ColumnName)) { O = DBNull.Value; } R[C.ColumnName] = O; } catch (Exception E) { MessageBox.Show("La colonna " + C.ColumnName + " della tabella " + table + " contiene il valore " + R[C.ColumnName].ToString() + " che non è stato possibile tradurre "); throw E; } } }
protected virtual bool SkipExtNulls(DataRow R, CopyContext Ctx) { foreach (string field in SkipFieldsWhenNull) { if (R[field] == DBNull.Value) { continue; } object o = null; if (myContext.IsDefined(field)) { //I traduttori privati prevalgono su quelli pubblici e non risentono del codice esterno o = myContext.Translate(field, R[field]); } else { o = Ctx.Translate(GetExternalCodeForField(field), R[field]); } if (o == null || o == DBNull.Value) { return(true); } } return(false); }
public override bool CopyTable(DataAccess Source, DataAccess Dest, CopyContext Ctx, CopyDisplay CD) { if (DontCopy) { applied = true; return(true); } return(base.CopyTable(Source, Dest, Ctx, CD)); }
/// <summary> /// Merge the translators created by this Copyer to an external list /// </summary> /// <returns></returns> public virtual void MergePostTranslatorsTo(CopyContext Ctx) { if (POSTTrlist.Count == 0) { return; } foreach (string code in POSTTrlist.Keys) { Ctx.Add(code, POSTTrlist[code]); } }
protected void Comment(DataAccess Dest, CopyContext Ctx, CopyDisplay CD) { DataTable T = Dest.CreateTableByName(table, "*"); //Verifica quanti codici sono definiti e crea una stringa string comment = ""; foreach (DataColumn C in T.Columns) { string extcode = GetExternalCodeForField(C.ColumnName); //esamina i campi a codifica esterna if (extcode != C.ColumnName) { comment += "[" + C.ColumnName + " -> " + extcode + "] "; } if (myContext.IsDefined(extcode)) { if (extcode == C.ColumnName) { comment += "[" + extcode + " priv]"; } } else { if (Ctx.IsDefined(extcode)) { if (extcode == C.ColumnName) { comment += "[" + extcode + " ext]"; } } else { if (extcode != C.ColumnName) { comment += "\r\n\r\n>>>>>>>> E R R O R : " + extcode + " is not a defined translator <<<<<<<<<\r\n\r\n"; } } } } foreach (string field in SkipFieldsWhenNull) { comment += "[Skips on " + field + " null] "; } if (comment != "") { comment = "Table " + table.ToUpper() + ":" + comment; CD.Comment(comment); } }
/// <summary> /// Checks whether external fields have already been defined /// </summary> /// <param name="Ctx"></param> /// <param name="L"></param> /// <returns></returns> public bool CheckDependencies(CopyContext Ctx, List <string> L) { foreach (DataColumn C in T.Columns) { string codetosearch = GetExternalCodeForField(C.ColumnName); if (!L.Contains(codetosearch)) { continue; //non è nella mandatory list, tutto ok } if (PRETrlist.ContainsKey(codetosearch)) { continue; //è un campo gestito da questo copyer stesso } //è nella mandatorylist, vede se il Contesto per quel campo è stato definito if (Ctx.IsDefined(codetosearch)) { continue; } return(false); //field has not yet been defined } return(true); }
public virtual bool CopyTable(DataAccess Source, DataAccess Dest, CopyContext Ctx, CopyDisplay CD) { int count = 0; QueryHelper QHS = Dest.GetQueryHelper(); try { DataTable Cols = Dest.SQLRunner("sp_columns " + table + ",'" + Dest.GetSys("userdb").ToString() + "'"); if (Cols.Rows.Count == 0) { Cols = Dest.SQLRunner("sp_columns " + table + ",'dbo'"); } if (ClearDestAtStart) { Dest.SQLRunner("DELETE FROM " + table); } if (dontCopy) { CD.Comment("Table " + table + " skipped"); applied = true; return(true); } if (skipIfNotEmpty) { if (Dest.RUN_SELECT_COUNT(table, null, false) > 0) { CD.Comment("Table " + table + " skipped cause destination is not empty"); applied = true; return(true); } } Comment(Dest, Ctx, CD); int not_to_copy = 0; int nullskipped = 0; int nrows = Source.RUN_SELECT_COUNT(table, filter, false); CD.Start(filter == null? table: table + " where " + filter, "copy", nrows); string insert = "INSERT INTO " + table + " VALUES(";//( string s = ""; string err; DataTable T = Dest.CreateTableByName(table, "*"); string col = QueryCreator.ColumnNameList(T); DataAccess.DataRowReader DRR = new DataAccess.DataRowReader(Source, table, col, null, filter); foreach (DataRow row in DRR) { CD.Update(1); if (!IsToCopy(row)) { not_to_copy++; continue; } if (SkipExtNulls(row, Ctx)) { nullskipped++; continue; } Translate(Ctx, row); if (skipduplicates) { int num = Dest.RUN_SELECT_COUNT(table, QHS.CmpKey(row), false); if (num > 0) { continue; } } count++; string values = GetSQLDataValues(row, Cols); s += insert + values; if (count == 10) { Dest.SQLRunner(s, 0, out err); if (err != null) { QueryCreator.ShowError(null, "Errore", err); StreamWriter fsw = new StreamWriter("temp.sql", false, Encoding.Default); fsw.Write(s.ToString()); fsw.Close(); MessageBox.Show("Errore durante la copia della tabella " + table + "\r\nLo script lanciato si trova nel file 'temp.sql'"); CD.Stop(false); return(false); } s = ""; count = 0; } } if (s != "") { Dest.SQLRunner(s, 0, out err); count = 0; if (err != null) { QueryCreator.ShowError(null, "Errore", err); StreamWriter fsw = new StreamWriter("temp.sql", false, Encoding.Default); fsw.Write(s.ToString()); fsw.Close(); MessageBox.Show("Errore durante la copia di " + table + "\r\nLo script lanciato si trova nel file 'temp.sql'"); CD.Stop(false); return(false); } s = ""; } CD.Stop(true); if (not_to_copy > 0 || nullskipped > 0) { CD.Comment(">>Not to copy:" + not_to_copy.ToString() + " NullSkipped:" + nullskipped.ToString()); } applied = true; return(true); } catch (Exception E) { QueryCreator.ShowException(E); CD.Stop(false); return(false); } }
public override bool CopyTable(DataAccess Source, DataAccess Dest, CopyContext Ctx, CopyDisplay CD) { applied = false; int count = 0; QueryHelper QHS = Dest.GetQueryHelper(); try { DataTable Cols = Dest.SQLRunner("sp_columns " + table + ",'" + Dest.GetSys("userdb").ToString() + "'"); if (Cols.Rows.Count == 0) { Cols = Dest.SQLRunner("sp_columns " + table + ",'dbo'"); } Comment(Dest, Ctx, CD); int nrows = Source.RUN_SELECT_COUNT(table, filter, false); CD.Start(filter == null ? table : table + " where " + filter, "copy", nrows); string insert = "INSERT INTO surplus VALUES(";//( string s = ""; string err; DataTable T = Dest.CreateTableByName(table, "*"); string col = QueryCreator.ColumnNameList(T); DataAccess.DataRowReader DRR = new DataAccess.DataRowReader(Source, "surplus", col, null, filter); foreach (DataRow row in DRR) { CD.Update(1); if (!IsToCopy(row)) { continue; // copia quelle che NON sono da inserire } Translate(Ctx, row); string delete = ""; DataTable T2 = Dest.RUN_SELECT("surplus", "*", null, QHS.CmpEq("ayear", row["ayear"]), null, false); if (T2.Rows.Count > 0) { DataRow RR = T2.Rows[0]; foreach (DataColumn CR in T2.Columns) { string field = CR.ColumnName; if (CR.DataType == typeof(decimal)) { if (RR[field] == DBNull.Value) { continue; } if (row[field] == DBNull.Value) { row[field] = RR[field]; } else { row[field] = CfgFn.GetNoNullDecimal(row[field]) + CfgFn.GetNoNullDecimal(RR[field]); } } } delete = "delete from surplus where " + QHS.CmpEq("ayear", row["ayear"]) + "\n\r"; } row["locked"] = "S"; count++; string values = GetSQLDataValues(row, Cols); s += delete + insert + values; if (count == 1) //è necessario farlo una riga alla volta { Dest.SQLRunner(s, 0, out err); if (err != null) { QueryCreator.ShowError(null, "Errore", err); StreamWriter fsw = new StreamWriter("temp.sql", false, Encoding.Default); fsw.Write(s.ToString()); fsw.Close(); MessageBox.Show("Errore durante la copia della tabella " + table + "\r\nLo script lanciato si trova nel file 'temp.sql'"); CD.Stop(false); return(false); } s = ""; count = 0; } } if (s != "") { Dest.SQLRunner(s, 0, out err); count = 0; if (err != null) { QueryCreator.ShowError(null, "Errore", err); StreamWriter fsw = new StreamWriter("temp.sql", false, Encoding.Default); fsw.Write(s.ToString()); fsw.Close(); MessageBox.Show("Errore durante la copia di " + table + "\r\nLo script lanciato si trova nel file 'temp.sql'"); CD.Stop(false); return(false); } s = ""; } CD.Stop(true); applied = true; return(true); } catch (Exception E) { QueryCreator.ShowException(E); CD.Stop(false); return(false); } }
/// <summary> /// Copy all tables respecting dependencies /// </summary> /// <param name="CD"></param> /// <returns></returns> public bool CopyAll(CopyDisplay CD) { CD.Start("Copying"); CD.Comment("Disable Triggers"); Dest.CallSP("enable_disable_triggers", new object[2] { Dest.GetSys("userdb").ToString(), 'D' }, false, 500); List <string> FieldToLookup = new List <string>(); foreach (Copyer C in AR) { foreach (string code in C.CodeDefined()) { FieldToLookup.Add(code); } } bool somethingcopied = true; Dictionary <string, translator> Trlist = new Dictionary <string, translator>(); while (somethingcopied) { somethingcopied = false; foreach (Copyer C in AR) { if (C.applied) { continue; } CopyContext Ctx = new CopyContext(Trlist); //verifica le dipendenze di C if (!C.CheckDependencies(Ctx, FieldToLookup)) { continue; //non può copiare ancora questa tabella, mancano dipendenze } C.MergePreTranslatorsTo(Ctx); bool res = C.CopyTable(Source, Dest, Ctx, CD); if (!res) { CD.Comment("Enable Triggers"); Dest.CallSP("enable_disable_triggers", new object[2] { Dest.GetSys("userdb").ToString(), 'E' }, false, 500); MessageBox.Show("La copia della tabella " + C.table + " non è stata effettuata", "Errore"); return(false); } somethingcopied = true; C.MergePostTranslatorsTo(Ctx); if (!C.applied) { CD.Comment("\r\n>>>>>>>>>>>>>>>>>Table " + C.table + " did not set APPLIED flag <<<<<<<<<<<<<<<<<<<<<<<<\r\n"); } } } foreach (Copyer C in AR) { if (!C.applied) { MessageBox.Show("La copia della tabella " + C.table + " non è stata effettuata per mancanza di dipendenze", "Errore"); } } CD.Comment("Enable Triggers"); Dest.CallSP("enable_disable_triggers", new object[2] { Dest.GetSys("userdb").ToString(), 'E' }, false, 500); return(true); }