os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/colname.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
# 2008 July 15
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. 
sl@0
    12
#
sl@0
    13
# The focus of this file is testing how SQLite generates the names
sl@0
    14
# of columns in a result set.
sl@0
    15
#
sl@0
    16
# $Id: colname.test,v 1.5 2008/09/13 01:20:15 drh Exp $
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
# Rules (applied in order):
sl@0
    22
#
sl@0
    23
# (1) If there is an AS clause, use it.
sl@0
    24
#
sl@0
    25
# (2) A non-trival expression (not a table column name) then the name is
sl@0
    26
#     a copy of the expression text.
sl@0
    27
#
sl@0
    28
# (3) If short_column_names=ON, then just the abbreviated column name without
sl@0
    29
#     the table name.
sl@0
    30
#
sl@0
    31
# (4) When short_column_names=OFF and full_column_names=OFF then
sl@0
    32
#     use case (2) for simple queries and case (5) for joins.
sl@0
    33
#
sl@0
    34
# (5) When short_column_names=OFF and full_column_names=ON then
sl@0
    35
#     use the form: TABLE.COLUMN
sl@0
    36
#
sl@0
    37
sl@0
    38
sl@0
    39
# Verify the default settings for short_column_name and full_column_name
sl@0
    40
#
sl@0
    41
do_test colname-1.1 {
sl@0
    42
  db eval {PRAGMA short_column_names}
sl@0
    43
} {1}
sl@0
    44
do_test colname-1.2 {
sl@0
    45
  db eval {PRAGMA full_column_names}
sl@0
    46
} {0}
sl@0
    47
sl@0
    48
# Tests for then short=ON and full=any
sl@0
    49
#
sl@0
    50
do_test colname-2.1 {
sl@0
    51
  db eval {
sl@0
    52
    CREATE TABLE tabc(a,b,c);
sl@0
    53
    INSERT INTO tabc VALUES(1,2,3);
sl@0
    54
    CREATE TABLE txyz(x,y,z);
sl@0
    55
    INSERT INTO txyz VALUES(4,5,6);
sl@0
    56
    CREATE TABLE tboth(a,b,c,x,y,z);
sl@0
    57
    INSERT INTO tboth VALUES(11,12,13,14,15,16);
sl@0
    58
    CREATE VIEW v1 AS SELECT tabC.a, txyZ.x, * 
sl@0
    59
      FROM tabc, txyz ORDER BY 1 LIMIT 1;
sl@0
    60
    CREATE VIEW v2 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, *
sl@0
    61
      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
sl@0
    62
  }
sl@0
    63
  execsql2 {
sl@0
    64
    SELECT * FROM tabc;
sl@0
    65
  }
sl@0
    66
} {a 1 b 2 c 3}
sl@0
    67
do_test colname-2.2 {
sl@0
    68
  execsql2 {
sl@0
    69
    SELECT Tabc.a, tAbc.b, taBc.c, * FROM tabc
sl@0
    70
  }
sl@0
    71
} {a 1 b 2 c 3 a 1 b 2 c 3}
sl@0
    72
do_test colname-2.3 {
sl@0
    73
  execsql2 {
sl@0
    74
    SELECT +tabc.a, -tabc.b, tabc.c, * FROM tabc
sl@0
    75
  }
sl@0
    76
} {+tabc.a 1 -tabc.b -2 c 3 a 1 b 2 c 3}
sl@0
    77
do_test colname-2.4 {
sl@0
    78
  execsql2 {
sl@0
    79
    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC, * FROM tabc
sl@0
    80
  }
sl@0
    81
} {AAA 1 BBB -2 CCC 3 a 1 b 2 c 3}
sl@0
    82
do_test colname-2.5 {
sl@0
    83
  execsql2 {
sl@0
    84
    SELECT tabc.a, txyz.x, * FROM tabc, txyz;
sl@0
    85
  }
sl@0
    86
} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
sl@0
    87
do_test colname-2.6 {
sl@0
    88
  execsql2 {
sl@0
    89
    SELECT tabc.a, txyz.x, tabc.*, txyz.* FROM tabc, txyz;
sl@0
    90
  }
sl@0
    91
} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
sl@0
    92
