private DBSqlParserColumn FindConstraintColumn(
            string schemaName,
            string tableName,
            string columnName
            )
        {
            //  Searches the collection of parsed column information for the
            //  specific column in the specific table, and returns the parsed
            //  column information if it's found.  If not found then it returns
            //  null instead.

            DBSqlParserColumnCollection columns = Columns;
            int columnCount = columns.Count;

            for (int i = 0; i < columnCount; ++i)
            {
                DBSqlParserColumn column = columns[i];

                if (CatalogMatch(column.SchemaName, schemaName) &&
                    CatalogMatch(column.TableName, tableName) &&
                    CatalogMatch(column.ColumnName, columnName))
                {
                    return(column);
                }
            }
            return(null);
        }
        internal DBSqlParserTable FindTableForColumn(
            DBSqlParserColumn column
            )
        {
            DBSqlParserTableCollection tables = Tables;
            int tableCount = tables.Count;

            for (int i = 0; i < tableCount; ++i)
            {
                DBSqlParserTable table = tables[i];

                // if the table name matches the correlation name, then we're certain
                // of a match
                if (string.Empty == column.DatabaseName &&
                    string.Empty == column.SchemaName &&
                    CatalogMatch(column.TableName, table.CorrelationName))
                {
                    return(table);
                }

                // otherwise, compare each part of the name (if they exist) with the
                // table and pick the one that matches all the parts that exist.
                if ((string.Empty == column.DatabaseName || CatalogMatch(column.DatabaseName, table.DatabaseName)) &&
                    (string.Empty == column.SchemaName || CatalogMatch(column.SchemaName, table.SchemaName)) &&
                    (string.Empty == column.TableName || CatalogMatch(column.TableName, table.TableName)))
                {
                    return(table);
                }
            }

            Debug.Assert(false, "Why didn't we find a match for the column?");
            return(null);
        }
Exemple #3
0
 //----------------------------------------------------------------------
 // this[]
 //
 internal DBSqlParserColumn this[int i]
 {
     get
     {
         DBSqlParserColumn value = (DBSqlParserColumn)InnerList[i];
         return(value);
     }
 }
        ////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////
        //
        // Methods
        //
        ////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////

        internal void CopySchemaInfoFrom(DBSqlParserColumn completedColumn)
        {
            _databaseName = completedColumn.DatabaseName;
            _schemaName   = completedColumn.SchemaName;
            _tableName    = completedColumn.TableName;
            _columnName   = completedColumn.ColumnName;
            _isKey        = completedColumn.IsKey;
            _isUnique     = completedColumn.IsUnique;
        }
