xref: /sqlite-3.40.0/test/quote.test (revision 7d44b22d)
1b19a2bc6Sdrh# 2001 September 15
287c40e88Sdrh#
3b19a2bc6Sdrh# The author disclaims copyright to this source code.  In place of
4b19a2bc6Sdrh# a legal notice, here is a blessing:
587c40e88Sdrh#
6b19a2bc6Sdrh#    May you do good and not evil.
7b19a2bc6Sdrh#    May you find forgiveness for yourself and forgive others.
8b19a2bc6Sdrh#    May you share freely, never taking more than you give.
987c40e88Sdrh#
1087c40e88Sdrh#***********************************************************************
1187c40e88Sdrh# This file implements regression tests for SQLite library.  The
1287c40e88Sdrh# focus of this file is the ability to specify table and column names
1387c40e88Sdrh# as quoted strings.
1487c40e88Sdrh#
15b556ce15Sdrh# $Id: quote.test,v 1.7 2007/04/25 11:32:30 drh Exp $
1687c40e88Sdrh
1787c40e88Sdrhset testdir [file dirname $argv0]
1887c40e88Sdrhsource $testdir/tester.tcl
190d92571dSdanset testprefix quote
2087c40e88Sdrh
2187c40e88Sdrh# Create a table with a strange name and with strange column names.
2287c40e88Sdrh#
2387c40e88Sdrhdo_test quote-1.0 {
243d94662aSdrh  catchsql {CREATE TABLE '@abc' ( '#xyz' int, '!pqr' text );}
2587c40e88Sdrh} {0 {}}
2687c40e88Sdrh
2787c40e88Sdrh# Insert, update and query the table.
2887c40e88Sdrh#
2987c40e88Sdrhdo_test quote-1.1 {
303d94662aSdrh  catchsql {INSERT INTO '@abc' VALUES(5,'hello')}
3187c40e88Sdrh} {0 {}}
323d94662aSdrhdo_test quote-1.2.1 {
333d94662aSdrh  catchsql {SELECT * FROM '@abc'}
343d94662aSdrh} {0 {5 hello}}
353d94662aSdrhdo_test quote-1.2.2 {
363d94662aSdrh  catchsql {SELECT * FROM [@abc]}  ;# SqlServer compatibility
373d94662aSdrh} {0 {5 hello}}
383d94662aSdrhdo_test quote-1.2.3 {
393d94662aSdrh  catchsql {SELECT * FROM `@abc`}  ;# MySQL compatibility
4087c40e88Sdrh} {0 {5 hello}}
4187c40e88Sdrhdo_test quote-1.3 {
423d94662aSdrh  catchsql {
433d94662aSdrh    SELECT '@abc'.'!pqr', '@abc'.'#xyz'+5 FROM '@abc'
443d94662aSdrh  }
4587c40e88Sdrh} {0 {hello 10}}
462398937bSdrhdo_test quote-1.3.1 {
472398937bSdrh  catchsql {
482398937bSdrh    SELECT '!pqr', '#xyz'+5 FROM '@abc'
492398937bSdrh  }
508df447f0Sdrh} {0 {!pqr 5}}
512398937bSdrhdo_test quote-1.3.2 {
522398937bSdrh  catchsql {
532398937bSdrh    SELECT "!pqr", "#xyz"+5 FROM '@abc'
542398937bSdrh  }
552398937bSdrh} {0 {hello 10}}
563d94662aSdrhdo_test quote-1.3.3 {
573d94662aSdrh  catchsql {
583d94662aSdrh    SELECT [!pqr], `#xyz`+5 FROM '@abc'
593d94662aSdrh  }
603d94662aSdrh} {0 {hello 10}}
61b556ce15Sdrhdo_test quote-1.3.4 {
622398937bSdrh  set r [catch {
632398937bSdrh    execsql {SELECT '@abc'.'!pqr', '@abc'.'#xyz'+5 FROM '@abc'}
642398937bSdrh  } msg ]
652398937bSdrh  lappend r $msg
662398937bSdrh} {0 {hello 10}}
6787c40e88Sdrhdo_test quote-1.4 {
6887c40e88Sdrh  set r [catch {
6987c40e88Sdrh    execsql {UPDATE '@abc' SET '#xyz'=11}
7087c40e88Sdrh  } msg ]
7187c40e88Sdrh  lappend r $msg
7287c40e88Sdrh} {0 {}}
7387c40e88Sdrhdo_test quote-1.5 {
7487c40e88Sdrh  set r [catch {
7587c40e88Sdrh    execsql {SELECT '@abc'.'!pqr', '@abc'.'#xyz'+5 FROM '@abc'}
7687c40e88Sdrh  } msg ]
7787c40e88Sdrh  lappend r $msg
7887c40e88Sdrh} {0 {hello 16}}
7987c40e88Sdrh
8087c40e88Sdrh# Drop the table with the strange name.
8187c40e88Sdrh#
8287c40e88Sdrhdo_test quote-1.6 {
8387c40e88Sdrh  set r [catch {
8487c40e88Sdrh    execsql {DROP TABLE '@abc'}
8587c40e88Sdrh  } msg ]
8687c40e88Sdrh  lappend r $msg
8787c40e88Sdrh} {0 {}}
8887c40e88Sdrh
890d92571dSdan#-------------------------------------------------------------------------
900d92571dSdan# Check that it is not possible to use double-quotes for a string
910d92571dSdan# constant in a CHECK constraint or CREATE INDEX statement. However,
920d92571dSdan# SQLite can load such a schema from disk.
930d92571dSdan#
940d92571dSdanreset_db
954b50da9cSdrhsqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 0
964b50da9cSdrhsqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
970d92571dSdando_execsql_test 2.0 {
980d92571dSdan  CREATE TABLE t1(x, y, z);
990d92571dSdan}
1000d92571dSdanforeach {tn sql errname} {
1010d92571dSdan  1 { CREATE TABLE xyz(a, b, c CHECK (c!="null") ) } null
1020d92571dSdan  2 { CREATE INDEX i2 ON t1(x, y, z||"abc") }        abc
1030d92571dSdan  3 { CREATE INDEX i3 ON t1("w") }                   w
1040d92571dSdan  4 { CREATE INDEX i4 ON t1(x) WHERE z="w" }         w
1050d92571dSdan} {
1060d92571dSdan  do_catchsql_test 2.1.$tn $sql [list 1 "no such column: $errname"]
1070d92571dSdan}
1080d92571dSdan
1090d92571dSdando_execsql_test 2.2 {
1100d92571dSdan  PRAGMA writable_schema = 1;
1110d92571dSdan  CREATE TABLE xyz(a, b, c CHECK (c!="null") );
1120d92571dSdan  CREATE INDEX i2 ON t1(x, y, z||"abc");
11344d44131Sdrh  CREATE INDEX i3 ON t1("w"||"");
1140d92571dSdan  CREATE INDEX i4 ON t1(x) WHERE z="w";
1150d92571dSdan}
1160d92571dSdan
1170d92571dSdandb close
1180d92571dSdansqlite3 db test.db
1190d92571dSdan
1200d92571dSdando_execsql_test 2.3.1 {
1210d92571dSdan  INSERT INTO xyz VALUES(1, 2, 3);
1220d92571dSdan}
1230d92571dSdando_catchsql_test 2.3.2 {
1240d92571dSdan  INSERT INTO xyz VALUES(1, 2, 'null');
12592e21ef0Sdrh} {1 {CHECK constraint failed: c!="null"}}
1260d92571dSdan
1270d92571dSdando_execsql_test 2.4 {
1280d92571dSdan  INSERT INTO t1 VALUES(1, 2, 3);
1290d92571dSdan  INSERT INTO t1 VALUES(4, 5, 'w');
1300d92571dSdan  SELECT * FROM t1 WHERE z='w';
1310d92571dSdan} {4 5 w}
1320d92571dSdando_execsql_test 2.5 {
1330d92571dSdan  SELECT sql FROM sqlite_master;
1340d92571dSdan} {
1350d92571dSdan  {CREATE TABLE t1(x, y, z)}
1360d92571dSdan  {CREATE TABLE xyz(a, b, c CHECK (c!="null") )}
1370d92571dSdan  {CREATE INDEX i2 ON t1(x, y, z||"abc")}
13844d44131Sdrh  {CREATE INDEX i3 ON t1("w"||"")}
1390d92571dSdan  {CREATE INDEX i4 ON t1(x) WHERE z="w"}
1400d92571dSdan}
1410d92571dSdan
14244d44131Sdrh# 2021-03-13
14344d44131Sdrh# ticket 1c24a659e6d7f3a1
14437f3ac8fSdanifcapable altertable {
14544d44131Sdrh  reset_db
14644d44131Sdrh    do_catchsql_test 3.0 {
14744d44131Sdrh      CREATE TABLE t1(a,b);
14844d44131Sdrh      CREATE INDEX x1 on t1("b");
14944d44131Sdrh      ALTER TABLE t1 DROP COLUMN b;
15044d44131Sdrh    } {1 {error in index x1 after drop column: no such column: b}}
15144d44131Sdrh  do_catchsql_test 3.1 {
15244d44131Sdrh    DROP TABLE t1;
15344d44131Sdrh    CREATE TABLE t1(a,"b");
15444d44131Sdrh    CREATE INDEX x1 on t1("b");
15544d44131Sdrh    ALTER TABLE t1 DROP COLUMN b;
15644d44131Sdrh  } {1 {error in index x1 after drop column: no such column: b}}
15744d44131Sdrh  do_catchsql_test 3.2 {
15844d44131Sdrh    DROP TABLE t1;
15944d44131Sdrh    CREATE TABLE t1(a,'b');
16044d44131Sdrh    CREATE INDEX x1 on t1("b");
16144d44131Sdrh    ALTER TABLE t1 DROP COLUMN b;
16244d44131Sdrh  } {1 {error in index x1 after drop column: no such column: b}}
16344d44131Sdrh  do_catchsql_test 3.3 {
16444d44131Sdrh    DROP TABLE t1;
16544d44131Sdrh    CREATE TABLE t1(a,"b");
16644d44131Sdrh    CREATE INDEX x1 on t1('b');
16744d44131Sdrh    ALTER TABLE t1 DROP COLUMN b;
16844d44131Sdrh  } {1 {error in index x1 after drop column: no such column: b}}
16944d44131Sdrh  do_catchsql_test 3.4 {
17044d44131Sdrh    DROP TABLE t1;
17144d44131Sdrh    CREATE TABLE t1(a, b, c);
17244d44131Sdrh    CREATE INDEX x1 ON t1("a"||"b");
17344d44131Sdrh    INSERT INTO t1 VALUES(1,2,3),(1,4,5);
17444d44131Sdrh    ALTER TABLE t1 DROP COLUMN b;
17544d44131Sdrh  } {1 {error in index x1 after drop column: no such column: b}}
176*7d44b22dSdrh  sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1
1770e8075abSdrh  do_catchsql_test 3.5 {
1780e8075abSdrh    DROP TABLE t1;
1790e8075abSdrh    CREATE TABLE t1(a, b, c);
1800e8075abSdrh    CREATE INDEX x1 ON t1("a"||"x");
1810e8075abSdrh    INSERT INTO t1 VALUES(1,2,3),(1,4,5);
1820e8075abSdrh    ALTER TABLE t1 DROP COLUMN b;
1830e8075abSdrh  } {0 {}}
18437f3ac8fSdan}
18587c40e88Sdrh
18687c40e88Sdrhfinish_test
187