os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/view.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2002 February 26
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing VIEW statements.
sl@0
    13
#
sl@0
    14
# $Id: view.test,v 1.38 2008/08/21 14:54:29 drh Exp $
sl@0
    15
set testdir [file dirname $argv0]
sl@0
    16
source $testdir/tester.tcl
sl@0
    17
sl@0
    18
# Omit this entire file if the library is not configured with views enabled.
sl@0
    19
ifcapable !view {
sl@0
    20
  finish_test
sl@0
    21
  return
sl@0
    22
}
sl@0
    23
sl@0
    24
do_test view-1.0 {
sl@0
    25
  execsql {
sl@0
    26
    CREATE TABLE t1(a,b,c);
sl@0
    27
    INSERT INTO t1 VALUES(1,2,3);
sl@0
    28
    INSERT INTO t1 VALUES(4,5,6);
sl@0
    29
    INSERT INTO t1 VALUES(7,8,9);
sl@0
    30
    SELECT * FROM t1;
sl@0
    31
  }
sl@0
    32
} {1 2 3 4 5 6 7 8 9}
sl@0
    33
sl@0
    34
do_test view-1.1 {
sl@0
    35
  execsql {
sl@0
    36
    BEGIN;
sl@0
    37
    CREATE VIEW IF NOT EXISTS v1 AS SELECT a,b FROM t1;
sl@0
    38
    SELECT * FROM v1 ORDER BY a;
sl@0
    39
  }
sl@0
    40
} {1 2 4 5 7 8}
sl@0
    41
do_test view-1.2 {
sl@0
    42
  catchsql {
sl@0
    43
    ROLLBACK;
sl@0
    44
    SELECT * FROM v1 ORDER BY a;
sl@0
    45
  }
sl@0
    46
} {1 {no such table: v1}}
sl@0
    47
do_test view-1.3 {
sl@0
    48
  execsql {
sl@0
    49
    CREATE VIEW v1 AS SELECT a,b FROM t1;
sl@0
    50
    SELECT * FROM v1 ORDER BY a;
sl@0
    51
  }
sl@0
    52
} {1 2 4 5 7 8}
sl@0
    53
do_test view-1.3.1 {
sl@0
    54
  db close
sl@0
    55
  sqlite3 db test.db
sl@0
    56
  execsql {
sl@0
    57
    SELECT * FROM v1 ORDER BY a;
sl@0
    58
  }
sl@0
    59
} {1 2 4 5 7 8}
sl@0
    60
do_test view-1.4 {
sl@0
    61
  catchsql {
sl@0
    62
    DROP VIEW IF EXISTS v1;
sl@0
    63
    SELECT * FROM v1 ORDER BY a;
sl@0
    64
  }
sl@0
    65
} {1 {no such table: v1}}
sl@0
    66
do_test view-1.5 {
sl@0
    67
  execsql {
sl@0
    68
    CREATE VIEW v1 AS SELECT a,b FROM t1;
sl@0
    69
    SELECT * FROM v1 ORDER BY a;
sl@0
    70
  }
sl@0
    71
} {1 2 4 5 7 8}
sl@0
    72
do_test view-1.6 {
sl@0
    73
  catchsql {
sl@0
    74
    DROP TABLE t1;
sl@0
    75
    SELECT * FROM v1 ORDER BY a;
sl@0
    76
  }
sl@0
    77
} {1 {no such table: main.t1}}
sl@0
    78
do_test view-1.7 {
sl@0
    79
  execsql {
sl@0
    80
    CREATE TABLE t1(x,a,b,c);
sl@0
    81
    INSERT INTO t1 VALUES(1,2,3,4);
sl@0
    82
    INSERT INTO t1 VALUES(4,5,6,7);
sl@0
    83
    INSERT INTO t1 VALUES(7,8,9,10);
sl@0
    84
    SELECT * FROM v1 ORDER BY a;
sl@0
    85
  }
sl@0
    86
} {2 3 5 6 8 9}
sl@0
    87
