]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Update column naming rules. Ticket #3221. Rules for column naming
authordrh <drh@noemail.net>
Tue, 15 Jul 2008 20:56:17 +0000 (20:56 +0000)
committerdrh <drh@noemail.net>
Tue, 15 Jul 2008 20:56:17 +0000 (20:56 +0000)
are still subject to change (except for the AS rule which we promise to
keep the same) but are more consistent now.  And the rules are tested
using a new test script. (CVS 5416)

FossilOrigin-Name: 61f6e19755b85bcb065f85fc425c2172badea308

manifest
manifest.uuid
src/select.c
test/colname.test [new file with mode: 0644]

index 06123531ce6896bdf7955fdae07b4b4b75ed1e02..5ab9e9e4eb734ef1f368c84ea4b313e3ad234380 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Continuing\swork\son\simproved\stest\scoverage.\s(CVS\s5415)
-D 2008-07-15T14:47:19
+C Update\scolumn\snaming\srules.\s\sTicket\s#3221.\s\sRules\sfor\scolumn\snaming\nare\sstill\ssubject\sto\schange\s(except\sfor\sthe\sAS\srule\swhich\swe\spromise\sto\nkeep\sthe\ssame)\sbut\sare\smore\sconsistent\snow.\s\sAnd\sthe\srules\sare\stested\nusing\sa\snew\stest\sscript.\s(CVS\s5416)
+D 2008-07-15T20:56:17
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in a03f7cb4f7ad50bc53a788c6c544430e81f95de4
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -141,7 +141,7 @@ F src/pragma.c 6fad83fbcc7ec6e76d91fe2805fe972ff3af6a0c
 F src/prepare.c c9bb0aacb7a571d049805699ed18f2bb136ea091
 F src/printf.c 2174222bc346a11b1eac2a654ccc4f635355ae7e
 F src/random.c 5c754319d38abdd6acd74601ee0105504adc508a
-F src/select.c 972bdef5063d1ec2b6375f8bf07e3eec2d5bb1cb
+F src/select.c f3a9d9f4eee19155af0885334f24a1479ae9bda5
 F src/shell.c 4b835fe734304ac22a3385868cd3790c1e4f7aa1
 F src/sqlite.h.in f85aad3e88f382474407672ad5950c5d4328826d
 F src/sqlite3ext.h 1e3887c9bd3ae66cb599e922824b04cd0d0f2c3e
@@ -241,6 +241,7 @@ F test/collate8.test 7ed2461305ac959886a064dc1e3cf15e155a183f
 F test/collate9.test 3adcc799229545940df2f25308dd1ad65869145a
 F test/collateA.test b8218ab90d1fa5c59dcf156efabb1b2599c580d6
 F test/colmeta.test 087c42997754b8c648819832241daf724f813322
+F test/colname.test fa138771a03d1851e6ec2d10f2a9592c3ea4d5dd
 F test/conflict.test bb29b052c60a1f7eb6382be77902061d1f305318
 F test/corrupt.test af069d971853dbe12af936910bfa49d92f7b16e9
 F test/corrupt2.test e56f45006e7eb3ee680ad3b61c92cc14ae09986e
@@ -607,7 +608,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P 7cf91e08c08ce515c24c738c7d079f5b81eebee6
-R bcfadd4ec8973f68be342c4f22b4e260
+P c942a38e9aa80770e7e2819e51b43fa7aa854d71
+R 4b3353fa20b1c242a7f991bd99f44a48
 U drh
-Z 9020cf2172e9c22d2421ab034ffa8201
+Z 6835a5cfc38caa7fcd38147e81944a8f
index 1b55c531a9a044b4303abf5117d8a9ff69ce7d06..a0b998fecb7977516ce28a1bf52effd85be7c33f 100644 (file)
@@ -1 +1 @@
-c942a38e9aa80770e7e2819e51b43fa7aa854d71
\ No newline at end of file
+61f6e19755b85bcb065f85fc425c2172badea308
\ No newline at end of file
index 30b9ddeb3ed98002faf33175f10a9b618773d812..54f9cc497555442b51389d6130c485e9ac58b620 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.456 2008/07/15 00:27:35 drh Exp $
+** $Id: select.c,v 1.457 2008/07/15 20:56:17 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -1138,20 +1138,25 @@ static int prepSelectStmt(Parse*, Select*);
 */
 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
   Table *pTab;
-  int i, j;
+  int i, j, rc;
   ExprList *pEList;
   Column *aCol, *pCol;
   sqlite3 *db = pParse->db;
