/ Check-in [b243681a]
Login

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

Overview
Comment:More test cases to do with invalidating precompiled statements. (CVS 2273)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b243681a0e328ee0bbf1140abfb60d65d2102ad5
User & Date: danielk1977 2005-01-24 13:03:32
Context
2005-01-24
23:27
Clarify documentation of DEFAULT CURRENT_TIME etc. (version 3.1.0 and greater only). (CVS 2274) check-in: 557eb2ec user: danielk1977 tags: trunk
13:03
More test cases to do with invalidating precompiled statements. (CVS 2273) check-in: b243681a user: danielk1977 tags: trunk
12:46
Use the cache with loading a large table in select2-2.0. (CVS 2272) check-in: bd65b180 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/trigger.c.

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
...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
** in order to complete the process of building the trigger.
*/
void sqlite3FinishTrigger(
  Parse *pParse,          /* Parser context */
  TriggerStep *pStepList, /* The triggered program */
  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
){
  Trigger *nt = 0;          /* The trigger whose construction is finishing up */
  sqlite3 *db = pParse->db;  /* The database */
  DbFixer sFix;

  if( pParse->nErr || pParse->pNewTrigger==0 ) goto triggerfinish_cleanup;
  nt = pParse->pNewTrigger;
  pParse->pNewTrigger = 0;
  nt->step_list = pStepList;
  while( pStepList ){
    pStepList->pTrig = nt;
    pStepList = pStepList->pNext;
  }
  if( sqlite3FixInit(&sFix, pParse, nt->iDb, "trigger", &nt->nameToken) 
          && sqlite3FixTriggerStep(&sFix, nt->step_list) ){
    goto triggerfinish_cleanup;
  }

  /* if we are not initializing, and this trigger is not on a TEMP table, 
  ** build the sqlite_master entry
  */
  if( !db->init.busy ){
................................................................................
    };
    int addr;
    Vdbe *v;

    /* Make an entry in the sqlite_master table */
    v = sqlite3GetVdbe(pParse);
    if( v==0 ) goto triggerfinish_cleanup;
    sqlite3BeginWriteOperation(pParse, 0, nt->iDb);
    sqlite3OpenMasterTable(v, nt->iDb);
    addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
    sqlite3VdbeChangeP3(v, addr+2, nt->name, 0); 
    sqlite3VdbeChangeP3(v, addr+3, nt->table, 0); 
    sqlite3VdbeChangeP3(v, addr+6, pAll->z, pAll->n);
    if( nt->iDb!=0 ){
      sqlite3ChangeCookie(db, v, nt->iDb);
    }
    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
    sqlite3VdbeOp3(v, OP_ParseSchema, nt->iDb, 0, 
       sqlite3MPrintf("type='trigger' AND name='%q'", nt->name), P3_DYNAMIC);
  }

  if( db->init.busy ){
    Table *pTab;
    sqlite3HashInsert(&db->aDb[nt->iDb].trigHash, 
                     nt->name, strlen(nt->name)+1, nt);
    pTab = sqlite3LocateTable(pParse, nt->table, db->aDb[nt->iTabDb].zName);
    assert( pTab!=0 );
    nt->pNext = pTab->pTrigger;
    pTab->pTrigger = nt;
    nt = 0;
  }

triggerfinish_cleanup:
  sqlite3DeleteTrigger(nt);
  sqlite3DeleteTrigger(pParse->pNewTrigger);
  pParse->pNewTrigger = 0;
  sqlite3DeleteTriggerStep(pStepList);
}

