Esempio n. 1
0
        /// <summary>
        /// Adds a column map into this table.
        /// </summary>
        /// <param name="mapping">The array containing a map to the column in
        /// the parent table that we want the column number to reference.</param>
        /// <param name="aliases"></param>
        public void SetColumnMap(int[] mapping, ObjectName[] aliases)
        {
            reverse_column_map = new int[Parent.ColumnCount];
            for (int i = 0; i < reverse_column_map.Length; ++i)
            {
                reverse_column_map[i] = -1;
            }
            column_map = mapping;

            this.aliases = aliases;

            DataTableInfo parentInfo = Parent.TableInfo;

            subsetTableInfo = new DataTableInfo(parentInfo.TableName);

            for (int i = 0; i < mapping.Length; ++i)
            {
                int            map_to  = mapping[i];
                DataColumnInfo colInfo = Parent.GetColumnInfo(map_to).Clone();
                colInfo.Name = aliases[i].Name;
                subsetTableInfo.AddColumn(colInfo);
                reverse_column_map[map_to] = i;
            }

            subsetTableInfo.IsReadOnly = true;
        }
Esempio n. 2
0
        public SubsetColumnTable(Table parent, int[] mapping, ObjectName[] aliases)
            : base(parent)
        {
            int[] reverseColumnMap = new int[Parent.TableInfo.ColumnCount];
            for (int i = 0; i < reverseColumnMap.Length; ++i)
            {
                reverseColumnMap[i] = -1;
            }

            DataTableInfo parentInfo = Parent.TableInfo;

            subsetTableInfo = new SubsetTableInfo(parentInfo.Name);

            for (int i = 0; i < mapping.Length; ++i)
            {
                int            mapTo     = mapping[i];
                DataColumnInfo colInfo   = Parent.TableInfo[mapTo];
                var            newColumn = subsetTableInfo.NewColumn(aliases[i].Name, colInfo.DataType);
                newColumn.DefaultExpression = colInfo.DefaultExpression;
                newColumn.IsNullable        = colInfo.IsNullable;

                subsetTableInfo.AddColumn(colInfo);
                reverseColumnMap[mapTo] = i;
            }

            subsetTableInfo.Setup(mapping, aliases);
            subsetTableInfo.IsReadOnly = true;
        }
Esempio n. 3
0
        public DataColumnInfo AddColumn(string name, DataType type, bool notNull)
        {
            DataColumnInfo column = AddColumn(name, type);

            column.IsNotNull = notNull;
            return(column);
        }
Esempio n. 4
0
        public DataColumnInfo AddColumn(string name, DataType type)
        {
            CheckMutable();

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            foreach (DataColumnInfo column in columns)
            {
                if (column.Name.Equals(name))
                {
                    throw new ArgumentException("Column '" + name + "' already exists in table '" + tableName + "'.");
                }
            }

            DataColumnInfo newColumn = new DataColumnInfo(this, name, type);

            columns.Add(newColumn);
            return(newColumn);
        }
Esempio n. 5
0
        ///<summary>
        ///</summary>
        ///<param name="database"></param>
        ///<param name="name"></param>
        ///<param name="fields"></param>
        public TemporaryTable(IDatabase database, String name, DataColumnInfo[] fields)
            : base(database)
        {
            tableStorage = new List<DataObject[]>();

            tableInfo = new DataTableInfo(new ObjectName(null, name));
            foreach (DataColumnInfo field in fields) {
                tableInfo.AddColumn(field.Clone());
            }
            tableInfo.IsReadOnly = true;
        }
Esempio n. 6
0
        // ---------- Static convenience methods ----------

        /// <summary>
        /// Creates a table with a single column with the given name and type.
        /// </summary>
        /// <param name="database"></param>
        /// <param name="columnName"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        internal static TemporaryTable SingleColumnTable(IDatabase database, String columnName, Type c)
        {
            DataType       ttype   = PrimitiveTypes.FromType(c);
            DataColumnInfo colInfo = new DataColumnInfo(null, columnName, ttype);
            TemporaryTable table   = new TemporaryTable(database, "single", new DataColumnInfo[] { colInfo });

            //      int type = TypeUtil.ToDbType(c);
            //      TableField[] fields =
            //                 { new TableField(columnName, type, Integer.MAX_VALUE, false) };
            //      TemporaryTable table = new TemporaryTable(database, "single", fields);
            return(table);
        }
