Пример #1
0
        /// <summary>
        /// Reads list of indexes using SHOW INDEX FROM query. Enumerates tables using
        /// given restrictions first and then reads indexes for each table.
        /// </summary>
        /// <param name="connection">The DataConnectionWrapper to be used for enumeration.</param>
        /// <param name="restrictions">The restrictions to be putted on the retrieved objects set.</param>
        /// <param name="sort">Sort expresion to append after ORDER BY clause.</param>
        /// <returns>Returns table with Database Objects which satisfy given restriction.</returns>
        protected override DataTable ReadTable(DataConnectionWrapper connection, object[] restrictions, string sort)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            DataTable result = PrepareTable();

            // Fill restrictions for the tables
            object[] tableRestrictions = restrictions;
            // If necessary, cut the restrictions
            if (restrictions != null && restrictions.Length > 3)
            {
                tableRestrictions    = new object[3];
                tableRestrictions[0] = restrictions[0];
                tableRestrictions[1] = restrictions[1];
                tableRestrictions[2] = restrictions[2];
            }

            // Read tables
            DataTable tables = ObjectDescriptor.EnumerateObjects(connection, TableDescriptor.TypeName, tableRestrictions, sort);

            if (tables == null || tables.Rows == null)
            {
                return(result);
            }

            // StringBuilder for query
            StringBuilder query = new StringBuilder(EnumerateSql);

            // Extract indexes for all tables
            foreach (DataRow table in tables.Rows)
            {
                // Truncate query
                if (query.Length > EnumerateSql.Length)
                {
                    query.Remove(EnumerateSql.Length, query.Length - EnumerateSql.Length);
                }

                // Write table idintifier
                QueryBuilder.WriteIdentifier(
                    DataInterpreter.GetStringNotNull(table, TableDescriptor.Attributes.Schema),
                    DataInterpreter.GetStringNotNull(table, TableDescriptor.Attributes.Name),
                    query);

                // Execute query to select indexes
                DataTable indexes = connection.ExecuteSelectTable(query.ToString());
                if (indexes == null || indexes.Rows == null)
                {
                    Debug.Fail("Failed to read indexes using query:\n" + query.ToString());
                    continue;
                }

                // Fetch data
                FetchData(restrictions, table, indexes, result);
            }

            // Accept changes for result
            result.AcceptChanges();

            // Return resulting table
            return(result);
        }