Ejemplo n.º 1
0
        internal SQLiteKeyReader(SQLiteConnection cnn, SQLiteDataReader reader, SQLiteStatement stmt)
        {
            List <string>                       item;
            Dictionary <string, int>            strs       = new Dictionary <string, int>();
            Dictionary <string, List <string> > strs1      = new Dictionary <string, List <string> >();
            List <SQLiteKeyReader.KeyInfo>      keyInfos   = new List <SQLiteKeyReader.KeyInfo>();
            List <SQLiteKeyReader.RowIdInfo>    rowIdInfos = new List <SQLiteKeyReader.RowIdInfo>();

            this._stmt = stmt;
            using (DataTable schema = cnn.GetSchema("Catalogs"))
            {
                foreach (DataRow row in schema.Rows)
                {
                    strs.Add((string)row["CATALOG_NAME"], Convert.ToInt32(row["ID"], CultureInfo.InvariantCulture));
                }
            }
            using (DataTable schemaTable = reader.GetSchemaTable(false, false))
            {
                foreach (DataRow dataRow in schemaTable.Rows)
                {
                    if (dataRow[SchemaTableOptionalColumn.BaseCatalogName] == DBNull.Value)
                    {
                        continue;
                    }
                    string str   = (string)dataRow[SchemaTableOptionalColumn.BaseCatalogName];
                    string item1 = (string)dataRow[SchemaTableColumn.BaseTableName];
                    if (strs1.ContainsKey(str))
                    {
                        item = strs1[str];
                    }
                    else
                    {
                        item = new List <string>();
                        strs1.Add(str, item);
                    }
                    if (item.Contains(item1))
                    {
                        continue;
                    }
                    item.Add(item1);
                }
                foreach (KeyValuePair <string, List <string> > keyValuePair in strs1)
                {
                    for (int i = 0; i < keyValuePair.Value.Count; i++)
                    {
                        string   str1     = keyValuePair.Value[i];
                        DataRow  dataRow1 = null;
                        string[] key      = new string[] { keyValuePair.Key, null, str1 };
                        using (DataTable dataTable = cnn.GetSchema("Indexes", key))
                        {
                            for (int j = 0; j < 2 && dataRow1 == null; j++)
                            {
                                foreach (DataRow row1 in dataTable.Rows)
                                {
                                    if (j != 0 || !(bool)row1["PRIMARY_KEY"])
                                    {
                                        if (j != 1 || !(bool)row1["UNIQUE"])
                                        {
                                            continue;
                                        }
                                        dataRow1 = row1;
                                        break;
                                    }
                                    else
                                    {
                                        dataRow1 = row1;
                                        break;
                                    }
                                }
                            }
                            if (dataRow1 != null)
                            {
                                string[] strArrays = new string[] { keyValuePair.Key, null, str1 };
                                using (DataTable schema1 = cnn.GetSchema("Tables", strArrays))
                                {
                                    int      num            = strs[keyValuePair.Key];
                                    int      num1           = Convert.ToInt32(schema1.Rows[0]["TABLE_ROOTPAGE"], CultureInfo.InvariantCulture);
                                    int      cursorForTable = stmt._sql.GetCursorForTable(stmt, num, num1);
                                    string[] key1           = new string[] { keyValuePair.Key, null, str1, (string)dataRow1["INDEX_NAME"] };
                                    using (DataTable dataTable1 = cnn.GetSchema("IndexColumns", key1))
                                    {
                                        bool flag = (string)dataRow1["INDEX_NAME"] == string.Concat("sqlite_master_PK_", str1);
                                        SQLiteKeyReader.KeyQuery keyQuery = null;
                                        List <string>            strs2    = new List <string>();
                                        for (int k = 0; k < dataTable1.Rows.Count; k++)
                                        {
                                            string stringOrNull = SQLiteConvert.GetStringOrNull(dataTable1.Rows[k]["COLUMN_NAME"]);
                                            bool   flag1        = true;
                                            foreach (DataRow row2 in schemaTable.Rows)
                                            {
                                                if (row2.IsNull(SchemaTableColumn.BaseColumnName) || !((string)row2[SchemaTableColumn.BaseColumnName] == stringOrNull) || !((string)row2[SchemaTableColumn.BaseTableName] == str1) || !((string)row2[SchemaTableOptionalColumn.BaseCatalogName] == keyValuePair.Key))
                                                {
                                                    continue;
                                                }
                                                if (flag)
                                                {
                                                    SQLiteKeyReader.RowIdInfo rowIdInfo = new SQLiteKeyReader.RowIdInfo()
                                                    {
                                                        databaseName = keyValuePair.Key,
                                                        tableName    = str1,
                                                        column       = (int)row2[SchemaTableColumn.ColumnOrdinal]
                                                    };
                                                    rowIdInfos.Add(rowIdInfo);
                                                }
                                                dataTable1.Rows.RemoveAt(k);
                                                k--;
                                                flag1 = false;
                                                break;
                                            }
                                            if (flag1)
                                            {
                                                strs2.Add(stringOrNull);
                                            }
                                        }
                                        if (!flag && strs2.Count > 0)
                                        {
                                            string[] strArrays1 = new string[strs2.Count];
                                            strs2.CopyTo(strArrays1);
                                            keyQuery = new SQLiteKeyReader.KeyQuery(cnn, keyValuePair.Key, str1, strArrays1);
                                        }
                                        for (int l = 0; l < dataTable1.Rows.Count; l++)
                                        {
                                            string stringOrNull1            = SQLiteConvert.GetStringOrNull(dataTable1.Rows[l]["COLUMN_NAME"]);
                                            SQLiteKeyReader.KeyInfo keyInfo = new SQLiteKeyReader.KeyInfo()
                                            {
                                                rootPage     = num1,
                                                cursor       = cursorForTable,
                                                database     = num,
                                                databaseName = keyValuePair.Key,
                                                tableName    = str1,
                                                columnName   = stringOrNull1,
                                                query        = keyQuery,
                                                column       = l
                                            };
                                            keyInfos.Add(keyInfo);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                keyValuePair.Value.RemoveAt(i);
                                i--;
                            }
                        }
                    }
                }
            }
            this._keyInfo = new SQLiteKeyReader.KeyInfo[keyInfos.Count];
            keyInfos.CopyTo(this._keyInfo);
            this._rowIdInfo = new SQLiteKeyReader.RowIdInfo[rowIdInfos.Count];
            rowIdInfos.CopyTo(this._rowIdInfo);
        }