Exemplo n.º 1
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;
        }
Exemplo n.º 2
0
        internal override SelectableScheme GetSelectableSchemeFor(int column, int originalColumn, Table table)
        {
            if (columnScheme == null) {
                columnScheme = new SelectableScheme[Parent.TableInfo.ColumnCount];
            }

            // Is there a local scheme available?
            SelectableScheme scheme = columnScheme[column];
            if (scheme == null) {
                // If we are asking for the selectable schema of this table we must
                // tell the parent we are looking for its selectable scheme.
                Table t = table;
                if (table == this) {
                    t = Parent;
                }

                // Scheme is not cached in this table so ask the parent.
                scheme = Parent.GetSelectableSchemeFor(column, originalColumn, t);
                if (table == this) {
                    columnScheme[column] = scheme;
                }
            } else {
                // If this has a cached scheme and we are in the correct domain then
                // return it.
                if (table == this) {
                    return scheme;
                } else {
                    // Otherwise we must calculate the subset of the scheme
                    return scheme.GetSubsetScheme(table, originalColumn);
                }
            }
            return scheme;
        }
Exemplo n.º 3
0
        internal ReferenceTable(Table table, DataTableInfo info)
            : base(table)
        {
            table_name = info.TableName;

            modifiedTableInfo = info;
        }
Exemplo n.º 4
0
 public ReferenceTable(Table parent, ObjectName name)
     : base(parent)
 {
     tableInfo = new DataTableInfo(name);
     parent.TableInfo.CopyColumnsTo(tableInfo);
     tableInfo.IsReadOnly = true;
 }
Exemplo n.º 5
0
        internal ReferenceTable(Table table, ObjectName tname)
            : base(table)
        {
            table_name = tname;

            // Create a modified table info based on the parent info.
            modifiedTableInfo = table.TableInfo.Clone(tname);
            modifiedTableInfo.IsReadOnly = true;
        }
Exemplo n.º 6
0
 public Table[] GetTables()
 {
     int size = rawInfo.Count;
     Table[] list = new Table[size];
     for (int i = 0; i < size; ++i) {
         list[i] = (Table) rawInfo[i].Table;
     }
     return list;
 }
