os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/insert2.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
# 2001 September 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.  The
sl@0
    12
# focus of this file is testing the INSERT statement that takes is
sl@0
    13
# result from a SELECT.
sl@0
    14
#
sl@0
    15
# $Id: insert2.test,v 1.19 2008/01/16 18:20:42 danielk1977 Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
# Create some tables with data that we can select against
sl@0
    21
#
sl@0
    22
do_test insert2-1.0 {
sl@0
    23
  execsql {CREATE TABLE d1(n int, log int);}
sl@0
    24
  for {set i 1} {$i<=20} {incr i} {
sl@0
    25
    for {set j 0} {(1<<$j)<$i} {incr j} {}
sl@0
    26
    execsql "INSERT INTO d1 VALUES($i,$j)"
sl@0
    27
  }
sl@0
    28
  execsql {SELECT * FROM d1 ORDER BY n}
sl@0
    29
} {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5}
sl@0
    30
sl@0
    31
# Insert into a new table from the old one.
sl@0
    32
#
sl@0
    33
do_test insert2-1.1.1 {
sl@0
    34
  execsql {
sl@0
    35
    CREATE TABLE t1(log int, cnt int);
sl@0
    36
    PRAGMA count_changes=on;
sl@0
    37
  }
sl@0
    38
  ifcapable explain {
sl@0
    39
    execsql {
sl@0
    40
      EXPLAIN INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
sl@0
    41
    }
sl@0
    42
  }
sl@0
    43
  execsql {
sl@0
    44
    INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
sl@0
    45
  }
sl@0
    46
} {6}
sl@0
    47
do_test insert2-1.1.2 {
sl@0
    48
  db changes
sl@0
    49
} {6}
sl@0
    50
do_test insert2-1.1.3 {
sl@0
    51
  execsql {SELECT * FROM t1 ORDER BY log}
sl@0
    52
} {0 1 1 1 2 2 3 4 4 8 5 4}
sl@0
    53
sl@0
    54
ifcapable compound {
sl@0
    55
do_test insert2-1.2.1 {
sl@0
    56
  catch {execsql {DROP TABLE t1}}
sl@0
    57
  execsql {
sl@0
    58
    CREATE TABLE t1(log int, cnt int);
sl@0
    59
    INSERT INTO t1 
sl@0
    60
       SELECT log, count(*) FROM d1 GROUP BY log
sl@0
    61
       EXCEPT SELECT n-1,log FROM d1;
sl@0
    62
  }
sl@0
    63
} {4}
sl@0
    64
do_test insert2-1.2.2 {
sl@0
    65
  execsql {
sl@0
    66
    SELECT * FROM t1 ORDER BY log;
sl@0
    67
  }
sl@0
    68
} {0 1 3 4 4 8 5 4}
sl@0
    69
do_test insert2-1.3.1 {
sl@0
    70
  catch {execsql {DROP TABLE t1}}
sl@0
    71
  execsql {
sl@0
    72
    CREATE TABLE t1(log int, cnt int);
sl@0
    73
    PRAGMA count_changes=off;
sl@0
    74
    INSERT INTO t1 
sl@0
    75
       SELECT log, count(*) FROM d1 GROUP BY log
sl@0
    76
       INTERSECT SELECT n-1,log FROM d1;
sl@0
    77
  }
sl@0
    78
} {}
sl@0
    79
do_test insert2-1.3.2 {
sl@0
    80
  execsql {
sl@0
    81
    SELECT * FROM t1 ORDER BY log;
sl@0
    82
  }
sl@0
    83
} {1 1 2 2}
sl@0
    84
} ;# ifcapable compound
sl@0
    85
execsql {PRAGMA count_changes=off;}
sl@0
    86
sl@0
    87
do_test insert2-1.4 {
sl@0
    88
  catch {execsql {DROP TABLE t1}}
sl@0
    89
  set r [execsql {
sl@0
    90
    CREATE TABLE t1(log int, cnt int);
sl@0
    91
    CREATE INDEX i1 ON t1(log);
sl@0
    92
    CREATE INDEX i2 ON t1(cnt);
sl@0
    93
    INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
sl@0
    94
    SELECT * FROM t1 ORDER BY log;
sl@0
    95
  }]
sl@0
    96
  lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
sl@0
    97
  lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
sl@0
    98
} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
sl@0
    99
