/ Check-in [983221b0]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Add pragma to set/get text encoding. Also fix an obscure problem where a temp trigger could be accidently dropped. (CVS 1537)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 983221b038813c5a7892564896e39597c223c4c3
User & Date: danielk1977 2004-06-07 07:52:18
Context
2004-06-07
10:00
Tables created with the CREATE TABLE <tbl> AS SELECT ... syntax now inherit column declaration types from the SELECT statement. (CVS 1538) check-in: 31c1668d user: danielk1977 tags: trunk
07:52
Add pragma to set/get text encoding. Also fix an obscure problem where a temp trigger could be accidently dropped. (CVS 1537) check-in: 983221b0 user: danielk1977 tags: trunk
01:52
Progress towards getting locking to work on windows. (CVS 1536) check-in: 4f7c0961 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/attach.c.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the ATTACH and DETACH commands.
**
** $Id: attach.c,v 1.12 2004/05/11 07:11:52 danielk1977 Exp $
*/
#include "sqliteInt.h"

/*
** This routine is called by the parser to process an ATTACH statement:
**
**     ATTACH DATABASE filename AS dbname
................................................................................
    sqliteCodecAttach(db, db->nDb-1, zKey, nKey);
  }
#endif
  sqliteFree(zFile);
  db->flags &= ~SQLITE_Initialized;
  if( pParse->nErr ) return;
  if( rc==SQLITE_OK ){
    rc = sqlite3Init(pParse->db, &pParse->zErrMsg);
  }
  if( rc ){
    int i = db->nDb - 1;
    assert( i>=2 );
    if( db->aDb[i].pBt ){
      sqlite3BtreeClose(db->aDb[i].pBt);
      db->aDb[i].pBt = 0;







|







 







|







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the ATTACH and DETACH commands.
**
** $Id: attach.c,v 1.13 2004/06/07 07:52:18 danielk1977 Exp $
*/
#include "sqliteInt.h"

/*
** This routine is called by the parser to process an ATTACH statement:
**
**     ATTACH DATABASE filename AS dbname
................................................................................
    sqliteCodecAttach(db, db->nDb-1, zKey, nKey);
  }
#endif
  sqliteFree(zFile);
  db->flags &= ~SQLITE_Initialized;
  if( pParse->nErr ) return;
  if( rc==SQLITE_OK ){
    rc = sqlite3ReadSchema(pParse->db);
  }
  if( rc ){
    int i = db->nDb - 1;
    assert( i>=2 );
    if( db->aDb[i].pBt ){
      sqlite3BtreeClose(db->aDb[i].pBt);
      db->aDb[i].pBt = 0;

Changes to src/build.c.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
..
34
35
36
37
38
39
40

41
42
43
44
45
46
47

48
49
50
51
52
53
54
...
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115
...
153
154
155
156
157
158
159

160
161
162
163
164
165
166
167
...
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
....
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412



1413
1414
1415
1416
1417
1418
1419
1420
1421
1422



1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434






1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
....
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
**     DROP INDEX
**     creating ID lists
**     BEGIN TRANSACTION
**     COMMIT
**     ROLLBACK
**     PRAGMA
**
** $Id: build.c,v 1.206 2004/06/03 16:08:41 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** This routine is called when a new SQL statement is beginning to
** be parsed.  Check to see if the schema for the database needs
................................................................................
** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
** If it does, then read it.
*/
void sqlite3BeginParse(Parse *pParse, int explainFlag){
  sqlite *db = pParse->db;
  int i;
  pParse->explain = explainFlag;

  if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
    int rc = sqlite3Init(db, &pParse->zErrMsg);
    if( rc!=SQLITE_OK ){
      pParse->rc = rc;
      pParse->nErr++;
    }
  }

  for(i=0; i<db->nDb; i++){
    DbClearProperty(db, i, DB_Locked);
    if( !db->aDb[i].inTrans ){
      DbClearProperty(db, i, DB_Cookie);
    }
  }
  pParse->nVar = 0;
................................................................................
** using the ATTACH command.
**
** See also sqlite3LocateTable().
*/
Table *sqlite3FindTable(sqlite *db, const char *zName, const char *zDatabase){
  Table *p = 0;
  int i;

  for(i=0; i<db->nDb; i++){
    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
    p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
    if( p ) break;
  }
  return p;
}
................................................................................
** for duplicate index names is done.)  The search order is
** TEMP first, then MAIN, then any auxiliary databases added
** using the ATTACH command.
*/
Index *sqlite3FindIndex(sqlite *db, const char *zName, const char *zDb){
  Index *p = 0;
  int i;

  for(i=0; i<db->nDb; i++){
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
    p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
    if( p ) break;
  }
  return p;
}
................................................................................
  int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);

  sqlite3Randomness(1, &r);
  *pSchemaCookie = *pSchemaCookie + r + 1;
  db->flags |= SQLITE_InternChanges;
  sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
  sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
/*
  if( db->next_cookie==db->aDb[0].schema_cookie ){
    unsigned char r;
    sqlite3Randomness(1, &r);
    db->next_cookie = db->aDb[0].schema_cookie + r + 1;
    db->flags |= SQLITE_InternChanges;
    sqlite3VdbeAddOp(v, OP_Integer, db->next_cookie, 0);
    sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
  }
*/
}

/*
** Measure the number of characters needed to output the given
** identifier.  The number returned includes any quotes used
** but does not include the null terminator.
*/
................................................................................

  /* Generate code to remove the table from the master table
  ** on disk.
  */
  v = sqlite3GetVdbe(pParse);
  if( v ){
    static VdbeOpList dropTable[] = {
      { OP_Rewind,     0, ADDR(10), 0},
      { OP_String8,     0, 0,        0}, /* 1 */
      { OP_MemStore,   1, 1,        0},
      { OP_MemLoad,    1, 0,        0}, /* 3 */
      { OP_Column,     0, 2,        0},
      { OP_Ne,         0, ADDR(9),  0},



      { OP_Delete,     0, 0,        0},
      { OP_Rewind,     0, ADDR(10), 0},
      { OP_Goto,       0, ADDR(3),  0},
      { OP_Next,       0, ADDR(3),  0}, /* 9 */
    };
    Index *pIdx;
    Trigger *pTrigger;
    sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);

    /* Drop all triggers associated with the table being dropped */



    pTrigger = pTab->pTrigger;
    while( pTrigger ){
      assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
      sqlite3DropTriggerPtr(pParse, pTrigger, 1);
      if( pParse->explain ){
        pTrigger = pTrigger->pNext;
      }else{
        pTrigger = pTab->pTrigger;
      }
    }

    /* Drop all SQLITE_MASTER entries that refer to the table */






    sqlite3OpenMasterTable(v, pTab->iDb);
    base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
    sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);

    /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
    if( pTab->iDb!=1 ){
      sqlite3OpenMasterTable(v, 1);
      base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
      sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
    }

    if( pTab->iDb!=1 ){  /* Temp database has no schema cookie */
      sqlite3ChangeCookie(db, v, pTab->iDb);
    }
    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
    if( !isView ){
      sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
        sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
      }
    }
