* Converted some of the test cases to use SQL::Abstract::Test.
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use SQL::Abstract::Test import => ['is_same_sql_bind'];
6
7
8 BEGIN {
9     eval "use DBD::SQLite";
10     plan $@
11         ? ( skip_all => 'needs DBD::SQLite for testing' )
12         : ( tests => 8 );
13 }
14
15 use lib qw(t/lib);
16
17 use_ok('DBICTest');
18
19 my $schema = DBICTest->init_schema();
20
21 my $sql_maker = $schema->storage->sql_maker;
22
23 $sql_maker->quote_char('`');
24 $sql_maker->name_sep('.');
25
26 my ($sql, @bind) = $sql_maker->select(
27           [
28             {
29               'me' => 'cd'
30             },
31             [
32               {
33                 'artist' => 'artist',
34                 '-join_type' => ''
35               },
36               {
37                 'artist.artistid' => 'me.artist'
38               }
39             ]
40           ],
41           [
42             {
43               'count' => '*'
44             }
45           ],
46           {
47             'artist.name' => 'Caterwauler McCrae',
48             'me.year' => 2001
49           },
50           [],
51           undef,
52           undef
53 );
54
55 is_same_sql_bind(
56   $sql, \@bind,
57   q/SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
58   'got correct SQL and bind parameters for count query with quoting'
59 );
60
61 ($sql, @bind) = $sql_maker->select(
62           [
63             {
64               'me' => 'cd'
65             }
66           ],
67           [
68             'me.cdid',
69             'me.artist',
70             'me.title',
71             'me.year'
72           ],
73           undef,
74           [
75             'year DESC'
76           ],
77           undef,
78           undef
79 );
80
81 TODO: {
82     local $TODO = "order_by with quoting needs fixing (ash/castaway)";
83
84     is($sql, 
85        q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, 
86        'quoted ORDER BY with DESC okay');
87 }
88
89 TODO: {
90     local $TODO = "select attr with star needs fixing (mst/nate)";
91
92     ($sql, @bind) = $sql_maker->select(
93           [
94             {
95               'me' => 'cd'
96             }
97           ],
98           [
99             'me.*'
100           ],
101           undef,
102           [],
103           undef,
104           undef    
105     );
106
107     is_same_sql_bind(
108       $sql, \@bind,
109       q/SELECT `me`.* FROM `cd` `me`/, [],
110       'select attr with me.* is right'
111     );
112 }
113
114 ($sql, @bind) = $sql_maker->select(
115           [
116             {
117               'me' => 'cd'
118             }
119           ],
120           [
121             'me.cdid',
122             'me.artist',
123             'me.title',
124             'me.year'
125           ],
126           undef,
127           [
128             \'year DESC'
129           ],
130           undef,
131           undef
132 );
133
134 is_same_sql_bind(
135   $sql, \@bind,
136   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
137   'did not quote ORDER BY with scalarref'
138 );
139
140 ($sql, @bind) = $sql_maker->update(
141           'group',
142           {
143             'order' => '12',
144             'name' => 'Bill'
145           }
146 );
147
148 is_same_sql_bind(
149   $sql, \@bind,
150   q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
151   'quoted table names for UPDATE'
152 );
153
154 $sql_maker->quote_char([qw/[ ]/]);
155
156 ($sql, @bind) = $sql_maker->select(
157           [
158             {
159               'me' => 'cd'
160             },
161             [
162               {
163                 'artist' => 'artist',
164                 '-join_type' => ''
165               },
166               {
167                 'artist.artistid' => 'me.artist'
168               }
169             ]
170           ],
171           [
172             {
173               'count' => '*'
174             }
175           ],
176           {
177             'artist.name' => 'Caterwauler McCrae',
178             'me.year' => 2001
179           },
180           [],
181           undef,
182           undef
183 );
184
185 is_same_sql_bind(
186   $sql, \@bind,
187   q/SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
188   'got correct SQL and bind parameters for count query with bracket quoting'
189 );
190
191
192 ($sql, @bind) = $sql_maker->update(
193           'group',
194           {
195             'order' => '12',
196             'name' => 'Bill'
197           }
198 );
199
200 is_same_sql_bind(
201   $sql, \@bind,
202   q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
203   'bracket quoted table names for UPDATE'
204 );