Esempio n. 7
0
        public DataColumnInfo Clone()
        {
            DataColumnInfo clone = new DataColumnInfo(tableInfo, (string)name.Clone(), type);

            clone.notNull = notNull;
            if (!String.IsNullOrEmpty(defaultExpressionString))
            {
                clone.defaultExpressionString = (string)defaultExpressionString.Clone();
            }
            clone.indexType          = (string)indexType.Clone();
            clone.baseTypeConstraint = (string)baseTypeConstraint.Clone();
            return(clone);
        }
Esempio n. 8
0
        ///<summary>
        ///</summary>
        ///<param name="database"></param>
        ///<param name="name"></param>
        ///<param name="fields"></param>
        public TemporaryTable(IDatabase database, String name, DataColumnInfo[] fields)
            : base(database)
        {
            tableStorage = new List<DataObject[]>();

            tableInfo = new DataTableInfo(new ObjectName(name));

            foreach (DataColumnInfo field in fields) {
                var newColumn = tableInfo.NewColumn(field.Name, field.DataType);
                newColumn.DefaultExpression = field.DefaultExpression;
                newColumn.IsNullable = field.IsNullable;
                tableInfo.AddColumn(newColumn);
            }
            tableInfo.IsReadOnly = true;
        }
Esempio n. 9
0
        /// <inheritdoc/>
        public override int FindFieldName(ObjectName v)
        {
            // Check this is the correct table first...
            ObjectName    tableName = v.Parent;
            DataTableInfo tableInfo = TableInfo;

            if (tableName != null && tableName.Equals(TableName))
            {
                // Look for the column name
                string colName = v.Name;
                int    size    = ColumnCount;
                for (int i = 0; i < size; ++i)
                {
                    DataColumnInfo col = tableInfo[i];
                    if (col.Name.Equals(colName))
                    {
                        return(i);
                    }
                }
            }
            return(-1);
        }
Esempio n. 10
0
        public static ITable OrderBy(this ITable table, int column, bool ascending)
        {
            // Check the field can be sorted
            DataColumnInfo colInfo = table.TableInfo[column];

            List <long> rows = new List <long>(table.SelectAll(column));

            // Reverse the list if we are not ascending
            if (ascending == false)
            {
                rows.Reverse();
            }

            // We now has an int[] array of rows from this table to make into a
            // new table.

            VirtualTable vTable = new VirtualTable((Table)table);

            vTable.Set((Table)table, rows);

            return(table);
        }
Esempio n. 11
0
        protected virtual void Init(DataTableInfo[] tableInfos)
        {
            // Generate look up tables for column_table and column_filter information
            int colCount = 0;

            for (int i = 0; i < tableInfos.Length; i++)
            {
                colCount += tableInfos[i].ColumnCount;
            }

            columnTable  = new int[colCount];
            columnFilter = new int[colCount];
            int index = 0;

            for (int i = 0; i < tableInfos.Length; ++i)
            {
                DataTableInfo curTableInfo = tableInfos[i];
                int           refColCount  = curTableInfo.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 sourceColumn = curTableInfo[n];
                    var newColumn    = new DataColumnInfo(this, sourceColumn.Name, sourceColumn.DataType);
                    newColumn.DefaultExpression = sourceColumn.DefaultExpression;
                    newColumn.IsNullable        = sourceColumn.IsNullable;
                    AddColumn(newColumn);
                }
            }

            IsReadOnly = true;
        }
Esempio n. 12
0
 internal void AddColumn(DataColumnInfo column)
 {
     CheckMutable();
     column.TableInfo = this;
     columns.Add(column);
 }
Esempio n. 13
0
 internal void AddColumn(DataColumnInfo column)
 {
     CheckMutable();
     column.TableInfo = this;
     columns.Add(column);
 }
Esempio n. 14
0
        protected virtual void Init(DataTableInfo[] tableInfos)
        {
            // Generate look up tables for column_table and column_filter information
            int colCount = 0;
            for (int i = 0; i < tableInfos.Length; i++) {
                colCount += tableInfos[i].ColumnCount;
            }

            columnTable = new int[colCount];
            columnFilter = new int[colCount];
            int index = 0;
            for (int i = 0; i < tableInfos.Length; ++i) {
                DataTableInfo curTableInfo = tableInfos[i];
                int refColCount = curTableInfo.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 sourceColumn = curTableInfo[n];
                    var newColumn = new DataColumnInfo(this, sourceColumn.Name, sourceColumn.DataType);
                    newColumn.DefaultExpression = sourceColumn.DefaultExpression;
                    newColumn.IsNullable = sourceColumn.IsNullable;
                    AddColumn(newColumn);
                }
            }

            IsReadOnly = true;
        }