do_test view-1.8 {
sl@0
    88
  db close
sl@0
    89
  sqlite3 db test.db
sl@0
    90
  execsql {
sl@0
    91
    SELECT * FROM v1 ORDER BY a;
sl@0
    92
  }
sl@0
    93
} {2 3 5 6 8 9}
sl@0
    94
sl@0
    95
do_test view-2.1 {
sl@0
    96
  execsql {
sl@0
    97
    CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5
sl@0
    98
  };  # No semicolon
sl@0
    99
  execsql2 {
sl@0
   100
    SELECT * FROM v2;
sl@0
   101
  }
sl@0
   102
} {x 7 a 8 b 9 c 10}
sl@0
   103
do_test view-2.2 {
sl@0
   104
  catchsql {
sl@0
   105
    INSERT INTO v2 VALUES(1,2,3,4);
sl@0
   106
  }
sl@0
   107
} {1 {cannot modify v2 because it is a view}}
sl@0
   108
do_test view-2.3 {
sl@0
   109
  catchsql {
sl@0
   110
    UPDATE v2 SET a=10 WHERE a=5;
sl@0
   111
  }
sl@0
   112
} {1 {cannot modify v2 because it is a view}}
sl@0
   113
do_test view-2.4 {
sl@0
   114
  catchsql {
sl@0
   115
    DELETE FROM v2;
sl@0
   116
  }
sl@0
   117
} {1 {cannot modify v2 because it is a view}}
sl@0
   118
do_test view-2.5 {
sl@0
   119
  execsql {
sl@0
   120
    INSERT INTO t1 VALUES(11,12,13,14);
sl@0
   121
    SELECT * FROM v2 ORDER BY x;
sl@0
   122
  }
sl@0
   123
} {7 8 9 10 11 12 13 14}
sl@0
   124
do_test view-2.6 {
sl@0
   125
  execsql {
sl@0
   126
    SELECT x FROM v2 WHERE a>10
sl@0
   127
  }
sl@0
   128
} {11}
sl@0
   129
sl@0
   130
# Test that column name of views are generated correctly.
sl@0
   131
#
sl@0
   132
do_test view-3.1 {
sl@0
   133
  execsql2 {
sl@0
   134
    SELECT * FROM v1 LIMIT 1
sl@0
   135
  }
sl@0
   136
} {a 2 b 3}
sl@0
   137
do_test view-3.2 {
sl@0
   138
  execsql2 {
sl@0
   139
    SELECT * FROM v2 LIMIT 1
sl@0
   140
  }
sl@0
   141
} {x 7 a 8 b 9 c 10}
sl@0
   142
do_test view-3.3.1 {
sl@0
   143
  execsql2 {
sl@0
   144
    DROP VIEW v1;
sl@0
   145
    CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1;
sl@0
   146
    SELECT * FROM v1 LIMIT 1
sl@0
   147
  }
sl@0
   148
} {xyz 2 pqr 7 c-b 1}
sl@0
   149
do_test view-3.3.2 {
sl@0
   150
  execsql2 {
sl@0
   151
    CREATE VIEW v1b AS SELECT t1.a, b+c, t1.c FROM t1;
sl@0
   152
    SELECT * FROM v1b LIMIT 1
sl@0
   153
  }
sl@0
   154
} {a 2 b+c 7 c 4}
sl@0
   155
sl@0
   156
ifcapable compound {
sl@0
   157
do_test  view-3.4 {
sl@0
   158
  execsql2 {
sl@0
   159
    CREATE VIEW v3 AS SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY b;
sl@0
   160
    SELECT * FROM v3 LIMIT 4;
sl@0
   161
  }
sl@0
   162
} {a 2 a 3 a 5 a 6}
sl@0
   163
do_test view-3.5 {
sl@0
   164
  execsql2 {
sl@0
   165
    CREATE VIEW v4 AS 
sl@0
   166
      SELECT a, b FROM t1 
sl@0
   167
      UNION
sl@0
   168
      SELECT b AS 'x', a AS 'y' FROM t1
sl@0
   169
      ORDER BY x, y;
sl@0
   170
    SELECT b FROM v4 ORDER BY b LIMIT 4;
sl@0
   171
  }
sl@0
   172
} {b 2 b 3 b 5 b 6}
sl@0
   173
} ;# ifcapable compound
sl@0
   174