/*
** Make a copy of all components of the given trigger step.  This has
** the effect of copying all Expr.token.z values into memory obtained
** from sqliteMalloc().  As initially created, the Expr.token.z values







|




|

|

|


|
|







 







|
|

|
|

<
|
<

|
|




|
|
|

|
|
|



|
<
|







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
...
224
225
226
227
228
229
230
231
232
233
234
235
236

237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263
** in order to complete the process of building the trigger.
*/
void sqlite3FinishTrigger(
  Parse *pParse,          /* Parser context */
  TriggerStep *pStepList, /* The triggered program */
  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
){
  Trigger *pTrig = 0;     /* The trigger whose construction is finishing up */
  sqlite3 *db = pParse->db;  /* The database */
  DbFixer sFix;

  if( pParse->nErr || pParse->pNewTrigger==0 ) goto triggerfinish_cleanup;
  pTrig = pParse->pNewTrigger;
  pParse->pNewTrigger = 0;
  pTrig->step_list = pStepList;
  while( pStepList ){
    pStepList->pTrig = pTrig;
    pStepList = pStepList->pNext;
  }
  if( sqlite3FixInit(&sFix, pParse, pTrig->iDb, "trigger", &pTrig->nameToken) 
          && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
    goto triggerfinish_cleanup;
  }

  /* if we are not initializing, and this trigger is not on a TEMP table, 
  ** build the sqlite_master entry
  */
  if( !db->init.busy ){
................................................................................
    };
    int addr;
    Vdbe *v;

    /* Make an entry in the sqlite_master table */
    v = sqlite3GetVdbe(pParse);
    if( v==0 ) goto triggerfinish_cleanup;
    sqlite3BeginWriteOperation(pParse, 0, pTrig->iDb);
    sqlite3OpenMasterTable(v, pTrig->iDb);
    addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
    sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0); 
    sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0); 
    sqlite3VdbeChangeP3(v, addr+6, pAll->z, pAll->n);

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

    sqlite3VdbeAddOp(v, OP_Close, 0, 0);
    sqlite3VdbeOp3(v, OP_ParseSchema, pTrig->iDb, 0, 
       sqlite3MPrintf("type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC);
  }

  if( db->init.busy ){
    Table *pTab;
    sqlite3HashInsert(&db->aDb[pTrig->iDb].trigHash, 
                     pTrig->name, strlen(pTrig->name)+1, pTrig);
    pTab = sqlite3LocateTable(pParse,pTrig->table,db->aDb[pTrig->iTabDb].zName);
    assert( pTab!=0 );
    pTrig->pNext = pTab->pTrigger;
    pTab->pTrigger = pTrig;
    pTrig = 0;
  }

triggerfinish_cleanup:
  sqlite3DeleteTrigger(pTrig);

  assert( !pParse->pNewTrigger );
  sqlite3DeleteTriggerStep(pStepList);
}