Esempio n. 15
0
 public DataColumnInfo Clone()
 {
     DataColumnInfo clone = new DataColumnInfo(tableInfo, (string)name.Clone(), type);
     clone.notNull = notNull;
     if (!String.IsNullOrEmpty(defaultExpressionString)) {
         clone.defaultExpressionString = (string) defaultExpressionString.Clone();
     }
     clone.indexType = (string)indexType.Clone();
     clone.baseTypeConstraint = (string)baseTypeConstraint.Clone();
     return clone;
 }
Esempio n. 16
0
        public DataColumnInfo AddColumn(string name, DataType type)
        {
            CheckMutable();

            if (name == null)
                throw new ArgumentNullException("name");
            if (type == null)
                throw new ArgumentNullException("type");

            foreach (DataColumnInfo column in columns) {
                if (column.Name.Equals(name))
                    throw new ArgumentException("Column '" + name + "' already exists in table '" + tableName + "'.");
            }

            DataColumnInfo newColumn = new DataColumnInfo(this, name, type);
            columns.Add(newColumn);
            return newColumn;
        }
Esempio n. 17
0
        // ---------- Static convenience methods ----------
        /// <summary>
        /// Creates a table with a single column with the given name and type.
        /// </summary>
        /// <param name="database"></param>
        /// <param name="columnName"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        internal static TemporaryTable SingleColumnTable(IDatabase database, String columnName, Type c)
        {
            DataType ttype = PrimitiveTypes.FromType(c);
            DataColumnInfo colInfo = new DataColumnInfo(null, columnName, ttype);
            TemporaryTable table = new TemporaryTable(database, "single", new DataColumnInfo[] { colInfo });

            //      int type = TypeUtil.ToDbType(c);
            //      TableField[] fields =
            //                 { new TableField(columnName, type, Integer.MAX_VALUE, false) };
            //      TemporaryTable table = new TemporaryTable(database, "single", fields);
            return table;
        }