sl@0
   175
sl@0
   176
do_test view-4.1 {
sl@0
   177
  catchsql {
sl@0
   178
    DROP VIEW t1;
sl@0
   179
  }
sl@0
   180
} {1 {use DROP TABLE to delete table t1}}
sl@0
   181
do_test view-4.2 {
sl@0
   182
  execsql {
sl@0
   183
    SELECT 1 FROM t1 LIMIT 1;
sl@0
   184
  }
sl@0
   185
} 1
sl@0
   186
do_test view-4.3 {
sl@0
   187
  catchsql {
sl@0
   188
    DROP TABLE v1;
sl@0
   189
  }
sl@0
   190
} {1 {use DROP VIEW to delete view v1}}
sl@0
   191
do_test view-4.4 {
sl@0
   192
  execsql {
sl@0
   193
     SELECT 1 FROM v1 LIMIT 1;
sl@0
   194
  }
sl@0
   195
} {1}
sl@0
   196
do_test view-4.5 {
sl@0
   197
  catchsql {
sl@0
   198
    CREATE INDEX i1v1 ON v1(xyz);
sl@0
   199
  }
sl@0
   200
} {1 {views may not be indexed}}
sl@0
   201
sl@0
   202
do_test view-5.1 {
sl@0
   203
  execsql {
sl@0
   204
    CREATE TABLE t2(y,a);
sl@0
   205
    INSERT INTO t2 VALUES(22,2);
sl@0
   206
    INSERT INTO t2 VALUES(33,3);
sl@0
   207
    INSERT INTO t2 VALUES(44,4);
sl@0
   208
    INSERT INTO t2 VALUES(55,5);
sl@0
   209
    SELECT * FROM t2;
sl@0
   210
  }
sl@0
   211
} {22 2 33 3 44 4 55 5}
sl@0
   212
do_test view-5.2 {
sl@0
   213
  execsql {
sl@0
   214
    CREATE VIEW v5 AS
sl@0
   215
      SELECT t1.x AS v, t2.y AS w FROM t1 JOIN t2 USING(a);
sl@0
   216
    SELECT * FROM v5;
sl@0
   217
  }
sl@0
   218
} {1 22 4 55}
sl@0
   219
sl@0
   220
# Verify that the view v5 gets flattened.  see sqliteFlattenSubquery().
sl@0
   221
# This will only work if EXPLAIN is enabled.
sl@0
   222
# Ticket #272
sl@0
   223
#
sl@0
   224
ifcapable {explain} {
sl@0
   225
do_test view-5.3 {
sl@0
   226
  lsearch [execsql {
sl@0
   227
    EXPLAIN SELECT * FROM v5;
sl@0
   228
  }] OpenEphemeral
sl@0
   229
} {-1}
sl@0
   230
do_test view-5.4 {
sl@0
   231
  execsql {
sl@0
   232
    SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
sl@0
   233
  }
sl@0
   234
} {1 22 22 2 4 55 55 5}
sl@0
   235
do_test view-5.5 {
sl@0
   236
  lsearch [execsql {
sl@0
   237
    EXPLAIN SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
sl@0
   238
  }] OpenEphemeral
sl@0
   239
} {-1}
sl@0
   240
do_test view-5.6 {
sl@0
   241
  execsql {
sl@0
   242
    SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
sl@0
   243
  }
sl@0
   244
} {22 2 1 22 55 5 4 55}
sl@0
   245
do_test view-5.7 {
sl@0
   246
  lsearch [execsql {
sl@0
   247
    EXPLAIN SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
sl@0
   248
  }] OpenEphemeral
sl@0
   249
} {-1}
sl@0
   250
do_test view-5.8 {
sl@0
   251
  execsql {
sl@0
   252
    SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
sl@0
   253
  }
sl@0
   254
} {1 2 3 4 1 22 22 2 4 5 6 7 4 55 55 5}
sl@0
   255
do_test view-5.9 {
sl@0
   256
  lsearch [execsql {
sl@0
   257
    EXPLAIN SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
sl@0
   258
  }] OpenEphemeral
sl@0
   259
} {-1}
sl@0
   260
} ;# endif explain
sl@0
   261
