DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
735 lines | 28.515kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
use Test::More;
2
use strict;
3
use warnings;
4

            
5
BEGIN {
6
    eval { require DBD::SQLite; 1 }
7
        or plan skip_all => 'DBD::SQLite required';
8
    eval { DBD::SQLite->VERSION >= 1.25 }
9
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
10

            
11
    plan 'no_plan';
12
    use_ok('DBIx::Custom');
13
}
14

            
15
# Function for test name
16
my $test;
17
sub test {
18
    $test = shift;
19
}
20

            
21
# Constant varialbes for test
22
my $CREATE_TABLE = {
23
    0 => 'create table table1 (key1 char(255), key2 char(255));',
24
    1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));',
25
    2 => 'create table table2 (key1 char(255), key3 char(255));'
26
};
27

            
28
my $SELECT_TMPL = {
29
    0 => 'select * from table1;'
30
};
31

            
32
my $DROP_TABLE = {
33
    0 => 'drop table table1'
34
};
35

            
36
my $NEW_ARGS = {
37
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
38
};
39

            
40
# Variables for test
41
my $dbi;
42
my $sth;
43
my $tmpl;
44
my @tmpls;
45
my $select_tmpl;
46
my $insert_tmpl;
47
my $update_tmpl;
48
my $params;
49
my $sql;
50
my $result;
51
my @rows;
52
my $rows;
53
my $query;
54
my @queries;
55
my $select_query;
56
my $insert_query;
57
my $update_query;
58
my $ret_val;
59

            
60

            
61
test 'disconnect';
62
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
63
$dbi->connect;
64
$dbi->disconnect;
65
ok(!$dbi->dbh, $test);
66

            
67

            
68
test 'connected';
69
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
70
ok(!$dbi->connected, "$test : not connected");
71
$dbi->connect;
72
ok($dbi->connected, "$test : connected");
73

            
74

            
75
test 'preapare';
76
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
77
$sth = $dbi->prepare($CREATE_TABLE->{0});
78
ok($sth, "$test : auto connect");
79
$sth->execute;
80
$sth = $dbi->prepare($DROP_TABLE->{0});
81
ok($sth, "$test : basic");
82

            
83

            
84
test 'do';
85
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
86
$ret_val = $dbi->do($CREATE_TABLE->{0});
87
ok(defined $ret_val, "$test : auto connect");
88
$ret_val = $dbi->do($DROP_TABLE->{0});
89
ok(defined $ret_val, "$test : basic");
90

            
version 0.0901
yuki-kimoto authored on 2009-12-17
91
test 'create_table';
92
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
93
$ret_val = $dbi->create_table(
94
                   'table1',
95
                   'key1 char(255)',
96
                   'key2 char(255)'
97
                 );
