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 static TemporaryTable SingleColumnTable(IContext database, string columnName, SqlType columnType) { var tableInfo = new TableInfo(new ObjectName("single")); tableInfo.AddColumn(columnName, columnType); tableInfo = tableInfo.AsReadOnly(); return(new TemporaryTable(database, tableInfo)); }
public static void MakeReadOnly() { var tableName = new ObjectName("tab1"); var tableInfo = new TableInfo(tableName); tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt())); tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22))); tableInfo = tableInfo.AsReadOnly(); Assert.True(tableInfo.Columns.IsReadOnly); }
public static void MutateReadOnlyTableInfo() { var tableName = new ObjectName("tab1"); var tableInfo = new TableInfo(tableName); tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt())); tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22))); tableInfo = tableInfo.AsReadOnly(); Assert.Throws <InvalidOperationException>(() => tableInfo.Columns.RemoveAt(0)); Assert.Throws <InvalidOperationException>(() => tableInfo.Columns.Add(new ColumnInfo("v", PrimitiveTypes.Integer()))); }
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 void CreateTable(TableInfo tableInfo, bool temporary) { var tableName = tableInfo.TableName; if (visibleTables.ContainsKey(tableName)) { throw new InvalidOperationException(String.Format("Table '{0}' already exists.", tableName)); } tableInfo = tableInfo.AsReadOnly(); var source = Composite.CreateTableSource(tableInfo, temporary); // Add this table (and an index set) for this table. AddVisibleTable(source, source.CreateIndexSet()); int tableId = source.TableId; Transaction.OnTableCreated(tableId, tableName); Transaction.CreateNativeSequence(tableName); }
public void CreateTable(TableInfo tableInfo, bool temporary) { var tableName = tableInfo.TableName; var source = FindVisibleTable(tableName, false); if (source != null) { throw new InvalidOperationException(String.Format("Table '{0}' already exists.", tableName)); } tableInfo = tableInfo.AsReadOnly(); source = Composite.CreateTableSource(tableInfo, temporary); // Add this table (and an index set) for this table. AddVisibleTable(source, source.CreateIndexSet()); int tableId = source.TableId; Transaction.Registry.RegisterEvent(new TableCreatedEvent(tableId, tableName)); Transaction.CreateNativeSequence(tableName); }
public TemporaryTable(IContext context, TableInfo tableInfo) : base(context) { this.tableInfo = tableInfo.AsReadOnly(); rows = new List <Field[]>(); }
public TemporaryTable(TableInfo tableInfo) { TableInfo = tableInfo.AsReadOnly(); rows = new List <SqlObject[]>(); }
public FunctionTable(ITable table, SqlExpression[] functionList, string[] columnNames, IRequest queryContext) : base(queryContext.Context) { // Make sure we are synchronized over the class. lock (typeof(FunctionTable)) { uniqueId = uniqueKeySeq; ++uniqueKeySeq; } uniqueId = (uniqueId & 0x0FFFFFFF) | 0x010000000; context = queryContext; ReferenceTable = table; varResolver = table.GetVariableResolver(); varResolver = varResolver.ForRow(0); // Create a DataTableInfo object for this function table. funTableInfo = new TableInfo(FunctionTableName); expList = new SqlExpression[functionList.Length]; expInfo = new byte[functionList.Length]; // Create a new DataColumnInfo for each expression, and work out if the // expression is simple or not. for (int i = 0; i < functionList.Length; ++i) { var expr = functionList[i]; // Examine the expression and determine if it is simple or not if (expr.IsConstant() && !expr.HasAggregate(context)) { // If expression is a constant, solve it var result = expr.Evaluate(context, null); if (result.ExpressionType != SqlExpressionType.Constant) { throw new InvalidOperationException(); } expr = result; expList[i] = expr; expInfo[i] = 1; } else { // Otherwise must be dynamic expList[i] = expr; expInfo[i] = 0; } // Make the column info funTableInfo.AddColumn(columnNames[i], expr.ReturnType(context, varResolver)); } // Make sure the table info isn't changed from this point on. funTableInfo = funTableInfo.AsReadOnly(); // routine tables are the size of the referring table. rowCount = table.RowCount; // Set schemes to 'blind search'. SetupIndexes(DefaultIndexTypes.BlindSearch); }
public bool AlterTable(TableInfo tableInfo) { tableInfo = tableInfo.AsReadOnly(); var tableName = tableInfo.TableName; // The current schema context is the schema of the table name string currentSchema = tableName.Parent.Name; using (var session = new SystemSession(Transaction, currentSchema)) { using (var context = session.CreateQuery()) { // Get the next unique id of the unaltered table. var nextId = NextUniqueId(tableName); // Drop the current table var cTable = GetTable(tableName); var droppedTableId = cTable.TableInfo.Id; DropTable(tableName); // And create the table CreateTable(tableInfo); var alteredTable = GetMutableTable(tableName); ITableSource source; if (!visibleTables.TryGet(tableName, out source)) { throw new InvalidOperationException(); } int alteredTableId = source.TableId; // Set the sequence id of the table source.SetUniqueId(nextId.ToInt64()); // Work out which columns we have to copy to where int[] colMap = new int[tableInfo.ColumnCount]; var origTd = cTable.TableInfo; for (int i = 0; i < colMap.Length; ++i) { string colName = tableInfo[i].ColumnName; colMap[i] = origTd.IndexOfColumn(colName); } // First move all the rows from the old table to the new table, // This does NOT update the indexes. var e = cTable.GetEnumerator(); while (e.MoveNext()) { int rowIndex = e.Current.RowId.RowNumber; var dataRow = alteredTable.NewRow(); for (int i = 0; i < colMap.Length; ++i) { int col = colMap[i]; if (col != -1) { dataRow.SetValue(i, cTable.GetValue(rowIndex, col)); } } dataRow.SetDefault(context); // Note we use a low level 'AddRow' method on the master table // here. This does not touch the table indexes. The indexes are // built later. int newRowNumber = source.AddRow(dataRow); // Set the record as committed added source.WriteRecordState(newRowNumber, RecordState.CommittedAdded); } // TODO: We need to copy any existing index definitions that might // have been set on the table being altered. // Rebuild the indexes in the new master table, source.BuildIndexes(); // Get the snapshot index set on the new table and set it here SetIndexSetForTable(source, source.CreateIndexSet()); // Flush this out of the table cache FlushTableCache(tableName); // Ensure the native sequence generator exists... Transaction.RemoveNativeSequence(tableName); Transaction.CreateNativeSequence(tableName); // Notify that this database object has been successfully dropped and // created. Transaction.OnTableDropped(droppedTableId, tableName); Transaction.OnTableCreated(alteredTableId, tableName); return(true); } } }
public void OnTableCompositeCreate(IQuery systemQuery) { // SYSTEM.PKEY_INFO var tableInfo = new TableInfo(SystemSchema.PrimaryKeyInfoTableName); tableInfo.AddColumn("id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("name", PrimitiveTypes.String()); tableInfo.AddColumn("schema", PrimitiveTypes.String()); tableInfo.AddColumn("table", PrimitiveTypes.String()); tableInfo.AddColumn("deferred", PrimitiveTypes.Numeric()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); // SYSTEM.PKEY_COLS tableInfo = new TableInfo(SystemSchema.PrimaryKeyColumnsTableName); tableInfo.AddColumn("pk_id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("column", PrimitiveTypes.String()); tableInfo.AddColumn("seq_no", PrimitiveTypes.Numeric()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); // SYSTEM.FKEY_INFO tableInfo = new TableInfo(SystemSchema.ForeignKeyInfoTableName); tableInfo.AddColumn("id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("name", PrimitiveTypes.String()); tableInfo.AddColumn("schema", PrimitiveTypes.String()); tableInfo.AddColumn("table", PrimitiveTypes.String()); tableInfo.AddColumn("ref_schema", PrimitiveTypes.String()); tableInfo.AddColumn("ref_table", PrimitiveTypes.String()); tableInfo.AddColumn("update_rule", PrimitiveTypes.Numeric()); tableInfo.AddColumn("delete_rule", PrimitiveTypes.Numeric()); tableInfo.AddColumn("deferred", PrimitiveTypes.Numeric()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); // SYSTEM.FKEY_COLS tableInfo = new TableInfo(SystemSchema.ForeignKeyColumnsTableName); tableInfo.AddColumn("fk_id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("fcolumn", PrimitiveTypes.String()); tableInfo.AddColumn("pcolumn", PrimitiveTypes.String()); tableInfo.AddColumn("seq_no", PrimitiveTypes.Numeric()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); // SYSTEM.UNIQUE_INFO tableInfo = new TableInfo(SystemSchema.UniqueKeyInfoTableName); tableInfo.AddColumn("id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("name", PrimitiveTypes.String()); tableInfo.AddColumn("schema", PrimitiveTypes.String()); tableInfo.AddColumn("table", PrimitiveTypes.String()); tableInfo.AddColumn("deferred", PrimitiveTypes.Numeric()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); // SYSTEM.UNIQUE_COLS tableInfo = new TableInfo(SystemSchema.UniqueKeyColumnsTableName); tableInfo.AddColumn("un_id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("column", PrimitiveTypes.String()); tableInfo.AddColumn("seq_no", PrimitiveTypes.Numeric()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); // SYSTEM.CHECK_INFO tableInfo = new TableInfo(SystemSchema.CheckInfoTableName); tableInfo.AddColumn("id", PrimitiveTypes.Numeric()); tableInfo.AddColumn("name", PrimitiveTypes.String()); tableInfo.AddColumn("schema", PrimitiveTypes.String()); tableInfo.AddColumn("table", PrimitiveTypes.String()); tableInfo.AddColumn("expression", PrimitiveTypes.String()); tableInfo.AddColumn("deferred", PrimitiveTypes.Numeric()); tableInfo.AddColumn("serialized_expression", PrimitiveTypes.Binary()); tableInfo = tableInfo.AsReadOnly(); systemQuery.Access().CreateTable(tableInfo); }