sl@0
   262
do_test view-6.1 {
sl@0
   263
  execsql {
sl@0
   264
    SELECT min(x), min(a), min(b), min(c), min(a+b+c) FROM v2;
sl@0
   265
  }
sl@0
   266
} {7 8 9 10 27}
sl@0
   267
do_test view-6.2 {
sl@0
   268
  execsql {
sl@0
   269
    SELECT max(x), max(a), max(b), max(c), max(a+b+c) FROM v2;
sl@0
   270
  }
sl@0
   271
} {11 12 13 14 39}
sl@0
   272
sl@0
   273
do_test view-7.1 {
sl@0
   274
  execsql {
sl@0
   275
    CREATE TABLE test1(id integer primary key, a);
sl@0
   276
    CREATE TABLE test2(id integer, b);
sl@0
   277
    INSERT INTO test1 VALUES(1,2);
sl@0
   278
    INSERT INTO test2 VALUES(1,3);
sl@0
   279
    CREATE VIEW test AS
sl@0
   280
      SELECT test1.id, a, b
sl@0
   281
      FROM test1 JOIN test2 ON test2.id=test1.id;
sl@0
   282
    SELECT * FROM test;
sl@0
   283
  }
sl@0
   284
} {1 2 3}
sl@0
   285
do_test view-7.2 {
sl@0
   286
  db close
sl@0
   287
  sqlite3 db test.db
sl@0
   288
  execsql {
sl@0
   289
    SELECT * FROM test;
sl@0
   290
  }
sl@0
   291
} {1 2 3}
sl@0
   292
do_test view-7.3 {
sl@0
   293
  execsql {
sl@0
   294
    DROP VIEW test;
sl@0
   295
    CREATE VIEW test AS
sl@0
   296
      SELECT test1.id, a, b
sl@0
   297
      FROM test1 JOIN test2 USING(id);
sl@0
   298
    SELECT * FROM test;
sl@0
   299
  }
sl@0
   300
} {1 2 3}
sl@0
   301
do_test view-7.4 {
sl@0
   302
  db close
sl@0
   303
  sqlite3 db test.db
sl@0
   304
  execsql {
sl@0
   305
    SELECT * FROM test;
sl@0
   306
  }
sl@0
   307
} {1 2 3}
sl@0
   308
do_test view-7.5 {
sl@0
   309
  execsql {
sl@0
   310
    DROP VIEW test;
sl@0
   311
    CREATE VIEW test AS
sl@0
   312
      SELECT test1.id, a, b
sl@0
   313
      FROM test1 NATURAL JOIN test2;
sl@0
   314
    SELECT * FROM test;
sl@0
   315
  }
sl@0
   316
} {1 2 3}
sl@0
   317
do_test view-7.6 {
sl@0
   318
  db close
sl@0
   319
  sqlite3 db test.db
sl@0
   320
  execsql {
sl@0
   321
    SELECT * FROM test;
sl@0
   322
  }
sl@0
   323
} {1 2 3}
sl@0
   324
sl@0
   325
do_test view-8.1 {
sl@0
   326
  execsql {
sl@0
   327
    CREATE VIEW v6 AS SELECT pqr, xyz FROM v1;
sl@0
   328
    SELECT * FROM v6 ORDER BY xyz;
sl@0
   329
  }
sl@0
   330
} {7 2 13 5 19 8 27 12}
sl@0
   331
do_test view-8.2 {
sl@0
   332
  db close
sl@0
   333
  sqlite3 db test.db
sl@0
   334
  execsql {
sl@0
   335
    SELECT * FROM v6 ORDER BY xyz;
sl@0
   336
  }
sl@0
   337
} {7 2 13 5 19 8 27 12}
sl@0
   338
