1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7use 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
23    sub _native_data_type {
24      return $type_map->{$_[1]};
25    }
26}
27
28my $schema = DBICTest->init_schema (storage_type => 'DBICTest::SQLite::AutoCast');
29
30# 'me.id' will be cast unlike the unqualified 'id'
31my $rs = $schema->resultset ('CD')->search ({
32  cdid => { '>', 5 },
33  'tracks.last_updated_at' => { '!=', undef },
34  'tracks.last_updated_on' => { '<', 2009 },
35  'tracks.position' => 4,
36  'tracks.single_track' => \[ '= ?', [ single_track => [1, 2, 3 ] ] ],
37}, { join => 'tracks' });
38
39my $bind = [
40  [ cdid => 5 ],
41  [ 'tracks.last_updated_on' => 2009 ],
42  [ 'tracks.position' => 4 ],
43  [ 'single_track' => [ 1, 2, 3] ],
44];
45
46is_same_sql_bind (
47  $rs->as_query,
48  '(
49    SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
50      FROM cd me
51      LEFT JOIN track tracks ON tracks.cd = me.cdid
52    WHERE
53          cdid > ?
54      AND tracks.last_updated_at IS NOT NULL
55      AND tracks.last_updated_on < ?
56      AND tracks.position = ?
57      AND tracks.single_track = ?
58  )',
59  $bind,
60  'expected sql with casting off',
61);
62
63$schema->storage->auto_cast (1);
64
65is_same_sql_bind (
66  $rs->as_query,
67  '(
68    SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
69      FROM cd me
70      LEFT JOIN track tracks ON tracks.cd = me.cdid
71    WHERE
72          cdid > CAST(? AS INT)
73      AND tracks.last_updated_at IS NOT NULL
74      AND tracks.last_updated_on < CAST (? AS DateTime)
75      AND tracks.position = ?
76      AND tracks.single_track = CAST(? AS INT)
77  )',
78  $bind,
79  'expected sql with casting on',
80);
81
82done_testing;
83