/*
** Make a copy of all components of the given trigger step.  This has
** the effect of copying all Expr.token.z values into memory obtained
** from sqliteMalloc().  As initially created, the Expr.token.z values

Changes to test/schema.test.

1
2
3
4
5
6
7
8
.
9
10
11
12
13
14
15
16

















17
18
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
57
58
59



60
61
62
63
64
65
66




67
68
69
70
71
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96



97




































































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
126
127
128
129
130
131
132
133
# 2004 Jun 27
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    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 implements regression tests for SQLite library.
#
# This file tests the various conditions under which an SQLITE_SCHEMA
# error should be returned.
#
# $Id: schema.test,v 1.1 2005/01/24 10:26:00 danielk1977 Exp $


















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

# schema-1.1: Test that if a table is dropped by one database connection, 
#             other database connections are aware of the schema change.
# schema-1.2: Test that if a view is dropped by one database connection,
#             other database connections are aware of the schema change.
#
do_test schema-1.1 {

  execsql {
    CREATE TABLE abc(a, b, c);
  }
  sqlite3 db2 test.db






  execsql {
    DROP TABLE abc;
  } db2










  catchsql {
    SELECT * FROM abc;
  }
} {1 {no such table: abc}}


do_test schema-1.2 {




  execsql {












    CREATE TABLE abc(a, b, c);
    CREATE VIEW abcview AS SELECT * FROM abc;
  }
  sqlite3 db2 test.db

  execsql {
    DROP VIEW abcview;
  } db2
  db2 close











  catchsql {
    SELECT * FROM abcview;

  }
} {1 {no such table: abcview}}






# Tests 2.1 to 2.4 check that prepared statements are invalidated when
# a collation sequence is deleted (but not when one is added).
#
do_test schema-2.1 {
  set sql {SELECT * FROM abc;}
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
  add_test_collate $::DB 1 1 1



  sqlite3_step $::STMT
} {SQLITE_DONE}
do_test schema-2.2 {
  sqlite3_reset $::STMT
} {SQLITE_OK}
do_test schema-2.3 {
  add_test_collate $::DB 0 0 0 




  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-2.4 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}


# Tests 3.1 to 3.4 check that prepared statements are invalidated when
# a database is DETACHed (but not when one is ATTACHed).
#
do_test schema-3.1 {
  set sql {SELECT * FROM abc;}
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
  execsql {
    ATTACH 'test2.db' AS aux;
  }
  sqlite3_step $::STMT
} {SQLITE_DONE}
do_test schema-3.2 {
  sqlite3_reset $::STMT
} {SQLITE_OK}
do_test schema-3.3 {
  execsql {
    DETACH aux;
  }
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-3.4 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}









































































# Test that if a CREATE TABLE statement fails because there are other
# btree cursors open on the same database file it does not corrupt
# the sqlite_master table.
#
do_test schema-4.1 {
  execsql {
    INSERT INTO abc VALUES(1, 2, 3);
  }
  set sql {SELECT * FROM abc}
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
  sqlite3_step $::STMT
} {SQLITE_ROW}
do_test schema-4.2 {
  catchsql {
    CREATE TABLE t2(a, b, c);
  }
} {1 {database table is locked}}
do_test schema-4.3 {
  sqlite3_finalize $::STMT
} {SQLITE_OK}
do_test schema-4.4 {
  sqlite3 db2 test.db
  execsql {
    SELECT * FROM abc
  } db2
} {1 2 3}
do_test schema-4.5 {
  db2 close
} {}

finish_test





|







 







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




<
<
<
<
<

>



|
>
>
>
>
>
>


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

|
|
|
|
|
<
>
>
>
>


|



>
|


|







|


|





|



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




|







|




|


|





|





<
<
<
<
1
2
3
4
5
6
7
8
.
9
10
11
12
13
14
15
16
17
18
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
57
58
59
60
61
62
63
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

86

87
88



89
90
91
92
93
94
95
96
97
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259




# 2004 Jan 24
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    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 implements regression tests for SQLite library.
#
# This file tests the various conditions under which an SQLITE_SCHEMA
# error should be returned.
#
# $Id: schema.test,v 1.2 2005/01/24 13:03:32 danielk1977 Exp $

#---------------------------------------------------------------------
# When any of the following types of SQL statements or actions are 
# executed, all pre-compiled statements are invalidated. An attempt
# to execute an invalidated statement always returns SQLITE_SCHEMA.
#
# CREATE/DROP TABLE...................................schema-1.*
# CREATE/DROP VIEW....................................schema-2.*
# CREATE/DROP TRIGGER.................................schema-3.*
# CREATE/DROP INDEX...................................schema-4.*
# DETACH..............................................schema-5.*
# Deleting a user-function............................schema-6.*
# Deleting a collation sequence.......................schema-7.*
# Setting or changing the authorization function......schema-8.*
#
# Note: Test cases schema-6.* are missing right now.
#

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






do_test schema-1.1 {
  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  execsql {
    CREATE TABLE abc(a, b, c);
  }
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-1.2 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}
do_test schema-1.3 {
  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  execsql {
    DROP TABLE abc;

  }
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-1.4 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}

ifcapable view {
  do_test schema-2.1 {
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
    execsql {
      CREATE VIEW v1 AS SELECT * FROM sqlite_master;
    }

    sqlite3_step $::STMT
  } {SQLITE_ERROR}
  do_test schema-2.2 {
    sqlite3_finalize $::STMT
  } {SQLITE_SCHEMA}
  do_test schema-2.3 {
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
    execsql {
      DROP VIEW v1;
    }
    sqlite3_step $::STMT
  } {SQLITE_ERROR}
  do_test schema-2.4 {
    sqlite3_finalize $::STMT
  } {SQLITE_SCHEMA}
}

ifcapable trigger {
  do_test schema-3.1 {
    execsql {
      CREATE TABLE abc(a, b, c);

    }

    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
    execsql {



      CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN
        SELECT 1, 2, 3;
      END;
    }
    sqlite3_step $::STMT
  } {SQLITE_ERROR}
  do_test schema-3.2 {
    sqlite3_finalize $::STMT
  } {SQLITE_SCHEMA}
  do_test schema-3.3 {
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
    execsql {

      DROP TRIGGER abc_trig;
    }

    sqlite3_step $::STMT
  } {SQLITE_ERROR}
  do_test schema-3.4 {
    sqlite3_finalize $::STMT
  } {SQLITE_SCHEMA}
}



do_test schema-4.1 {

  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]

  execsql {
    CREATE INDEX abc_index ON abc(a);
  }
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-4.2 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}
do_test schema-4.3 {

  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  execsql {
    DROP INDEX abc_index;
  }
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-4.4 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}

#---------------------------------------------------------------------
# Tests 5.1 to 5.4 check that prepared statements are invalidated when
# a database is DETACHed (but not when one is ATTACHed).
#
do_test schema-5.1 {
  set sql {SELECT * FROM abc;}
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
  execsql {
    ATTACH 'test2.db' AS aux;
  }
  sqlite3_step $::STMT
} {SQLITE_DONE}
do_test schema-5.2 {
  sqlite3_reset $::STMT
} {SQLITE_OK}
do_test schema-5.3 {
  execsql {
    DETACH aux;
  }
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-5.4 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}

#---------------------------------------------------------------------
# Tests 7.* check that prepared statements are invalidated when
# a collation sequence is deleted (but not when one is added).
#
do_test schema-7.1 {
  set sql {SELECT * FROM abc;}
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
  add_test_collate $::DB 1 1 1
  sqlite3_step $::STMT
} {SQLITE_DONE}
do_test schema-7.2 {
  sqlite3_reset $::STMT
} {SQLITE_OK}
do_test schema-7.3 {
  add_test_collate $::DB 0 0 0 
  sqlite3_step $::STMT
} {SQLITE_ERROR}
do_test schema-7.4 {
  sqlite3_finalize $::STMT
} {SQLITE_SCHEMA}

#---------------------------------------------------------------------
# Tests 8.1 and 8.2 check that prepared statements are invalidated when
# the authorization function is set.
#
ifcapable auth {
  do_test schema-8.1 {
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
    db auth {}
    sqlite3_step $::STMT
  } {SQLITE_ERROR}
  do_test schema-8.3 {
    sqlite3_finalize $::STMT
  } {SQLITE_SCHEMA}
}

#---------------------------------------------------------------------
# schema-9.1: Test that if a table is dropped by one database connection, 
#             other database connections are aware of the schema change.
# schema-9.2: Test that if a view is dropped by one database connection,
#             other database connections are aware of the schema change.
#
do_test schema-9.1 {
  sqlite3 db2 test.db
  execsql {
    DROP TABLE abc;
  } db2
  db2 close
  catchsql {
    SELECT * FROM abc;
  }
} {1 {no such table: abc}}
execsql {
  CREATE TABLE abc(a, b, c);
}
ifcapable view {
  do_test schema-9.2 {
    execsql {
      CREATE VIEW abcview AS SELECT * FROM abc;
    }
    sqlite3 db2 test.db
    execsql {
      DROP VIEW abcview;
    } db2
    db2 close
    catchsql {
      SELECT * FROM abcview;
    }
  } {1 {no such table: abcview}}
}

#---------------------------------------------------------------------
# Test that if a CREATE TABLE statement fails because there are other
# btree cursors open on the same database file it does not corrupt
# the sqlite_master table.
#
do_test schema-10.1 {
  execsql {
    INSERT INTO abc VALUES(1, 2, 3);
  }
  set sql {SELECT * FROM abc}
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
  sqlite3_step $::STMT
} {SQLITE_ROW}
do_test schema-10.2 {
  catchsql {
    CREATE TABLE t2(a, b, c);
  }
} {1 {database table is locked}}
do_test schema-10.3 {
  sqlite3_finalize $::STMT
} {SQLITE_OK}
do_test schema-10.4 {
  sqlite3 db2 test.db
  execsql {
    SELECT * FROM abc
  } db2
} {1 2 3}
do_test schema-10.5 {
  db2 close
} {}

finish_test