................................................................................
** specified auxiliary database and the temp database are made writable.
*/
void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
  sqlite *db = pParse->db;
  Vdbe *v = sqlite3GetVdbe(pParse);
  if( v==0 ) return;
  sqlite3VdbeAddOp(v, OP_Transaction, iDb, 1);
  if( iDb!=1 && (iDb>63 || !(pParse->cookieMask & ((u64)1<<iDb))) ){
    sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
    pParse->cookieMask |= ((u64)1<<iDb);
  }
  if( setStatement ){
    sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
  }
  if( iDb!=1 ){







|







 







>







>







 







>
|







 







>
|







 







<
<
<
<
<
<
<
<
<
<







 







|
|


|
|
>
>
>

|

|





|
>
>
>











|
>
>
>
>
>
>



<
<
<
<
<
<
<
<
<
|
<







 







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
..
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
...
931
932
933
934
935
936
937










938
939
940
941
942
943
944
....
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443









1444

1445
1446
1447
1448
1449
1450
1451
....
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
**     DROP INDEX
**     creating ID lists
**     BEGIN TRANSACTION
**     COMMIT
**     ROLLBACK
**     PRAGMA
**
** $Id: build.c,v 1.207 2004/06/07 07:52:18 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** This routine is called when a new SQL statement is beginning to
** be parsed.  Check to see if the schema for the database needs
................................................................................
** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
** If it does, then read it.
*/
void sqlite3BeginParse(Parse *pParse, int explainFlag){
  sqlite *db = pParse->db;
  int i;
  pParse->explain = explainFlag;
#if 0
  if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
    int rc = sqlite3Init(db, &pParse->zErrMsg);
    if( rc!=SQLITE_OK ){
      pParse->rc = rc;
      pParse->nErr++;
    }
  }
#endif
  for(i=0; i<db->nDb; i++){
    DbClearProperty(db, i, DB_Locked);
    if( !db->aDb[i].inTrans ){
      DbClearProperty(db, i, DB_Cookie);
    }
  }
  pParse->nVar = 0;
................................................................................
** using the ATTACH command.
**
** See also sqlite3LocateTable().
*/
Table *sqlite3FindTable(sqlite *db, const char *zName, const char *zDatabase){
  Table *p = 0;
  int i;
  int rc = sqlite3ReadSchema(db);
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
    p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
    if( p ) break;
  }
  return p;
}
................................................................................
** for duplicate index names is done.)  The search order is
** TEMP first, then MAIN, then any auxiliary databases added
** using the ATTACH command.
*/
Index *sqlite3FindIndex(sqlite *db, const char *zName, const char *zDb){
  Index *p = 0;
  int i;
  int rc = sqlite3ReadSchema(db);
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
    p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
    if( p ) break;
  }
  return p;
}
................................................................................
  int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);

  sqlite3Randomness(1, &r);
  *pSchemaCookie = *pSchemaCookie + r + 1;
  db->flags |= SQLITE_InternChanges;
  sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
  sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);










}

/*
** Measure the number of characters needed to output the given
** identifier.  The number returned includes any quotes used
** but does not include the null terminator.
*/
................................................................................

  /* Generate code to remove the table from the master table
  ** on disk.
  */
  v = sqlite3GetVdbe(pParse);
  if( v ){
    static VdbeOpList dropTable[] = {
      { OP_Rewind,     0, ADDR(13), 0},
      { OP_String8,    0, 0,        0}, /* 1 */
      { OP_MemStore,   1, 1,        0},
      { OP_MemLoad,    1, 0,        0}, /* 3 */
      { OP_Column,     0, 2,        0}, /* sqlite_master.tbl_name */
      { OP_Ne,         0, ADDR(12), 0},
      { OP_String8,    0, 0,        "trigger"},
      { OP_Column,     0, 2,        0}, /* sqlite_master.type */
      { OP_Eq,         0, ADDR(12), 0},
      { OP_Delete,     0, 0,        0},
      { OP_Rewind,     0, ADDR(13), 0},
      { OP_Goto,       0, ADDR(3),  0},
      { OP_Next,       0, ADDR(3),  0}, /* 12 */
    };
    Index *pIdx;
    Trigger *pTrigger;
    sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);

    /* Drop all triggers associated with the table being dropped. Code
    ** is generated to remove entries from sqlite_master and/or
    ** sqlite_temp_master if required.
    */
    pTrigger = pTab->pTrigger;
    while( pTrigger ){
      assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
      sqlite3DropTriggerPtr(pParse, pTrigger, 1);
      if( pParse->explain ){
        pTrigger = pTrigger->pNext;
      }else{
        pTrigger = pTab->pTrigger;
      }
    }

    /* Drop all SQLITE_MASTER table and index entries that refer to the
    ** table. The program name loops through the master table and deletes
    ** every row that refers to a table of the same name as the one being
    ** dropped. Triggers are handled seperately because a trigger can be
    ** created in the temp database that refers to a table in another
    ** database.
    */
    sqlite3OpenMasterTable(v, pTab->iDb);
    base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
    sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);









    sqlite3ChangeCookie(db, v, pTab->iDb);

    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
    if( !isView ){
      sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
        sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
      }
    }
