Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[dbsrgits/DBIx-Class.git] / t / 93autocast.t
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
23     sub _native_data_type {
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,
36   'me.single_track' => \[ '= ?', [ single_track => [1, 2, 3 ] ] ],
37 }, { join => 'tracks' });
38
39 my $bind = [
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 ],
48 ];
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 > ?
58       AND me.single_track = ?
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)
77       AND me.single_track = CAST(? AS INT)
78       AND tracks.last_updated_at IS NOT NULL
79       AND tracks.last_updated_on < CAST (? AS DateTime)
80       AND tracks.position = ?
81   )',
82   $bind,
83   'expected sql with casting on',
84 );
85
86 done_testing;