/ Check-in [441cabb6]
Login

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

Overview
Comment:Add missing comments and fix other code issues in the new functions in callback.c.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | reuse-schema
Files: files | file ages | folders
SHA3-256: 441cabb62fe14bbef8b19066941426eeb2de128564ec2bfb762228fdae794447
User & Date: dan 2019-02-14 15:47:18
Wiki:reuse-schema
Context
2019-02-14
15:56
Merge latest trunk into this branch. check-in: 577d1638 user: dan tags: reuse-schema
15:47
Add missing comments and fix other code issues in the new functions in callback.c. check-in: 441cabb6 user: dan tags: reuse-schema
2019-02-13
19:17
Fix for sqlite3_table_column_metadata() on REUSE_SCHEMA databases. check-in: 53220ad7 user: dan tags: reuse-schema
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/callback.c.

   538    538   /*
   539    539   ** Global linked list of SchemaPool objects. Read and write access must
   540    540   ** be protected by the SQLITE_MUTEX_STATIC_MASTER mutex.
   541    541   */
   542    542   static SchemaPool *SQLITE_WSD schemaPoolList = 0;
   543    543   
   544    544   #ifdef SQLITE_TEST
          545  +/*
          546  +** Return a pointer to the head of the linked list of SchemaPool objects.
          547  +** This is used by the virtual table in file test_schemapool.c.
          548  +*/
   545    549   SchemaPool *sqlite3SchemaPoolList(void){ return schemaPoolList; }
   546    550   #endif
   547    551   
   548    552   /*
   549         -** Check that the schema of db iDb is writable (either because it is the temp
   550         -** db schema or because the db handle was opened without
          553  +** Check that the schema of db iDb is writable (either because it is the 
          554  +** temp db schema or because the db handle was opened without
   551    555   ** SQLITE_OPEN_REUSE_SCHEMA). If so, do nothing. Otherwise, leave an 
   552    556   ** error in the Parse object.
   553    557   */
   554    558   void sqlite3SchemaWritable(Parse *pParse, int iDb){
   555    559     if( iDb!=1 && (pParse->db->openFlags & SQLITE_OPEN_REUSE_SCHEMA) 
   556    560      && IN_DECLARE_VTAB==0
   557    561     ){
   558    562       sqlite3ErrorMsg(pParse, "attempt to modify read-only schema");
   559    563     }
   560    564   }
   561    565   
          566  +/*
          567  +** The schema object passed as the only argument was allocated using
          568  +** sqlite3_malloc() and then populated using the usual mechanism. This
          569  +** function frees both the Schema object and its contents.
          570  +*/
   562    571   static void schemaDelete(Schema *pSchema){
   563    572     sqlite3SchemaClear((void*)pSchema);
   564    573     sqlite3_free(pSchema);
   565    574   }
   566    575   
          576  +/*
          577  +** When this function is called, the database connection Db must be
          578  +** using a schema-pool (Db.pSPool!=0) and must currently have Db.pSchema
          579  +** set to point to a populated schema object checked out from the 
          580  +** schema-pool. It is also assumed that the STATIC_MASTER mutex is held.
          581  +** This function returns the Schema object to the schema-pool and sets
          582  +** Db.pSchema to point to the schema-pool's static, empty, Schema object.
          583  +*/
   567    584   static void schemaRelease(Db *pDb){
   568    585     assert( pDb->pSPool && pDb->pSchema );
   569    586     assert( pDb->pSchema->schemaFlags & DB_SchemaLoaded );
   570    587     assert( sqlite3_mutex_held(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)) );
   571    588   
   572    589     pDb->pSchema->pNext = pDb->pSPool->pSchema;
   573    590     pDb->pSPool->pSchema = pDb->pSchema;