sl@0
   100
do_test insert2-2.0 {
sl@0
   101
  execsql {
sl@0
   102
    CREATE TABLE t3(a,b,c);
sl@0
   103
    CREATE TABLE t4(x,y);
sl@0
   104
    INSERT INTO t4 VALUES(1,2);
sl@0
   105
    SELECT * FROM t4;
sl@0
   106
  }
sl@0
   107
} {1 2}
sl@0
   108
do_test insert2-2.1 {
sl@0
   109
  execsql {
sl@0
   110
    INSERT INTO t3(a,c) SELECT * FROM t4;
sl@0
   111
    SELECT * FROM t3;
sl@0
   112
  }
sl@0
   113
} {1 {} 2}
sl@0
   114
do_test insert2-2.2 {
sl@0
   115
  execsql {
sl@0
   116
    DELETE FROM t3;
sl@0
   117
    INSERT INTO t3(c,b) SELECT * FROM t4;
sl@0
   118
    SELECT * FROM t3;
sl@0
   119
  }
sl@0
   120
} {{} 2 1}
sl@0
   121
do_test insert2-2.3 {
sl@0
   122
  execsql {
sl@0
   123
    DELETE FROM t3;
sl@0
   124
    INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4;
sl@0
   125
    SELECT * FROM t3;
sl@0
   126
  }
sl@0
   127
} {hi 2 1}
sl@0
   128
sl@0
   129
integrity_check insert2-3.0
sl@0
   130
sl@0
   131
# File table t4 with lots of data
sl@0
   132
#
sl@0
   133
do_test insert2-3.1 {
sl@0
   134
  execsql {
sl@0
   135
    SELECT * from t4;
sl@0
   136
  }
sl@0
   137
} {1 2}
sl@0
   138
do_test insert2-3.2 {
sl@0
   139
  set x [db total_changes]
sl@0
   140
  execsql {
sl@0
   141
    BEGIN;
sl@0
   142
    INSERT INTO t4 VALUES(2,4);
sl@0
   143
    INSERT INTO t4 VALUES(3,6);
sl@0
   144
    INSERT INTO t4 VALUES(4,8);
sl@0
   145
    INSERT INTO t4 VALUES(5,10);
sl@0
   146
    INSERT INTO t4 VALUES(6,12);
sl@0
   147
    INSERT INTO t4 VALUES(7,14);
sl@0
   148
    INSERT INTO t4 VALUES(8,16);
sl@0
   149
    INSERT INTO t4 VALUES(9,18);
sl@0
   150
    INSERT INTO t4 VALUES(10,20);
sl@0
   151
    COMMIT;
sl@0
   152
  }
sl@0
   153
  expr [db total_changes] - $x
sl@0
   154
} {9}
sl@0
   155
do_test insert2-3.2.1 {
sl@0
   156
  execsql {
sl@0
   157
    SELECT count(*) FROM t4;
sl@0
   158
  }
sl@0
   159
} {10}
sl@0
   160
do_test insert2-3.3 {
sl@0
   161
  ifcapable subquery {
sl@0
   162
    execsql {
sl@0
   163
      BEGIN;
sl@0
   164
      INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
sl@0
   165
      INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
sl@0
   166
      INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
sl@0
   167
      INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
sl@0
   168
      COMMIT;
sl@0
   169
      SELECT count(*) FROM t4;
sl@0
   170
    }
sl@0
   171
  } else {
sl@0
   172
    db function max_x_t4 {execsql {SELECT max(x) FROM t4}}
sl@0
   173
    execsql {
sl@0
   174
      BEGIN;
sl@0
   175
      INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
sl@0
   176
      INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
sl@0
   177
      INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
sl@0
   178
      INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
sl@0
   179
      COMMIT;
sl@0
   180
      SELECT count(*) FROM t4;
sl@0
   181
    }
sl@0
   182
  }
sl@0
   183
} {160}
sl@0
   184
do_test insert2-3.4 {
sl@0
   185
  execsql {
sl@0
   186
    BEGIN;
sl@0
   187
    UPDATE t4 SET y='lots of data for the row where x=' || x
sl@0
   188
                     || ' and y=' || y || ' - even more data to fill space';
sl@0
   189
    COMMIT;
sl@0
   190
    SELECT count(*) FROM t4;
sl@0
   191
  }
sl@0
   192
} {160}
sl@0
   193
