System.Data.SQLite
Check-in [cb20e39503]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:More index tweaks -- not done yet.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sourceforge
Files: files | file ages | folders
SHA1: cb20e39503f7103d2eebbe00a7e58b145ca92cf8
User & Date: rmsimpson 2006-02-17 06:06:10
Context
2006-02-17
06:06
Platform compatibility changes check-in: 2253540b75 user: rmsimpson tags: sourceforge
06:06
More index tweaks -- not done yet. check-in: cb20e39503 user: rmsimpson tags: sourceforge
2006-02-15
05:40
no message check-in: 4aaa3c5cf6 user: rmsimpson tags: sourceforge
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLiteConnection.cs.

1049
1050
1051
1052
1053
1054
1055
1056

1057
1058
1059
1060
1061
1062
1063
....
1088
1089
1090
1091
1092
1093
1094

1095
1096
1097
1098
1099
1100
1101
1102
....
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125



1126
1127


1128






1129


1130
1131
1132
1133
1134
1135
1136
....
1144
1145
1146
1147
1148
1149
1150

1151



1152

















1153





1154
1155
1156
1157
1158
1159
1160
    /// <param name="strTable">The table to retrieve index information for, can be null</param>
    /// <returns>DataTable</returns>
    private DataTable Schema_Indexes(string strCatalog, string strTable, string strIndex)
    {
      DataTable tbl = new DataTable("Indexes");
      DataRow row;
      DataTable tblSchema;
      string primaryKey;


      tbl.Locale = CultureInfo.InvariantCulture;
      tbl.Columns.Add("TABLE_CATALOG", typeof(string));
      tbl.Columns.Add("TABLE_SCHEMA", typeof(string));
      tbl.Columns.Add("TABLE_NAME", typeof(string));
      tbl.Columns.Add("INDEX_CATALOG", typeof(string));
      tbl.Columns.Add("INDEX_SCHEMA", typeof(string));
................................................................................

      using (SQLiteCommand cmdTables = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[sqlite_master] WHERE [type] LIKE 'table'", strCatalog), this))
      {
        using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
        {
          while (rdTables.Read())
          {

            primaryKey = "";
            if (String.IsNullOrEmpty(strTable) || String.Compare(rdTables.GetString(2), strTable, true, CultureInfo.InvariantCulture) == 0)
            {
              // First, look for any rowid indexes -- which sqlite defines are INTEGER PRIMARY KEY columns.
              // Such indexes are not listed in the indexes list but count as indexes just the same.
              using (SQLiteCommand cmdTable = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}]", strCatalog, rdTables.GetString(2)), this))
              {
                using (SQLiteDataReader rdTable = cmdTable.ExecuteReader(CommandBehavior.SchemaOnly))
................................................................................
                  tblSchema = rdTable.GetSchemaTable(false);
                  foreach (DataRow schemaRow in tblSchema.Rows)
                  {
                    if (schemaRow.IsNull("DeclaredType") == false)
                    {
                      if ((bool)schemaRow[SchemaTableColumn.IsKey] == true)
                      {
                        row = tbl.NewRow();

                        row["TABLE_CATALOG"] = strCatalog;
                        row["TABLE_NAME"] = rdTables.GetString(2);
                        row["INDEX_CATALOG"] = strCatalog;
                        row["PRIMARY_KEY"] = true;

                        // If the primary key is of type INTEGER, then its a rowid and we need to make a fake index entry for it.
                        if (String.Compare((string)schemaRow["DeclaredType"], "INTEGER", true, CultureInfo.InvariantCulture) == 0)
                        {
                          row["INDEX_NAME"] = String.Format(CultureInfo.InvariantCulture, "sqlite_master_PK_{0}", rdTables.GetString(2));
                          row["UNIQUE"] = true;
                          tbl.Rows.Add(row);
                          break;
                        }



                        // Otherwise, record this primary key's index name so we can match it up later and mark it as a primary key
                        else primaryKey = String.Format(CultureInfo.InvariantCulture, "sqlite_autoindex_{0}_", rdTables.GetString(2));


                      }






                    }


                  }
                }
              }

              // Now fetch all the rest of the indexes.
              using (SQLiteCommand cmd = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].index_list([{1}])", strCatalog, rdTables.GetString(2)), this))
              {
................................................................................
                      row = tbl.NewRow();

                      row["TABLE_CATALOG"] = strCatalog;
                      row["TABLE_NAME"] = rdTables.GetString(2);
                      row["INDEX_CATALOG"] = strCatalog;
                      row["INDEX_NAME"] = rd.GetString(1);
                      row["UNIQUE"] = rd.GetBoolean(2);

                      



                      if (rd.GetString(1).StartsWith(primaryKey, StringComparison.InvariantCultureIgnoreCase))

















                        row["PRIMARY_KEY"] = true;






                      tbl.Rows.Add(row);
                    }
                  }
                }
              }
            }







|
>







 







>
|







 







|
<
<
<
<
<




<
|
<
<

>
>
>
|
<
>
>
|
>
>
>
>
>
>
|
>
>







 