98
ok(defined $ret_val, "$test : create_table");
99

            
cleanup
yuki-kimoto authored on 2010-02-11
100
$dbi->insert('table1', {key1 => 1, key2 => 2});
version 0.0901
yuki-kimoto authored on 2009-12-17
101
ok(!$@, "$test : table exist");
102

            
103
$ret_val = $dbi->drop_table('table1');
104
ok(defined $ret_val, "$test : drop table");
105

            
106
eval{$dbi->select('table1')};
107
ok($@, "$test : table not exist");
packaging one directory
yuki-kimoto authored on 2009-11-16
108

            
109
# Prepare table
110
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
111
$dbi->connect;
112
$dbi->do($CREATE_TABLE->{0});
113
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
114
$sth->execute(1, 2);
115
$sth->execute(3, 4);
116

            
117

            
118
test 'DBIx::Custom::Result test';
119
$tmpl = "select key1, key2 from table1";
120
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
121
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
122

            
123
@rows = ();
124
while (my $row = $result->fetch) {
125
    push @rows, [@$row];
126
}
127
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
128

            
version 0.0901
yuki-kimoto authored on 2009-12-17
129
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
130
@rows = ();
131
while (my @row = $result->fetch) {
132
    push @rows, [@row];
133
}
134
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
135

            
version 0.0901
yuki-kimoto authored on 2009-12-17
136
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
137
@rows = ();
138
while (my $row = $result->fetch_hash) {
139
    push @rows, {%$row};
140
}
141
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
142

            
version 0.0901
yuki-kimoto authored on 2009-12-17
143
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
144
@rows = ();
145
while (my %row = $result->fetch_hash) {
146
    push @rows, {%row};
147
}
148
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
149

            
version 0.0901
yuki-kimoto authored on 2009-12-17
150
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
151
$rows = $result->fetch_all;
152
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
153

            
version 0.0901
yuki-kimoto authored on 2009-12-17
154
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
155
@rows = $result->fetch_all;
156
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
157

            
version 0.0901
yuki-kimoto authored on 2009-12-17
158
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
159
@rows = $result->fetch_hash_all;
160
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context");
161

            
version 0.0901
yuki-kimoto authored on 2009-12-17
162
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
163
@rows = $result->fetch_all;
164
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context");
165

            
166

            
167
test 'Insert query return value';
168
$dbi->do($DROP_TABLE->{0});
169
$dbi->do($CREATE_TABLE->{0});
170
$tmpl = "insert into table1 {insert key1 key2}";
171
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
172
$ret_val = $dbi->query($query, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
173
ok($ret_val, $test);
174

            
175

            
version 0.0901
yuki-kimoto authored on 2009-12-17
176
test 'Direct query';
packaging one directory
yuki-kimoto authored on 2009-11-16
177
$dbi->do($DROP_TABLE->{0});
178
$dbi->do($CREATE_TABLE->{0});
179
$insert_tmpl = "insert into table1 {insert key1 key2}";
version 0.0901
yuki-kimoto authored on 2009-12-17
180
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}, sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
181
    my $query = shift;
182
    $query->bind_filter(sub {
Simplify key search
yuki-kimoto authored on 2010-02-11
183
        my ($value, $table, $column) = @_;
184
        if ($column eq 'key2') {
packaging one directory
yuki-kimoto authored on 2009-11-16
185
            return $value + 1;
186
        }
187
        return $value;
188
    });
189
});
Simplify key search
yuki-kimoto authored on 2010-02-11
190
$result = $dbi->query(['table1', $SELECT_TMPL->{0}]);
packaging one directory
yuki-kimoto authored on 2009-11-16
191
$rows = $result->fetch_hash_all;
192
is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
193

            
Simplify key search
yuki-kimoto authored on 2010-02-11
194
__END__
packaging one directory
yuki-kimoto authored on 2009-11-16
195

            
196
test 'Filter basic';
197
$dbi->do($DROP_TABLE->{0});
198
$dbi->do($CREATE_TABLE->{0});
199

            
200
$insert_tmpl  = "insert into table1 {insert key1 key2};";
201
$insert_query = $dbi->create_query($insert_tmpl);
202
$insert_query->bind_filter(sub {
Simplify key search
yuki-kimoto authored on 2010-02-11
203
    my ($value, $table, $column) = @_;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
204
    
Simplify key search
yuki-kimoto authored on 2010-02-11
205
    if ($table eq '' && $column eq 'key1')
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
206
    {
packaging one directory
yuki-kimoto authored on 2009-11-16
207
        return $value * 2;
208
    }
209
    return $value;
210
});
version 0.0901
yuki-kimoto authored on 2009-12-17
211
$dbi->query($insert_query, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
212
$select_query = $dbi->create_query($SELECT_TMPL->{0});
213
$select_query->fetch_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
214
    my ($value, $key, $dbi, $infos) = @_;
215
    my ($type, $sth, $i) = @{$infos}{qw/type sth index/};
216
    
217
    if ($key eq 'key2' && $type =~ /char/ && ref $sth eq 'DBI::st' 
218
        && $i == 1 && $dbi->isa('DBIx::Custom'))
219
    {
packaging one directory
yuki-kimoto authored on 2009-11-16
220
        return $value * 3;
221
    }
222
    return $value;
223
});
version 0.0901
yuki-kimoto authored on 2009-12-17
224
$result = $dbi->query($select_query);
packaging one directory
yuki-kimoto authored on 2009-11-16
225
$rows = $result->fetch_hash_all;
226
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
227

            
228
$dbi->do("delete from table1;");
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
229
$insert_query->no_bind_filters(['key1']);
230
$select_query->no_fetch_filters(['key2']);
version 0.0901
yuki-kimoto authored on 2009-12-17
231
$dbi->query($insert_query, {key1 => 1, key2 => 2});
232
$result = $dbi->query($select_query);
packaging one directory
yuki-kimoto authored on 2009-11-16
233
$rows = $result->fetch_hash_all;
234
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
235

            
236
$dbi->do($DROP_TABLE->{0});
237
$dbi->do($CREATE_TABLE->{0});
238
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
239
$insert_query = $dbi->create_query($insert_tmpl);
240
$insert_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
241
    my ($value, $key, $dbi, $infos) = @_;
