Commit | Line | Data |
d047d650 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | use DBIC::SqlMakerTest; |
8 | |
9 | { # Fake storage driver for sqlite with autocast |
10 | package DBICTest::SQLite::AutoCast; |
11 | use base qw/ |
12 | DBIx::Class::Storage::DBI::AutoCast |
13 | DBIx::Class::Storage::DBI::SQLite |
14 | /; |
15 | use mro 'c3'; |
16 | |
17 | my $type_map = { |
18 | datetime => 'DateTime', |
19 | integer => 'INT', |
20 | int => undef, # no conversion |
21 | }; |
22 | |
0bd2c1cd |
23 | sub _native_data_type { |
d047d650 |
24 | return $type_map->{$_[1]}; |
25 | } |
26 | } |
27 | |
28 | my $schema = DBICTest->init_schema (storage_type => 'DBICTest::SQLite::AutoCast'); |
29 | |
30 | # 'me.id' will be cast unlike the unqualified 'id' |
31 | my $rs = $schema->resultset ('CD')->search ({ |
32 | cdid => { '>', 5 }, |
33 | 'tracks.last_updated_at' => { '!=', undef }, |
34 | 'tracks.last_updated_on' => { '<', 2009 }, |
35 | 'tracks.position' => 4, |
0e773352 |
36 | 'me.single_track' => \[ '= ?', [ single_track => [1, 2, 3 ] ] ], |
d047d650 |
37 | }, { join => 'tracks' }); |
38 | |
a7741335 |
39 | my $bind = [ |
0e773352 |
40 | [ { sqlt_datatype => 'integer', dbic_colname => 'cdid' } |
41 | => 5 ], |
42 | [ { sqlt_datatype => 'integer', dbic_colname => 'single_track' } |
43 | => [ 1, 2, 3] ], |
44 | [ { sqlt_datatype => 'datetime', dbic_colname => 'tracks.last_updated_on' } |
45 | => 2009 ], |
46 | [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' } |
47 | => 4 ], |
a7741335 |
48 | ]; |
d047d650 |
49 | |
50 | is_same_sql_bind ( |
51 | $rs->as_query, |
52 | '( |
53 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
54 | FROM cd me |
55 | LEFT JOIN track tracks ON tracks.cd = me.cdid |
56 | WHERE |
57 | cdid > ? |
0e773352 |
58 | AND me.single_track = ? |
d047d650 |
59 | AND tracks.last_updated_at IS NOT NULL |
60 | AND tracks.last_updated_on < ? |
61 | AND tracks.position = ? |
62 | )', |
63 | $bind, |
64 | 'expected sql with casting off', |
65 | ); |
66 | |
67 | $schema->storage->auto_cast (1); |
68 | |
69 | is_same_sql_bind ( |
70 | $rs->as_query, |
71 | '( |
72 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
73 | FROM cd me |
74 | LEFT JOIN track tracks ON tracks.cd = me.cdid |
75 | WHERE |
76 | cdid > CAST(? AS INT) |
0e773352 |
77 | AND me.single_track = CAST(? AS INT) |
d047d650 |
78 | AND tracks.last_updated_at IS NOT NULL |
d0e58487 |
79 | AND tracks.last_updated_on < CAST (? AS DateTime) |
d047d650 |
80 | AND tracks.position = ? |
81 | )', |
82 | $bind, |
83 | 'expected sql with casting on', |
84 | ); |
85 | |
86 | done_testing; |