Exemple #5
0
        internal DBSqlParserColumn Add(
            string databaseName,
            string schemaName,
            string tableName,
            string columnName,
            string alias
            )
        {
            DBSqlParserColumn p = new DBSqlParserColumn(databaseName, schemaName, tableName, columnName, alias);

            return(Add(p));
        }
        protected DBSqlParserColumn FindCompletedColumn(
            DBSqlParserTable table,
            DBSqlParserColumn searchColumn
            )
        {
            DBSqlParserColumnCollection columns = table.Columns;
            int columnsCount = columns.Count;

            for (int i = 0; i < columnsCount; ++i)
            {
                DBSqlParserColumn column = columns[i];

                // Compare each part of the name (if they exist) with the
                // table and pick the one that matches all the parts that exist.
                if (CatalogMatch(column.ColumnName, searchColumn.ColumnName))
                {
                    return(column);
                }
            }

            // MDAC 87152: ROWID and ROWNUM shouldn't fire an assert here:
            //Debug.Assert(false, "Why didn't we find a match for the search column?");
            return(null);
        }
        private void CompleteSchemaInformation()
        {
            DBSqlParserColumnCollection columns = Columns;
            DBSqlParserTableCollection  tables  = Tables;

            int columnCount = columns.Count;
            int tableCount  = tables.Count;

            // First, derive all of the information we can about each table
            for (int i = 0; i < tableCount; i++)
            {
                DBSqlParserTable            table        = tables[i];
                DBSqlParserColumnCollection tableColumns = GatherTableColumns(table);
                table.Columns = tableColumns;
            }

            // Next, derive all of the column information we can.
            for (int i = 0; i < columnCount; i++)
            {
                DBSqlParserColumn column = columns[i];
                DBSqlParserTable  table  = FindTableForColumn(column);

                if (!column.IsExpression)
                {
                    // If this is a '*' column, then we have to expand the '*' into
                    // its component parts.
                    if ("*" == column.ColumnName)
                    {
                        // Remove the existing "*" column entry and replace it with the
                        // complete list of columns in the table.
                        columns.RemoveAt(i);

                        // If this is a tablename.* column, then add references to the
                        // all columns in the specified table, otherwise add references
                        // to all columns in all tables.
                        if (String.Empty != column.TableName)
                        {
                            DBSqlParserColumnCollection tableColumns = table.Columns;
                            int tableColumnCount = tableColumns.Count;

                            for (int j = 0; j < tableColumnCount; ++j)
                            {
                                columns.Insert(i + j, tableColumns[j]);
                            }
                            columnCount += tableColumnCount - 1;                                // don't forget to adjust our loop end
                            i           += tableColumnCount - 1;
                        }
                        else
                        {
                            for (int k = 0; k < tableCount; k++)
                            {
                                table = tables[k];

                                DBSqlParserColumnCollection tableColumns = table.Columns;
                                int tableColumnCount = tableColumns.Count;

                                for (int j = 0; j < tableColumnCount; ++j)
                                {
                                    columns.Insert(i + j, tableColumns[j]);
                                }
                                columnCount += tableColumnCount - 1;                                    // don't forget to adjust our loop end
                                i           += tableColumnCount;
                            }
                        }
                    }
                    else
                    {
                        // if this isn't a '*' column, we find the table that the column belongs
                        // to, and ask it's column collection for the completed column info (that
                        // contains information about key values, etc.)
                        DBSqlParserColumn completedColumn = FindCompletedColumn(table, column);

                        if (null != completedColumn)                         // MDAC 87152
                        {
                            column.CopySchemaInfoFrom(completedColumn);
                        }
                        else
                        {
                            column.CopySchemaInfoFrom(table);
                        }
                    }
                }
            }

            // Finally, derive the key column information for each table
            for (int i = 0; i < tableCount; i++)
            {
                DBSqlParserTable table = tables[i];
                GatherKeyColumns(table);
            }
        }
        protected override void GatherKeyColumns(
            DBSqlParserTable table
            )
        {
            //  Called after the table and column information is completed to
            //  identify which columns in the select-list are key columns for
            //  their table.

            OracleCommand    cmd = null;
            OracleDataReader rdr = null;

            try {
                try {
                    cmd = _connection.CreateCommand();

                    cmd.Transaction = _connection.Transaction; // must set the transaction context to be the same as the command, or we'll throw when we execute.

                    string schemaName = CatalogCase(table.SchemaName);
                    string tableName  = CatalogCase(table.TableName);

                    string synonymSchemaName = schemaName;
                    string synonymTableName  = tableName;

                    // First, we have to "dereference" a synonym, if it was specified, because
                    // synonyms don't have catalog items stored for them, they're for the table
                    // or view that the synonym references.

                    cmd.CommandText = GetSynonymQueryStatement(schemaName, tableName);
                    rdr             = cmd.ExecuteReader();

                    if (rdr.Read())
                    {
                        synonymSchemaName = rdr.GetString(0);
                        synonymTableName  = rdr.GetString(1);
                    }

                    rdr.Dispose();

                    // Now we have the real schema name and table name, go and derive the key
                    // columns

                    cmd.CommandText = GetConstraintQueryStatement(synonymSchemaName, synonymTableName);
                    rdr             = cmd.ExecuteReader();

                    ArrayList constraintColumnNames = new ArrayList();
                    bool      isUniqueConstraint;

                    if (true == (_moreConstraints = rdr.Read()))
                    {
                        while (GetConstraint(rdr, out isUniqueConstraint, constraintColumnNames))
                        {
                            bool foundAllColumns       = true;
                            int  constraintColumnCount = constraintColumnNames.Count;

                            DBSqlParserColumn[] constraintColumn = new DBSqlParserColumn[constraintColumnCount];

                            for (int j = 0; j < constraintColumnCount; ++j)
                            {
                                DBSqlParserColumn column = FindConstraintColumn(
                                    schemaName,
                                    tableName,
                                    (string)constraintColumnNames[j]
                                    );

                                if (null == column)
                                {
                                    foundAllColumns = false;
                                    break;
                                }

                                constraintColumn[j] = column;
                            }

                            if (foundAllColumns)
                            {
                                for (int j = 0; j < constraintColumnCount; ++j)
                                {
                                    constraintColumn[j].SetAsKey(isUniqueConstraint);
                                }

                                break;
                            }
                        }
                    }
                }
                finally
                {
                    if (null != cmd)
                    {
                        cmd.Dispose();
                        cmd = null;
                    }

                    if (null != rdr)
                    {
                        rdr.Dispose();
                        rdr = null;
                    }
                }
            }
            catch { // Prevent exception filters from running in our space
                throw;
            }
        }