242
    my ($table, $column) = @{$infos}{qw/table column/};
243
    
packaging one directory
yuki-kimoto authored on 2009-11-16
244
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
245
        return $value * 3;
246
    }
247
    return $value;
248
});
version 0.0901
yuki-kimoto authored on 2009-12-17
249
$dbi->query($insert_query, {table1 => {key1 => 1, key2 => 2}});
packaging one directory
yuki-kimoto authored on 2009-11-16
250
$select_query = $dbi->create_query($SELECT_TMPL->{0});
version 0.0901
yuki-kimoto authored on 2009-12-17
251
$result       = $dbi->query($select_query);
packaging one directory
yuki-kimoto authored on 2009-11-16
252
$rows = $result->fetch_hash_all;
253
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
254

            
255
test 'Filter in';
256
$insert_tmpl  = "insert into table1 {insert key1 key2};";
257
$insert_query = $dbi->create_query($insert_tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
258
$dbi->query($insert_query, {key1 => 2, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
259
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
260
$select_query = $dbi->create_query($select_tmpl);
261
$select_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
262
    my ($value, $key, $dbi, $infos) = @_;
263
    my ($table, $column) = @{$infos}{qw/table column/};
264
    
packaging one directory
yuki-kimoto authored on 2009-11-16
265
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
266
        return $value * 2;
267
    }
268
    return $value;
269
});
version 0.0901
yuki-kimoto authored on 2009-12-17
270
$result = $dbi->query($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}});
packaging one directory
yuki-kimoto authored on 2009-11-16
271
$rows = $result->fetch_hash_all;
272
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter");
273

            
274

            
275
test 'DBIx::Custom::SQL::Template basic tag';
276
$dbi->do($DROP_TABLE->{0});
277
$dbi->do($CREATE_TABLE->{1});
278
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
279
$sth->execute(1, 2, 3, 4, 5);
280
$sth->execute(6, 7, 8, 9, 10);
281

            
282
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
283
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
284
$result = $dbi->query($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
285
$rows = $result->fetch_hash_all;
286
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
287

            
288
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
289
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
290
$result = $dbi->query($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
packaging one directory
yuki-kimoto authored on 2009-11-16
291
$rows = $result->fetch_hash_all;
292
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
293

            
294
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
295
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
296
$result = $dbi->query($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
297
$rows = $result->fetch_hash_all;
298
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot");
299

            
300
$tmpl = "select * from table1 where {<= key1} and {like key2};";
301
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
302
$result = $dbi->query($query, {key1 => 1, key2 => '%2%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
303
$rows = $result->fetch_hash_all;
304
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
305

            
306
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
307
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
308
$result = $dbi->query($query, {table1 => {key1 => 1, key2 => '%2%'}});
packaging one directory
yuki-kimoto authored on 2009-11-16
309
$rows = $result->fetch_hash_all;
310
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table");
311

            
312
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
313
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
314
$result = $dbi->query($query, {'table1.key1' => 1, 'table1.key2' => '%2%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
315
$rows = $result->fetch_hash_all;
316
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot");
317

            
318

            
319
test 'DIB::Custom::SQL::Template in tag';
320
$dbi->do($DROP_TABLE->{0});
321
$dbi->do($CREATE_TABLE->{1});
322
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
323
$sth->execute(1, 2, 3, 4, 5);
324
$sth->execute(6, 7, 8, 9, 10);
325

            
326
$tmpl = "select * from table1 where {in key1 2};";
327
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
328
$result = $dbi->query($query, {key1 => [9, 1]});
packaging one directory
yuki-kimoto authored on 2009-11-16
329
$rows = $result->fetch_hash_all;
330
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
331

            
332
$tmpl = "select * from table1 where {in table1.key1 2};";
333
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
334
$result = $dbi->query($query, {table1 => {key1 => [9, 1]}});
packaging one directory
yuki-kimoto authored on 2009-11-16
335
$rows = $result->fetch_hash_all;
336
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
337

            
338
$tmpl = "select * from table1 where {in table1.key1 2};";
339
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
340
$result = $dbi->query($query, {'table1.key1' => [9, 1]});
packaging one directory
yuki-kimoto authored on 2009-11-16
341
$rows = $result->fetch_hash_all;
342
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot");
343

            
344

            
345
test 'DBIx::Custom::SQL::Template insert tag';
346
$dbi->do("delete from table1");
347
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
348
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
349

            
version 0.0901
yuki-kimoto authored on 2009-12-17
350
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
351
$rows = $result->fetch_hash_all;
352
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
353

            
354
$dbi->do("delete from table1");
355
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
356
$dbi->query($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
357
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
358
$rows = $result->fetch_hash_all;
359
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
360

            
361
$dbi->do("delete from table1");
362
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
363
$dbi->query($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
364
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
365
$rows = $result->fetch_hash_all;
366
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot");
367

            
368

            
369
test 'DBIx::Custom::SQL::Template update tag';
370
$dbi->do("delete from table1");
371
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
version 0.0901
yuki-kimoto authored on 2009-12-17
372
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
373
$dbi->query($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
packaging one directory
yuki-kimoto authored on 2009-11-16
374

            
375
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
376
$dbi->query($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
377

            
version 0.0901
yuki-kimoto authored on 2009-12-17
378
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
379
$rows = $result->fetch_hash_all;
380
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
381
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
382

            
383
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
384
$dbi->query($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
385
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
386
$rows = $result->fetch_hash_all;
387
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
388
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
389

            
390
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
391
$dbi->query($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5});
392
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
393
$rows = $result->fetch_hash_all;
394
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5},
395
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot");
396

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
397
test 'transaction';
packaging one directory
yuki-kimoto authored on 2009-11-16
398
$dbi->do($DROP_TABLE->{0});
399
$dbi->do($CREATE_TABLE->{0});
remove run_transaction().
yuki-kimoto authored on 2010-01-30
400
$dbi->transaction->run(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
401
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
version 0.0901
yuki-kimoto authored on 2009-12-17
402
    $dbi->query($insert_tmpl, {key1 => 1, key2 => 2});
403
    $dbi->query($insert_tmpl, {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
404
});
version 0.0901
yuki-kimoto authored on 2009-12-17
405
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
406
$rows   = $result->fetch_hash_all;
407
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
408

            
409
$dbi->do($DROP_TABLE->{0});
410
$dbi->do($CREATE_TABLE->{0});
411
$dbi->dbh->{RaiseError} = 0;
412
eval{
remove run_transaction().
yuki-kimoto authored on 2010-01-30
413
    $dbi->transaction->run(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
414
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
version 0.0901
yuki-kimoto authored on 2009-12-17
415
        $dbi->query($insert_tmpl, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
416
        die "Fatal Error";
version 0.0901
yuki-kimoto authored on 2009-12-17
417
        $dbi->query($insert_tmpl, {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
418
    })
419
};
420
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
421
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
version 0.0901
yuki-kimoto authored on 2009-12-17
422
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
423
$rows   = $result->fetch_hash_all;
424
is_deeply($rows, [], "$test : rollback");
425

            
426

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
427

            
packaging one directory
yuki-kimoto authored on 2009-11-16
428
test 'Error case';
429
$dbi = DBIx::Custom->new;
remove run_transaction().
yuki-kimoto authored on 2010-01-30
430
eval{$dbi->transaction->run};
packaging one directory
yuki-kimoto authored on 2009-11-16
431
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
432

            
433
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit');
434
eval{$dbi->connect;};
435
ok($@, "$test : connect error");
436

            
437
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
438
$dbi->connect;
439
$dbi->dbh->{AutoCommit} = 0;
remove run_transaction().
yuki-kimoto authored on 2010-01-30
440
eval{$dbi->transaction->run};
packaging one directory
yuki-kimoto authored on 2009-11-16
441
like($@, qr/AutoCommit must be true before transaction start/,
remove run_transaction().
yuki-kimoto authored on 2010-01-30
442
         "$test : transaction auto commit is false");
packaging one directory
yuki-kimoto authored on 2009-11-16
443

            
444
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
445
$sql = 'laksjdf';
446
eval{$dbi->prepare($sql)};
447
like($@, qr/$sql/, "$test : prepare fail");
448

            
449
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
450
$sql = 'laksjdf';
451
eval{$dbi->do($sql, qw/1 2 3/)};
452
like($@, qr/$sql/, "$test : do fail");
453

            
454
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
455
eval{$dbi->create_query("{p }")};
456
ok($@, "$test : create_query invalid SQL template");
457

            
458
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
459
$dbi->do($CREATE_TABLE->{0});
460
$query = $dbi->create_query("select * from table1 where {= key1}");
version 0.0901
yuki-kimoto authored on 2009-12-17
461
eval{$dbi->query($query, {key2 => 1})};
packaging one directory
yuki-kimoto authored on 2009-11-16
462
like($@, qr/Corresponding key is not found in your parameters/, 
463
        "$test : execute corresponding key not found");
464

            
465

            
466
test 'insert';
467
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
468
$dbi->do($CREATE_TABLE->{0});
469
$dbi->insert('table1', {key1 => 1, key2 => 2});
470
$dbi->insert('table1', {key1 => 3, key2 => 4});
version 0.0901
yuki-kimoto authored on 2009-12-17
471
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
472
$rows   = $result->fetch_hash_all;
473
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
474

            
475
$dbi->do('delete from table1');
476
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub {
477
    my $query = shift;
478
    $query->bind_filter(sub {
479
        my ($value, $key) = @_;
480
        if ($key eq 'key1') {
481
            return $value * 3;
482
        }
483
        return $value;
484
    });
485
});
version 0.0901
yuki-kimoto authored on 2009-12-17
486
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
487
$rows   = $result->fetch_hash_all;
488
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
489

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
490
$dbi->insert('table1', {key1 => 1, key2 => 2}, '   ', sub {
491
    my $query = shift;
492
    like($query->sql, qr/insert into table1 \(.+\) values \(\?, \?\)    ;/, 
493
        "$test: append statement");
494
});
packaging one directory
yuki-kimoto authored on 2009-11-16
495

            
496
test 'insert error';
497
eval{$dbi->insert('table1')};
498
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
499

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
500
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, '', 'aaa')};
packaging one directory
yuki-kimoto authored on 2009-11-16
501
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
502

            
503

            
504
test 'update';
505
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
506
$dbi->do($CREATE_TABLE->{1});
507
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
508
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
509
$dbi->update('table1', {key2 => 11}, {key1 => 1});
version 0.0901
yuki-kimoto authored on 2009-12-17
510
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
511
$rows   = $result->fetch_hash_all;
512
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
513
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
514
                  "$test : basic");
515
                  
add DBIx::Custom::Column
yuki-kimoto authored on 2010-02-11
516
#$dbi->do("delete from table1");
517
#$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
518
#$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
519
#$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
520
#$result = $dbi->query($SELECT_TMPL->{0});
521
#$rows   = $result->fetch_hash_all;
522
#is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
523
#                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
524
#                  "$test : update key same as search key");
packaging one directory
yuki-kimoto authored on 2009-11-16
525

            
526
$dbi->do("delete from table1");
527
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
528
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
529
$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
530
    my $query = shift;
531
    $query->bind_filter(sub {
532
        my ($value, $key) = @_;
533
        if ($key eq 'key2') {
534
            return $value * 2;
535
        }
536
        return $value;
537
    });
538
});
version 0.0901
yuki-kimoto authored on 2009-12-17
539
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
540
$rows   = $result->fetch_hash_all;
541
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
542
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
543
                  "$test : query edit callback");
544

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
545
$dbi->update('table1', {key2 => 11}, {key1 => 1}, '   ', sub {
546
    my $query = shift;
547
    is($query->sql, 'update table1 set key2 = ? where key1 = ?    ;',
548
       "$test: append statement");
549
});
550

            
packaging one directory
yuki-kimoto authored on 2009-11-16
551

            
552
test 'update error';
553
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
554
$dbi->do($CREATE_TABLE->{1});
555
eval{$dbi->update('table1')};
556
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/,
557
         "$test : update key-value pairs not specified");
558

            
559
eval{$dbi->update('table1', {key2 => 1})};
560
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/,
561
         "$test : where key-value pairs not specified");
562

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
563
eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, '', 'aaa')};
packaging one directory
yuki-kimoto authored on 2009-11-16
564
like($@, qr/Query edit callback must be code reference/, 
565
         "$test : query edit callback not code reference");
566

            
567

            
568
test 'update_all';
569
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
570
$dbi->do($CREATE_TABLE->{1});
571
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
572
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
573
$dbi->update_all('table1', {key2 => 10}, sub {
574
    my $query = shift;
575
    $query->bind_filter(sub {
576
        my ($value, $key) = @_;
577
        return $value * 2;
578
    })
579
});
version 0.0901
yuki-kimoto authored on 2009-12-17
580
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
581
$rows   = $result->fetch_hash_all;
582
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
583
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
584
                  "$test : query edit callback");
585

            
586

            
587
test 'delete';
588
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
589
$dbi->do($CREATE_TABLE->{0});
590
$dbi->insert('table1', {key1 => 1, key2 => 2});
591
$dbi->insert('table1', {key1 => 3, key2 => 4});
592
$dbi->delete('table1', {key1 => 1});
version 0.0901
yuki-kimoto authored on 2009-12-17
593
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
594
$rows   = $result->fetch_hash_all;
595
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
596

            
597
$dbi->do("delete from table1;");
598
$dbi->insert('table1', {key1 => 1, key2 => 2});
599
$dbi->insert('table1', {key1 => 3, key2 => 4});
600
$dbi->delete('table1', {key2 => 1}, sub {
601
    my $query = shift;
602
    $query->bind_filter(sub {
603
        my ($value, $key) = @_;
604
        return $value * 2;
605
    });
606
});
version 0.0901
yuki-kimoto authored on 2009-12-17
607
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
608
$rows   = $result->fetch_hash_all;
609
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
610

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
611
$dbi->delete('table1', {key1 => 1}, '   ', sub {
612
    my $query = shift;
613
    is($query->sql, 'delete from table1 where key1 = ?    ;',
614
       "$test: append statement");
615
});
616

            
617

            
packaging one directory
yuki-kimoto authored on 2009-11-16
618
$dbi->delete_all('table1');
619
$dbi->insert('table1', {key1 => 1, key2 => 2});
620
$dbi->insert('table1', {key1 => 3, key2 => 4});
621
$dbi->delete('table1', {key1 => 1, key2 => 2});
622
$rows = $dbi->select('table1')->fetch_hash_all;
623
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
624

            
625

            
626
test 'delete error';
627
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
628
$dbi->do($CREATE_TABLE->{0});
629
eval{$dbi->delete('table1')};
630
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
631
         "$test : where key-value pairs not specified");