Esempio n. 18
0
        public static ITable All(this ITable table, IQueryContext context, Expression expression, Operator op, ITable other)
        {
            // Check the table only has 1 column
            if (other.TableInfo.ColumnCount != 1)
            {
                throw new ApplicationException("Input table <> 1 columns.");
            }

            // Handle trivial case of no entries to select from
            if (table.RowCount == 0)
            {
                return(table);
            }

            var theTable = table as Table;

            if (theTable == null)
            {
                return(table);
            }

            // If 'table' is empty then we return the complete set.  ALL { empty set }
            // is always true.
            if (other.RowCount == 0)
            {
                return(table);
            }

            // Is the lhs expression a constant?
            if (expression.IsConstant())
            {
                // We know lhs is a constant so no point passing arguments,
                DataObject value = expression.Evaluate(context);
                bool       comparedToTrue;

                // The various operators
                if (op.IsEquivalent(Operator.Greater) ||
                    op.IsEquivalent(Operator.GreaterOrEqual))
                {
                    // Find the maximum value in the table
                    DataObject cell = other.GetLastCell(0);
                    comparedToTrue = CompareCells(value, cell, op);
                }
                else if (op.IsEquivalent(Operator.Smaller) ||
                         op.IsEquivalent(Operator.SmallerOrEqual))
                {
                    // Find the minimum value in the table
                    DataObject cell = other.GetFirstCell(0);
                    comparedToTrue = CompareCells(value, cell, op);
                }
                else if (op.IsEquivalent(Operator.Equal))
                {
                    // Only true if rhs is a single value
                    DataObject cell = other.GetSingleCell(0);
                    comparedToTrue = (cell != null && CompareCells(value, cell, op));
                }
                else if (op.IsEquivalent(Operator.NotEqual))
                {
                    // true only if lhs_cell is not found in column.
                    comparedToTrue = !other.ColumnContainsValue(0, value);
                }
                else
                {
                    throw new ApplicationException("Don't understand operator '" + op + "' in ALL.");
                }

                // If matched return this table
                if (comparedToTrue)
                {
                    return(table);
                }

                // No entries matches so return an empty table.
                return(table.EmptySelect());
            }

            Table sourceTable;
            int   colIndex;
            // Is the lhs expression a single variable?
            ObjectName expVar = expression.AsVariable();

            // NOTE: It'll be less common for this part to be called.
            if (expVar == null)
            {
                // This is a complex expression so make a FunctionTable as our new
                // source.
                DatabaseQueryContext dbContext = (DatabaseQueryContext)context;
                FunctionTable        funTable  = new FunctionTable((Table)table, new[] { expression }, new [] { "1" }, dbContext);
                sourceTable = funTable;
                colIndex    = 0;
            }
            else
            {
                // The expression is an easy to resolve reference in this table.
                sourceTable = (Table)table;

                colIndex = sourceTable.FindFieldName(expVar);
                if (colIndex == -1)
                {
                    throw new ApplicationException("Can't find column '" + expVar + "'.");
                }
            }

            // Check that the first column of 'table' is of a compatible type with
            // source table column (lhs_col_index).
            // ISSUE: Should we convert to the correct type via a FunctionTable?
            DataColumnInfo sourceCol = sourceTable.TableInfo[colIndex];
            DataColumnInfo destCol   = other.TableInfo[0];

            if (!sourceCol.DataType.IsComparable(destCol.DataType))
            {
                throw new ApplicationException("The type of the sub-query expression " +
                                               sourceCol.DataType + " is incompatible " +
                                               "with the sub-query " + destCol.DataType +
                                               ".");
            }

            // We now have all the information to solve this query.
            // We work output as follows:
            //   For >, >= type ALL we find the highest value in 'table' and
            //   select from 'source' all the rows that are >, >= than the
            //   highest value.
            //   For <, <= type ALL we find the lowest value in 'table' and
            //   select from 'source' all the rows that are <, <= than the
            //   lowest value.
            //   For = type ALL we see if 'table' contains a single value.  If it
            //   does we select all from 'source' that equals the value, otherwise an
            //   empty table.
            //   For <> type ALL we use the 'not in' algorithm.

            IList <long> selectList;

            if (op.IsEquivalent(Operator.Greater) ||
                op.IsEquivalent(Operator.GreaterOrEqual))
            {
                // Select the last from the set (the highest value),
                DataObject highestCell = other.GetLastCell(0);
                // Select from the source table all rows that are > or >= to the
                // highest cell,
                selectList = sourceTable.SelectRows(colIndex, op, highestCell).ToList();
            }
            else if (op.IsEquivalent(Operator.Smaller) ||
                     op.IsEquivalent(Operator.SmallerOrEqual))
            {
                // Select the first from the set (the lowest value),
                DataObject lowestCell = other.GetFirstCell(0);
                // Select from the source table all rows that are < or <= to the
                // lowest cell,
                selectList = sourceTable.SelectRows(colIndex, op, lowestCell).ToList();
            }
            else if (op.IsEquivalent(Operator.Equal))
            {
                // Select the single value from the set (if there is one).
                DataObject singleCell = other.GetSingleCell(0);
                if (singleCell != null)
                {
                    // Select all from source_table all values that = this cell
                    selectList = sourceTable.SelectRows(colIndex, op, singleCell).ToList();
                }
                else
                {
                    // No single value so return empty set (no value in LHS will equal
                    // a value in RHS).
                    return(table.EmptySelect());
                }
            }
            else if (op.IsEquivalent(Operator.NotEqual))
            {
                // Equiv. to NOT IN
                selectList = sourceTable.NotIn(other, colIndex, 0).ToList();
            }
            else
            {
                throw new ApplicationException("Don't understand operator '" + op + "' in ALL.");
            }

            // Make into a table to return.
            VirtualTable rtable = new VirtualTable(theTable);

            rtable.Set((Table)table, selectList);
            return(rtable);
        }
