Example #1
0
        public void SynchronizeTable(SchemaTable schemaTable)
        {
            var smoTable = GetSmoTable(schemaTable);
            if (smoTable == null)
            {
                smoTable = new Table(GetSmoDatabase(), schemaTable.Name);
                foreach (var column in schemaTable.Columns)
                {
                    var newcol = column.GetSmoColumn(smoTable);
                    smoTable.Columns.Add(newcol);
                }
                smoTable.Create();

                // primary key
                if (!schemaTable.IsLogTable && !schemaTable.IsProvodkasTable)
                {
                    var pk = new Index(smoTable, "PK_" + schemaTable.Name);
                    var icol = new IndexedColumn(pk, schemaTable.GetPrimaryKeyColumn().Name, false);
                    pk.IndexedColumns.Add(icol);
                    pk.IndexKeyType = IndexKeyType.DriPrimaryKey;

                    pk.IsClustered = true;
                    pk.FillFactor = 50;

                    pk.Create();
                }
            }
            else
            {
                foreach (var schemaColumn in schemaTable.Columns)
                {
                    if (smoTable.Columns.Contains(schemaColumn.Name))
                    {
                        if ((schemaColumn.Table.GetPrimaryKeyColumn()==null || schemaColumn.Name != schemaColumn.Table.GetPrimaryKeyColumn().Name) &&
                            !(schemaColumn.DataType is TimestampDataType))
                        {
                            var smoColumn = smoTable.Columns[schemaColumn.Name];
                            var newDataType = schemaColumn.DataType.GetSmoDataType();
                            if (!smoColumn.DataType.Equals(newDataType))
                                smoColumn.DataType = newDataType;
                            if (smoColumn.Nullable != !schemaColumn.IsNotNullable)
                                smoColumn.Nullable = !schemaColumn.IsNotNullable;
                        }
                    }
                    else
                    {
                        var newcol = schemaColumn.GetSmoColumn(smoTable);
                        smoTable.Columns.Add(newcol);
                    }
                }
                smoTable.Alter();
            }
        }
Example #2
0
        void EmitSaveRegistrFromTempTable(StringBuilder sql, string indent, SchemaTable registrTable)
        {
            sql.Append(indent + "INSERT " + registrTable.Get4PartsTableName() + "(");
            foreach (var col in registrTable.Columns)
                sql.Append("[" + col.Name + "],");
            sql.RemoveLastChar(1);
            sql.Append(") SELECT ");
            foreach (var col in registrTable.Columns)
                sql.Append("[" + col.Name + "],");
            sql.RemoveLastChar(1);
            sql.AppendLine(" FROM " + registrTable.GetRegistrTempTableName());

            sql.Append(indent + "INSERT " + GetProvodkasTable().Get4PartsTableName() + "(tableRecordID,provodkaRecordID,registrID)");
            sql.AppendLine(" SELECT @masterID,[" + registrTable.GetPrimaryKeyColumn().Name + "]," + registrTable.ID.AsSQL() + " FROM " + registrTable.GetRegistrTempTableName());
        }