Exemple #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
            if (_dumperCaps.DropConstraint)
            {
                this.DropConstraints(oldTable.GetReferences());
                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);
            }

            if (_dumperCaps.DropConstraint)
            {
                // newTable.Constraints are allready created
                this.CreateConstraints(newTable.GetReferences());
            }

            DropRecreatedTempTable(tmptable);
        }