................................................................................
   577    594   }
   578    595   
   579    596   /*
   580    597   ** The schema for database iDb of database handle db, which was opened
   581    598   ** with SQLITE_OPEN_REUSE_SCHEMA, has just been parsed. This function either
   582    599   ** finds a matching SchemaPool object on the global list (schemaPoolList) or
   583    600   ** else allocates a new one and sets the Db.pSPool variable accordingly.
          601  +**
          602  +** SQLITE_OK is returned if no error occurs, or an SQLite error code 
          603  +** (SQLITE_NOMEM) otherwise.
   584    604   */
   585    605   int sqlite3SchemaConnect(sqlite3 *db, int iDb, u64 cksum){
   586    606     Schema *pSchema = db->aDb[iDb].pSchema;
   587    607     SchemaPool *p;
   588    608   
   589    609     assert( pSchema && iDb!=1 && db->aDb[iDb].pSPool==0 );
   590    610   
................................................................................
   625    645   
   626    646     sqlite3_mutex_leave( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER) );
   627    647   
   628    648     db->aDb[iDb].pSPool = p;
   629    649     return (p ? SQLITE_OK : SQLITE_NOMEM);
   630    650   }
   631    651   
          652  +/*
          653  +** If parameter iDb is 1 (the temp db), or if connection handle db was not
          654  +** opened with the SQLITE_OPEN_REUSE_SCHEMA flag, this function is a no-op.
          655  +** Otherwise, it disconnects from the schema-pool associated with database
          656  +** iDb, assuming it is connected.
          657  +**
          658  +** If parameter bNew is true, then Db.pSchema is set to point to a new, empty,
          659  +** Schema object obtained from sqlite3_malloc(). Or, if bNew is false, then
          660  +** Db.pSchema is set to NULL before returning.
          661  +**
          662  +** If the bNew parameter is true, then this function may allocate memory. 
          663  +** If the allocation attempt fails, then SQLITE_NOMEM is returned and the
          664  +** schema-pool is not disconnected from. Or, if no OOM error occurs, 
          665  +** SQLITE_OK is returned.
          666  +*/
   632    667   int sqlite3SchemaDisconnect(sqlite3 *db, int iDb, int bNew){
   633    668     int rc = SQLITE_OK;
   634    669     if( IsReuseSchema(db) && iDb!=1 ){
   635    670       Db *pDb = &db->aDb[iDb];
   636    671       SchemaPool *pSPool = pDb->pSPool;
   637    672       assert_schema_state_ok(db);
   638    673       assert( pDb->pSchema );
................................................................................
   700    735         pRet->pNext = 0;
   701    736       }
   702    737       sqlite3_mutex_leave( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER) );
   703    738     }
   704    739     return pRet;
   705    740   }
   706    741   
          742  +/*
          743  +** Return all sharable schemas held by database handle db back to their
          744  +** respective schema-pools. Db.pSchema variables are left pointing to
          745  +** the static, empty, Schema object owned by each schema-pool.
          746  +*/
   707    747   void sqlite3SchemaReleaseAll(sqlite3 *db){
   708    748     int i;
   709    749     assert_schema_state_ok(db);
   710    750     sqlite3_mutex_enter( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER) );
   711    751     for(i=0; i<db->nDb; i++){
   712    752       if( i!=1 ){
   713    753         Db *pDb = &db->aDb[i];
................................................................................
   715    755           schemaRelease(pDb);
   716    756         }
   717    757       }
   718    758     }
   719    759     sqlite3_mutex_leave( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER) );
   720    760   }
   721    761   
          762  +/*
          763  +** Release any sharable schema held by connection iDb of database handle
          764  +** db. Db.pSchema is left pointing to the static, empty, Schema object
          765  +** owned by the schema-pool.
          766  +*/
   722    767   void sqlite3SchemaRelease(sqlite3 *db, int iDb){
   723    768     Db *pDb = &db->aDb[iDb];
   724    769     assert( iDb!=1 );
   725    770     assert_schema_state_ok(db);
   726    771     sqlite3_mutex_enter( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER) );
   727    772     if( pDb->pSPool && DbHasProperty(db, iDb, DB_SchemaLoaded) ){
   728    773       schemaRelease(pDb);
   729    774     }
   730    775     sqlite3_mutex_leave( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER) );
   731    776   }
   732    777   
   733    778   /*
   734         -** Find and return the schema associated with a BTree.  Create
   735         -** a new one if necessary.
          779  +** In most cases, this function finds and returns the schema associated 
          780  +** with BTree handle pBt, creating a new one if necessary. However, if
          781  +** the database handle was opened with the SQLITE_OPEN_REUSE_SCHEMA flag
          782  +** specified, a new, empty, Schema object in memory obtained by 
          783  +** sqlite3_malloc() is always returned.
   736    784   */
   737    785   Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
   738    786     Schema *p;
   739    787     if( pBt && (db->openFlags & SQLITE_OPEN_REUSE_SCHEMA)==0 ){
   740    788       p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
   741    789     }else{
   742    790       p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));

Changes to test/reuse1.test.

    84     84     4  { ALTER TABLE t1 RENAME TO t3 }
    85     85     5  { ALTER TABLE t1 ADD COLUMN xyz }
    86     86     6  { VACUUM }
    87     87     7  { DROP INDEX i1 }
    88     88     8  { DROP TABLE t1 }
    89     89     9  { DROP TRIGGER tr1 }
    90     90     10 { ANALYZE }
           91  +  11 { ALTER TABLE t1 RENAME z TO zzz }
    91     92   } {
    92     93     do_catchsql_test 1.5.$tn $sql {1 {attempt to modify read-only schema}}
    93     94   }
    94     95   
    95     96   #-------------------------------------------------------------------------
    96     97   #
    97     98   reset_db