................................................................................
** specified auxiliary database and the temp database are made writable.
*/
void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
  sqlite *db = pParse->db;
  Vdbe *v = sqlite3GetVdbe(pParse);
  if( v==0 ) return;
  sqlite3VdbeAddOp(v, OP_Transaction, iDb, 1);
  if( (iDb>63 || !(pParse->cookieMask & ((u64)1<<iDb))) ){
    sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
    pParse->cookieMask |= ((u64)1<<iDb);
  }
  if( setStatement ){
    sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
  }
  if( iDb!=1 ){

Changes to src/main.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
...
377
378
379
380
381
382
383






















384
385
386
387
388
389
390
...
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
...
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
....
1026
1027
1028
1029
1030
1031
1032
1033



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043


1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
....
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
....
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088


1089
1090
1091

1092
1093
1094
1095
1096
1097
1098
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.205 2004/06/06 09:44:04 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** A pointer to this structure is used to communicate information
................................................................................
/*
** Initialize all database files - the main database file, the file
** used to store temporary tables, and any additional database files
** created using ATTACH statements.  Return a success code.  If an
** error occurs, write an error message into *pzErrMsg.
**
** After the database is initialized, the SQLITE_Initialized
** bit is set in the flags field of the sqlite structure.  An
** attempt is made to initialize the database as soon as it
** is opened.  If that fails (perhaps because another process
** has the sqlite_master table locked) than another attempt
** is made the first time the database is accessed.
*/
int sqlite3Init(sqlite *db, char **pzErrMsg){
  int i, rc;
  
  if( db->init.busy ) return SQLITE_OK;
  assert( (db->flags & SQLITE_Initialized)==0 );
  rc = SQLITE_OK;
................................................................................
  }

  if( rc!=SQLITE_OK ){
    db->flags &= ~SQLITE_Initialized;
  }
  return rc;
}























/*
** The version of the library
*/
const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
const char sqlite3_version[] = SQLITE_VERSION;

................................................................................
  int rc = SQLITE_OK;

  if( sqlite3SafetyOn(db) ){
    rc = SQLITE_MISUSE;
    goto prepare_out;
  }

  if( !db->init.busy ){
    if( (db->flags & SQLITE_Initialized)==0 ){
      int cnt = 1;
      rc = sqlite3Init(db, &zErrMsg);
      if( rc!=SQLITE_OK ){
        goto prepare_out;
      }
      if( zErrMsg ){
        sqliteFree(zErrMsg);
        zErrMsg = 0;
      }
    }
  }
  assert( (db->flags & SQLITE_Initialized)!=0 || db->init.busy );

  if( db->pVdbe==0 ){ db->nChange = 0; }
  memset(&sParse, 0, sizeof(sParse));
  sParse.db = db;
  sqlite3RunParser(&sParse, zSql, &zErrMsg);

  if( db->xTrace && !db->init.busy ){
    /* Trace only the statment that was compiled.
................................................................................
** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_*
** macros from sqliteInt.h. If we end up creating a new database file
** (not opening an existing one), the text encoding of the database
** will be set to this value.
*/
static int openDatabase(
  const char *zFilename, /* Database filename UTF-8 encoded */
  sqlite3 **ppDb,        /* OUT: Returned database handle */
  const char **options,  /* Null terminated list of db options, or null */
  u8 def_enc             /* One of TEXT_Utf8, TEXT_Utf16le or TEXT_Utf16be */
){
  sqlite3 *db;
  int rc, i;
  char *zErrMsg = 0;

#ifdef SQLITE_TEST
  for(i=0; options && options[i]; i++){
    char const *zOpt = options[i];
    if( 0==sqlite3StrICmp(zOpt, "-utf8") ){
      def_enc = TEXT_Utf8;
    }else if( 0==sqlite3StrICmp(zOpt, "-utf16le") ){
      def_enc = TEXT_Utf16le;
    }else if( 0==sqlite3StrICmp(zOpt, "-utf16be") ){
      def_enc = TEXT_Utf16be;
    }
  }
#endif

  /* Allocate the sqlite data structure */
  db = sqliteMalloc( sizeof(sqlite) );
  if( db==0 ) goto opendb_out;
  db->priorNewRowid = 0;
  db->magic = SQLITE_MAGIC_BUSY;
  db->nDb = 2;
  db->aDb = db->aDbStatic;
  db->enc = def_enc;
  db->autoCommit = 1;
  /* db->flags |= SQLITE_ShortColNames; */
  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
  for(i=0; i<db->nDb; i++){
    sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
    sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
................................................................................
    sqlite3Error(db, rc, 0);
    db->magic = SQLITE_MAGIC_CLOSED;
    goto opendb_out;
  }
  db->aDb[0].zName = "main";
  db->aDb[1].zName = "temp";

  /* Attempt to read the schema */



  sqlite3RegisterBuiltinFunctions(db);
  rc = sqlite3Init(db, &zErrMsg);
  if( sqlite3_malloc_failed ){
    sqlite3_close(db);
    db = 0;
    goto opendb_out;
  }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
    sqlite3Error(db, rc, "%s", zErrMsg, 0);
    db->magic = SQLITE_MAGIC_CLOSED;
  }else{


    db->magic = SQLITE_MAGIC_OPEN;
  }
  if( zErrMsg ) sqliteFree(zErrMsg);

opendb_out:
  *ppDb = db;
  return sqlite3_errcode(db);
}

/*
................................................................................
** Open a new database handle.
*/
int sqlite3_open(
  const char *zFilename, 
  sqlite3 **ppDb, 
  const char **options
){
  return openDatabase(zFilename, ppDb, options, TEXT_Utf8);
}

/*
** Open a new database handle.
*/
int sqlite3_open16(
  const void *zFilename, 
................................................................................
  assert( ppDb );

  zFilename8 = sqlite3utf16to8(zFilename, -1, SQLITE_BIGENDIAN);
  if( !zFilename8 ){
    *ppDb = 0;
    return SQLITE_NOMEM;
  }

  /* FIX ME: Also need to translate the option strings */

  if( SQLITE_BIGENDIAN ){
    rc = openDatabase(zFilename8, ppDb, options, TEXT_Utf16be);
  }else{
    rc = openDatabase(zFilename8, ppDb, options, TEXT_Utf16le);


  }

  sqliteFree(zFilename8);

  return rc;
}

