os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/insert3.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
# 2005 January 13
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 corner cases of the INSERT statement.
sl@0
    13
#
sl@0
    14
# $Id: insert3.test,v 1.7 2007/09/12 17:01:45 danielk1977 Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
# All the tests in this file require trigger support
sl@0
    20
#
sl@0
    21
ifcapable {trigger} {
sl@0
    22
sl@0
    23
# Create a table and a corresponding insert trigger.  Do a self-insert
sl@0
    24
# into the table.
sl@0
    25
#
sl@0
    26
do_test insert3-1.0 {
sl@0
    27
  execsql {
sl@0
    28
    CREATE TABLE t1(a,b);
sl@0
    29
    CREATE TABLE log(x UNIQUE, y);
sl@0
    30
    CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
sl@0
    31
      UPDATE log SET y=y+1 WHERE x=new.a;
sl@0
    32
      INSERT OR IGNORE INTO log VALUES(new.a, 1);
sl@0
    33
    END;
sl@0
    34
    INSERT INTO t1 VALUES('hello','world');
sl@0
    35
    INSERT INTO t1 VALUES(5,10);
sl@0
    36
    SELECT * FROM log ORDER BY x;
sl@0
    37
  }
sl@0
    38
} {5 1 hello 1}
sl@0
    39
do_test insert3-1.1 {
sl@0
    40
  execsql {
sl@0
    41
    INSERT INTO t1 SELECT a, b+10 FROM t1;
sl@0
    42
    SELECT * FROM log ORDER BY x;
sl@0
    43
  }
sl@0
    44
} {5 2 hello 2}
sl@0
    45
do_test insert3-1.2 {
sl@0
    46
  execsql {
sl@0
    47
    CREATE TABLE log2(x PRIMARY KEY,y);
sl@0
    48
    CREATE TRIGGER r2 BEFORE INSERT ON t1 BEGIN
sl@0
    49
      UPDATE log2 SET y=y+1 WHERE x=new.b;
sl@0
    50
      INSERT OR IGNORE INTO log2 VALUES(new.b,1);
sl@0
    51
    END;
sl@0
    52
    INSERT INTO t1 VALUES(453,'hi');
sl@0
    53
    SELECT * FROM log ORDER BY x;
sl@0
    54
  }
sl@0
    55
} {5 2 453 1 hello 2}
sl@0
    56
do_test insert3-1.3 {
sl@0
    57
  execsql {
sl@0
    58
    SELECT * FROM log2 ORDER BY x;
sl@0
    59
  }
sl@0
    60
} {hi 1}
sl@0
    61
ifcapable compound {
sl@0
    62
  do_test insert3-1.4.1 {
sl@0
    63
    execsql {
sl@0
    64
      INSERT INTO t1 SELECT * FROM t1;
sl@0
    65
      SELECT 'a:', x, y FROM log UNION ALL 
sl@0
    66
          SELECT 'b:', x, y FROM log2 ORDER BY x;
sl@0
    67
    }
sl@0
    68
  } {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
sl@0
    69
  do_test insert3-1.4.2 {
sl@0
    70
    execsql {
sl@0
    71
      SELECT 'a:', x, y FROM log UNION ALL 
sl@0
    72
          SELECT 'b:', x, y FROM log2 ORDER BY x, y;
sl@0
    73
    }
sl@0
    74
  } {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
sl@0
    75
  do_test insert3-1.5 {
sl@0
    76
    execsql {
sl@0
    77
      INSERT INTO t1(a) VALUES('xyz');
sl@0
    78
      SELECT * FROM log ORDER BY x;
sl@0
    79
    }
sl@0
    80
  } {5 4 453 2 hello 4 xyz 1}
sl@0
    81
}
sl@0
    82
sl@0
    83
do_test insert3-2.1 {
sl@0
    84
  execsql {
sl@0
    85
    CREATE TABLE t2(
sl@0
    86
      a INTEGER PRIMARY KEY,
sl@0
    87
      b DEFAULT 'b',
sl@0
    88
      c DEFAULT 'c'
sl@0
    89
    );
sl@0
    90
    CREATE TABLE t2dup(a,b,c);
sl@0
    91
    CREATE TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN
sl@0
    92
      INSERT INTO t2dup(a,b,c) VALUES(new.a,new.b,new.c);
sl@0
    93
    END;
sl@0
    94
    INSERT INTO t2(a) VALUES(123);
sl@0
    95
    INSERT INTO t2(b) VALUES(234);
sl@0
    96
    INSERT INTO t2(c) VALUES(345);
sl@0
    97
    SELECT * FROM t2dup;
sl@0
    98
  }
sl@0
    99
} {123 b c -1 234 c -1 b 345}
sl@0
   100
do_test insert3-2.2 {
sl@0
   101
  execsql {
sl@0
   102
    DELETE FROM t2dup;
sl@0
   103
    INSERT INTO t2(a) SELECT 1 FROM t1 LIMIT 1;
sl@0
   104
    INSERT INTO t2(b) SELECT 987 FROM t1 LIMIT 1;
sl@0
   105
    INSERT INTO t2(c) SELECT 876 FROM t1 LIMIT 1;
sl@0
   106
    SELECT * FROM t2dup;
sl@0
   107
  }
sl@0
   108
} {1 b c -1 987 c -1 b 876}
sl@0
   109
sl@0
   110
# Test for proper detection of malformed WHEN clauses on INSERT triggers.
sl@0
   111
#
sl@0
   112
do_test insert3-3.1 {
sl@0
   113
  execsql {
sl@0
   114
    CREATE TABLE t3(a,b,c);
sl@0
   115
    CREATE TRIGGER t3r1 BEFORE INSERT on t3 WHEN nosuchcol BEGIN
sl@0
   116
      SELECT 'illegal WHEN clause';
sl@0
   117
    END;
sl@0
   118
  }
sl@0
   119
} {}
sl@0
   120
do_test insert3-3.2 {
sl@0
   121
  catchsql {
sl@0
   122
    INSERT INTO t3 VALUES(1,2,3)
sl@0
   123
  }
sl@0
   124
} {1 {no such column: nosuchcol}}
sl@0
   125
do_test insert3-3.3 {
sl@0
   126
  execsql {
sl@0
   127
    CREATE TABLE t4(a,b,c);
sl@0
   128
    CREATE TRIGGER t4r1 AFTER INSERT on t4 WHEN nosuchcol BEGIN
sl@0
   129
      SELECT 'illegal WHEN clause';
sl@0
   130
    END;
sl@0
   131
  }
sl@0
   132
} {}
sl@0
   133
do_test insert3-3.4 {
sl@0
   134
  catchsql {
sl@0
   135
    INSERT INTO t4 VALUES(1,2,3)
sl@0
   136
  }
sl@0
   137
} {1 {no such column: nosuchcol}}
sl@0
   138
sl@0
   139
} ;# ifcapable {trigger}
sl@0
   140
sl@0
   141
# Tests for the INSERT INTO ... DEFAULT VALUES construct
sl@0
   142
#
sl@0
   143
do_test insert3-3.5 {
sl@0
   144
  execsql {
sl@0
   145
    CREATE TABLE t5(
sl@0
   146
      a INTEGER PRIMARY KEY,
sl@0
   147
      b DEFAULT 'xyz'
sl@0
   148
    );
sl@0
   149
    INSERT INTO t5 DEFAULT VALUES;
sl@0
   150
    SELECT * FROM t5;
sl@0
   151
  }
sl@0
   152
} {1 xyz}
sl@0
   153
do_test insert3-3.6 {
sl@0
   154
  execsql {
sl@0
   155
    INSERT INTO t5 DEFAULT VALUES;
sl@0
   156
    SELECT * FROM t5;
sl@0
   157
  }
sl@0
   158
} {1 xyz 2 xyz}
sl@0
   159
sl@0
   160
ifcapable bloblit {
sl@0
   161
  do_test insert3-3.7 {
sl@0
   162
    execsql {
sl@0
   163
      CREATE TABLE t6(x,y DEFAULT 4.3, z DEFAULT x'6869');
sl@0
   164
      INSERT INTO t6 DEFAULT VALUES;
sl@0
   165
      SELECT * FROM t6;
sl@0
   166
    }
sl@0
   167
  } {{} 4.3 hi}
sl@0
   168
}
sl@0
   169
db close
sl@0
   170
sl@0
   171
finish_test