System.Data.SQLite
Check-in [9bb5fe6f96]
Not logged in

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

Overview
Comment:Further improvements to the catalog name and master table name handling in the connection class.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9bb5fe6f96bf60b36ce340100902b70c453ea50c
User & Date: mistachkin 2018-03-09 17:41:30
Context
2018-03-09
17:45
The GetSchemaTable method must verify the base table name (for a column) actually refers to a base table before attempting to query its metadata. Pursuant to [baf42ee135]. check-in: 63ae5401bf user: mistachkin tags: trunk
17:41
Further improvements to the catalog name and master table name handling in the connection class. check-in: 9bb5fe6f96 user: mistachkin tags: trunk
17:27
Add GetMasterTableName method for use by the SQLiteConnection class. check-in: f5b88735be user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

  1391   1391       private const string DefaultZipVfsVersion = null;
  1392   1392   #endif
  1393   1393   
  1394   1394       private const int SQLITE_FCNTL_CHUNK_SIZE = 6;
  1395   1395       private const int SQLITE_FCNTL_WIN32_AV_RETRY = 9;
  1396   1396   
  1397   1397       private const string _dataDirectory = "|DataDirectory|";
  1398         -    private const string _masterdb = "sqlite_master";
  1399         -    private const string _tempmasterdb = "sqlite_temp_master";
         1398  +
         1399  +    private static string _defaultCatalogName = "main";
         1400  +    private static string _defaultMasterTableName = "sqlite_master";
         1401  +
         1402  +    private static string _temporaryCatalogName = "temp";
         1403  +    private static string _temporaryMasterTableName = "sqlite_temp_master";
  1400   1404       #endregion
  1401   1405   
  1402   1406       ///////////////////////////////////////////////////////////////////////////////////////////////
  1403   1407   
  1404   1408       #region Private Static Data
  1405   1409       /// <summary>
  1406   1410       /// The managed assembly containing this type.
................................................................................
  1630   1634       private SQLiteCommitCallback _commitCallback;
  1631   1635       private SQLiteTraceCallback _traceCallback;
  1632   1636       private SQLiteRollbackCallback _rollbackCallback;
  1633   1637       #endregion
  1634   1638   
  1635   1639       ///////////////////////////////////////////////////////////////////////////////////////////////
  1636   1640   
         1641  +    private static string GetDefaultCatalogName()
         1642  +    {
         1643  +        return _defaultCatalogName;
         1644  +    }
         1645  +
         1646  +    ///////////////////////////////////////////////////////////////////////////////////////////////
         1647  +
         1648  +    private static bool IsDefaultCatalogName(
         1649  +        string catalogName
         1650  +        )
         1651  +    {
         1652  +        return String.Compare(catalogName, GetDefaultCatalogName(),
         1653  +            StringComparison.OrdinalIgnoreCase) == 0;
         1654  +    }
         1655  +
         1656  +    ///////////////////////////////////////////////////////////////////////////////////////////////
         1657  +
         1658  +    private static string GetTemporaryCatalogName()
         1659  +    {
         1660  +        return _temporaryCatalogName;
         1661  +    }
         1662  +
         1663  +    ///////////////////////////////////////////////////////////////////////////////////////////////
         1664  +
         1665  +    private static bool IsTemporaryCatalogName(
         1666  +        string catalogName
         1667  +        )
         1668  +    {
         1669  +        return String.Compare(catalogName, GetTemporaryCatalogName(),
         1670  +            StringComparison.OrdinalIgnoreCase) == 0;
         1671  +    }
         1672  +
         1673  +    ///////////////////////////////////////////////////////////////////////////////////////////////
         1674  +
  1637   1675       private static string GetMasterTableName(
  1638   1676           bool temporary
  1639   1677           )
  1640   1678       {
  1641         -        return temporary ? _tempmasterdb : _masterdb;
         1679  +        return temporary ? _temporaryMasterTableName : _defaultMasterTableName;
  1642   1680       }
  1643   1681   
  1644   1682       ///////////////////////////////////////////////////////////////////////////////////////////////
  1645   1683   
  1646   1684       /// <summary>
  1647   1685       /// This event is raised whenever the database is opened or closed.
  1648   1686       /// </summary>