/*
** The following routine destroys a virtual machine that is created by
** the sqlite3_compile() routine. The integer returned is an SQLITE_
** success/failure code that describes the result of executing the virtual







|







 







|
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<





<
<
<
<
<
<
<
<
<
<
<
<
<







|







 







|
>
>
>

<
<
<
|
<
<
<
|

>
>
|

<







 







|







 







<
<
<
<
|
<
<
>
>

<

>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
345
346
347
348
349
350
351
352




353
354
355
356
357
358
359
...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
...
859
860
861
862
863
864
865















866
867
868
869
870
871
872
...
973
974
975
976
977
978
979
980


981
982
983
984
985













986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
....
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025



1026



1027
1028
1029
1030
1031
1032

1033
1034
1035
1036
1037
1038
1039
....
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
....
1061
1062
1063
1064
1065
1066
1067




1068


1069
1070
1071

1072
1073
1074
1075
1076
1077
1078
1079
1080
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.206 2004/06/07 07:52:18 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** A pointer to this structure is used to communicate information
................................................................................
/*
** Initialize all database files - the main database file, the file
** used to store temporary tables, and any additional database files
** created using ATTACH statements.  Return a success code.  If an
** error occurs, write an error message into *pzErrMsg.
**
** After the database is initialized, the SQLITE_Initialized
** bit is set in the flags field of the sqlite structure. 




*/
int sqlite3Init(sqlite *db, char **pzErrMsg){
  int i, rc;
  
  if( db->init.busy ) return SQLITE_OK;
  assert( (db->flags & SQLITE_Initialized)==0 );
  rc = SQLITE_OK;
................................................................................
  }

  if( rc!=SQLITE_OK ){
    db->flags &= ~SQLITE_Initialized;
  }
  return rc;
}

/*
** This routine is a no-op if the database schema is already initialised.
** Otherwise, the schema is loaded. An error code is returned.
*/
int sqlite3ReadSchema(sqlite *db){
  int rc = SQLITE_OK;
  char *zErrMsg = 0;

  if( !db->init.busy ){
    if( (db->flags & SQLITE_Initialized)==0 ){
      rc = sqlite3Init(db, &zErrMsg);
    }
  }
  assert( (db->flags & SQLITE_Initialized)!=0 || db->init.busy );

  sqlite3Error(db, rc, zErrMsg);
  if( zErrMsg ){
    sqliteFree(zErrMsg);
  }
  return rc;
}

/*
** The version of the library
*/
const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
const char sqlite3_version[] = SQLITE_VERSION;

................................................................................
  int rc = SQLITE_OK;

  if( sqlite3SafetyOn(db) ){
    rc = SQLITE_MISUSE;
    goto prepare_out;
  }
















  if( db->pVdbe==0 ){ db->nChange = 0; }
  memset(&sParse, 0, sizeof(sParse));
  sParse.db = db;
  sqlite3RunParser(&sParse, zSql, &zErrMsg);

  if( db->xTrace && !db->init.busy ){
    /* Trace only the statment that was compiled.
................................................................................
** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_*
** macros from sqliteInt.h. If we end up creating a new database file
** (not opening an existing one), the text encoding of the database
** will be set to this value.
*/
static int openDatabase(
  const char *zFilename, /* Database filename UTF-8 encoded */
  sqlite3 **ppDb         /* OUT: Returned database handle */


){
  sqlite3 *db;
  int rc, i;
  char *zErrMsg = 0;














  /* Allocate the sqlite data structure */
  db = sqliteMalloc( sizeof(sqlite) );
  if( db==0 ) goto opendb_out;
  db->priorNewRowid = 0;
  db->magic = SQLITE_MAGIC_BUSY;
  db->nDb = 2;
  db->aDb = db->aDbStatic;
  db->enc = TEXT_Utf8;
  db->autoCommit = 1;
  /* db->flags |= SQLITE_ShortColNames; */
  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
  for(i=0; i<db->nDb; i++){
    sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
    sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
................................................................................
    sqlite3Error(db, rc, 0);
    db->magic = SQLITE_MAGIC_CLOSED;
    goto opendb_out;
  }
  db->aDb[0].zName = "main";
  db->aDb[1].zName = "temp";

  /* Register all built-in functions, but do not attempt to read the
  ** database schema yet. This is delayed until the first time the database
  ** is accessed.
  */
  sqlite3RegisterBuiltinFunctions(db);



  if( rc==SQLITE_OK ){



    db->magic = SQLITE_MAGIC_OPEN;
  }else{
    sqlite3Error(db, rc, "%s", zErrMsg, 0);
    if( zErrMsg ) sqliteFree(zErrMsg);
    db->magic = SQLITE_MAGIC_CLOSED;
  }


opendb_out:
  *ppDb = db;
  return sqlite3_errcode(db);
}

/*
................................................................................
** Open a new database handle.
*/
int sqlite3_open(
  const char *zFilename, 
  sqlite3 **ppDb, 
  const char **options
){
  return openDatabase(zFilename, ppDb);
}

/*
** Open a new database handle.
*/
int sqlite3_open16(
  const void *zFilename, 
................................................................................
  assert( ppDb );

  zFilename8 = sqlite3utf16to8(zFilename, -1, SQLITE_BIGENDIAN);
  if( !zFilename8 ){
    *ppDb = 0;
    return SQLITE_NOMEM;
  }




  rc = openDatabase(zFilename8, ppDb);


  if( rc==SQLITE_OK && *ppDb ){
    sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
  }

  sqliteFree(zFilename8);

  return rc;
}

/*
** The following routine destroys a virtual machine that is created by
** the sqlite3_compile() routine. The integer returned is an SQLITE_
** success/failure code that describes the result of executing the virtual

Changes to src/pragma.c.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
199
200
201
202
203
204
205

206
207
208
209
210
211
212
...
238
239
240
241
242
243
244

245
246
247
248
249
250
251
...
288
289
290
291
292
293
294

295
296
297
298
299
300
301
...
331
332
333
334
335
336
337

338
339
340
341
342
343
344
...
363
364
365
366
367
368
369

370
371
372
373
374
375
376
...
391
392
393
394
395
396
397

398
399
400
401
402
403
404
...
413
414
415
416
417
418
419

420
421
422
423
424
425
426
...
438
439
440
441
442
443
444

445
446
447
448
449
450
451
...
470
471
472
473
474
475
476

477
478
479
480
481
482
483
...
501
502
503
504
505
506
507

508
509
510
511
512
513
514
...
526
527
528
529
530
531
532

533
534
535
536
537
538
539
...
541
542
543
544
545
546
547

548
549
550
551
552
553
554
...
573
574
575
576
577
578
579

580
581
582
583
584
585
586
...
676
677
678
679
680
681
682















































683























684
685
686
687
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the PRAGMA command.
**
** $Id: pragma.c,v 1.36 2004/06/03 16:08:42 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** Interpret the given string as a boolean value.
*/
................................................................................
      { OP_Dup,         0, 0,        0},
      { OP_Integer,     0, 0,        0},
      { OP_Ne,          0, 6,        0},
      { OP_Integer,     0, 0,        0},  /* 5 */
      { OP_Callback,    1, 0,        0},
    };
    int addr;

    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
      sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
    }else{
      int size = atoi(zRight);
................................................................................
  ** to its default value when the database is closed and reopened.
  ** N should be a positive integer.
  */
  if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
    static VdbeOpList getCacheSize[] = {
      { OP_Callback,    1, 0,        0},
    };

    if( pRight->z==pLeft->z ){
      int size = db->cache_size;;
      if( size<0 ) size = -size;
      sqlite3VdbeAddOp(v, OP_Integer, size, 0);
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
      sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
................................................................................
      { OP_AddImm,      1, 0,        0},
      { OP_Callback,    1, 0,        0},
      { OP_Halt,        0, 0,        0},
      { OP_AddImm,     -1, 0,        0},  /* 9 */
      { OP_Callback,    1, 0,        0}
    };
    int addr;

    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "synchronous", P3_STATIC);
      addr = sqlite3VdbeAddOpList(v, ArraySize(getSync), getSync);
      sqlite3VdbeChangeP2(v, addr+2, addr+9);
    }else{
      int size = db->cache_size;
................................................................................
  ** default value will be restored the next time the database is
  ** opened.
  */
  if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
    static VdbeOpList getSync[] = {
      { OP_Callback,    1, 0,        0},
    };

    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "synchronous", P3_STATIC);
      sqlite3VdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
      sqlite3VdbeAddOpList(v, ArraySize(getSync), getSync);
    }else{
      int size = db->cache_size;
................................................................................

  if( flagPragma(pParse, zLeft, zRight) ){
    /* The flagPragma() call also generates any necessary code */
  }else

  if( sqlite3StrICmp(zLeft, "table_info")==0 ){
    Table *pTab;

    pTab = sqlite3FindTable(db, zRight, 0);
    if( pTab ){
      int i;
      sqlite3VdbeSetNumCols(v, 6);
      sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC);
      sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
      sqlite3VdbeSetColName(v, 2, "type", P3_STATIC);
................................................................................
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "index_info")==0 ){
    Index *pIdx;
    Table *pTab;

    pIdx = sqlite3FindIndex(db, zRight, 0);
    if( pIdx ){
      int i;
      pTab = pIdx->pTable;
      sqlite3VdbeSetNumCols(v, 3);
      sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC);
      sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC);
