Commit | Line | Data |
5e6893d4 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
6edb8b2f |
5 | use Test::Fatal; |
5e6893d4 |
6 | |
7 | use lib qw(t/lib); |
8 | use DBIC::SqlMakerTest; |
9 | use DateTime; |
10 | |
11 | use_ok('DBICTest'); |
12 | |
13 | my $schema = DBICTest->init_schema(); |
14 | |
15 | my $sql_maker = $schema->storage->sql_maker; |
16 | |
17 | my $date = DateTime->new( |
18 | year => 2010, |
19 | month => 12, |
20 | day => 14, |
21 | hour => 12, |
22 | minute => 12, |
23 | second => 12, |
24 | ); |
25 | |
26 | my $date2 = $date->clone->set_day(16); |
27 | |
5a44a8ec |
28 | use Devel::Dwarn; |
29 | |
30 | Dwarn [$schema->resultset('Artist')->search(undef, { |
31 | select => [ |
f1082627 |
32 | [ -dt_diff => [second => { -dt => $date }, { -dt => $date2 }] ], |
33 | [ -dt_diff => [day => { -dt => $date }, { -dt => $date2 }] ], |
5a44a8ec |
34 | ], |
35 | as => [qw(seconds days)], |
36 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
37 | rows => 1, |
38 | })->all]; |
39 | |
5e6893d4 |
40 | is_same_sql_bind ( |
41 | \[ $sql_maker->select ('artist', '*', { 'artist.when_began' => { -dt => $date } } ) ], |
42 | "SELECT * |
43 | FROM artist |
44 | WHERE artist.when_began = ? |
45 | ", |
46 | [['artist.when_began', '2010-12-14 12:12:12']], |
6edb8b2f |
47 | '-dt works' |
5e6893d4 |
48 | ); |
49 | |
50 | is_same_sql_bind ( |
51 | \[ $sql_maker->update ('artist', |
52 | { 'artist.when_began' => { -dt => $date } }, |
53 | { 'artist.when_ended' => { '<' => { -dt => $date2 } } }, |
54 | ) ], |
55 | "UPDATE artist |
56 | SET artist.when_began = ? |
57 | WHERE artist.when_ended < ? |
58 | ", |
59 | [ |
60 | ['artist.when_began', '2010-12-14 12:12:12'], |
61 | ['artist.when_ended', '2010-12-16 12:12:12'], |
62 | ], |
6edb8b2f |
63 | '-dt works' |
5e6893d4 |
64 | ); |
65 | |
66 | is_same_sql_bind ( |
67 | \[ $sql_maker->select ('artist', '*', { |
68 | -and => [ |
69 | { -op => [ '=', 12, { -dt_month => { -ident => 'artist.when_began' } } ] }, |
70 | { -op => [ '=', 2010, { -dt_get => [year => \'artist.when_began'] } ] }, |
71 | { -op => [ '=', 14, { -dt_get => [day_of_month => \'artist.when_began'] } ] }, |
6edb8b2f |
72 | { -op => [ '=', 100, { -dt_diff => [second => { -ident => 'artist.when_began' }, \'artist.when_ended'] } ] }, |
73 | { -op => [ '=', 10, { -dt_diff => [day => { -ident => 'artist.when_played_last' }, \'artist.when_ended'] } ] }, |
5e6893d4 |
74 | ] |
75 | } ) ], |
76 | "SELECT * |
77 | FROM artist |
78 | WHERE ( ( |
c9700c1c |
79 | ( ? = STRFTIME('%m', artist.when_began) ) AND |
80 | ( ? = STRFTIME('%Y', artist.when_began) ) AND |
81 | ( ? = STRFTIME('%d', artist.when_began) ) AND |
6edb8b2f |
82 | ( ? = ( STRFTIME('%s', artist.when_began) - STRFTIME('%s', artist.when_ended))) AND |
83 | ( ? = ( JULIANDAY(artist.when_played_last) - JULIANDAY(artist.when_ended))) |
5e6893d4 |
84 | ) ) |
85 | ", |
86 | [ |
87 | ['', 12], |
88 | ['', 2010], |
89 | ['', 14], |
6edb8b2f |
90 | ['', 100], |
5e6893d4 |
91 | ['', 10], |
92 | ], |
6edb8b2f |
93 | '-dt_month, -dt_get, and -dt_diff work' |
5e6893d4 |
94 | ); |
95 | |
6edb8b2f |
96 | like exception { $sql_maker->select('foo', '*', { -dt_diff => [year => \'artist.lololol', \'artist.fail'] }) }, qr/date diff not supported for part "year" with database "SQLite"/, 'SQLite does not support year diff'; |
97 | |
5e6893d4 |
98 | done_testing; |