do_test view-8.3 {
sl@0
   339
  execsql {
sl@0
   340
    CREATE VIEW v7 AS SELECT pqr+xyz AS a FROM v6;
sl@0
   341
    SELECT * FROM v7 ORDER BY a;
sl@0
   342
  }
sl@0
   343
} {9 18 27 39}
sl@0
   344
sl@0
   345
ifcapable subquery {
sl@0
   346
  do_test view-8.4 {
sl@0
   347
    execsql {
sl@0
   348
      CREATE VIEW v8 AS SELECT max(cnt) AS mx FROM
sl@0
   349
        (SELECT a%2 AS eo, count(*) AS cnt FROM t1 GROUP BY eo);
sl@0
   350
      SELECT * FROM v8;
sl@0
   351
    }
sl@0
   352
  } 3
sl@0
   353
  do_test view-8.5 {
sl@0
   354
    execsql {
sl@0
   355
      SELECT mx+10, mx*2 FROM v8;
sl@0
   356
    }
sl@0
   357
  } {13 6}
sl@0
   358
  do_test view-8.6 {
sl@0
   359
    execsql {
sl@0
   360
      SELECT mx+10, pqr FROM v6, v8 WHERE xyz=2;
sl@0
   361
    }
sl@0
   362
  } {13 7}
sl@0
   363
  do_test view-8.7 {
sl@0
   364
    execsql {
sl@0
   365
      SELECT mx+10, pqr FROM v6, v8 WHERE xyz>2;
sl@0
   366
    }
sl@0
   367
  } {13 13 13 19 13 27}
sl@0
   368
} ;# ifcapable subquery
sl@0
   369
sl@0
   370
# Tests for a bug found by Michiel de Wit involving ORDER BY in a VIEW.
sl@0
   371
#
sl@0
   372
do_test view-9.1 {
sl@0
   373
  execsql {
sl@0
   374
    INSERT INTO t2 SELECT * FROM t2 WHERE a<5;
sl@0
   375
    INSERT INTO t2 SELECT * FROM t2 WHERE a<4;
sl@0
   376
    INSERT INTO t2 SELECT * FROM t2 WHERE a<3;
sl@0
   377
    SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1;
sl@0
   378
  }
sl@0
   379
} {1 2 4 8}
sl@0
   380
do_test view-9.2 {
sl@0
   381
  execsql {
sl@0
   382
    SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
sl@0
   383
  }
sl@0
   384
} {1 2 4}
sl@0
   385
do_test view-9.3 {
sl@0
   386
  execsql {
sl@0
   387
    CREATE VIEW v9 AS 
sl@0
   388
       SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
sl@0
   389
    SELECT * FROM v9;
sl@0
   390
  }
sl@0
   391
} {1 2 4}
sl@0
   392
do_test view-9.4 {
sl@0
   393
  execsql {
sl@0
   394
    SELECT * FROM v9 ORDER BY 1 DESC;
sl@0
   395
  }
sl@0
   396
} {4 2 1}
sl@0
   397
do_test view-9.5 {
sl@0
   398
  execsql {
sl@0
   399
    CREATE VIEW v10 AS 
sl@0
   400
       SELECT DISTINCT a, count(*) FROM t2 GROUP BY a ORDER BY 2 LIMIT 3;
sl@0
   401
    SELECT * FROM v10;
sl@0
   402
  }
sl@0
   403
} {5 1 4 2 3 4}
sl@0
   404
do_test view-9.6 {
sl@0
   405
  execsql {
sl@0
   406
    SELECT * FROM v10 ORDER BY 1;
sl@0
   407
  }
sl@0
   408
} {3 4 4 2 5 1}
sl@0
   409
sl@0
   410
# Tables with columns having peculiar quoted names used in views
sl@0
   411
# Ticket #756.
sl@0
   412
#
sl@0
   413
