protected virtual void Init(IEnumerable<ITable> tables) { var tablesArray = tables.ToArray(); referenceList = tablesArray; int colCount = ColumnCount; indexes = new ColumnIndex[colCount]; vtTableInfo = new TableInfo(new ObjectName("#VIRTUAL TABLE#")); // Generate look up tables for column_table and column_filter information columnTable = new int[colCount]; columnFilter = new int[colCount]; int index = 0; for (int i = 0; i < referenceList.Length; ++i) { var curTable = referenceList[i]; var curTableInfo = curTable.TableInfo; int refColCount = curTable.ColumnCount(); // For each column for (int n = 0; n < refColCount; ++n) { columnFilter[index] = n; columnTable[index] = i; ++index; // Add this column to the data table info of this table. var columnInfo = curTableInfo[n]; var newColumnInfo = new ColumnInfo(columnInfo.ColumnName, columnInfo.ColumnType) { DefaultExpression = columnInfo.DefaultExpression, IsNotNull = columnInfo.IsNotNull, IndexType = columnInfo.IndexType }; vtTableInfo.AddColumnSafe(newColumnInfo); } } vtTableInfo = vtTableInfo.AsReadOnly(); }
public static void Serialize(ColumnInfo columnInfo, BinaryWriter writer) { writer.Write(3); // Version writer.Write(columnInfo.ColumnName); TypeSerializer.SerializeTo(writer, columnInfo.ColumnType); writer.Write(columnInfo.IsNotNull ? (byte)1 : (byte)0); if (columnInfo.DefaultExpression != null) { writer.Write((byte)1); SqlExpression.Serialize(columnInfo.DefaultExpression, writer); } else { writer.Write((byte)0); } }
protected override void ExecuteStatement(ExecutionContext context) { //if (!context.User.CanAlterTable(TableName)) // throw new InvalidAccessException(context.Request.UserName(), TableName); var table = context.Request.Access().GetTable(TableName); if (table == null) throw new ObjectNotFoundException(TableName); var tableInfo = table.TableInfo; var newTableInfo = new TableInfo(tableInfo.TableName); var checker = ColumnChecker.Default(context.Request, TableName); bool tableAltered = false; bool markDropped = false; for (int n = 0; n < tableInfo.ColumnCount; ++n) { var column = tableInfo[n]; string columnName = column.ColumnName; var columnType = column.ColumnType; var defaultExpression = column.DefaultExpression; if (Action.ActionType == AlterTableActionType.SetDefault && CheckColumnNamesMatch(context.Request, ((SetDefaultAction)Action).ColumnName, columnName)) { var exp = ((SetDefaultAction)Action).DefaultExpression; exp = checker.CheckExpression(exp); defaultExpression = exp; tableAltered = true; } else if (Action.ActionType == AlterTableActionType.DropDefault && CheckColumnNamesMatch(context.Request, ((DropDefaultAction)Action).ColumnName, columnName)) { defaultExpression = null; tableAltered = true; } else if (Action.ActionType == AlterTableActionType.DropColumn && CheckColumnNamesMatch(context.Request, ((DropColumnAction)Action).ColumnName, columnName)) { // Check there are no referential links to this column var refs = context.Request.Access().QueryTableImportedForeignKeys(TableName); foreach (var reference in refs) { CheckColumnConstraint(columnName, reference.ForeignColumnNames, reference.ForeignTable, reference.ConstraintName); } // Or from it refs = context.Request.Access().QueryTableForeignKeys(TableName); foreach (var reference in refs) { CheckColumnConstraint(columnName, reference.ColumnNames, reference.TableName, reference.ConstraintName); } // Or that it's part of a primary key var primaryKey = context.Request.Access().QueryTablePrimaryKey(TableName); if (primaryKey != null) CheckColumnConstraint(columnName, primaryKey.ColumnNames, TableName, primaryKey.ConstraintName); // Or that it's part of a unique set var uniques = context.Request.Access().QueryTableUniqueKeys(TableName); foreach (var unique in uniques) { CheckColumnConstraint(columnName, unique.ColumnNames, TableName, unique.ConstraintName); } markDropped = true; tableAltered = true; } var newColumn = new ColumnInfo(columnName, columnType); if (defaultExpression != null) newColumn.DefaultExpression = defaultExpression; newColumn.IndexType = column.IndexType; newColumn.IsNotNull = column.IsNotNull; // If not dropped then add to the new table definition. if (!markDropped) { newTableInfo.AddColumn(newColumn); } } if (Action.ActionType == AlterTableActionType.AddColumn) { var col = ((AddColumnAction)Action).Column; checker.CheckExpression(col.DefaultExpression); var columnName = col.ColumnName; var columnType = col.ColumnType; // If column name starts with [table_name]. then strip it off columnName = checker.StripTableName(TableName.Name, columnName); if (tableInfo.IndexOfColumn(col.ColumnName) != -1) throw new InvalidOperationException("The column '" + col.ColumnName + "' is already in the table '" + tableInfo.TableName + "'."); var newColumn = new ColumnInfo(columnName, columnType) { IsNotNull = col.IsNotNull, DefaultExpression = col.DefaultExpression }; newTableInfo.AddColumn(newColumn); tableAltered = true; } if (Action.ActionType == AlterTableActionType.DropConstraint) { string constraintName = ((DropConstraintAction)Action).ConstraintName; int dropCount = context.Request.Access().DropTableConstraint(TableName, constraintName); if (dropCount == 0) throw new InvalidOperationException("Named constraint to drop on table " + TableName + " was not found: " + constraintName); } else if (Action.ActionType == AlterTableActionType.DropPrimaryKey) { if (!context.Request.Access().DropTablePrimaryKey(TableName, null)) throw new InvalidOperationException("No primary key to delete on table " + TableName); } if (Action.ActionType == AlterTableActionType.AddConstraint) { var constraint = ((AddConstraintAction)Action).Constraint; bool foreignConstraint = (constraint.ConstraintType == ConstraintType.ForeignKey); ObjectName refTname = null; if (foreignConstraint) { refTname = context.Request.Access().ResolveTableName(constraint.ReferenceTable); } var columnNames = checker.StripColumnList(TableName.FullName, constraint.Columns); columnNames = checker.StripColumnList(constraint.ReferenceTable, columnNames); var expression = checker.CheckExpression(constraint.CheckExpression); columnNames = checker.CheckColumns(columnNames); IEnumerable<string> refCols = null; if (foreignConstraint && constraint.ReferenceColumns != null) { var referencedChecker = ColumnChecker.Default(context.Request, refTname); refCols = referencedChecker.CheckColumns(constraint.ReferenceColumns); } var newConstraint = new ConstraintInfo(constraint.ConstraintName, constraint.ConstraintType, TableName, columnNames.ToArray()); if (foreignConstraint) { if (refCols == null) throw new InvalidOperationException("Could not create a Foreign Key constraint with no reference columns"); newConstraint.ForeignTable = refTname; newConstraint.ForeignColumnNames = refCols.ToArray(); newConstraint.OnDelete = constraint.OnDelete; newConstraint.OnUpdate = constraint.OnUpdate; } if (constraint.ConstraintType == ConstraintType.Check) newConstraint.CheckExpression = expression; context.Request.Access().AddConstraint(TableName, newConstraint); } // Alter the existing table to the new format... if (tableAltered) { if (newTableInfo.ColumnCount == 0) throw new InvalidOperationException("Can not ALTER table to have 0 columns."); context.DirectAccess.AlterObject(newTableInfo); } else { // If the table wasn't physically altered, check the constraints. // Calling this method will also make the transaction check all // deferred constraints during the next commit. context.Request.Access().CheckConstraintViolations(TableName); } }
public static ColumnInfo Deserialize(BinaryReader reader, ITypeResolver typeResolver) { var version = reader.ReadInt32(); if (version != 3) throw new FormatException("Invalid version of the Column-Info"); var columnName = reader.ReadString(); var columnType = TypeSerializer.Deserialize(reader, typeResolver); var notNull = reader.ReadByte() == 1; var columnInfo = new ColumnInfo(columnName, columnType); columnInfo.IsNotNull = notNull; var hasDefault = reader.ReadByte() == 1; if (hasDefault) columnInfo.DefaultExpression = SqlExpression.Deserialize(reader); return columnInfo; }
private void SetColumnMap(int[] mapping) { reverseColumnMap = new int[Parent.ColumnCount()]; for (int i = 0; i < reverseColumnMap.Length; ++i) { reverseColumnMap[i] = -1; } var parentInfo = Parent.TableInfo; subsetTableInfo = new TableInfo(parentInfo.TableName); for (int i = 0; i < mapping.Length; ++i) { int mapTo = mapping[i]; var origColumnInfo = Parent.TableInfo[mapTo]; var columnInfo = new ColumnInfo(aliases[i].Name, origColumnInfo.ColumnType) { DefaultExpression = origColumnInfo.DefaultExpression, IsNotNull = origColumnInfo.IsNotNull, IndexType = origColumnInfo.IndexType }; subsetTableInfo.AddColumnSafe(columnInfo); reverseColumnMap[mapTo] = i; } subsetTableInfo = subsetTableInfo.AsReadOnly(); }
public RangeSetUpdater(IRequest context, ColumnInfo field, IndexRangeSet indexRangeSet) { this.context = context; this.field = field; this.indexRangeSet = indexRangeSet; }
public RangeSetCalculator(IRequest context, ColumnInfo field, IndexRangeSet rangeSet) { this.context = context; this.field = field; this.rangeSet = rangeSet; }
internal QueryResultColumn(string name, ColumnInfo columnInfo) : this(name, columnInfo.ColumnType, columnInfo.Offset, columnInfo.IsNotNull) { }