................................................................................
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "index_list")==0 ){
    Index *pIdx;
    Table *pTab;

    pTab = sqlite3FindTable(db, zRight, 0);
    if( pTab ){
      v = sqlite3GetVdbe(pParse);
      pIdx = pTab->pIndex;
    }
    if( pTab && pIdx ){
      int i = 0; 
................................................................................
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 ){
    FKey *pFK;
    Table *pTab;

    pTab = sqlite3FindTable(db, zRight, 0);
    if( pTab ){
      v = sqlite3GetVdbe(pParse);
      pFK = pTab->pFKey;
    }
    if( pTab && pFK ){
      int i = 0; 
................................................................................
        pFK = pFK->pNextFrom;
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "database_list")==0 ){
    int i;

    sqlite3VdbeSetNumCols(v, 3);
    sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
    sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
    sqlite3VdbeSetColName(v, 2, "file", P3_STATIC);
    for(i=0; i<db->nDb; i++){
      if( db->aDb[i].pBt==0 ) continue;
      assert( db->aDb[i].zName!=0 );
................................................................................
  ** Note that it is possible for the library compile-time options to
  ** override this setting
  */
  if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
    static VdbeOpList getTmpDbLoc[] = {
      { OP_Callback,    1, 0,        0},
    };

    if( pRight->z==pLeft->z ){
      sqlite3VdbeAddOp(v, OP_Integer, db->temp_store, 0);
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "temp_store", P3_STATIC);
      sqlite3VdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
    }else{
      changeTempStorage(pParse, zRight);
................................................................................
  ** Note that it is possible for the library compile-time options to
  ** override this setting
  */
  if( sqlite3StrICmp(zLeft, "default_temp_store")==0 ){
    static VdbeOpList getTmpDbLoc[] = {
      { OP_ReadCookie,  0, 5,        0},
      { OP_Callback,    1, 0,        0}};

    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "temp_store", P3_STATIC);
      sqlite3VdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
    }else{
      sqlite3BeginWriteOperation(pParse, 0, 0);
      sqlite3VdbeAddOp(v, OP_Integer, getTempStore(zRight), 0);
................................................................................
      sqlite3EndWriteOperation(pParse);
    }
  }else

#ifndef NDEBUG
  if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
    extern void sqlite3ParserTrace(FILE*, char *);

    if( getBoolean(zRight) ){
      sqlite3ParserTrace(stdout, "parser: ");
    }else{
      sqlite3ParserTrace(0, 0);
    }
  }else
#endif
................................................................................
      { OP_Integer,     0, 0,        0},
      { OP_Ne,          0, 0,        0},    /* 2 */
      { OP_String8,      0, 0,        "ok"},
      { OP_Callback,    1, 0,        0},
    };

    /* Initialize the VDBE program */

    sqlite3VdbeSetNumCols(v, 1);
    sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC);
    sqlite3VdbeAddOpList(v, ArraySize(initCode), initCode);

    /* Do an integrity check on each database file */
    for(i=0; i<db->nDb; i++){
      HashElem *x;
................................................................................
          sqlite3VdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC);
        }
      } 
    }
    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
    sqlite3VdbeChangeP2(v, addr+2, addr+ArraySize(endCode));
  }else







































































  {}
  sqliteFree(zLeft);
  sqliteFree(zRight);
}