do_test view-10.1 {
sl@0
   414
  execsql {
sl@0
   415
    CREATE TABLE t3("9" integer, [4] text);
sl@0
   416
    INSERT INTO t3 VALUES(1,2);
sl@0
   417
    CREATE VIEW v_t3_a AS SELECT a.[9] FROM t3 AS a;
sl@0
   418
    CREATE VIEW v_t3_b AS SELECT "4" FROM t3;
sl@0
   419
    SELECT * FROM v_t3_a;
sl@0
   420
  }
sl@0
   421
} {1}
sl@0
   422
do_test view-10.2 {
sl@0
   423
  execsql {
sl@0
   424
    SELECT * FROM v_t3_b;
sl@0
   425
  }
sl@0
   426
} {2}
sl@0
   427
sl@0
   428
do_test view-11.1 {
sl@0
   429
  execsql {
sl@0
   430
    CREATE TABLE t4(a COLLATE NOCASE);
sl@0
   431
    INSERT INTO t4 VALUES('This');
sl@0
   432
    INSERT INTO t4 VALUES('this');
sl@0
   433
    INSERT INTO t4 VALUES('THIS');
sl@0
   434
    SELECT * FROM t4 WHERE a = 'THIS';
sl@0
   435
  }
sl@0
   436
} {This this THIS}
sl@0
   437
ifcapable subquery {
sl@0
   438
  do_test view-11.2 {
sl@0
   439
    execsql {
sl@0
   440
      SELECT * FROM (SELECT * FROM t4) WHERE a = 'THIS';
sl@0
   441
    }
sl@0
   442
  } {This this THIS}
sl@0
   443
}
sl@0
   444
do_test view-11.3 {
sl@0
   445
  execsql {
sl@0
   446
    CREATE VIEW v11 AS SELECT * FROM t4;
sl@0
   447
    SELECT * FROM v11 WHERE a = 'THIS';
sl@0
   448
  }
sl@0
   449
} {This this THIS}
sl@0
   450
sl@0
   451
# Ticket #1270:  Do not allow parameters in view definitions.
sl@0
   452
#
sl@0
   453
do_test view-12.1 {
sl@0
   454
  catchsql {
sl@0
   455
    CREATE VIEW v12 AS SELECT a FROM t1 WHERE b=?
sl@0
   456
  }
sl@0
   457
} {1 {parameters are not allowed in views}}
sl@0
   458
sl@0
   459
ifcapable attach {
sl@0
   460
  do_test view-13.1 {
sl@0
   461
    file delete -force test2.db
sl@0
   462
    catchsql {
sl@0
   463
      ATTACH 'test2.db' AS two;
sl@0
   464
      CREATE TABLE two.t2(x,y);
sl@0
   465
      CREATE VIEW v13 AS SELECT y FROM two.t2;
sl@0
   466
    }
sl@0
   467
  } {1 {view v13 cannot reference objects in database two}}
sl@0
   468
}
sl@0
   469
sl@0
   470
# Ticket #1658
sl@0
   471
#
sl@0
   472
do_test view-14.1 {
sl@0
   473
  catchsql {
sl@0
   474
    CREATE TEMP VIEW t1 AS SELECT a,b FROM t1;
sl@0
   475
    SELECT * FROM temp.t1;
sl@0
   476
  }
sl@0
   477
} {1 {view t1 is circularly defined}}
sl@0
   478
sl@0
   479
# Tickets #1688, #1709
sl@0
   480
#
sl@0
   481
do_test view-15.1 {
sl@0
   482
  execsql2 {
sl@0
   483
    CREATE VIEW v15 AS SELECT a AS x, b AS y FROM t1;
sl@0
   484
    SELECT * FROM v15 LIMIT 1;
sl@0
   485
  }
sl@0
   486
} {x 2 y 3}
sl@0
   487
do_test view-15.2 {
sl@0
   488
  execsql2 {
sl@0
   489
    SELECT x, y FROM v15 LIMIT 1
sl@0
   490
  }
sl@0
   491
} {x 2 y 3}
sl@0
   492
sl@0
   493