Exemplo n.º 7
0
 internal void Set(Table[] tables, IEnumerable<long>[] rows)
 {
     for (int i = 0; i < tables.Length; ++i) {
         rowList[i] = new List<long>(rows[i]);
     }
     if (rows.Length > 0) {
         rowCount = rowList[0].Count;
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Constructs the result set.
        /// </summary>
        /// <param name="query"></param>
        /// <param name="result"></param>
        public QueryResult(SqlQuery query, Table result)
        {
            this.query = query;
            this.result = result;
            streamableBlobMap = new Dictionary<long, StreamableObject>();

            resultRowCount = result.RowCount;

            // HACK: Read the contents of the first row so that we can pick up
            //   any errors with reading, and also to fix the 'uniquekey' bug
            //   that causes a new transaction to be started if 'uniquekey' is
            //   a column and the value is resolved later.
            IRowEnumerator rowEnum = result.GetRowEnumerator();
            if (rowEnum.MoveNext()) {
                int rowIndex = rowEnum.RowIndex;
                for (int c = 0; c < result.ColumnCount; ++c) {
                    result.GetCell(c, rowIndex);
                }
            }

            // If simple enum, note it here
            resultIsSimpleEnum = (rowEnum is SimpleRowEnumerator);
            rowEnum = null;

            // Build 'row_index_map' if not a simple enum
            if (!resultIsSimpleEnum) {
                rowIndexMap = new List<int>(result.RowCount);

                IRowEnumerator en = result.GetRowEnumerator();
                while (en.MoveNext()) {
                    rowIndexMap.Add(en.RowIndex);
                }
            }

            // This is a safe operation provides we are shared.
            // Copy all the TableField columns from the table to our own
            // QueryResultColumn array, naming each column by what is returned from
            // the 'GetResolvedVariable' method.
            int colCount = result.ColumnCount;
            colDesc = new QueryResultColumn[colCount];
            for (int i = 0; i < colCount; ++i) {
                VariableName v = result.GetResolvedVariable(i);
                string fieldName;
                if (v.TableName == null) {
                    // This means the column is an alias
                    fieldName = String.Format("@a{0}", v.Name);
                } else {
                    // This means the column is an schema/table/column reference
                    fieldName = String.Format("@f{0}", v);
                }

                colDesc[i] = new QueryResultColumn(fieldName, result.GetColumnInfo(i));
            }

            locked = 0;
        }
Exemplo n.º 9
0
        protected override void Init(Table[] tables)
        {
            base.Init(tables);

            int tableCount = tables.Length;
            rowList = new IList<long>[tableCount];
            for (int i = 0; i < tableCount; ++i) {
                rowList[i] = new List<long>();
            }
        }
Exemplo n.º 10
0
        public IList<int> RegexSearch(Table table, int column, string regularExpression, string expressionOps)
        {
            // Get the ordered column,
            IList<int> row_list = table.SelectAll(column);
            // The result matched rows,
            List<int> result_list = new List<int>();

            // Make into a new list that matches the pattern,
            Regex regex;

            try {
                RegexOptions options = RegexOptions.None;
                if (expressionOps != null) {
                    if (expressionOps.IndexOf('i') != -1) {
                        options |= RegexOptions.IgnoreCase;
                    }
                    if (expressionOps.IndexOf('s') != -1) {
                        options |= RegexOptions.Singleline;
                    }
                    if (expressionOps.IndexOf('m') != -1) {
                        options |= RegexOptions.Multiline;
                    }
                }

                regex = new Regex(regularExpression, options);
            } catch (Exception) {
                // Incorrect syntax means we always match to an empty list,
                return result_list;
            }

            // For each row in the column, test it against the regular expression.
            int size = row_list.Count;
            for (int i = 0; i < size; ++i) {
                int row_index = row_list[i];
                TObject cell = table.GetCell(column, row_index);
                // Only try and match against non-null cells.
                if (!cell.IsNull) {
                    Object ob = cell.Object;
                    String str = ob.ToString();
                    // If the column matches the regular expression then return it,
                    if (regex.IsMatch(str)) {
                        result_list.Add(row_index);
                    }
                }
            }

            return result_list;
        }
        ///<summary>
        ///</summary>
        ///<param name="left"></param>
        ///<param name="right"></param>
        public NaturallyJoinedTable(Table left, Table right)
        {
            Init(new Table[] { left, right });

            leftRowCount = left.RowCount;
            rightRowCount = right.RowCount;

            // Build lookup tables for the rows in the parent tables if necessary
            // (usually it's not necessary).

            // If the left or right tables are simple enumerations, we can optimize
            // our access procedure,
            leftIsSimpleEnum = (left.GetRowEnumerator() is SimpleRowEnumerator);
            rightIsSimpleEnum = (right.GetRowEnumerator() is SimpleRowEnumerator);

            leftSet = !leftIsSimpleEnum ? CreateLookupRowList(left) : null;
            rightSet = !rightIsSimpleEnum ? CreateLookupRowList(right) : null;
        }
Exemplo n.º 12
0
        internal override SelectableScheme GetSelectableSchemeFor(int column, int originalColumn, Table table)
        {
            // First check if the given SelectableScheme is in the column_scheme array
            SelectableScheme scheme = columnScheme[column];
            if (scheme != null) {
                if (table == this)
                    return scheme;

                return scheme.GetSubsetScheme(table, originalColumn);
            }

            // If it isn't then we need to calculate it
            SelectableScheme ss;

            // Optimization: The table may be naturally ordered by a column.  If it
            // is we don't try to generate an ordered set.
            if (sortedAgainstColumn != -1 &&
                sortedAgainstColumn == column) {
                InsertSearch isop = new InsertSearch(this, column, CalculateRowReferenceList().Cast<int>());
                isop.RecordUid = false;
                ss = isop;
                columnScheme[column] = ss;
                if (table != this) {
                    ss = ss.GetSubsetScheme(table, originalColumn);
                }

            } else {
                // Otherwise we must generate the ordered set from the information in
                // a parent index.
                Table parentTable = referenceList[vtTableInfo.IndexOfTable(column)];
                ss = parentTable.GetSelectableSchemeFor(vtTableInfo.AdjustColumnOffset(column), originalColumn, table);
                if (table == this) {
                    columnScheme[column] = ss;
                }
            }

            return ss;
        }
Exemplo n.º 13
0
 public VirtualTable(Table[] tables)
     : base(tables)
 {
 }
Exemplo n.º 14
0
 internal JoinedTable(Table table)
     : this(new Table[] { table })
 {
 }
Exemplo n.º 15
0
 protected JoinedTable(Table[] tables)
 {
     CallInit(tables);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Copies the cell from the given table (src_col, src_row) to the 
 /// last row of the column specified of this table.
 /// </summary>
 /// <param name="table"></param>
 /// <param name="src_col"></param>
 /// <param name="src_row"></param>
 /// <param name="to_col"></param>
 public void SetCellFrom(Table table, int src_col, long src_row, String to_col)
 {
     ObjectName v = ResolveToVariable(to_col);
     DataObject cell = table.GetValue(src_col, src_row);
     SetRowCell(cell, FindFieldName(v), row_count - 1);
 }
Exemplo n.º 17
0
 private void CallInit(Table[] tables)
 {
     vtTableInfo = new JoinedTableInfo(new ObjectName("#VIRTUAL TABLE#"), tables.Select(x => x.TableInfo).ToArray());
     Init(tables);
 }
Exemplo n.º 18
0
 private void CallInit(Table[] tables)
 {
     Init(tables);
 }
Exemplo n.º 19
0
 internal abstract SelectableScheme GetSelectableSchemeFor(int column, int originalColumn, Table table);
Exemplo n.º 20
0
 public void SetCellFrom(Table table, int src_col, int src_row, string to_col)
 {
     ObjectName v = ResolveToVariable(to_col);
     DataObject cell = table.GetValue(src_col, src_row);
     SetRowCell(cell, TableInfo.IndexOfColumn(v), rowCount - 1);
 }
Exemplo n.º 21
0
        /// <summary>
        /// Helper function for initializing the variables in the joined table.
        /// </summary>
        /// <param name="tables"></param>
        protected virtual void Init(Table[] tables)
        {
            int tableCount = tables.Length;
            referenceList = tables;

            int colCount = ColumnCount;
            columnScheme = new SelectableScheme[colCount];

            vtTableInfo = new DataTableInfo(new ObjectName(null, "#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) {
                Table curTable = referenceList[i];
                DataTableInfo 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.
                    vtTableInfo.AddColumn(curTableInfo[n].Clone());
                }

            }

            vtTableInfo.IsReadOnly = true;
        }
Exemplo n.º 22
0
 /// <summary>
 /// Disposes this object.
 /// </summary>
 public void Dispose()
 {
     while (locked > 0) {
         UnlockRoot(-1);
     }
     result = null;
     rowIndexMap = null;
     colDesc = null;
 }
Exemplo n.º 23
0
 /// <summary>
 /// Constructs this <see cref="TemporaryTable"/> based on the given 
 /// <see cref="Table"/> object.
 /// </summary>
 /// <param name="based_on"></param>
 public TemporaryTable(Table based_on)
     : base(based_on.Database)
 {
     tableInfo = based_on.TableInfo.Clone();
     tableInfo.IsReadOnly = true;
 }
Exemplo n.º 24
0
 /// <summary>
 /// Constructs this <see cref="TemporaryTable"/> based on the 
 /// fields from the given <see cref="Table"/> object.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="based_on"></param>
 public TemporaryTable(String name, Table based_on)
     : base(based_on.Database)
 {
     tableInfo = based_on.TableInfo.Clone(new ObjectName(null, name));
     tableInfo.IsReadOnly = true;
 }
Exemplo n.º 25
0
 public VirtualTable(Table table)
     : base(table)
 {
 }
Exemplo n.º 26
0
        ///<summary>
        /// Returns a Table that is this function table merged with the cross
        /// reference table.
        ///</summary>
        ///<param name="maxColumn"></param>
        /// <remarks>
        /// The result table includes only one row from each group.
        /// <para>
        /// The 'max_column' argument is optional (can be null).  If it's set to a
        /// column in the reference table, then the row with the max value from the
        /// group is used as the group row.  For example, 'Part.id' will return the
        /// row with the maximum part.id from each group.
        /// </para>
        /// </remarks>
        ///<returns></returns>
        public Table MergeWithReference(ObjectName maxColumn)
        {
            Table table = ReferenceTable;

            IList<long> rowList;

            if (wholeTableAsGroup) {
                // Whole table is group, so take top entry of table.

                rowList = new List<long>(1);
                IEnumerator<long> rowEnum = table.GetRowEnumerator();
                if (rowEnum.MoveNext()) {
                    rowList.Add(rowEnum.Current);
                } else {
                    // MAJOR HACK: If the referencing table has no elements then we choose
                    //   an arbitary index from the reference table to merge so we have
                    //   at least one element in the table.
                    //   This is to fix the 'SELECT COUNT(*) FROM empty_table' bug.
                    rowList.Add(Int32.MaxValue - 1);
                }
            } else if (table.RowCount == 0) {
                rowList = new List<long>(0);
            } else if (groupLinks != null) {
                // If we are grouping, reduce down to only include one row from each
                // group.
                if (maxColumn == null) {
                    rowList = GetTopFromEachGroup();
                } else {
                    int col_num = ReferenceTable.FindFieldName(maxColumn);
                    rowList = GetMaxFromEachGroup(col_num);
                }
            } else {
                // OPTIMIZATION: This should be optimized.  It should be fairly trivial
                //   to generate a Table implementation that efficiently merges this
                //   function table with the reference table.

                // This means there is no grouping, so merge with entire table,
                long rowCount = table.RowCount;
                rowList = new List<long>((int)rowCount);
                IEnumerator<long> en = table.GetRowEnumerator();
                while (en.MoveNext()) {
                    rowList.Add(en.Current);
                }
            }

            // Create a virtual table that's the new group table merged with the
            // functions in this...

            Table[] tabs = new Table[] { table, this };
            IList<long>[] rowSets = new IList<long>[] { rowList, rowList };

            VirtualTable outTable = new VirtualTable(tabs);
            outTable.Set(tabs, rowSets);
            table = outTable;
            return table;
        }
Exemplo n.º 27
0
        ///<summary>
        ///</summary>
        ///<param name="crossRefTable"></param>
        ///<param name="inExpList"></param>
        ///<param name="columnNames"></param>
        ///<param name="context"></param>
        public FunctionTable(Table crossRefTable, Expression[] inExpList, string[] columnNames, IQueryContext context)
            : base(context.Connection.Database)
        {
            // Make sure we are synchronized over the class.
            lock (typeof (FunctionTable)) {
                uniqueId = UniqueKeySeq;
                ++UniqueKeySeq;
            }
            uniqueId = (uniqueId & 0x0FFFFFFF) | 0x010000000;

            this.context = context;

            this.crossRefTable = crossRefTable;
            crResolver = crossRefTable.GetVariableResolver();
            crResolver.SetId = 0;

            // Create a DataTableInfo object for this function table.
            funTableInfo = new DataTableInfo(FunctionTableName);

            expList = new Expression[inExpList.Length];
            expInfo = new byte[inExpList.Length];

            // Create a new DataColumnInfo for each expression, and work out if the
            // expression is simple or not.
            for (int i = 0; i < inExpList.Length; ++i) {
                Expression expr = inExpList[i];
                // Examine the expression and determine if it is simple or not
                if (expr.IsConstant() && !expr.HasAggregateFunction(context)) {
                    // If expression is a constant, solve it
                    DataObject result = expr.Evaluate(null, null, context);
                    expr = Expression.Constant(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(crResolver, context));
            }

            // Make sure the table info isn't changed from this point on.
            funTableInfo.IsReadOnly = true;

            // Function tables are the size of the referring table.
            row_count = crossRefTable.RowCount;

            // Set schemes to 'blind search'.
            BlankSelectableSchemes(1);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Helper function for initializing the variables in the joined table.
        /// </summary>
        /// <param name="tables"></param>
        protected virtual void Init(Table[] tables)
        {
            referenceList = tables;

            int colCount = TableInfo.ColumnCount;
            columnScheme = new SelectableScheme[colCount];
        }
Exemplo n.º 29
0
        /// <inheritdoc/>
        internal override SelectableScheme GetSelectableSchemeFor(int column, int originalColumn, Table table)
        {
            // We need to map the original_column if the original column is a reference
            // in this subset column table.  Otherwise we leave as is.
            // The reason is because FilterTable pretends the call came from its
            // parent if a request is made on this table.
            int mappedOriginalColumn = originalColumn;
            if (table == this) {
                mappedOriginalColumn = subsetTableInfo.MapColumn(originalColumn);
            }

            return base.GetSelectableSchemeFor(subsetTableInfo.MapColumn(column), mappedOriginalColumn, table);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Copies the contents of the row of the given Table onto the end of 
        /// this table.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="row"></param>
        /// <remarks>
        /// Only copies columns that exist in both tables.
        /// </remarks>
        public void CopyFrom(Table table, int row)
        {
            NewRow();

            ObjectName[] vars = new ObjectName[table.ColumnCount];
            for (int i = 0; i < vars.Length; ++i) {
                vars[i] = table.GetResolvedVariable(i);
            }

            for (int i = 0; i < ColumnCount; ++i) {
                ObjectName v = GetResolvedVariable(i);
                String col_name = v.Name;
                try {
                    int tcol_index = -1;
                    for (int n = 0; n < vars.Length || tcol_index == -1; ++n) {
                        if (vars[n].Name.Equals(col_name)) {
                            tcol_index = n;
                        }
                    }
                    SetRowCell(table.GetValue(tcol_index, row), i, row_count - 1);
                } catch (Exception e) {
                    throw new ApplicationException(e.Message, e);
                }
            }
        }