632

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
633
eval{$dbi->delete('table1', {key1 => 1}, '', 'aaa')};
packaging one directory
yuki-kimoto authored on 2009-11-16
634
like($@, qr/Query edit callback must be code reference/, 
635
         "$test : query edit callback not code ref");
636

            
637

            
638
test 'delete_all';
639
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
640
$dbi->do($CREATE_TABLE->{0});
641
$dbi->insert('table1', {key1 => 1, key2 => 2});
642
$dbi->insert('table1', {key1 => 3, key2 => 4});
643
$dbi->delete_all('table1');
version 0.0901
yuki-kimoto authored on 2009-12-17
644
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
645
$rows   = $result->fetch_hash_all;
646
is_deeply($rows, [], "$test : basic");
647

            
648

            
649
test 'select';
650
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
651
$dbi->do($CREATE_TABLE->{0});
652
$dbi->insert('table1', {key1 => 1, key2 => 2});
653
$dbi->insert('table1', {key1 => 3, key2 => 4});
654
$rows = $dbi->select('table1')->fetch_hash_all;
655
is_deeply($rows, [{key1 => 1, key2 => 2},
656
                  {key1 => 3, key2 => 4}], "$test : table");
657

            
658
$rows = $dbi->select('table1', ['key1'])->fetch_hash_all;
659
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
660

            
661
$rows = $dbi->select('table1', {key1 => 1})->fetch_hash_all;
662
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
663

            
664
$rows = $dbi->select('table1', ['key1'], {key1 => 3})->fetch_hash_all;
665
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
666

            
667
$rows = $dbi->select('table1', "order by key1 desc limit 1")->fetch_hash_all;
668
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
669

            
670
$rows = $dbi->select('table1', {key1 => 2}, sub {
671
    my $query = shift;
672
    $query->bind_filter(sub {
673
        my ($value, $key) = @_;
674
        if ($key eq 'key1') {
675
            return $value - 1;
676
        }
677
        return $value;
678
    });
679
})->fetch_hash_all;
680
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
681

            
682
$dbi->do($CREATE_TABLE->{2});
683
$dbi->insert('table2', {key1 => 1, key3 => 5});
684
$rows = $dbi->select([qw/table1 table2/],
685
                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
686
                     {'table1.key2' => 2},
687
                     "where table1.key1 = table2.key1")->fetch_hash_all;
