Move find_co_root into DBICTest::Util
[dbsrgits/DBIx-Class-Historic.git] / t / sqlmaker / quotes.t
CommitLineData
2a816814 1use strict;
58d387fe 2use warnings;
3
2a816814 4use Test::More;
c61a0748 5
6use lib qw(t/lib);
a5a7bb73 7use DBICTest ':DiffSQL';
2a816814 8
2cfc22dd 9my $schema = DBICTest->init_schema( no_deploy => 1 );
22b15c96 10
02a2db55 11$schema->connection(
12 @{ $schema->storage->_dbi_connect_info },
13 { AutoCommit => 1, quote_char => [qw/[ ]/] }
14);
2a816814 15
2cfc22dd 16my $rs = $schema->resultset('CD')->search(
17 { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
18 { join => 'artist' }
19)->count_rs;
20
21my $expected_bind = [
22 [ { dbic_colname => "artist.name", sqlt_datatype => "varchar", sqlt_size => 100 }
23 => 'Caterwauler McCrae' ],
24 [ { dbic_colname => "me.year", sqlt_datatype => "varchar", sqlt_size => 100 }
25 => 2001 ],
26];
5a1f2d73 27
02a2db55 28is_same_sql_bind(
2cfc22dd 29 $rs->as_query,
30 "(SELECT COUNT( * ) FROM cd [me] JOIN [artist] [artist] ON [artist].[artistid] = [me].[artist] WHERE ( [artist].[name] = ? AND [me].[year] = ? ))",
02a2db55 31 $expected_bind,
32 'got correct SQL for count query with bracket quoting'
33);
34
35$schema->storage->sql_maker->quote_char('`');
36$schema->storage->sql_maker->name_sep('.');
37
2cfc22dd 38is_same_sql_bind (
39 $rs->as_query,
40 "(SELECT COUNT( * ) FROM cd `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? ))",
02a2db55 41 $expected_bind,
2cfc22dd 42 'got correct SQL for count query with mysql quoting'
9b459129 43);
2a816814 44
2cfc22dd 45# !!! talk to ribasushi *explicitly* before modfying these tests !!!
46{
47 is_same_sql_bind(
48 $schema->resultset('CD')->search({}, { order_by => 'year DESC', columns => 'cdid' })->as_query,
49 '(SELECT `me`.`cdid` FROM cd `me` ORDER BY `year DESC`)',
50 [],
51 'quoted ORDER BY with DESC (should use a scalarref anyway)'
52 );
e535069e 53
2cfc22dd 54 is_same_sql_bind(
55 $schema->resultset('CD')->search({}, { order_by => \'year DESC', columns => 'cdid' })->as_query,
56 '(SELECT `me`.`cdid` FROM cd `me` ORDER BY year DESC)',
57 [],
58 'did not quote ORDER BY with scalarref',
59 );
60}
e535069e 61
2cfc22dd 62is_same_sql(
63 scalar $schema->storage->sql_maker->update('group', { order => 12, name => 'Bill' }),
02a2db55 64 'UPDATE `group` SET `name` = ?, `order` = ?',
65 'quoted table names for UPDATE' );
56166f36 66
67done_testing;