do_test insert2-3.5 {
sl@0
   194
  ifcapable subquery {
sl@0
   195
    execsql {
sl@0
   196
      BEGIN;
sl@0
   197
      INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4;
sl@0
   198
      SELECT count(*) from t4;
sl@0
   199
      ROLLBACK;
sl@0
   200
    }
sl@0
   201
  } else {
sl@0
   202
    execsql {
sl@0
   203
      BEGIN;
sl@0
   204
      INSERT INTO t4 SELECT x+max_x_t4()+1,y FROM t4;
sl@0
   205
      SELECT count(*) from t4;
sl@0
   206
      ROLLBACK;
sl@0
   207
    }
sl@0
   208
  }
sl@0
   209
} {320}
sl@0
   210
do_test insert2-3.6 {
sl@0
   211
  execsql {
sl@0
   212
    SELECT count(*) FROM t4;
sl@0
   213
  }
sl@0
   214
} {160}
sl@0
   215
do_test insert2-3.7 {
sl@0
   216
  execsql {
sl@0
   217
    BEGIN;
sl@0
   218
    DELETE FROM t4 WHERE x!=123;
sl@0
   219
    SELECT count(*) FROM t4;
sl@0
   220
    ROLLBACK;
sl@0
   221
  }
sl@0
   222
} {1}
sl@0
   223
do_test insert2-3.8 {
sl@0
   224
  db changes
sl@0
   225
} {159}
sl@0
   226
integrity_check insert2-3.9
sl@0
   227
sl@0
   228
# Ticket #901
sl@0
   229
#
sl@0
   230
ifcapable tempdb {
sl@0
   231
  do_test insert2-4.1 {
sl@0
   232
    execsql {
sl@0
   233
      CREATE TABLE Dependencies(depId integer primary key,
sl@0
   234
        class integer, name str, flag str);
sl@0
   235
      CREATE TEMPORARY TABLE DepCheck(troveId INT, depNum INT,
sl@0
   236
        flagCount INT, isProvides BOOL, class INTEGER, name STRING,
sl@0
   237
        flag STRING);
sl@0
   238
      INSERT INTO DepCheck 
sl@0
   239
         VALUES(-1, 0, 1, 0, 2, 'libc.so.6', 'GLIBC_2.0');
sl@0
   240
      INSERT INTO Dependencies 
sl@0
   241
         SELECT DISTINCT 
sl@0
   242
             NULL, 
sl@0
   243
             DepCheck.class, 
sl@0
   244
             DepCheck.name, 
sl@0
   245
             DepCheck.flag 
sl@0
   246
         FROM DepCheck LEFT OUTER JOIN Dependencies ON 
sl@0
   247
             DepCheck.class == Dependencies.class AND 
sl@0
   248
             DepCheck.name == Dependencies.name AND 
sl@0
   249
             DepCheck.flag == Dependencies.flag 
sl@0
   250
         WHERE 
sl@0
   251
             Dependencies.depId is NULL;
sl@0
   252
    };
sl@0
   253
  } {}
sl@0
   254
}
sl@0
   255
sl@0
   256
#--------------------------------------------------------------------
sl@0
   257
# Test that the INSERT works when the SELECT statement (a) references
sl@0
   258
# the table being inserted into and (b) is optimized to use an index
sl@0
   259
# only.
sl@0
   260
do_test insert2-5.1 {
sl@0
   261
  execsql {
sl@0
   262
    CREATE TABLE t2(a, b);
sl@0
   263
    INSERT INTO t2 VALUES(1, 2);
sl@0
   264
    CREATE INDEX t2i1 ON t2(a);
sl@0
   265
    INSERT INTO t2 SELECT a, 3 FROM t2 WHERE a = 1;
sl@0
   266
    SELECT * FROM t2;
sl@0
   267
  }
sl@0
   268
} {1 2 1 3}
sl@0
   269
ifcapable subquery {
sl@0
   270
  do_test insert2-5.2 {
sl@0
   271
    execsql {
sl@0
   272
      INSERT INTO t2 SELECT (SELECT a FROM t2), 4;
sl@0
   273
      SELECT * FROM t2;
sl@0
   274
    }
sl@0
   275
  } {1 2 1 3 1 4}
sl@0
   276
}
sl@0
   277
sl@0
   278
finish_test