do_test view-16.1 {
sl@0
   494
  catchsql {
sl@0
   495
    CREATE VIEW IF NOT EXISTS v1 AS SELECT * FROM t1;
sl@0
   496
  }
sl@0
   497
} {0 {}}
sl@0
   498
do_test view-16.2 {
sl@0
   499
  execsql {
sl@0
   500
    SELECT sql FROM sqlite_master WHERE name='v1'
sl@0
   501
  }
sl@0
   502
} {{CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1}}
sl@0
   503
do_test view-16.3 {
sl@0
   504
  catchsql {
sl@0
   505
    DROP VIEW IF EXISTS nosuchview
sl@0
   506
  }
sl@0
   507
} {0 {}}
sl@0
   508
sl@0
   509
# correct error message when attempting to drop a view that does not
sl@0
   510
# exist.
sl@0
   511
#
sl@0
   512
do_test view-17.1 {
sl@0
   513
  catchsql {
sl@0
   514
    DROP VIEW nosuchview
sl@0
   515
  }
sl@0
   516
} {1 {no such view: nosuchview}}
sl@0
   517
do_test view-17.2 {
sl@0
   518
  catchsql {
sl@0
   519
    DROP VIEW main.nosuchview
sl@0
   520
  }
sl@0
   521
} {1 {no such view: main.nosuchview}}
sl@0
   522
sl@0
   523
do_test view-18.1 {
sl@0
   524
  execsql {
sl@0
   525
    DROP VIEW t1;
sl@0
   526
    DROP TABLE t1;
sl@0
   527
    CREATE TABLE t1(a, b, c);
sl@0
   528
    INSERT INTO t1 VALUES(1, 2, 3);
sl@0
   529
    INSERT INTO t1 VALUES(4, 5, 6);
sl@0
   530
sl@0
   531
    CREATE VIEW vv1 AS SELECT * FROM t1;
sl@0
   532
    CREATE VIEW vv2 AS SELECT * FROM vv1;
sl@0
   533
    CREATE VIEW vv3 AS SELECT * FROM vv2;
sl@0
   534
    CREATE VIEW vv4 AS SELECT * FROM vv3;
sl@0
   535
    CREATE VIEW vv5 AS SELECT * FROM vv4;
sl@0
   536
sl@0
   537
    SELECT * FROM vv5;
sl@0
   538
  }
sl@0
   539
} {1 2 3 4 5 6}
sl@0
   540
sl@0
   541
# Ticket #3308
sl@0
   542
# Make sure "rowid" columns in a view are named correctly.
sl@0
   543
#
sl@0
   544
do_test view-19.1 {
sl@0
   545
  execsql {
sl@0
   546
    CREATE VIEW v3308a AS SELECT rowid, * FROM t1;
sl@0
   547
  }
sl@0
   548
  execsql2 {
sl@0
   549
    SELECT * FROM v3308a
sl@0
   550
  }
sl@0
   551
} {rowid 1 a 1 b 2 c 3 rowid 2 a 4 b 5 c 6}
sl@0
   552
do_test view-19.2 {
sl@0
   553
  execsql {
sl@0
   554
    CREATE VIEW v3308b AS SELECT t1.rowid, t1.a, t1.b+t1.c FROM t1;
sl@0
   555
  }
sl@0
   556
  execsql2 {
sl@0
   557
    SELECT * FROM v3308b
sl@0
   558
  }
sl@0
   559
} {rowid 1 a 1 t1.b+t1.c 5 rowid 2 a 4 t1.b+t1.c 11}
sl@0
   560
do_test view-19.3 {
sl@0
   561
  execsql {
sl@0
   562
    CREATE VIEW v3308c AS SELECT t1.oid, A, t1.b+t1.c AS x FROM t1;
sl@0
   563
  }
sl@0
   564
  execsql2 {
sl@0
   565
    SELECT * FROM v3308c
sl@0
   566
  }
sl@0
   567
} {rowid 1 a 1 x 5 rowid 2 a 4 x 11}
sl@0
   568
sl@0
   569
finish_test