Drop-in legacy code for DB2-AS/400
[dbsrgits/DBIx-Class.git] / t / sqlahacks / sql_maker / sql_maker.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use lib qw(t/lib);
8 use DBIC::SqlMakerTest;
9
10 use_ok('DBICTest');
11
12 my $schema = DBICTest->init_schema(no_deploy => 1);
13
14 my $sql_maker = $schema->storage->sql_maker;
15
16
17 {
18   my ($sql, @bind) = $sql_maker->insert(
19             'lottery',
20             {
21               'day' => '2008-11-16',
22               'numbers' => [13, 21, 34, 55, 89]
23             }
24   );
25
26   is_same_sql_bind(
27     $sql, \@bind,
28     q/INSERT INTO lottery (day, numbers) VALUES (?, ?)/,
29       [ ['day' => '2008-11-16'], ['numbers' => [13, 21, 34, 55, 89]] ],
30     'sql_maker passes arrayrefs in insert'
31   );
32
33
34   ($sql, @bind) = $sql_maker->update(
35             'lottery',
36             {
37               'day' => '2008-11-16',
38               'numbers' => [13, 21, 34, 55, 89]
39             }
40   );
41
42   is_same_sql_bind(
43     $sql, \@bind,
44     q/UPDATE lottery SET day = ?, numbers = ?/,
45       [ ['day' => '2008-11-16'], ['numbers' => [13, 21, 34, 55, 89]] ],
46     'sql_maker passes arrayrefs in update'
47   );
48 }
49
50 # make sure the cookbook caveat of { $op, \'...' } no longer applies
51 {
52   my ($sql, @bind) = $sql_maker->where({
53     last_attempt => \ '< now() - interval "12 hours"',
54     next_attempt => { '<', \ 'now() - interval "12 hours"' },
55     created => [
56       { '<=', \ '1969' },
57       \ '> 1984',
58     ],
59   });
60   is_same_sql_bind(
61     $sql,
62     \@bind,
63     'WHERE
64           (created <= 1969 OR created > 1984 )
65       AND last_attempt < now() - interval "12 hours"
66       AND next_attempt < now() - interval "12 hours"
67     ',
68     [],
69   );
70 }
71
72 # Make sure the carp/croak override in SQLA works (via SQLAHacks)
73 my $file = quotemeta (__FILE__);
74 throws_ok (sub {
75   $schema->resultset ('Artist')->search ({}, { order_by => { -asc => 'stuff', -desc => 'staff' } } )->as_query;
76 }, qr/$file/, 'Exception correctly croak()ed');
77
78 done_testing;