................................................................................
  1798   1836   
  1799   1837           // Reattach all attached databases from the existing connection
  1800   1838           using (DataTable tbl = connection.GetSchema("Catalogs"))
  1801   1839           {
  1802   1840             foreach (DataRow row in tbl.Rows)
  1803   1841             {
  1804   1842               string str = row[0].ToString();
  1805         -            if (String.Compare(str, "main", StringComparison.OrdinalIgnoreCase) != 0
  1806         -              && String.Compare(str, "temp", StringComparison.OrdinalIgnoreCase) != 0)
         1843  +
         1844  +            if (!IsDefaultCatalogName(str) && !IsTemporaryCatalogName(str))
  1807   1845               {
  1808   1846                 using (SQLiteCommand cmd = CreateCommand())
  1809   1847                 {
  1810   1848                   cmd.CommandText = HelperMethods.StringFormat(CultureInfo.InvariantCulture, "ATTACH DATABASE '{0}' AS [{1}]", row[1], row[0]);
  1811   1849                   cmd.ExecuteNonQuery();
  1812   1850                 }
  1813   1851               }
................................................................................
  3202   3240           {
  3203   3241               CheckDisposed();
  3204   3242   
  3205   3243               if (_sql == null)
  3206   3244                   throw new InvalidOperationException(
  3207   3245                       "Database connection not valid for getting file name.");
  3208   3246   
  3209         -            return _sql.GetFileName("main");
         3247  +            return _sql.GetFileName(GetDefaultCatalogName());
  3210   3248           }
  3211   3249       }
  3212   3250   
  3213   3251       /// <summary>
  3214   3252       /// Returns the string "main".
  3215   3253       /// </summary>
  3216   3254   #if !PLATFORM_COMPACTFRAMEWORK
................................................................................
  3217   3255       [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  3218   3256   #endif
  3219   3257       public override string Database
  3220   3258       {
  3221   3259         get
  3222   3260         {
  3223   3261           CheckDisposed();
  3224         -        return "main";
         3262  +        return GetDefaultCatalogName();
  3225   3263         }
  3226   3264       }
  3227   3265   
  3228   3266       internal static string MapUriPath(string path)
  3229   3267       {
  3230   3268           if (path.StartsWith ("file://", StringComparison.OrdinalIgnoreCase))
  3231   3269               return path.Substring (7);
................................................................................
  5752   5790         tbl.Columns.Add("PRIMARY_KEY", typeof(bool));
  5753   5791         tbl.Columns.Add("EDM_TYPE", typeof(string));
  5754   5792         tbl.Columns.Add("AUTOINCREMENT", typeof(bool));
  5755   5793         tbl.Columns.Add("UNIQUE", typeof(bool));
  5756   5794   
  5757   5795         tbl.BeginLoadData();
  5758   5796   
  5759         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         5797  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  5760   5798   
  5761         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         5799  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  5762   5800   
  5763   5801         using (SQLiteCommand cmdTables = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table' OR [type] LIKE 'view'", strCatalog, master), this))
  5764   5802         using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
  5765   5803         {
  5766   5804           while (rdTables.Read())
  5767   5805           {
  5768   5806             if (String.IsNullOrEmpty(strTable) || String.Compare(strTable, rdTables.GetString(2), StringComparison.OrdinalIgnoreCase) == 0)
................................................................................
  5855   5893         tbl.Columns.Add("PAGES", typeof(int));
  5856   5894         tbl.Columns.Add("FILTER_CONDITION", typeof(string));
  5857   5895         tbl.Columns.Add("INTEGRATED", typeof(bool));
  5858   5896         tbl.Columns.Add("INDEX_DEFINITION", typeof(string));
  5859   5897   
  5860   5898         tbl.BeginLoadData();
  5861   5899   
  5862         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         5900  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  5863   5901   
  5864         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         5902  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  5865   5903   
  5866   5904         using (SQLiteCommand cmdTables = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
  5867   5905         using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
  5868   5906         {
  5869   5907           while (rdTables.Read())
  5870   5908           {
  5871   5909             maybeRowId = false;
................................................................................
  6002   6040         tbl.Columns.Add("TABLE_NAME", typeof(string));
  6003   6041         tbl.Columns.Add("TRIGGER_NAME", typeof(string));
  6004   6042         tbl.Columns.Add("TRIGGER_DEFINITION", typeof(string));
  6005   6043   
  6006   6044         tbl.BeginLoadData();
  6007   6045   
  6008   6046         if (String.IsNullOrEmpty(table)) table = null;
  6009         -      if (String.IsNullOrEmpty(catalog)) catalog = "main";
  6010         -      string master = GetMasterTableName(String.Compare(catalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         6047  +      if (String.IsNullOrEmpty(catalog)) catalog = GetDefaultCatalogName();
         6048  +      string master = GetMasterTableName(IsTemporaryCatalogName(catalog));
  6011   6049   
  6012   6050         using (SQLiteCommand cmd = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT [type], [name], [tbl_name], [rootpage], [sql], [rowid] FROM [{0}].[{1}] WHERE [type] LIKE 'trigger'", catalog, master), this))
  6013   6051         using (SQLiteDataReader rd = (SQLiteDataReader)cmd.ExecuteReader())
  6014   6052         {
  6015   6053           while (rd.Read())
  6016   6054           {
  6017   6055             if (String.Compare(rd.GetString(1), triggerName, StringComparison.OrdinalIgnoreCase) == 0
................................................................................
  6056   6094         tbl.Columns.Add("TABLE_NAME", typeof(string));
  6057   6095         tbl.Columns.Add("TABLE_TYPE", typeof(string));
  6058   6096         tbl.Columns.Add("TABLE_ID", typeof(long));
  6059   6097         tbl.Columns.Add("TABLE_ROOTPAGE", typeof(int));
  6060   6098         tbl.Columns.Add("TABLE_DEFINITION", typeof(string));
  6061   6099         tbl.BeginLoadData();
  6062   6100   
  6063         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         6101  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  6064   6102   
  6065         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         6103  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  6066   6104   
  6067   6105         using (SQLiteCommand cmd = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT [type], [name], [tbl_name], [rootpage], [sql], [rowid] FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
  6068   6106         using (SQLiteDataReader rd = (SQLiteDataReader)cmd.ExecuteReader())
  6069   6107         {
  6070   6108           while (rd.Read())
  6071   6109           {
  6072   6110             strItem = rd.GetString(0);
................................................................................
  6122   6160         tbl.Columns.Add("IS_UPDATABLE", typeof(bool));
  6123   6161         tbl.Columns.Add("DESCRIPTION", typeof(string));
  6124   6162         tbl.Columns.Add("DATE_CREATED", typeof(DateTime));
  6125   6163         tbl.Columns.Add("DATE_MODIFIED", typeof(DateTime));
  6126   6164   
  6127   6165         tbl.BeginLoadData();
  6128   6166   
  6129         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         6167  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  6130   6168   
  6131         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         6169  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  6132   6170   
  6133   6171         using (SQLiteCommand cmd = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'view'", strCatalog, master), this))
  6134   6172         using (SQLiteDataReader rd = (SQLiteDataReader)cmd.ExecuteReader())
  6135   6173         {
  6136   6174           while (rd.Read())
  6137   6175           {
  6138   6176             if (String.Compare(rd.GetString(1), strView, StringComparison.OrdinalIgnoreCase) == 0
................................................................................
  6269   6307         tbl.Columns.Add("COLUMN_NAME", typeof(string));
  6270   6308         tbl.Columns.Add("ORDINAL_POSITION", typeof(int));
  6271   6309         tbl.Columns.Add("INDEX_NAME", typeof(string));
  6272   6310         tbl.Columns.Add("COLLATION_NAME", typeof(string));
  6273   6311         tbl.Columns.Add("SORT_MODE", typeof(string));
  6274   6312         tbl.Columns.Add("CONFLICT_OPTION", typeof(int));
  6275   6313   
  6276         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         6314  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  6277   6315   
  6278         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         6316  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  6279   6317   
  6280   6318         tbl.BeginLoadData();
  6281   6319   
  6282   6320         using (SQLiteCommand cmdTables = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
  6283   6321         using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
  6284   6322         {
  6285   6323           while (rdTables.Read())
................................................................................
  6431   6469         tbl.Columns.Add("COLLATION_SCHEMA", typeof(string));
  6432   6470         tbl.Columns.Add("COLLATION_NAME", typeof(string));
  6433   6471         tbl.Columns.Add("PRIMARY_KEY", typeof(bool));
  6434   6472         tbl.Columns.Add("EDM_TYPE", typeof(string));
  6435   6473         tbl.Columns.Add("AUTOINCREMENT", typeof(bool));
  6436   6474         tbl.Columns.Add("UNIQUE", typeof(bool));
  6437   6475   
  6438         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         6476  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  6439   6477   
  6440         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         6478  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  6441   6479   
  6442   6480         tbl.BeginLoadData();
  6443   6481   
  6444   6482         using (SQLiteCommand cmdViews = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'view'", strCatalog, master), this))
  6445   6483         using (SQLiteDataReader rdViews = cmdViews.ExecuteReader())
  6446   6484         {
  6447   6485           while (rdViews.Read())
................................................................................
  6536   6574         tbl.Columns.Add("FKEY_TO_SCHEMA", typeof(string));
  6537   6575         tbl.Columns.Add("FKEY_TO_TABLE", typeof(string));
  6538   6576         tbl.Columns.Add("FKEY_TO_COLUMN", typeof(string));
  6539   6577         tbl.Columns.Add("FKEY_ON_UPDATE", typeof(string));
  6540   6578         tbl.Columns.Add("FKEY_ON_DELETE", typeof(string));
  6541   6579         tbl.Columns.Add("FKEY_MATCH", typeof(string));
  6542   6580   
  6543         -      if (String.IsNullOrEmpty(strCatalog)) strCatalog = "main";
         6581  +      if (String.IsNullOrEmpty(strCatalog)) strCatalog = GetDefaultCatalogName();
  6544   6582   
  6545         -      string master = GetMasterTableName(String.Compare(strCatalog, "temp", StringComparison.OrdinalIgnoreCase) == 0);
         6583  +      string master = GetMasterTableName(IsTemporaryCatalogName(strCatalog));
  6546   6584   
  6547   6585         tbl.BeginLoadData();
  6548   6586   
  6549   6587         using (SQLiteCommand cmdTables = new SQLiteCommand(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "SELECT * FROM [{0}].[{1}] WHERE [type] LIKE 'table'", strCatalog, master), this))
  6550   6588         using (SQLiteDataReader rdTables = cmdTables.ExecuteReader())
  6551   6589         {
  6552   6590           while (rdTables.Read())