Exemplo n.º 1
0
        public virtual void RecreateTable(TableInfo oldTable, TableInfo newTable)
        {
            if (oldTable.GroupId != newTable.GroupId) throw new InternalError("DBSH-00143 Recreate is not possible: oldTable.GroupId != newTable.GroupId");
            var columnMap = GetColumnMap(oldTable, newTable);
            int id = System.Threading.Interlocked.Increment(ref _lastAlterTableId);
            string tmptable = GenerateTempTableName(id);

            // remove constraints
            //DropConstraints(oldTable.GetReferencedFrom(), DropFlags.None);
            this.DropConstraints(oldTable.Constraints);

            RenameTable(oldTable, tmptable);

            var old = oldTable.CloneTable();
            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.Name,
                   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].Name)
                            : Format("^%i ^as %i", old.Columns[srcindex].Name, newTable.Columns[dstindex].Name)),
                   old.FullName);
            if (hasident) AllowIdentityInsert(newTable.FullName, false);

            // newTable.Constraints are allready created
            //CreateConstraints(newTable.GetReferencedFrom());

            PutCmd("^drop ^table %i", tmptable);
        }
Exemplo n.º 2
0
        public void UpdateData(TableInfo table, SingleTableDataScript script, LinkedDatabaseInfo linkedInfo)
        {
            if (script == null) return;
            int delcnt = 0, inscnt = 0, updrows = 0, updflds = 0;

            string linkedInfoStr = linkedInfo != null ? linkedInfo.ToString() : "";

            foreach (var del in script.Deletes)
            {
                Put("^delete ^from %s%f", linkedInfoStr, table.FullName);
                Where(table.FullName, del.CondCols, del.CondValues);
                Put(";&n");
                delcnt++;
            }
            foreach (var upd in script.Updates)
            {
                Put("^update %s%f ^set ", linkedInfoStr, 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]].CommonType));
                }
                Where(table.FullName, upd.CondCols, upd.CondValues);
                Put(";&n");
                updrows++;
                updflds += upd.Values.Length;
            }
            ColumnInfo autoinc = null;
            if (table != null) autoinc = table.FindAutoIncrementColumn();
            bool isIdentityInsert = false;
            foreach (var ins in script.Inserts)
            {
                if (autoinc != null)
                {
                    if (Array.IndexOf(ins.Columns, autoinc.Name) >= 0)
                    {
                        if (!isIdentityInsert) AllowIdentityInsert(table.FullName, true);
                        isIdentityInsert = true;
                    }
                    else
                    {
                        if (isIdentityInsert) AllowIdentityInsert(table.FullName, false);
                        isIdentityInsert = false;
                    }
                }
                var vals = new List<ValueTypeHolder>();
                var insColumns = new List<string>();
                for (int i = 0; i < ins.Columns.Length; i++)
                {
                    var col = table.Columns[ins.Columns[i]];
                    if (col != null)
                    {
                        insColumns.Add(ins.Columns[i]);
                        vals.Add(new ValueTypeHolder(ins.Values[i], col.CommonType));
                    }
                }
                if (insColumns.Count > 0)
                {
                    Put("^insert ^into %s%f (%,i) ^values (%,v);&n", linkedInfoStr, table.FullName, insColumns, vals);
                }
                inscnt++;
            }
            if (isIdentityInsert) AllowIdentityInsert(table.FullName, false);
        }