os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/trigger4.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
# The author disclaims copyright to this source code.  In place of
sl@0
     2
# a legal notice, here is a blessing:
sl@0
     3
#
sl@0
     4
#    May you do good and not evil.
sl@0
     5
#    May you find forgiveness for yourself and forgive others.
sl@0
     6
#    May you share freely, never taking more than you give.
sl@0
     7
#
sl@0
     8
#***********************************************************************
sl@0
     9
#
sl@0
    10
# This file tests the triggers of views.
sl@0
    11
#
sl@0
    12
sl@0
    13
set testdir [file dirname $argv0]
sl@0
    14
source $testdir/tester.tcl
sl@0
    15
sl@0
    16
# If either views or triggers are disabled in this build, omit this file.
sl@0
    17
ifcapable {!trigger || !view} {
sl@0
    18
  finish_test
sl@0
    19
  return
sl@0
    20
}
sl@0
    21
sl@0
    22
do_test trigger4-1.1 {
sl@0
    23
  execsql {
sl@0
    24
    create table test1(id integer primary key,a);
sl@0
    25
    create table test2(id integer,b);
sl@0
    26
    create view test as
sl@0
    27
      select test1.id as id,a as a,b as b
sl@0
    28
      from test1 join test2 on test2.id =  test1.id;
sl@0
    29
    create trigger I_test instead of insert on test
sl@0
    30
      begin
sl@0
    31
        insert into test1 (id,a) values (NEW.id,NEW.a);
sl@0
    32
        insert into test2 (id,b) values (NEW.id,NEW.b);
sl@0
    33
      end;
sl@0
    34
    insert into test values(1,2,3);
sl@0
    35
    select * from test1;
sl@0
    36
  }
sl@0
    37
} {1 2}
sl@0
    38
do_test trigger4-1.2 {
sl@0
    39
  execsql {
sl@0
    40
    select * from test2;
sl@0
    41
  }
sl@0
    42
} {1 3}
sl@0
    43
do_test trigger4-1.3 {
sl@0
    44
  db close
sl@0
    45
  sqlite3 db test.db
sl@0
    46
  execsql {
sl@0
    47
    insert into test values(4,5,6);
sl@0
    48
    select * from test1;
sl@0
    49
  }
sl@0
    50
} {1 2 4 5}
sl@0
    51
do_test trigger4-1.4 {
sl@0
    52
  execsql {
sl@0
    53
    select * from test2;
sl@0
    54
  }
sl@0
    55
} {1 3 4 6}
sl@0
    56
sl@0
    57
do_test trigger4-2.1 {
sl@0
    58
  execsql {
sl@0
    59
    create trigger U_test instead of update on test
sl@0
    60
      begin
sl@0
    61
        update test1 set a=NEW.a where id=NEW.id;
sl@0
    62
        update test2 set b=NEW.b where id=NEW.id;
sl@0
    63
      end;
sl@0
    64
    update test set a=22 where id=1;
sl@0
    65
    select * from test1;
sl@0
    66
  }
sl@0
    67
} {1 22 4 5}
sl@0
    68
do_test trigger4-2.2 {
sl@0
    69
  execsql {
sl@0
    70
    select * from test2;
sl@0
    71
  }
sl@0
    72
} {1 3 4 6}
sl@0
    73
do_test trigger4-2.3 {
sl@0
    74
  db close
sl@0
    75
  sqlite3 db test.db
sl@0
    76
  execsql {
sl@0
    77
    update test set b=66 where id=4;
sl@0
    78
    select * from test1;
sl@0
    79
  }
sl@0
    80
} {1 22 4 5}
sl@0
    81
do_test trigger4-2.4 {
sl@0
    82
  execsql {
sl@0
    83
    select * from test2;
sl@0
    84
  }
sl@0
    85
} {1 3 4 66}
sl@0
    86
sl@0
    87
do_test trigger4-3.1 {
sl@0
    88
  catchsql {
sl@0
    89
    drop table test2;
sl@0
    90
    insert into test values(7,8,9);
sl@0
    91
  }
sl@0
    92
} {1 {no such table: main.test2}}
sl@0
    93
do_test trigger4-3.2 {
sl@0
    94
  db close
sl@0
    95
  sqlite3 db test.db
sl@0
    96
  catchsql {
sl@0
    97
    insert into test values(7,8,9);
sl@0
    98
  }
sl@0
    99
} {1 {no such table: main.test2}}
sl@0
   100
do_test trigger4-3.3 {
sl@0
   101
  catchsql {
sl@0
   102
    update test set a=222 where id=1;
sl@0
   103
  }
sl@0
   104
} {1 {no such table: main.test2}}
sl@0
   105
do_test trigger4-3.4 {
sl@0
   106
  execsql {
sl@0
   107
    select * from test1;
sl@0
   108
  }
sl@0
   109
} {1 22 4 5}
sl@0
   110
