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