Exemple #9
0
        private void BuildSchemaTable()
        {
            Debug.Assert(null == _schemaTable, "BuildSchemaTable: schema table already exists");
            Debug.Assert(null != _columnInfo, "BuildSchemaTable: no columnInfo");

            int                         columnCount = FieldCount;
            OracleSqlParser             parser;
            DBSqlParserColumnCollection parsedColumns      = null;
            int                         parsedColumnsCount = 0;

            if (_keyInfoRequested)
            {
                parser = new OracleSqlParser();
                parser.Parse(_statementText, _connection);

                parsedColumns      = parser.Columns;
                parsedColumnsCount = parsedColumns.Count;
            }


            DataTable schemaTable = new DataTable("SchemaTable");

            schemaTable.MinimumCapacity = columnCount;

            DataColumn name      = new DataColumn("ColumnName", typeof(System.String));
            DataColumn ordinal   = new DataColumn("ColumnOrdinal", typeof(System.Int32));
            DataColumn size      = new DataColumn("ColumnSize", typeof(System.Int32));
            DataColumn precision = new DataColumn("NumericPrecision", typeof(System.Int16));
            DataColumn scale     = new DataColumn("NumericScale", typeof(System.Int16));

            DataColumn dataType   = new DataColumn("DataType", typeof(System.Type));
            DataColumn oracleType = new DataColumn("ProviderType", typeof(System.Int32));

            DataColumn isLong       = new DataColumn("IsLong", typeof(System.Boolean));
            DataColumn isNullable   = new DataColumn("AllowDBNull", typeof(System.Boolean));
            DataColumn isAliased    = new DataColumn("IsAliased", typeof(System.Boolean));
            DataColumn isExpression = new DataColumn("IsExpression", typeof(System.Boolean));
            DataColumn isKey        = new DataColumn("IsKey", typeof(System.Boolean));
            DataColumn isUnique     = new DataColumn("IsUnique", typeof(System.Boolean));

            DataColumn baseSchemaName = new DataColumn("BaseSchemaName", typeof(System.String));
            DataColumn baseTableName  = new DataColumn("BaseTableName", typeof(System.String));
            DataColumn baseColumnName = new DataColumn("BaseColumnName", typeof(System.String));


            ordinal.DefaultValue = 0;
            isLong.DefaultValue  = false;

            DataColumnCollection columns = schemaTable.Columns;

            columns.Add(name);
            columns.Add(ordinal);
            columns.Add(size);
            columns.Add(precision);
            columns.Add(scale);

            columns.Add(dataType);
            columns.Add(oracleType);

            columns.Add(isLong);
            columns.Add(isNullable);
            columns.Add(isAliased);
            columns.Add(isExpression);
            columns.Add(isKey);
            columns.Add(isUnique);

            columns.Add(baseSchemaName);
            columns.Add(baseTableName);
            columns.Add(baseColumnName);

            for (int i = 0; i < columnCount; ++i)
            {
                OracleColumn column = _columnInfo[i];

                DataRow newRow = schemaTable.NewRow();

                newRow[name]    = column.ColumnName;
                newRow[ordinal] = column.Ordinal;

                if (column.IsLong | column.IsLob)
                {
                    newRow[size] = Int32.MaxValue;          //MDAC 82554
                }
                else
                {
                    newRow[size] = column.Size;
                }

                newRow[precision] = column.Precision;
                newRow[scale]     = column.Scale;

                newRow[dataType]   = column.GetFieldType();
                newRow[oracleType] = column.OracleType;

                newRow[isLong]     = column.IsLong | column.IsLob;
                newRow[isNullable] = column.IsNullable;

                if (_keyInfoRequested && parsedColumnsCount == columnCount)
                {
                    DBSqlParserColumn parsedColumn = parsedColumns[i];

                    newRow[isAliased]      = parsedColumn.IsAliased;
                    newRow[isExpression]   = parsedColumn.IsExpression;
                    newRow[isKey]          = parsedColumn.IsKey;
                    newRow[isUnique]       = parsedColumn.IsUnique;
                    newRow[baseSchemaName] = SetSchemaValue(OracleSqlParser.CatalogCase(parsedColumn.SchemaName));
                    newRow[baseTableName]  = SetSchemaValue(OracleSqlParser.CatalogCase(parsedColumn.TableName));
                    newRow[baseColumnName] = SetSchemaValue(OracleSqlParser.CatalogCase(parsedColumn.ColumnName));
                }
                else
                {
                    newRow[isAliased]      = DBNull.Value;      // don't know
                    newRow[isExpression]   = DBNull.Value;      // don't know
                    newRow[isKey]          = DBNull.Value;      // don't know
                    newRow[isUnique]       = DBNull.Value;      // don't know
                    newRow[baseSchemaName] = DBNull.Value;      // don't know
                    newRow[baseTableName]  = DBNull.Value;      // don't know
                    newRow[baseColumnName] = DBNull.Value;      // don't know
                }

                schemaTable.Rows.Add(newRow);
                newRow.AcceptChanges();
            }

            // mark all columns as readonly
            for (int i = 0; i < columns.Count; i++)
            {
                columns[i].ReadOnly = true;
            }

//          DataSet dataset = new DataSet();
//          dataset.Tables.Add(schemaTable);
//          Debug.WriteLine(dataset.GetXml());
//          dataset.Tables.Remove(schemaTable);
            _schemaTable = schemaTable;
        }
Exemple #10
0
 //----------------------------------------------------------------------
 // Insert
 //
 internal void Insert(int index, DBSqlParserColumn value)
 {
     InnerList.Insert(index, value);
 }
Exemple #11
0
        ////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////
        //
        // Methods
        //
        ////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////

        //----------------------------------------------------------------------
        // Add()
        //
        internal DBSqlParserColumn Add(DBSqlParserColumn value)
        {
            OnValidate(value);
            InnerList.Add(value);
            return(value);
        }