public override void Process(Expressions.RenameTableExpression expression) { truncator.Truncate(expression); FirebirdSchemaProvider schema = new FirebirdSchemaProvider(this); TableDefinition tableDef = schema.GetTableDefinition(expression.OldName); tableDef.Name = expression.NewName; CreateTableExpression createNew = new CreateTableExpression() { TableName = expression.NewName, SchemaName = String.Empty }; //copy column definitions (nb: avoid to copy key names, because in firebird they must be globally unique, so let it rename) tableDef.Columns.ToList().ForEach(x => createNew.Columns.Add(new ColumnDefinition() { Name = x.Name, DefaultValue = x.DefaultValue, IsForeignKey = x.IsForeignKey, IsIdentity = x.IsIdentity, IsIndexed = x.IsIndexed, IsNullable = x.IsNullable, IsPrimaryKey = x.IsPrimaryKey, IsUnique = x.IsUnique, ModificationType = x.ModificationType, Precision = x.Precision, Size = x.Size, Type = x.Type, CustomType = x.CustomType })); Process(createNew); int columnCount = tableDef.Columns.Count; string[] columns = tableDef.Columns.Select(x => x.Name).ToArray(); InsertDataExpression data = new InsertDataExpression(); using (DataSet ds = ReadTableData(String.Empty, expression.OldName)) { foreach (DataRow dr in ds.Tables[0].Rows) { InsertionDataDefinition insert = new InsertionDataDefinition(); for (int i = 0; i < columnCount; i++) { insert.Add(new KeyValuePair <string, object>(columns[i], dr.ItemArray[i])); } data.Rows.Add(insert); } } Process(data); DeleteTableExpression delTable = new DeleteTableExpression() { TableName = expression.OldName, SchemaName = String.Empty }; Process(delTable); }
private static InsertionDataDefinition GetData(object dataAsAnonymousType) { var data = new InsertionDataDefinition(); var properties = TypeDescriptor.GetProperties(dataAsAnonymousType); foreach (PropertyDescriptor property in properties) data.Add(new KeyValuePair<string, object>(property.Name, property.GetValue(dataAsAnonymousType))); return data; }
private static InsertionDataDefinition GetData(object dataAsAnonymousType) { var data = new InsertionDataDefinition(); var properties = TypeDescriptor.GetProperties(dataAsAnonymousType); foreach (PropertyDescriptor property in properties) { data.Add(new KeyValuePair <string, object>(property.Name, property.GetValue(dataAsAnonymousType))); } return(data); }
public void Truncate(InsertDataExpression expression) { expression.TableName = Truncate(expression.TableName); List <InsertionDataDefinition> insertions = new List <InsertionDataDefinition>(); foreach (InsertionDataDefinition insertion in expression.Rows) { InsertionDataDefinition newInsertion = new InsertionDataDefinition(); foreach (var data in insertion) { newInsertion.Add(new KeyValuePair <string, object>(Truncate(data.Key), data.Value)); } insertions.Add(newInsertion); } expression.Rows.Clear(); expression.Rows.AddRange(insertions); }
protected void SetupUndoDeleteData(DeleteDataExpression expression) { CanUndo = true; FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor); FirebirdTableSchema table = schema.GetTableSchema(expression.TableName); using (IDataReader ds = Processor.ReadTableData(String.Empty, expression.TableName)) { foreach (DeletionDataDefinition deletion in expression.Rows) { InsertDataExpression insert = new InsertDataExpression() { SchemaName = String.Empty, TableName = expression.TableName }; while (ds.Read()) { bool match = true; if (!expression.IsAllRows) { foreach (var where in deletion) { if (ds[where.Key].ToString() != where.Value.ToString()) { match = false; break; } } } if (match) { InsertionDataDefinition insertion = new InsertionDataDefinition(); foreach (ColumnDefinition colDef in table.Definition.Columns) { insertion.Add(new KeyValuePair <string, object>(colDef.Name, ds[colDef.Name])); } insert.Rows.Add(insertion); } } UndoExpressions.Add(insert); } } }
public void Truncate(InsertDataExpression expression) { expression.TableName = Truncate(expression.TableName); List<InsertionDataDefinition> insertions = new List<InsertionDataDefinition>(); foreach (InsertionDataDefinition insertion in expression.Rows) { InsertionDataDefinition newInsertion = new InsertionDataDefinition(); foreach (var data in insertion) { newInsertion.Add(new KeyValuePair<string, object>(Truncate(data.Key), data.Value)); } insertions.Add(newInsertion); } expression.Rows.Clear(); expression.Rows.AddRange(insertions); }
protected virtual InsertionDataDefinition CreateVersionInfoInsertionData(long version, Type type, TimeSpan elapsedTime) { var insertData = new InsertionDataDefinition { new KeyValuePair<string, object>( VersionTableMetaData.ColumnName, version ) }; if ((Processor.Options.StoreExtendedData) && (VersionTableMetaData is IExtendedVersionTableMetadata)) { var metadata = Conventions.GetMetadataForMigration(type); insertData.Add(new KeyValuePair<string, object>(((IExtendedVersionTableMetadata)VersionTableMetaData).DescriptionColumnName, metadata.Description)); insertData.Add(new KeyValuePair<string, object>(((IExtendedVersionTableMetadata)VersionTableMetaData).DateAppliedColumnName, DateTime.UtcNow)); insertData.Add(new KeyValuePair<string, object>(((IExtendedVersionTableMetadata)VersionTableMetaData).ElapsedTimeColumnName, elapsedTime.TotalMilliseconds)); } return insertData; }
protected void SetupUndoDeleteTable(DeleteTableExpression expression) { CanUndo = true; FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor); //Table and columns CreateTableExpression table = new CreateTableExpression() { TableName = expression.TableName, SchemaName = expression.SchemaName, }; TableDefinition tableDef = schema.GetTableDefinition(expression.TableName); foreach (ColumnDefinition colDef in tableDef.Columns) { table.Columns.Add(colDef); } UndoExpressions.Add(table); //Indices foreach (IndexDefinition indexDef in tableDef.Indexes) { CreateIndexExpression indexExpression = new CreateIndexExpression() { Index = indexDef }; UndoExpressions.Add(indexExpression); } //Foreign keys foreach (ForeignKeyDefinition fkDef in tableDef.ForeignKeys) { CreateForeignKeyExpression fkExpression = new CreateForeignKeyExpression() { ForeignKey = fkDef }; UndoExpressions.Add(fkExpression); } //Data InsertDataExpression values = new InsertDataExpression() { TableName = expression.TableName, SchemaName = expression.SchemaName }; using (IDataReader data = Processor.ReadTableData(String.Empty, expression.TableName)) { int columnCount = data.FieldCount; while (data.Read()) { InsertionDataDefinition insert = new InsertionDataDefinition(); for (int i = 0; i < columnCount; i++) { insert.Add(new KeyValuePair <string, object>(data.GetName(i), data.GetValue(i))); } values.Rows.Add(insert); } } UndoExpressions.Add(values); //Triggers FirebirdTableSchema tableSchema = schema.GetTableSchema(expression.TableName); foreach (TriggerInfo trigger in tableSchema.Triggers) { PerformDBOperationExpression createTrigger = Processor.CreateTriggerExpression(expression.TableName, trigger); UndoExpressions.Add(createTrigger); } }