Example #1
0
        public void CreateTable(TableInfo obj)
        {
            var tnew = obj.CloneTable(_database);

            _database.Tables.Add(tnew);
            tnew.AfterLoadLink();
        }
Example #2
0
        public void CreateTable(TableInfo table, DataScript data)
        {
            TableInfo tbl = table.CloneTable();

            AddOperation(new AlterOperation_CreateTable {
                NewObject = tbl, Data = data
            });
        }
Example #3
0
 public InMemoryTable(TableInfo table, ICdlReader reader)
 {
     Initialize();
     m_structure = table.CloneTable();
     while (reader.Read())
     {
         m_rows.Add(new ArrayDataRecord(reader));
     }
 }
Example #4
0
        public static InMemoryTable FromEnumerable <T>(TableInfo table, IEnumerable <T> rows)
            where T : ICdlRecord
        {
            var res = new InMemoryTable();

            res.m_structure = table.CloneTable();
            foreach (ICdlRecord rec in rows)
            {
                res.m_rows.Add(new ArrayDataRecord(rec));
            }
            return(res);
        }
Example #5
0
 public InMemoryTable(TableInfo table, XmlElement xml)
 {
     Initialize();
     m_structure = table.CloneTable();
     using (XmlNodeReader xr = new XmlNodeReader(xml))
     {
         foreach (var rec in CdlTool.LoadFromXml(m_structure, xr))
         {
             m_rows.Add(new ArrayDataRecord(rec));
         }
     }
 }
Example #6
0
        public CdlFileWriter(string file, TableInfo table)
        {
            var fw = new FileInfo(file).Create();

            _bw = new BinaryWriter(fw);
            var tcopy = table.CloneTable();

            tcopy.ForeignKeys.Clear();
            if (tcopy.PrimaryKey != null)
            {
                tcopy.PrimaryKey.ConstraintName = null;
            }
            string data = JsonTool.Serialize(tcopy, x => x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto);

            _bw.Write(data);
        }
Example #7
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);
        }
Example #8
0
        public virtual void CreateTable(TableInfo tableSrc)
        {
            var table = tableSrc.CloneTable();

            table.AfterLoadLink();
            Put("^create ^table %l%f ( &>&n", table.GetLinkedInfo(), table.FullName);
            bool first = true;

            _primaryKeyWrittenInCreateTable = false;
            foreach (var col in table.Columns)
            {
                if (!first)
                {
                    Put(", &n");
                }
                first = false;
                Put("%i ", col.Name);
                ColumnDefinition(col, true, true, true);
            }
            if (table.PrimaryKey != null && !_primaryKeyWrittenInCreateTable)
            {
                if (!first)
                {
                    Put(", &n");
                }
                first = false;
                if (table.PrimaryKey.ConstraintName != null)
                {
                    Put("^constraint %i", table.PrimaryKey.ConstraintName);
                }
                Put(" ^primary ^key (%,i)", table.PrimaryKey.Columns);
            }
            foreach (var cnt in table.ForeignKeys)
            {
                if (!first)
                {
                    Put(", &n");
                }
                first = false;
                CreateForeignKeyCore(cnt);
            }
            foreach (var cnt in table.Uniques)
            {
                if (!first)
                {
                    Put(", &n");
                }
                first = false;
                CreateUniqueCore(cnt);
            }
            foreach (var cnt in table.Checks)
            {
                if (!first)
                {
                    Put(", &n");
                }
                first = false;
                CreateCheckCore(cnt);
            }
            Put("&<&n)");
            EndCommand();
            foreach (var ix in table.Indexes)
            {
                CreateIndex(ix);
            }
        }
Example #9
0
        public ICdlWriter CreateWriter(TableInfo inputRowFormat, CopyTableTargetOptions options, IShellContext context, DataFormatSettings sourceDataFormat)
        {
            var connection = GetConnectionProvider(context);

            using (var conn = connection.Connect())
            {
                var db = new DatabaseInfo();
                db.LinkedInfo = LinkedInfo;
                var tbl = inputRowFormat.CloneTable(db);
                tbl.FullName = GetFullName(context);
                foreach (var col in tbl.Columns)
                {
                    col.AutoIncrement = false;
                }
                tbl.ForeignKeys.Clear();
                if (tbl.PrimaryKey != null)
                {
                    tbl.PrimaryKey.ConstraintName = null;
                }
                tbl.AfterLoadLink();

                if (IdentityColumn != null)
                {
                    var col = new ColumnInfo(tbl);
                    col.Name          = IdentityColumn;
                    col.DataType      = "int";
                    col.AutoIncrement = true;
                    col.NotNull       = true;
                    var pk = new PrimaryKeyInfo(tbl);
                    pk.Columns.Add(new ColumnReference {
                        RefColumn = col
                    });
                    pk.ConstraintName = "PK_" + tbl.Name;
                    tbl.PrimaryKey    = pk;
                    tbl.Columns.Insert(0, col);
                }

                //var sw = new StringWriter();
                var so  = new ConnectionSqlOutputStream(conn, null, connection.Factory.CreateDialect());
                var dmp = connection.Factory.CreateDumper(so, new SqlFormatProperties());
                if (DropIfExists)
                {
                    dmp.DropTable(tbl, true);
                }

                bool useExistingTable = false;
                if (UseIfExists)
                {
                    var ts = context.GetDatabaseStructure(connection.ProviderString);
                    useExistingTable = ts.FindTableLike(tbl.FullName.Schema, tbl.FullName.Name) != null;
                }

                if (!useExistingTable)
                {
                    tbl.Columns.ForEach(x => x.EnsureDataType(connection.Factory.CreateSqlTypeProvider()));
                    dmp.CreateTable(tbl);
                }
                //using (var cmd = conn.CreateCommand())
                //{
                //    cmd.CommandText = sw.ToString();
                //    cmd.ExecuteNonQuery();
                //}

                return(new TableWriter(context, connection, GetFullName(context), inputRowFormat, options, useExistingTable ? null : tbl, LinkedInfo, sourceDataFormat));
            }
        }
Example #10
0
        //public InMemoryTable(InMemoryTable oldTable, InMemoryTableOperation op)
        //{
        //    Initialize();
        //    m_structure = op.m_table.Clone();
        //    var colindexes = op.ColIndexes;
        //    foreach (var row in oldTable.Rows)
        //    {
        //        m_rows.Add(new ArrayDataRecord(row, colindexes, m_structure));
        //    }
        //}

        public InMemoryTable(TableInfo table)
        {
            Initialize();
            m_structure = table.CloneTable();
        }
Example #11
0
 public CdlTable(TableInfo structure)
 {
     m_structure    = structure.CloneTable();
     Rows           = new CdlRowCollection(this);
     m_defConvertor = new CdlValueConvertor(new DataFormatSettings());
 }
 public static TableInfo WithoutReferences(this TableInfo table)
 {
     table = table.CloneTable();
     table.ForeignKeys.Clear();
     return(table);
 }
 public static TableInfo WithoutIndexes(this TableInfo table)
 {
     table = table.CloneTable();
     table.Indexes.Clear();
     return(table);
 }