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