do_test trigger4-3.5 {
sl@0
   111
  execsql {
sl@0
   112
    create table test2(id,b);
sl@0
   113
    insert into test values(7,8,9);
sl@0
   114
    select * from test1;
sl@0
   115
  }
sl@0
   116
} {1 22 4 5 7 8}
sl@0
   117
do_test trigger4-3.6 {
sl@0
   118
  execsql {
sl@0
   119
    select * from test2;
sl@0
   120
  }
sl@0
   121
} {7 9}
sl@0
   122
do_test trigger4-3.7 {
sl@0
   123
  db close
sl@0
   124
  sqlite3 db test.db
sl@0
   125
  execsql {
sl@0
   126
    update test set b=99 where id=7;
sl@0
   127
    select * from test2;
sl@0
   128
  }
sl@0
   129
} {7 99}
sl@0
   130
sl@0
   131
do_test trigger4-4.1 {
sl@0
   132
    db close
sl@0
   133
    file delete -force trigtest.db
sl@0
   134
    file delete -force trigtest.db-journal
sl@0
   135
    sqlite3 db trigtest.db
sl@0
   136
    catchsql {drop table tbl; drop view vw}
sl@0
   137
    execsql {
sl@0
   138
	create table tbl(a integer primary key, b integer);
sl@0
   139
	create view vw as select * from tbl;
sl@0
   140
	create trigger t_del_tbl instead of delete on vw for each row begin
sl@0
   141
	  delete from tbl where a = old.a;
sl@0
   142
	end;
sl@0
   143
	create trigger t_upd_tbl instead of update on vw for each row begin
sl@0
   144
	  update tbl set a=new.a, b=new.b where a = old.a;
sl@0
   145
	end;
sl@0
   146
	create trigger t_ins_tbl instead of insert on vw for each row begin
sl@0
   147
	  insert into tbl values (new.a,new.b);
sl@0
   148
	end;
sl@0
   149
	insert into tbl values(101,1001);
sl@0
   150
	insert into tbl values(102,1002);
sl@0
   151
	insert into tbl select a+2, b+2 from tbl;
sl@0
   152
	insert into tbl select a+4, b+4 from tbl;
sl@0
   153
	insert into tbl select a+8, b+8 from tbl;
sl@0
   154
	insert into tbl select a+16, b+16 from tbl;
sl@0
   155
	insert into tbl select a+32, b+32 from tbl;
sl@0
   156
	insert into tbl select a+64, b+64 from tbl;
sl@0
   157
	select count(*) from vw;
sl@0
   158
    }
sl@0
   159
} {128}
sl@0
   160
do_test trigger4-4.2 {
sl@0
   161
    execsql {select a, b from vw where a<103 or a>226 order by a}
sl@0
   162
} {101 1001 102 1002 227 1127 228 1128}
sl@0
   163
sl@0
   164
#test delete from view
sl@0
   165
do_test trigger4-5.1 {
sl@0
   166
    catchsql {delete from vw where a>101 and a<2000}
sl@0
   167
} {0 {}}
sl@0
   168
do_test trigger4-5.2 {
sl@0
   169
    execsql {select * from vw}
sl@0
   170
} {101 1001}
sl@0
   171
sl@0
   172
#test insert into view
sl@0
   173
do_test trigger4-6.1 {
sl@0
   174
    catchsql {
sl@0
   175
	insert into vw values(102,1002);
sl@0
   176
	insert into vw select a+2, b+2 from vw;
sl@0
   177
	insert into vw select a+4, b+4 from vw;
sl@0
   178
	insert into vw select a+8, b+8 from vw;
sl@0
   179
	insert into vw select a+16, b+16 from vw;
sl@0
   180
	insert into vw select a+32, b+32 from vw;
sl@0
   181
	insert into vw select a+64, b+64 from vw;
sl@0
   182
    }
sl@0
   183
} {0 {}}
sl@0
   184
do_test trigger4-6.2 {
sl@0
   185
    execsql {select count(*) from vw}
sl@0
   186
} {128}
sl@0
   187
sl@0
   188
#test update of view
sl@0
   189
do_test trigger4-7.1 {
sl@0
   190
    catchsql {update vw set b=b+1000 where a>101 and a<2000}
sl@0
   191
} {0 {}}
sl@0
   192
do_test trigger4-7.2 {
sl@0
   193
    execsql {select a, b from vw where a<=102 or a>=227 order by a}
sl@0
   194
} {101 1001 102 2002 227 2127 228 2128}
sl@0
   195
sl@0
   196
integrity_check trigger4-99.9
sl@0
   197
db close
sl@0
   198
file delete -force trigtest.db trigtest.db-journal
sl@0
   199
sl@0
   200
finish_test