\Q-uote column/alias names in regexes in _resolve_aliastypes_from_select_args
[dbsrgits/DBIx-Class.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
b25af2a8 35is_same_sql_bind(
36 $schema->resultset('Quotes')->search({})->as_query,
1c8dc11f 37 '(SELECT [me].[`has` [more]] "quotes"], [me].[has # comment], [me].[artistid] FROM [`with` [some]] "quotes"] [me])',
b25af2a8 38 [],
39 'got correct escaped quotes with bracket quoting'
40);
41
02a2db55 42$schema->storage->sql_maker->quote_char('`');
43$schema->storage->sql_maker->name_sep('.');
44
2cfc22dd 45is_same_sql_bind (
46 $rs->as_query,
47 "(SELECT COUNT( * ) FROM cd `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? ))",
02a2db55 48 $expected_bind,
2cfc22dd 49 'got correct SQL for count query with mysql quoting'
9b459129 50);
2a816814 51
b25af2a8 52is_same_sql_bind(
53 $schema->resultset('Quotes')->search({})->as_query,
1c8dc11f 54 '(SELECT `me`.```has`` [more] "quotes"`, `me`.`has # comment`, `me`.`artistid` FROM ```with`` [some] "quotes"` `me`)',
b25af2a8 55 [],
56 'got correct escaped quotes with mysql quoting'
57);
58
1c8dc11f 59is_same_sql_bind(
60 $schema->resultset('Artist')->search({}, { prefetch => 'quotes' })->as_query,
61 '(SELECT `me`.`artistid`, `me`.`name`, `me`.`rank`, `me`.`charfield`, `quotes`.```has`` [more] "quotes"`, `quotes`.`has # comment`, `quotes`.`artistid` FROM `artist` `me` LEFT JOIN ```with`` [some] "quotes"` `quotes` ON `quotes`.`artistid` = `me`.`artistid`)',
62 [],
63 'got correct escaped comment with prefetch and mysql quoting'
64);
65
2cfc22dd 66# !!! talk to ribasushi *explicitly* before modfying these tests !!!
67{
68 is_same_sql_bind(
69 $schema->resultset('CD')->search({}, { order_by => 'year DESC', columns => 'cdid' })->as_query,
70 '(SELECT `me`.`cdid` FROM cd `me` ORDER BY `year DESC`)',
71 [],
72 'quoted ORDER BY with DESC (should use a scalarref anyway)'
73 );
e535069e 74
2cfc22dd 75 is_same_sql_bind(
76 $schema->resultset('CD')->search({}, { order_by => \'year DESC', columns => 'cdid' })->as_query,
77 '(SELECT `me`.`cdid` FROM cd `me` ORDER BY year DESC)',
78 [],
79 'did not quote ORDER BY with scalarref',
80 );
81}
e535069e 82
2cfc22dd 83is_same_sql(
84 scalar $schema->storage->sql_maker->update('group', { order => 12, name => 'Bill' }),
02a2db55 85 'UPDATE `group` SET `name` = ?, `order` = ?',
86 'quoted table names for UPDATE' );
56166f36 87
88done_testing;