|







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







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




7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
...
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
...
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
...
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
...
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
...
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
...
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
...
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the PRAGMA command.
**
** $Id: pragma.c,v 1.37 2004/06/07 07:52:18 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** Interpret the given string as a boolean value.
*/
................................................................................
      { OP_Dup,         0, 0,        0},
      { OP_Integer,     0, 0,        0},
      { OP_Ne,          0, 6,        0},
      { OP_Integer,     0, 0,        0},  /* 5 */
      { OP_Callback,    1, 0,        0},
    };
    int addr;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
      sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
    }else{
      int size = atoi(zRight);
................................................................................
  ** to its default value when the database is closed and reopened.
  ** N should be a positive integer.
  */
  if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
    static VdbeOpList getCacheSize[] = {
      { OP_Callback,    1, 0,        0},
    };
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( pRight->z==pLeft->z ){
      int size = db->cache_size;;
      if( size<0 ) size = -size;
      sqlite3VdbeAddOp(v, OP_Integer, size, 0);
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
      sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
................................................................................
      { OP_AddImm,      1, 0,        0},
      { OP_Callback,    1, 0,        0},
      { OP_Halt,        0, 0,        0},
      { OP_AddImm,     -1, 0,        0},  /* 9 */
      { OP_Callback,    1, 0,        0}
    };
    int addr;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "synchronous", P3_STATIC);
      addr = sqlite3VdbeAddOpList(v, ArraySize(getSync), getSync);
      sqlite3VdbeChangeP2(v, addr+2, addr+9);
    }else{
      int size = db->cache_size;
................................................................................
  ** default value will be restored the next time the database is
  ** opened.
  */
  if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
    static VdbeOpList getSync[] = {
      { OP_Callback,    1, 0,        0},
    };
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "synchronous", P3_STATIC);
      sqlite3VdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
      sqlite3VdbeAddOpList(v, ArraySize(getSync), getSync);
    }else{
      int size = db->cache_size;
................................................................................

  if( flagPragma(pParse, zLeft, zRight) ){
    /* The flagPragma() call also generates any necessary code */
  }else

  if( sqlite3StrICmp(zLeft, "table_info")==0 ){
    Table *pTab;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    pTab = sqlite3FindTable(db, zRight, 0);
    if( pTab ){
      int i;
      sqlite3VdbeSetNumCols(v, 6);
      sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC);
      sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
      sqlite3VdbeSetColName(v, 2, "type", P3_STATIC);
................................................................................
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "index_info")==0 ){
    Index *pIdx;
    Table *pTab;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    pIdx = sqlite3FindIndex(db, zRight, 0);
    if( pIdx ){
      int i;
      pTab = pIdx->pTable;
      sqlite3VdbeSetNumCols(v, 3);
      sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC);
      sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC);
................................................................................
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "index_list")==0 ){
    Index *pIdx;
    Table *pTab;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    pTab = sqlite3FindTable(db, zRight, 0);
    if( pTab ){
      v = sqlite3GetVdbe(pParse);
      pIdx = pTab->pIndex;
    }
    if( pTab && pIdx ){
      int i = 0; 
................................................................................
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 ){
    FKey *pFK;
    Table *pTab;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    pTab = sqlite3FindTable(db, zRight, 0);
    if( pTab ){
      v = sqlite3GetVdbe(pParse);
      pFK = pTab->pFKey;
    }
    if( pTab && pFK ){
      int i = 0; 
................................................................................
        pFK = pFK->pNextFrom;
      }
    }
  }else

  if( sqlite3StrICmp(zLeft, "database_list")==0 ){
    int i;
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    sqlite3VdbeSetNumCols(v, 3);
    sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
    sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
    sqlite3VdbeSetColName(v, 2, "file", P3_STATIC);
    for(i=0; i<db->nDb; i++){
      if( db->aDb[i].pBt==0 ) continue;
      assert( db->aDb[i].zName!=0 );
................................................................................
  ** Note that it is possible for the library compile-time options to
  ** override this setting
  */
  if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
    static VdbeOpList getTmpDbLoc[] = {
      { OP_Callback,    1, 0,        0},
    };
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( pRight->z==pLeft->z ){
      sqlite3VdbeAddOp(v, OP_Integer, db->temp_store, 0);
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "temp_store", P3_STATIC);
      sqlite3VdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
    }else{
      changeTempStorage(pParse, zRight);
................................................................................
  ** Note that it is possible for the library compile-time options to
  ** override this setting
  */
  if( sqlite3StrICmp(zLeft, "default_temp_store")==0 ){
    static VdbeOpList getTmpDbLoc[] = {
      { OP_ReadCookie,  0, 5,        0},
      { OP_Callback,    1, 0,        0}};
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( pRight->z==pLeft->z ){
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "temp_store", P3_STATIC);
      sqlite3VdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
    }else{
      sqlite3BeginWriteOperation(pParse, 0, 0);
      sqlite3VdbeAddOp(v, OP_Integer, getTempStore(zRight), 0);
................................................................................
      sqlite3EndWriteOperation(pParse);
    }
  }else

#ifndef NDEBUG
  if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
    extern void sqlite3ParserTrace(FILE*, char *);
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    if( getBoolean(zRight) ){
      sqlite3ParserTrace(stdout, "parser: ");
    }else{
      sqlite3ParserTrace(0, 0);
    }
  }else
