public virtual void RecreateTable(ITableStructure oldTable, ITableStructure newTable) { if (oldTable.GroupId != newTable.GroupId) { throw new InternalError("DAE-00040 Recreate is not possible: oldTable.GroupId != newTable.GroupId"); } var columnMap = GetColumnMap(oldTable, newTable); int id = System.Threading.Interlocked.Increment(ref m_lastAlterTableId); string tmptable = ConnTools.GenerateTempTableName(id); // remove constraints //DropConstraints(oldTable.GetReferencedFrom(), DropFlags.None); DropConstraints(oldTable.Constraints, DropFlags.None); RenameTable(oldTable.FullName, tmptable); TableStructure old = new TableStructure(oldTable); old.FullName = new NameWithSchema(oldTable.FullName.Schema, tmptable); CreateTable(newTable); var idcol = newTable.FindAutoIncrementColumn(); bool hasident = idcol != null && columnMap[idcol.ColumnOrder] >= 0; if (hasident) { AllowIdentityInsert(newTable.FullName, true); } PutCmd("^insert ^into %f (%,i) select %,s ^from %f", newTable.FullName, from c in newTable.Columns where columnMap[c.ColumnOrder] >= 0 select c.ColumnName, from dstindex in ( from i in PyList.Range(newTable.Columns.Count) where columnMap[i] >= 0 select i ) let srcindex = columnMap[dstindex] select (srcindex < 0 // srcindex < 0 should not occur thanks to filtering ? Format("^null ^as %i", newTable.Columns[dstindex].ColumnName) : Format("^%i ^as %i", old.Columns[srcindex].ColumnName, newTable.Columns[dstindex].ColumnName)), old.FullName); if (hasident) { AllowIdentityInsert(newTable.FullName, false); } // newTable.Constraints are allready created //CreateConstraints(newTable.GetReferencedFrom()); PutCmd("^drop ^table %i", tmptable); }
protected void GenerateFillTable(ITableStructure tbl, ISqlDumper dmp) { var autocol = tbl.FindAutoIncrementColumn(); if (autocol != null) { dmp.AllowIdentityInsert(tbl.FullName, true); } var colnames = from c in tbl.Columns select c.ColumnName; dmp.PutCmd("^insert ^into %f (%,i) ^select %,i ^from %s.%f", tbl.FullName, colnames, colnames, SourceDb, tbl.FullName); if (autocol != null) { dmp.AllowIdentityInsert(tbl.FullName, false); } }
protected bool HasIdentity(IDataQueue queue) { ITableStructure ts = queue.GetRowFormat; ITableStructure dst_ts = DestinationTable; IColumnStructure autoinc = dst_ts.FindAutoIncrementColumn(); bool hasident = false; if (autoinc != null) { if (ts.Columns.Count != dst_ts.Columns.Count) { // determine whether auto-inc column is inserted hasident = ts.Columns.IndexOfIf(col => col.ColumnName == autoinc.ColumnName) >= 0; } else { hasident = true; } } return(hasident); }
public void UpdateData(ITableStructure table, DataScript script, ISaveDataProgress progress) { if (script == null) { return; } int delcnt = 0, inscnt = 0, updrows = 0, updflds = 0; if (progress != null) { updrows = progress.GetCurrent(SaveProgressMeasure.UpdatedRows); updflds = progress.GetCurrent(SaveProgressMeasure.UpdatedFields); } foreach (var del in script.Deletes) { Put("^delete ^from %f", table.FullName); Where(table.FullName, del.CondCols, del.CondValues); EndCommand(); delcnt++; if (progress != null) { progress.SetCurrent(SaveProgressMeasure.DeletedRows, delcnt); } if (progress != null && progress.IsCanceled) { throw new OperationCanceledError(); } } foreach (var upd in script.Updates) { Put("^update %f ^set ", table.FullName); for (int i = 0; i < upd.Columns.Length; i++) { if (i > 0) { Put(", "); } Put("%i=%v", upd.Columns[i], new ValueTypeHolder(upd.Values[i], table.Columns[upd.Columns[i]].DataType)); } Where(table.FullName, upd.CondCols, upd.CondValues); EndCommand(); updrows++; updflds += upd.Values.Length; if (progress != null) { progress.SetCurrent(SaveProgressMeasure.UpdatedRows, updrows); progress.SetCurrent(SaveProgressMeasure.UpdatedFields, updflds); } if (progress != null && progress.IsCanceled) { throw new OperationCanceledError(); } } IColumnStructure autoinc = table.FindAutoIncrementColumn(); bool isIdentityInsert = false; foreach (var ins in script.Inserts) { if (autoinc != null) { if (ins.Columns.Contains(autoinc.ColumnName)) { if (!isIdentityInsert) { AllowIdentityInsert(table.FullName, true); } isIdentityInsert = true; } else { if (isIdentityInsert) { AllowIdentityInsert(table.FullName, false); } isIdentityInsert = false; } } var vals = new ValueTypeHolder[ins.Columns.Length]; for (int i = 0; i < ins.Columns.Length; i++) { vals[i] = new ValueTypeHolder(ins.Values[i], table.Columns[ins.Columns[i]].DataType); } PutCmd("^insert ^into %f (%,i) ^values (%,v)", table.FullName, ins.Columns, vals); inscnt++; if (progress != null) { progress.SetCurrent(SaveProgressMeasure.InsertedRows, inscnt); } if (progress != null && progress.IsCanceled) { throw new OperationCanceledError(); } } if (isIdentityInsert) { AllowIdentityInsert(table.FullName, false); } }