>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>







1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
....
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
....
1106
1107
1108
1109
1110
1111
1112
1113





1114
1115
1116
1117

1118


1119
1120
1121
1122
1123

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
....
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
    /// <param name="strTable">The table to retrieve index information for, can be null</param>
    /// <returns>DataTable</returns>
    private DataTable Schema_Indexes(string strCatalog, string strTable, string strIndex)
    {
      DataTable tbl = new DataTable("Indexes");
      DataRow row;
      DataTable tblSchema;
      Collections.Generic.List<int> primaryKeys = new List<int>();
      bool maybeRowId;

      tbl.Locale = CultureInfo.InvariantCulture;
      tbl.Columns.Add("TABLE_CATALOG", typeof(string));
      tbl.Columns.Add("TABLE_SCHEMA", typeof(string));
      tbl.Columns.Add("TABLE_NAME", typeof(string));
      tbl.Columns.Add("INDEX_CATALOG", typeof(string));
      tbl.Columns.Add("INDEX_SCHEMA", typeof(string));
................................................................................

      using (SQLiteCommand cmdTables = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[sqlite_master] WHERE [type] LIKE 'table'", strCatalog), this))
      {
        using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
        {
          while (rdTables.Read())
          {
            maybeRowId = false;
            primaryKeys.Clear();
            if (String.IsNullOrEmpty(strTable) || String.Compare(rdTables.GetString(2), strTable, true, CultureInfo.InvariantCulture) == 0)
            {
              // First, look for any rowid indexes -- which sqlite defines are INTEGER PRIMARY KEY columns.
              // Such indexes are not listed in the indexes list but count as indexes just the same.
              using (SQLiteCommand cmdTable = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}]", strCatalog, rdTables.GetString(2)), this))
              {
                using (SQLiteDataReader rdTable = cmdTable.ExecuteReader(CommandBehavior.SchemaOnly))
................................................................................
                  tblSchema = rdTable.GetSchemaTable(false);
                  foreach (DataRow schemaRow in tblSchema.Rows)
                  {
                    if (schemaRow.IsNull("DeclaredType") == false)
                    {
                      if ((bool)schemaRow[SchemaTableColumn.IsKey] == true)
                      {
                        primaryKeys.Add((int)schemaRow[SchemaTableColumn.ColumnOrdinal]);






                        // If the primary key is of type INTEGER, then its a rowid and we need to make a fake index entry for it.
                        if (String.Compare((string)schemaRow["DeclaredType"], "INTEGER", true, CultureInfo.InvariantCulture) == 0)
                        {

                          maybeRowId = true;


                        }
                      }
                    }
                  }
                  if (primaryKeys.Count == 1 && maybeRowId == true)

                  {
                    row = tbl.NewRow();

                    row["TABLE_CATALOG"] = strCatalog;
                    row["TABLE_NAME"] = rdTables.GetString(2);
                    row["INDEX_CATALOG"] = strCatalog;
                    row["PRIMARY_KEY"] = true;
                    row["INDEX_NAME"] = String.Format(CultureInfo.InvariantCulture, "sqlite_master_PK_{0}", rdTables.GetString(2));
                    row["UNIQUE"] = true;

                    tbl.Rows.Add(row);
                    primaryKeys.Clear();
                  }
                }
              }

              // Now fetch all the rest of the indexes.
              using (SQLiteCommand cmd = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].index_list([{1}])", strCatalog, rdTables.GetString(2)), this))
              {
................................................................................
                      row = tbl.NewRow();

                      row["TABLE_CATALOG"] = strCatalog;
                      row["TABLE_NAME"] = rdTables.GetString(2);
                      row["INDEX_CATALOG"] = strCatalog;
                      row["INDEX_NAME"] = rd.GetString(1);
                      row["UNIQUE"] = rd.GetBoolean(2);
                      row["PRIMARY_KEY"] = false;

                      // Now for the really hard work.  Figure out which index is the primary key index.
                      // The only way to figure it out is to check if the index was an autoindex and if we have a non-rowid
                      // primary key, and all the columns in the given index match the primary key columns
                      if (primaryKeys.Count > 0 && rd.GetString(1).StartsWith("sqlite_autoindex_" + rdTables.GetString(2), StringComparison.InvariantCultureIgnoreCase) == true)
                      {
                        using (SQLiteCommand cmdDetails = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].index_info([{1}])", strCatalog, rd.GetString(1)), this))
                        {
                          using (SQLiteDataReader rdDetails = cmdDetails.ExecuteReader())
                          {
                            int nMatches = 0;
                            while (rdDetails.Read())
                            {
                              if (primaryKeys.Contains(rdDetails.GetInt32(1)) == false)
                              {
                                nMatches = 0;
                                break;
                              }
                              nMatches++;
                            }
                            if (nMatches == primaryKeys.Count)
                            {
                              row["PRIMARY_KEY"] = true;
                              primaryKeys.Clear();
                            }
                          }
                        }
                      }

                      tbl.Rows.Add(row);
                    }
                  }
                }
              }
            }