#endif
................................................................................
      { OP_Integer,     0, 0,        0},
      { OP_Ne,          0, 0,        0},    /* 2 */
      { OP_String8,      0, 0,        "ok"},
      { OP_Callback,    1, 0,        0},
    };

    /* Initialize the VDBE program */
    if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
    sqlite3VdbeSetNumCols(v, 1);
    sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC);
    sqlite3VdbeAddOpList(v, ArraySize(initCode), initCode);

    /* Do an integrity check on each database file */
    for(i=0; i<db->nDb; i++){
      HashElem *x;
................................................................................
          sqlite3VdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC);
        }
      } 
    }
    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
    sqlite3VdbeChangeP2(v, addr+2, addr+ArraySize(endCode));
  }else
  /*
  **   PRAGMA encoding
  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
  **
  ** In it's first form, this pragma returns the encoding of the main
  ** database. If the database is not initialized, it is initialized now.
  **
  ** The second form of this pragma is a no-op if the main database file
  ** has not already been initialized. In this case it sets the default
  ** encoding that will be used for the main database file if a new file
  ** is created. If an existing main database file is opened, then the
  ** default text encoding for the existing database is used.
  ** 
  ** In all cases new databases created using the ATTACH command are
  ** created to use the same default text encoding as the main database. If
  ** the main database has not been initialized and/or created when ATTACH
  ** is executed, this is done before the ATTACH operation.
  **
  ** In the second form this pragma sets the text encoding to be used in
  ** new database files created using this database handle. It is only
  ** useful if invoked immediately after the main database i
  */
  if( sqlite3StrICmp(zLeft, "encoding")==0 ){
    struct EncName {
      char *zName;
      u8 enc;
    } encnames[] = {
      { "UTF-8", TEXT_Utf8 },
      { "UTF-16le", TEXT_Utf16le },
      { "UTF-16be", TEXT_Utf16be },
      { "UTF-16", TEXT_Utf16 },
      { "UTF8", TEXT_Utf8 },
      { "UTF16le", TEXT_Utf16le },
      { "UTF16be", TEXT_Utf16be },
      { "UTF16", TEXT_Utf16 },
      { 0, 0 }
    };
    struct EncName *pEnc;
    if( pRight->z==pLeft->z ){    /* "PRAGMA encoding" */
      if( SQLITE_OK!=sqlite3ReadSchema(pParse->db) ) return;
      sqlite3VdbeSetNumCols(v, 1);
      sqlite3VdbeSetColName(v, 0, "encoding", P3_STATIC);
      sqlite3VdbeAddOp(v, OP_String8, 0, 0);
      for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
        if( pEnc->enc==pParse->db->enc ){
          sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
          break;
        }
      }
      sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
    }else{                        /* "PRAGMA encoding = XXX" */
      /* Only change the value of sqlite.enc if the database handle is not
      ** initialized. If the main database exists, the new sqlite.enc value
      ** will be overwritten when the schema is next loaded. If it does not
      ** already exists, it will be created to use the new encoding value.
      */
      if( !(pParse->db->flags&SQLITE_Initialized) ){
        for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
          if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
            pParse->db->enc = pEnc->enc;
            break;
          }
        }
        if( !pEnc->zName ){
          sqlite3Error(pParse->db, SQLITE_ERROR, 
              "Unsupported encoding: %s", zRight);
        }
      }
    }
  }else

  {}
  sqliteFree(zLeft);
  sqliteFree(zRight);
}

Changes to src/sqliteInt.h.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
....
1370
1371
1372
1373
1374
1375
1376

**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.270 2004/06/07 01:52:14 drh Exp $
*/
#include "config.h"
#include "sqlite3.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................
void sqlite3Error(sqlite *, int, const char*,...);
int sqlite3utfTranslate(const void *, int , u8 , void **, int *, u8);
u8 sqlite3UtfReadBom(const void *zData, int nData);
void *sqlite3HexToBlob(const char *z);
int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
const char *sqlite3ErrStr(int);
int sqlite3ReadUniChar(const char *zStr, int *pOffset, u8 *pEnc, int fold);








|







 







>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
....
1370
1371
1372
1373
1374
1375
1376
1377
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.271 2004/06/07 07:52:18 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite3.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................
void sqlite3Error(sqlite *, int, const char*,...);
int sqlite3utfTranslate(const void *, int , u8 , void **, int *, u8);
u8 sqlite3UtfReadBom(const void *zData, int nData);
void *sqlite3HexToBlob(const char *z);
int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
const char *sqlite3ErrStr(int);
int sqlite3ReadUniChar(const char *zStr, int *pOffset, u8 *pEnc, int fold);
int sqlite3ReadSchema(sqlite *db);

Changes to src/trigger.c.

417
418
419
420
421
422
423


424
425
426
427
428
429
430
...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
...
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  int i;
  const char *zDb;
  const char *zName;
  int nName;
  sqlite *db = pParse->db;

  if( sqlite3_malloc_failed ) goto drop_trigger_cleanup;


  assert( pName->nSrc==1 );
  zDb = pName->a[0].zDatabase;
  zName = pName->a[0].zName;
  nName = strlen(zName);
  for(i=0; i<db->nDb; i++){
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
................................................................................
      return;
    }
  }