688
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
689

            
690
test 'Cache';
691
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
692
DBIx::Custom->query_cache_max(2);
693
$dbi->do($CREATE_TABLE->{0});
cleanup
yuki-kimoto authored on 2009-12-22
694
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches};
695
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys};
packaging one directory
yuki-kimoto authored on 2009-11-16
696
$tmpls[0] = "insert into table1 {insert key1 key2}";
697
$queries[0] = $dbi->create_query($tmpls[0]);
698
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
699
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
700
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
701

            
702
$tmpls[1] = "select * from table1";
703
$queries[1] = $dbi->create_query($tmpls[1]);
704
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
705
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
706
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
707
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second");
708
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
709

            
710
$tmpls[2] = "select key1, key2 from table1";
711
$queries[2] = $dbi->create_query($tmpls[2]);
712
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
713
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
714
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
715
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
716
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
717
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
718

            
719
$queries[1] = $dbi->create_query($tmpls[1]);
720
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
721
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
fix timeformat tests
yuki-kimoto authored on 2009-11-23
722
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
723
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
fix timeformat tests
yuki-kimoto authored on 2009-11-23
724
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
725
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
726

            
fix timeformat tests
yuki-kimoto authored on 2009-11-23
727
$query = $dbi->create_query($tmpls[0]);
728
$query->bind_filter('aaa');
729
$query = $dbi->create_query($tmpls[0]);
730
ok(!$query->bind_filter, "$test : only cached sql and key_infos");
731
$query->bind_filter('bbb');
732
$query = $dbi->create_query($tmpls[0]);
733
ok(!$query->bind_filter, "$test : only cached sql and key_infos");
734

            
735