-
-  if( sqlite3SelectResolve(pParse, pSelect, 0) ){
-    return 0;
-  }
-
-  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
-  if( prepSelectStmt(pParse, pSelect) ){
-    return 0;
+  int savedFlags;
+
+  savedFlags = db->flags;
+  db->flags &= ~SQLITE_FullColNames;
+  db->flags |= SQLITE_ShortColNames;
+  rc = sqlite3SelectResolve(pParse, pSelect, 0);
+  if( rc==SQLITE_OK ){
+    while( pSelect->pPrior ) pSelect = pSelect->pPrior;
+    rc = prepSelectStmt(pParse, pSelect);
+    if( rc==SQLITE_OK ){
+      rc = sqlite3SelectResolve(pParse, pSelect, 0);
+    }
   }
-  if( sqlite3SelectResolve(pParse, pSelect, 0) ){
+  db->flags = savedFlags;
+  if( rc ){
     return 0;
   }
   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
@@ -1362,8 +1367,8 @@ static int prepSelectStmt(Parse *pParse, Select *p){
     struct ExprList_item *a = pEList->a;
     ExprList *pNew = 0;
     int flags = pParse->db->flags;
-    int longNames = (flags & SQLITE_FullColNames)!=0 &&
-                      (flags & SQLITE_ShortColNames)==0;
+    int longNames = (flags & SQLITE_FullColNames)!=0
+                      && (flags & SQLITE_ShortColNames)==0;
 
     for(k=0; k<pEList->nExpr; k++){
       Expr *pE = a[k].pExpr;
@@ -1435,9 +1440,14 @@ static int prepSelectStmt(Parse *pParse, Select *p){
               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
               if( pExpr==0 ) break;
               setQuotedToken(pParse, &pLeft->token, zTabName);
+#if 1
               setToken(&pExpr->span, 
                   sqlite3MPrintf(db, "%s.%s", zTabName, zName));
               pExpr->span.dyn = 1;
+#else
+              pExpr->span = pRight->token;
+              pExpr->span.dyn = 0;
+#endif
               pExpr->token.z = 0;
               pExpr->token.n = 0;
               pExpr->token.dyn = 0;
diff --git a/test/colname.test b/test/colname.test
new file mode 100644 (file)
index 0000000..781ffb4
--- /dev/null
@@ -0,0 +1,254 @@
+# 2008 July 15
+#
+# 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. 
+#
+# The focus of this file is testing how SQLite generates the names
+# of columns in a result set.
+#
+# $Id: colname.test,v 1.1 2008/07/15 20:56:17 drh Exp $
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+# Rules (applied in order):
+#
+# (1) If there is an AS clause, use it.
+#
+# (2) A non-trival expression (not a table column name) then the name is
+#     a copy of the expression text.
+#
+# (3) If short_column_names=ON, then just the abbreviated column name without
+#     the table name.
+#
+# (4) When short_column_names=OFF and full_column_names=OFF then
+#     use case (2) for simple queries and case (5) for joins.
+#
+# (5) When short_column_names=OFF and full_column_names=ON then
+#     use the form: TABLE.COLUMN
+#
+
+
+# Verify the default settings for short_column_name and full_column_name
+#
+do_test colname-1.1 {
+  db eval {PRAGMA short_column_names}
+} {1}
+do_test colname-1.2 {
+  db eval {PRAGMA full_column_names}
+} {0}
+
+# Tests for then short=ON and full=any
+#
+do_test colname-2.1 {
+  db eval {
+    CREATE TABLE tabc(a,b,c);
+    INSERT INTO tabc VALUES(1,2,3);
+    CREATE TABLE txyz(x,y,z);
+    INSERT INTO txyz VALUES(4,5,6);
+    CREATE TABLE tboth(a,b,c,x,y,z);
+    INSERT INTO tboth VALUES(11,12,13,14,15,16);
+    CREATE VIEW v1 AS SELECT tabC.a, txyZ.x, * 
+      FROM tabc, txyz ORDER BY 1 LIMIT 1;
+    CREATE VIEW v2 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, *
+      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
+  }
+  execsql2 {
+    SELECT * FROM tabc;
+  }
+} {a 1 b 2 c 3}
+do_test colname-2.2 {
+  execsql2 {
+    SELECT Tabc.a, tAbc.b, taBc.c, * FROM tabc
+  }
+} {a 1 b 2 c 3 a 1 b 2 c 3}
+do_test colname-2.3 {
+  execsql2 {
+    SELECT +tabc.a, -tabc.b, tabc.c, * FROM tabc
+  }
+} {+tabc.a 1 -tabc.b -2 c 3 a 1 b 2 c 3}
+do_test colname-2.4 {
+  execsql2 {
+    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC, * FROM tabc
+  }
+} {AAA 1 BBB -2 CCC 3 a 1 b 2 c 3}
+do_test colname-2.5 {
+  execsql2 {
+    SELECT tabc.a, txyz.x, * FROM tabc, txyz;
+  }
+} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
+do_test colname-2.6 {
+  execsql2 {
+    SELECT tabc.a, txyz.x, tabc.*, txyz.* FROM tabc, txyz;
+  }
+} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
+do_test colname-2.7 {
+  execsql2 {
+    SELECT tabc.a, txyz.x, tboth.a, tboth.x, * FROM tabc, txyz, tboth;
+  }
+} {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}
+do_test colname-2.8 {
+  execsql2 {
+    SELECT * FROM v1 ORDER BY 2;
+  }
+} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
+do_test colname-2.9 {
+  execsql2 {
+    SELECT * FROM v2 ORDER BY 2;
+  }
+} {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}
+
+
+# Tests for short=OFF and full=OFF
+#
+do_test colname-3.1 {
+  db eval {
+    PRAGMA short_column_names=OFF;
+    PRAGMA full_column_names=OFF;
+    CREATE VIEW v3 AS SELECT tabC.a, txyZ.x, *
+      FROM tabc, txyz ORDER BY 1 LIMIT 1;
+    CREATE VIEW v4 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, * 
+      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
+  }
+  execsql2 {
+    SELECT * FROM tabc;
+  }
+} {a 1 b 2 c 3}
+do_test colname-3.2 {
+  execsql2 {
+    SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
+  }
+} {Tabc.a 1 tAbc.b 2 taBc.c 3}
+do_test colname-3.3 {
+  execsql2 {
+    SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
+  }
+} {+tabc.a 1 -tabc.b -2 tabc.c 3}
+do_test colname-3.4 {
+  execsql2 {
+    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
+  }
+} {AAA 1 BBB -2 CCC 3}
+do_test colname-3.5 {
+  execsql2 {
+    SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
+  }
+} {Tabc.a 1 Txyz.x 4 a 1 b 2 c 3 x 4 y 5 z 6}
+do_test colname-3.6 {
+  execsql2 {
+    SELECT tabc.*, txyz.* FROM tabc, txyz;
+  }
+} {a 1 b 2 c 3 x 4 y 5 z 6}
+do_test colname-3.7 {
+  execsql2 {
+    SELECT * FROM tabc, txyz, tboth;
+  }
+} {a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
+do_test colname-3.8 {
+  execsql2 {
+    SELECT v1.a, * FROM v1 ORDER BY 2;
+  }
+} {v1.a 1 a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
+do_test colname-3.9 {
+  execsql2 {
+    SELECT * FROM v2 ORDER BY 2;
+  }
+} {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}
+do_test colname-3.10 {
+  execsql2 {
+    SELECT * FROM v3 ORDER BY 2;
+  }
+} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
+do_test colname-3.11 {
+  execsql2 {
+    SELECT * FROM v4 ORDER BY 2;
+  }
+} {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}
+
+# Test for short=OFF and full=ON
+#
+do_test colname-4.1 {
+breakpoint
+  db eval {
+    PRAGMA short_column_names=OFF;
+    PRAGMA full_column_names=ON;
+    CREATE VIEW v5 AS SELECT tabC.a, txyZ.x, *
+      FROM tabc, txyz ORDER BY 1 LIMIT 1;
+    CREATE VIEW v6 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, * 
+      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
+  }
+  execsql2 {
+    SELECT * FROM tabc;
+  }
+} {tabc.a 1 tabc.b 2 tabc.c 3}
+do_test colname-4.2 {
+  execsql2 {
+    SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
+  }
+} {tabc.a 1 tabc.b 2 tabc.c 3}
+do_test colname-4.3 {
+  execsql2 {
+    SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
+  }
+} {+tabc.a 1 -tabc.b -2 tabc.c 3}
+do_test colname-4.4 {
+  execsql2 {
+    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
+  }
+} {AAA 1 BBB -2 CCC 3}
+do_test colname-4.5 {
+  execsql2 {
+    SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
+  }
+} {tabc.a 1 txyz.x 4 tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
+do_test colname-4.6 {
+  execsql2 {
+    SELECT tabc.*, txyz.* FROM tabc, txyz;
+  }
+} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
+do_test colname-4.7 {
+  execsql2 {
+    SELECT * FROM tabc, txyz, tboth;
+  }
+} {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}
+do_test colname-4.8 {
+  execsql2 {
+    SELECT * FROM v1 ORDER BY 2;
+  }
+} {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}
+do_test colname-4.9 {
+  execsql2 {
+    SELECT * FROM v2 ORDER BY 2;
+  }
+} {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}
+do_test colname-4.10 {
+  execsql2 {
+    SELECT * FROM v3 ORDER BY 2;
+  }
+} {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}
+do_test colname-4.11 {
+  execsql2 {
+    SELECT * FROM v4 ORDER BY 2;
+  }
+} {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}
+do_test colname-4.12 {
+breakpoint
+  execsql2 {
+    SELECT * FROM v5 ORDER BY 2;
+  }
+} {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}
+do_test colname-4.13 {
+  execsql2 {
+    SELECT * FROM v6 ORDER BY 2;
+  }
+} {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}
+
+
+finish_test