#endif

  /* Generate code to destroy the database record of the trigger.
  */
  if( pTable!=0 && !nested && (v = sqlite3GetVdbe(pParse))!=0 ){
    int base;
    static VdbeOpList dropTrigger[] = {
      { OP_Rewind,     0, ADDR(9),  0},
      { OP_String8,     0, 0,        0}, /* 1 */
      { OP_Column,     0, 1,        0},
      { OP_Ne,         0, ADDR(8),  0},
      { OP_String8,     0, 0,        "trigger"},
................................................................................
      { OP_Next,       0, ADDR(1),  0}, /* 8 */
    };

    sqlite3BeginWriteOperation(pParse, 0, pTrigger->iDb);
    sqlite3OpenMasterTable(v, pTrigger->iDb);
    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
    sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);
    if( pTrigger->iDb!=1 ){
      sqlite3ChangeCookie(db, v, pTrigger->iDb);
    }
    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
    sqlite3EndWriteOperation(pParse);
  }

  /*
   * If this is not an "explain", then delete the trigger structure.
   */
  if( !pParse->explain ){
    const char *zName = pTrigger->name;
    int nName = strlen(zName);
    if( pTable->pTrigger == pTrigger ){
      pTable->pTrigger = pTrigger->pNext;
    }else{
      Trigger *cc = pTable->pTrigger;







>
>







 







|







 







<
|
<

<



|
|







417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
...
486
487
488
489
490
491
492

493

494

495
496
497
498
499
500
501
502
503
504
505
506
  int i;
  const char *zDb;
  const char *zName;
  int nName;
  sqlite *db = pParse->db;

  if( sqlite3_malloc_failed ) goto drop_trigger_cleanup;
  if( SQLITE_OK!=sqlite3ReadSchema(db) ) goto drop_trigger_cleanup;

  assert( pName->nSrc==1 );
  zDb = pName->a[0].zDatabase;
  zName = pName->a[0].zName;
  nName = strlen(zName);
  for(i=0; i<db->nDb; i++){
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
................................................................................
      return;
    }
  }
#endif

  /* Generate code to destroy the database record of the trigger.
  */
  if( pTable!=0 && (v = sqlite3GetVdbe(pParse))!=0 ){
    int base;
    static VdbeOpList dropTrigger[] = {
      { OP_Rewind,     0, ADDR(9),  0},
      { OP_String8,     0, 0,        0}, /* 1 */
      { OP_Column,     0, 1,        0},
      { OP_Ne,         0, ADDR(8),  0},
      { OP_String8,     0, 0,        "trigger"},
................................................................................
      { OP_Next,       0, ADDR(1),  0}, /* 8 */
    };

    sqlite3BeginWriteOperation(pParse, 0, pTrigger->iDb);
    sqlite3OpenMasterTable(v, pTrigger->iDb);
    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
    sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0);

    sqlite3ChangeCookie(db, v, pTrigger->iDb);

    sqlite3VdbeAddOp(v, OP_Close, 0, 0);

  }

  /*
  ** If this is not an "explain", then delete the trigger structure.
  */
  if( !pParse->explain ){
    const char *zName = pTrigger->name;
    int nName = strlen(zName);
    if( pTable->pTrigger == pTrigger ){
      pTable->pTrigger = pTrigger->pNext;
    }else{
      Trigger *cc = pTable->pTrigger;

Changes to test/attach3.test.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
183
184
185
186
187
188
189
























190
191
192
193
194
195
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the ATTACH and DETACH commands
# and schema changes to attached databases.
#
# $Id: attach3.test,v 1.5 2004/05/29 10:23:20 danielk1977 Exp $
#


set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Create tables t1 and t2 in the main database
................................................................................
  }
} {}
do_test attach3-8.2 {
  execsql {
    SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
  }
} {}

























finish_test











|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the ATTACH and DETACH commands
# and schema changes to attached databases.
#
# $Id: attach3.test,v 1.6 2004/06/07 07:52:19 danielk1977 Exp $
#


set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Create tables t1 and t2 in the main database
................................................................................
  }
} {}
do_test attach3-8.2 {
  execsql {
    SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
  }
} {}

# Try to trick SQLite into dropping the wrong temp trigger.
do_test attach3-9.0 {
  execsql {
    CREATE TABLE main.t4(a, b, c);
    CREATE TABLE aux.t4(a, b, c);
    CREATE TEMP TRIGGER tst_trigger BEFORE INSERT ON aux.t4 BEGIN 
      SELECT 'hello world';
    END;
    SELECT count(*) FROM sqlite_temp_master;
  }
} {1}
do_test attach3-9.1 {
  execsql {
    DROP TABLE main.t4;
    SELECT count(*) FROM sqlite_temp_master;
  }
} {1}
do_test attach3-9.2 {
  execsql {
    DROP TABLE aux.t4;
    SELECT count(*) FROM sqlite_temp_master;
  }
} {0}

finish_test




Changes to test/enc2.test.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

118
119
120
121
122
123
124
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The focus of
# this file is testing the SQLite routines used for converting between the
# various suported unicode encodings (UTF-8, UTF-16, UTF-16le and
# UTF-16be).
#
# $Id: enc2.test,v 1.4 2004/05/27 17:22:56 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

db close

# Return the UTF-8 representation of the supplied UTF-16 string $str. 
................................................................................
  CREATE TABLE t1(a PRIMARY KEY, b, c);
  INSERT INTO t1 VALUES('one', 'I', 1);
}
# This proc tests that we can open and manipulate the test.db 
# database, and that it is possible to retreive values in
# various text encodings.
#
proc run_test_script {t} {

# Open the database and pull out a (the) row.
do_test $t.1 {
  set DB [sqlite db test.db]
  execsql {SELECT * FROM t1}
} {one I 1}

................................................................................
  utf8 [sqlite3_column_text16 $STMT 0]
} {five}

do_test $t.9 {
  sqlite3_finalize $STMT
} SQLITE_OK

do_test $t.99 {
  db close
} {}

}

# The three unicode encodings understood by SQLite.
set encodings [list -utf8 -utf16be -utf16le]

set i 1
foreach enc $encodings {
  file delete -force test.db
  sqlite db test.db $enc

  execsql $dbcontents
  db close
  run_test_script enc2-$i
  incr i
}

finish_test







|







 







|







 







|
|
|




|





>


|




9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The focus of
# this file is testing the SQLite routines used for converting between the
# various suported unicode encodings (UTF-8, UTF-16, UTF-16le and
# UTF-16be).
#
# $Id: enc2.test,v 1.5 2004/06/07 07:52:19 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

db close

# Return the UTF-8 representation of the supplied UTF-16 string $str. 
................................................................................
  CREATE TABLE t1(a PRIMARY KEY, b, c);
  INSERT INTO t1 VALUES('one', 'I', 1);
}
# This proc tests that we can open and manipulate the test.db 
# database, and that it is possible to retreive values in
# various text encodings.
#
proc run_test_script {t enc} {

# Open the database and pull out a (the) row.
do_test $t.1 {
  set DB [sqlite db test.db]
  execsql {SELECT * FROM t1}
} {one I 1}

................................................................................
  utf8 [sqlite3_column_text16 $STMT 0]
} {five}

do_test $t.9 {
  sqlite3_finalize $STMT
} SQLITE_OK

do_test $t.10 {
  db eval {PRAGMA encoding}
} $enc

}

# The three unicode encodings understood by SQLite.
set encodings [list UTF-8 UTF-16le UTF-16be]

set i 1
foreach enc $encodings {
  file delete -force test.db
  sqlite db test.db $enc
  db eval "PRAGMA encoding = \"$enc\""
  execsql $dbcontents
  db close
  run_test_script enc2-$i $enc
  incr i
}

finish_test