do_test colname-2.7 {
sl@0
    93
  execsql2 {
sl@0
    94
    SELECT tabc.a, txyz.x, tboth.a, tboth.x, * FROM tabc, txyz, tboth;
sl@0
    95
  }
sl@0
    96
} {a 11 x 14 a 11 x 14 a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
sl@0
    97
do_test colname-2.8 {
sl@0
    98
  execsql2 {
sl@0
    99
    SELECT * FROM v1 ORDER BY 2;
sl@0
   100
  }
sl@0
   101
} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
sl@0
   102
do_test colname-2.9 {
sl@0
   103
  execsql2 {
sl@0
   104
    SELECT * FROM v2 ORDER BY 2;
sl@0
   105
  }
sl@0
   106
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
sl@0
   107
sl@0
   108
sl@0
   109
# Tests for short=OFF and full=OFF
sl@0
   110
#
sl@0
   111
do_test colname-3.1 {
sl@0
   112
  db eval {
sl@0
   113
    PRAGMA short_column_names=OFF;
sl@0
   114
    PRAGMA full_column_names=OFF;
sl@0
   115
    CREATE VIEW v3 AS SELECT tabC.a, txyZ.x, *
sl@0
   116
      FROM tabc, txyz ORDER BY 1 LIMIT 1;
sl@0
   117
    CREATE VIEW v4 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, * 
sl@0
   118
      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
sl@0
   119
  }
sl@0
   120
  execsql2 {
sl@0
   121
    SELECT * FROM tabc;
sl@0
   122
  }
sl@0
   123
} {a 1 b 2 c 3}
sl@0
   124
do_test colname-3.2 {
sl@0
   125
  execsql2 {
sl@0
   126
    SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
sl@0
   127
  }
sl@0
   128
} {Tabc.a 1 tAbc.b 2 taBc.c 3}
sl@0
   129
do_test colname-3.3 {
sl@0
   130
  execsql2 {
sl@0
   131
    SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
sl@0
   132
  }
sl@0
   133
} {+tabc.a 1 -tabc.b -2 tabc.c 3}
sl@0
   134
do_test colname-3.4 {
sl@0
   135
  execsql2 {
sl@0
   136
    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
sl@0
   137
  }
sl@0
   138
} {AAA 1 BBB -2 CCC 3}
sl@0
   139
do_test colname-3.5 {
sl@0
   140
  execsql2 {
sl@0
   141
    SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
sl@0
   142
  }
sl@0
   143
} {Tabc.a 1 Txyz.x 4 a 1 b 2 c 3 x 4 y 5 z 6}
sl@0
   144
do_test colname-3.6 {
sl@0
   145
  execsql2 {
sl@0
   146
    SELECT tabc.*, txyz.* FROM tabc, txyz;
sl@0
   147
  }
sl@0
   148
} {a 1 b 2 c 3 x 4 y 5 z 6}
sl@0
   149
do_test colname-3.7 {
sl@0
   150
  execsql2 {
sl@0
   151
    SELECT * FROM tabc, txyz, tboth;
sl@0
   152
  }
sl@0
   153
} {a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
sl@0
   154
do_test colname-3.8 {
sl@0
   155
  execsql2 {
sl@0
   156
    SELECT v1.a, * FROM v1 ORDER BY 2;
sl@0
   157
  }
sl@0
   158
} {v1.a 1 a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
sl@0
   159
do_test colname-3.9 {
sl@0
   160
  execsql2 {
sl@0
   161
    SELECT * FROM v2 ORDER BY 2;
sl@0
   162
  }
sl@0
   163
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
sl@0
   164
do_test colname-3.10 {
sl@0
   165
  execsql2 {
sl@0
   166
    SELECT * FROM v3 ORDER BY 2;
sl@0
   167
  }
sl@0
   168
} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
sl@0
   169
do_test colname-3.11 {
sl@0
   170
  execsql2 {
sl@0
   171
    SELECT * FROM v4 ORDER BY 2;
sl@0
   172
  }
sl@0
   173
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
sl@0
   174
sl@0
   175
# Test for short=OFF and full=ON
sl@0
   176
#
sl@0
   177
do_test colname-4.1 {
sl@0
   178
  db eval {
sl@0
   179
    PRAGMA short_column_names=OFF;
sl@0
   180
    PRAGMA full_column_names=ON;
sl@0
   181
    CREATE VIEW v5 AS SELECT tabC.a, txyZ.x, *
sl@0
   182
      FROM tabc, txyz ORDER BY 1 LIMIT 1;
sl@0
   183
    CREATE VIEW v6 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, * 
sl@0
   184
      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
sl@0
   185
  }
sl@0
   186
  execsql2 {
sl@0
   187
    SELECT * FROM tabc;
sl@0
   188
  }
sl@0
   189
} {tabc.a 1 tabc.b 2 tabc.c 3}
sl@0
   190
do_test colname-4.2 {
sl@0
   191
  execsql2 {
sl@0
   192
    SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
sl@0
   193
  }
sl@0
   194
} {tabc.a 1 tabc.b 2 tabc.c 3}
sl@0
   195
do_test colname-4.3 {
sl@0
   196
  execsql2 {
sl@0
   197
    SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
sl@0
   198
  }
sl@0
   199
} {+tabc.a 1 -tabc.b -2 tabc.c 3}
sl@0
   200
do_test colname-4.4 {
sl@0
   201
  execsql2 {
sl@0
   202
    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
sl@0
   203
  }
sl@0
   204
} {AAA 1 BBB -2 CCC 3}
sl@0
   205
do_test colname-4.5 {
sl@0
   206
  execsql2 {
sl@0
   207
    SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
sl@0
   208
  }
sl@0
   209
} {tabc.a 1 txyz.x 4 tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
sl@0
   210
do_test colname-4.6 {
sl@0
   211
  execsql2 {
sl@0
   212
    SELECT tabc.*, txyz.* FROM tabc, txyz;
sl@0
   213
  }
sl@0
   214
} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
sl@0
   215
do_test colname-4.7 {
sl@0
   216
  execsql2 {
sl@0
   217
    SELECT * FROM tabc, txyz, tboth;
sl@0
   218
  }
sl@0
   219
} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6 tboth.a 11 tboth.b 12 tboth.c 13 tboth.x 14 tboth.y 15 tboth.z 16}
sl@0
   220
do_test colname-4.8 {
sl@0
   221
  execsql2 {
sl@0
   222
    SELECT * FROM v1 ORDER BY 2;
sl@0
   223
  }
sl@0
   224
} {v1.a 1 v1.x 4 v1.a:1 1 v1.b 2 v1.c 3 v1.x:1 4 v1.y 5 v1.z 6}
sl@0
   225
do_test colname-4.9 {
sl@0
   226
  execsql2 {
sl@0
   227
    SELECT * FROM v2 ORDER BY 2;
sl@0
   228
  }
sl@0
   229
} {v2.a 1 v2.x 4 v2.a:1 11 v2.x:1 14 v2.a:2 1 v2.b 2 v2.c 3 v2.x:2 4 v2.y 5 v2.z 6 v2.a:3 11 v2.b:1 12 v2.c:1 13 v2.x:3 14 v2.y:1 15 v2.z:1 16}
sl@0
   230
do_test colname-4.10 {
sl@0
   231
  execsql2 {
sl@0
   232
    SELECT * FROM v3 ORDER BY 2;
sl@0
   233
  }
sl@0
   234
} {v3.a 1 v3.x 4 v3.a:1 1 v3.b 2 v3.c 3 v3.x:1 4 v3.y 5 v3.z 6}
sl@0
   235
do_test colname-4.11 {
sl@0
   236
  execsql2 {
sl@0
   237
    SELECT * FROM v4 ORDER BY 2;
sl@0
   238
  }
sl@0
   239
} {v4.a 1 v4.x 4 v4.a:1 11 v4.x:1 14 v4.a:2 1 v4.b 2 v4.c 3 v4.x:2 4 v4.y 5 v4.z 6 v4.a:3 11 v4.b:1 12 v4.c:1 13 v4.x:3 14 v4.y:1 15 v4.z:1 16}
sl@0
   240
do_test colname-4.12 {
sl@0
   241
  execsql2 {
sl@0
   242
    SELECT * FROM v5 ORDER BY 2;
sl@0
   243
  }
sl@0
   244
} {v5.a 1 v5.x 4 v5.a:1 1 v5.b 2 v5.c 3 v5.x:1 4 v5.y 5 v5.z 6}
sl@0
   245
do_test colname-4.13 {
sl@0
   246
  execsql2 {
sl@0
   247
    SELECT * FROM v6 ORDER BY 2;
sl@0
   248
  }
sl@0
   249
} {v6.a 1 v6.x 4 v6.a:1 11 v6.x:1 14 v6.a:2 1 v6.b 2 v6.c 3 v6.x:2 4 v6.y 5 v6.z 6 v6.a:3 11 v6.b:1 12 v6.c:1 13 v6.x:3 14 v6.y:1 15 v6.z:1 16}
sl@0
   250
sl@0
   251
# ticket #3229
sl@0
   252
do_test colname-5.1 {
sl@0
   253
  lreplace [db eval {
sl@0
   254
    SELECT x.* FROM sqlite_master X LIMIT 1;
sl@0
   255
  }] 3 3 x
sl@0
   256
} {table tabc tabc x {CREATE TABLE tabc(a,b,c)}}
sl@0
   257
sl@0
   258
# ticket #3370, #3371, #3372
sl@0
   259
#
sl@0
   260
do_test colname-6.1 {
sl@0
   261
  db close
sl@0
   262
  sqlite3 db test.db
sl@0
   263
  db eval {
sl@0
   264
    CREATE TABLE t6(a, ['a'], ["a"], "[a]", [`a`]);
sl@0
   265
    INSERT INTO t6 VALUES(1,2,3,4,5);
sl@0
   266
  }
sl@0
   267
  execsql2 {SELECT * FROM t6}
sl@0
   268
} {a 1 'a' 2 {"a"} 3 {[a]} 4 `a` 5}
sl@0
   269
do_test colname-6.2 {
sl@0
   270
  execsql2 {SELECT ['a'], [`a`], "[a]", [a], ["a"] FROM t6}
sl@0
   271
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
sl@0
   272
do_test colname-6.3 {
sl@0
   273
  execsql2 {SELECT "'a'", "`a`", "[a]", "a", """a""" FROM t6}
sl@0
   274
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
sl@0
   275
do_test colname-6.4 {
sl@0
   276
  execsql2 {SELECT `'a'`, ```a```, `[a]`, `a`, `"a"` FROM t6}
sl@0
   277
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
sl@0
   278
do_test colname-6.11 {
sl@0
   279
  execsql2 {SELECT a, max(a) AS m FROM t6}
sl@0
   280
} {a 1 m 1}
sl@0
   281
do_test colname-6.12 {
sl@0
   282
  execsql2 {SELECT `a`, max(a) AS m FROM t6}
sl@0
   283
} {a 1 m 1}
sl@0
   284
do_test colname-6.13 {
sl@0
   285
  execsql2 {SELECT "a", max(a) AS m FROM t6}
sl@0
   286
} {a 1 m 1}
sl@0
   287
do_test colname-6.14 {
sl@0
   288
  execsql2 {SELECT [a], max(a) AS m FROM t6}
sl@0
   289
} {a 1 m 1}
sl@0
   290
do_test colname-6.15 {
sl@0
   291
  execsql2 {SELECT t6.a, max(a) AS m FROM t6}
sl@0
   292
} {a 1 m 1}
sl@0
   293
do_test colname-6.16 {
sl@0
   294
  execsql2 {SELECT ['a'], max(['a']) AS m FROM t6}
sl@0
   295
} {'a' 2 m 2}
sl@0
   296
do_test colname-6.17 {
sl@0
   297
  execsql2 {SELECT ["a"], max(["a"]) AS m FROM t6}
sl@0
   298
} {{"a"} 3 m 3}
sl@0
   299
do_test colname-6.18 {
sl@0
   300
  execsql2 {SELECT "[a]", max("[a]") AS m FROM t6}
sl@0
   301
} {{[a]} 4 m 4}
sl@0
   302
do_test colname-6.19 {
sl@0
   303
  execsql2 {SELECT "`a`", max([`a`]) AS m FROM t6}
sl@0
   304
} {`a` 5 m 5}
sl@0
   305
sl@0
   306
finish_test