summaryrefslogtreecommitdiff
path: root/t/40_multiple_statements.t
blob: 7f98693491be0d78c30a413a6d34ab708a7fb336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl

use strict;
BEGIN {
	$|  = 1;
	$^W = 1;
}

use t::lib::Test qw/connect_ok/;
use Test::More;
use Test::NoWarnings;

plan tests => 21;

{
	# DBD::SQLite prepares/does the first statement only;
	# the following statements will be discarded silently.

	my $dbh = connect_ok( RaiseError => 1 );
	eval { $dbh->do(q/
		create table foo (id integer);
		insert into foo (id) values (1);
		insert into foo (id) values (2);
	/)};
	ok !$@, "do succeeds anyway";
	diag $@ if $@;
	my $got = $dbh->selectall_arrayref('select id from foo');
	ok !@$got, "but got nothing as the inserts were discarded";
}

{
	# As of 1.29_01, you can do bulk inserts with the help of
	# "sqlite_allows_multiple_statements" and
	# "sqlite_unprepared_statements" attributes.
	my $dbh = connect_ok(
		RaiseError => 1,
		sqlite_allow_multiple_statements => 1,
	);
	ok $dbh->{sqlite_allow_multiple_statements}, "allows multiple statements";
	eval { $dbh->do(q/
		create table foo (id integer);
		insert into foo (id) values (1);
		insert into foo (id) values (2);
	/, { sqlite_allow_multiple_statements => 1 })};
	ok !$@, "do succeeds anyway";
	diag $@ if $@;

	my $got = $dbh->selectall_arrayref('select id from foo');
	ok $got->[0][0] == 1
	&& $got->[1][0] == 2, "and got the inserted values";
}

{
	# Do it more explicitly
	my $dbh = connect_ok(
		RaiseError => 1,
		sqlite_allow_multiple_statements => 1,
	);
	ok $dbh->{sqlite_allow_multiple_statements}, "allows multiple statements";
	my $statement = q/
		create table foo (id integer);
		insert into foo (id) values (1);
		insert into foo (id) values (2);
	/;
	$dbh->begin_work;
	eval {
		while ($statement) {
			my $sth = $dbh->prepare($statement);
			$sth->execute;
			$statement = $sth->{sqlite_unprepared_statements};
		}
	};
	ok !$@, "executed multiple statements successfully";
	diag $@ if $@;
	$@ ? $dbh->rollback : $dbh->commit;

	my $got = $dbh->selectall_arrayref('select id from foo');
	ok $got->[0][0] == 1
	&& $got->[1][0] == 2, "and got the inserted values";
}

{
	# Placeholders
	my $dbh = connect_ok(
		RaiseError => 1,
		sqlite_allow_multiple_statements => 1,
	);
	ok $dbh->{sqlite_allow_multiple_statements}, "allows multiple statements";
	eval { $dbh->do(q/
		create table foo (id integer);
		insert into foo (id) values (?);
		insert into foo (id) values (?);
	/, undef, 1, 2)};
	ok !$@, "do succeeds anyway";
	diag $@ if $@;

	my $got = $dbh->selectall_arrayref('select id from foo');
	ok $got->[0][0] == 1
	&& $got->[1][0] == 2, "and got the inserted values";
}

{
	# Do it more explicitly
	my $dbh = connect_ok(
		RaiseError => 1,
		sqlite_allow_multiple_statements => 1,
	);
	ok $dbh->{sqlite_allow_multiple_statements}, "allows multiple statements";
	my $statement = q/
		create table foo (id integer);
		insert into foo (id) values (?);
		insert into foo (id) values (?);
	/;
	$dbh->begin_work;
	eval {
		my @params = (1, 2);
		while ($statement) {
			my $sth = $dbh->prepare($statement);
			$sth->execute(splice @params, 0, $sth->{NUM_OF_PARAMS});
			$statement = $sth->{sqlite_unprepared_statements};
		}
	};
	ok !$@, "executed multiple statements successfully";
	diag $@ if $@;
	$@ ? $dbh->rollback : $dbh->commit;

	ok !$@, "executed multiple statements successfully";
	diag $@ if $@;

	my $got = $dbh->selectall_arrayref('select id from foo');
	ok $got->[0][0] == 1
	&& $got->[1][0] == 2, "and got the inserted values";
}