Esempio n. 19
0
        public static ITable Any(this ITable theTable, IQueryContext context, Expression expression, Operator op, Table rightTable)
        {
            ITable table = rightTable;

            // Check the table only has 1 column
            if (table.TableInfo.ColumnCount != 1)
            {
                throw new ApplicationException("Input table <> 1 columns.");
            }

            // Handle trivial case of no entries to select from
            if (theTable.RowCount == 0)
            {
                return(theTable);
            }

            // If 'table' is empty then we return an empty set.  ANY { empty set } is
            // always false.
            if (table.RowCount == 0)
            {
                return(theTable.EmptySelect());
            }

            // Is the lhs expression a constant?
            if (expression.IsConstant())
            {
                // We know lhs is a constant so no point passing arguments,
                DataObject value = expression.Evaluate(null, context);
                // Select from the table.
                IEnumerable <long> list = table.SelectRows(0, op, value);
                if (list.Any())
                {
                    // There's some entries so return the whole table,
                    return(theTable);
                }

                // No entries matches so return an empty table.
                return(theTable.EmptySelect());
            }

            Table sourceTable;
            int   lhsColIndex;
            // Is the lhs expression a single variable?
            ObjectName expVar = expression.AsVariable();

            // NOTE: It'll be less common for this part to be called.
            if (expVar == null)
            {
                // This is a complex expression so make a FunctionTable as our new
                // source.
                FunctionTable funTable = new FunctionTable((Table)theTable, new Expression[] { expression }, new String[] { "1" }, context);
                sourceTable = funTable;
                lhsColIndex = 0;
            }
            else
            {
                // The expression is an easy to resolve reference in this table.
                sourceTable = (Table)theTable;
                lhsColIndex = sourceTable.FindFieldName(expVar);
                if (lhsColIndex == -1)
                {
                    throw new ApplicationException("Can't find column '" + expVar + "'.");
                }
            }

            // Check that the first column of 'table' is of a compatible type with
            // source table column (lhs_col_index).
            // ISSUE: Should we convert to the correct type via a FunctionTable?
            DataColumnInfo sourceCol = sourceTable.TableInfo[lhsColIndex];
            DataColumnInfo destCol   = table.TableInfo[0];

            if (!sourceCol.DataType.IsComparable(destCol.DataType))
            {
                throw new ApplicationException("The type of the sub-query expression " +
                                               sourceCol.DataType + " is incompatible " +
                                               "with the sub-query " + destCol.DataType +
                                               ".");
            }

            // We now have all the information to solve this query.
            // We work output as follows:
            //   For >, >= type ANY we find the lowest value in 'table' and
            //   select from 'source' all the rows that are >, >= than the
            //   lowest value.
            //   For <, <= type ANY we find the highest value in 'table' and
            //   select from 'source' all the rows that are <, <= than the
            //   highest value.
            //   For = type ANY we use same method from INHelper.
            //   For <> type ANY we iterate through 'source' only including those
            //   rows that a <> query on 'table' returns size() != 0.

            IList <long> selectRows;

            if (op.IsEquivalent(Operator.Greater) ||
                op.IsEquivalent(Operator.GreaterOrEqual))
            {
                // Select the first from the set (the lowest value),
                DataObject lowestCell = table.GetFirstCell(0);
                // Select from the source table all rows that are > or >= to the
                // lowest cell,
                selectRows = sourceTable.SelectRows(lhsColIndex, op, lowestCell).ToList();
            }
            else if (op.IsEquivalent(Operator.Smaller) ||
                     op.IsEquivalent(Operator.SmallerOrEqual))
            {
                // Select the last from the set (the highest value),
                DataObject highestCell = table.GetLastCell(0);
                // Select from the source table all rows that are < or <= to the
                // highest cell,
                selectRows = sourceTable.SelectRows(lhsColIndex, op, highestCell).ToList();
            }
            else if (op.IsEquivalent(Operator.Equal))
            {
                // Equiv. to IN
                selectRows = sourceTable.In(table, lhsColIndex, 0);
            }
            else if (op.IsEquivalent(Operator.NotEqual))
            {
                // Select the value that is the same of the entire column
                DataObject cell = table.GetSingleCell(0);
                if (cell != null)
                {
                    // All values from 'source_table' that are <> than the given cell.
                    selectRows = sourceTable.SelectRows(lhsColIndex, op, cell).ToList();
                }
                else
                {
                    // No, this means there are different values in the given set so the
                    // query evaluates to the entire table.
                    return(theTable);
                }
            }
            else
            {
                throw new ApplicationException("Don't understand operator '" + op + "' in ANY.");
            }

            // Make into a table to return.
            VirtualTable rtable = new VirtualTable((Table)theTable);

            rtable.Set((Table)theTable, selectRows);

            return(rtable);
        }