1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
<?php
/*
* Basic testing for Idiorm
*
* Checks that the generated SQL is correct
*
*/
require_once dirname(__FILE__) . "/../idiorm.php";
require_once dirname(__FILE__) . "/test_classes.php";
// Enable logging
ORM::configure('logging', true);
// Set up the dummy database connection
$db = new DummyPDO('sqlite::memory:');
ORM::set_db($db);
ORM::for_table('widget')->find_many();
$expected = "SELECT * FROM `widget`";
Tester::check_equal("Basic unfiltered find_many query", $expected);
ORM::for_table('widget')->find_one();
$expected = "SELECT * FROM `widget` LIMIT 1";
Tester::check_equal("Basic unfiltered find_one query", $expected);
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1";
Tester::check_equal("Filtering on ID", $expected);
ORM::for_table('widget')->count();
$expected = "SELECT COUNT(*) AS `count` FROM `widget`";
Tester::check_equal("COUNT query", $expected);
ORM::for_table('widget')->where('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1";
Tester::check_equal("Single where clause", $expected);
ORM::for_table('widget')->where('name', 'Fred')->where('age', 10)->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND `age` = '10' LIMIT 1";
Tester::check_equal("Multiple WHERE clauses", $expected);
ORM::for_table('widget')->where_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` LIKE '%Fred%' LIMIT 1";
Tester::check_equal("where_like method", $expected);
ORM::for_table('widget')->where_not_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` NOT LIKE '%Fred%' LIMIT 1";
Tester::check_equal("where_not_like method", $expected);
ORM::for_table('widget')->where_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IN ('Fred', 'Joe')";
Tester::check_equal("where_in method", $expected);
ORM::for_table('widget')->where_not_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` NOT IN ('Fred', 'Joe')";
Tester::check_equal("where_not_in method", $expected);
ORM::for_table('widget')->limit(5)->find_many();
$expected = "SELECT * FROM `widget` LIMIT 5";
Tester::check_equal("LIMIT clause", $expected);
ORM::for_table('widget')->limit(5)->offset(5)->find_many();
$expected = "SELECT * FROM `widget` LIMIT 5 OFFSET 5";
Tester::check_equal("LIMIT and OFFSET clause", $expected);
ORM::for_table('widget')->order_by_desc('name')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` DESC LIMIT 1";
Tester::check_equal("ORDER BY DESC", $expected);
ORM::for_table('widget')->order_by_asc('name')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC LIMIT 1";
Tester::check_equal("ORDER BY ASC", $expected);
ORM::for_table('widget')->order_by_asc('name')->order_by_desc('age')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC, `age` DESC LIMIT 1";
Tester::check_equal("Multiple ORDER BY", $expected);
ORM::for_table('widget')->where('name', 'Fred')->limit(5)->offset(5)->order_by_asc('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' ORDER BY `name` ASC LIMIT 5 OFFSET 5";
Tester::check_equal("Complex query", $expected);
ORM::for_table('widget')->where_lt('age', 10)->where_gt('age', 5)->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` < '10' AND `age` > '5'";
Tester::check_equal("Less than and greater than", $expected);
ORM::for_table('widget')->where_lte('age', 10)->where_gte('age', 5)->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` <= '10' AND `age` >= '5'";
Tester::check_equal("Less than or equal and greater than or equal", $expected);
ORM::for_table('widget')->where_raw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND (`age` = '5' OR `age` = '10')";
Tester::check_equal("Raw WHERE clause", $expected);
ORM::for_table('widget')->where('age', 18)->where_raw('(`name` = ? OR `name` = ?)', array('Fred', 'Bob'))->where('size', 'large')->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` = '18' AND (`name` = 'Fred' OR `name` = 'Bob') AND `size` = 'large'";
Tester::check_equal("Raw WHERE clause in method chain", $expected);
ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w WHERE `name` = ? AND `age` = ?', array('Fred', 5))->find_many();
$expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'";
Tester::check_equal("Raw query", $expected);
ORM::for_table('widget')->select('name')->find_many();
$expected = "SELECT `name` FROM `widget`";
Tester::check_equal("Simple result column", $expected);
ORM::for_table('widget')->select('name')->select('age')->find_many();
$expected = "SELECT `name`, `age` FROM `widget`";
Tester::check_equal("Multiple simple result columns", $expected);
ORM::for_table('widget')->select('widget.name')->find_many();
$expected = "SELECT `widget`.`name` FROM `widget`";
Tester::check_equal("Specify table name and column in result columns", $expected);
ORM::for_table('widget')->select('widget.name', 'widget_name')->find_many();
$expected = "SELECT `widget`.`name` AS `widget_name` FROM `widget`";
Tester::check_equal("Aliases in result columns", $expected);
ORM::for_table('widget')->select_expr('COUNT(*)', 'count')->find_many();
$expected = "SELECT COUNT(*) AS `count` FROM `widget`";
Tester::check_equal("Literal expression in result columns", $expected);
ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Simple join", $expected);
ORM::for_table('widget')->inner_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Inner join", $expected);
ORM::for_table('widget')->left_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` LEFT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Left outer join", $expected);
ORM::for_table('widget')->right_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` RIGHT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Right outer join", $expected);
ORM::for_table('widget')->full_outer_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` FULL OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Full outer join", $expected);
ORM::for_table('widget')
->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))
->join('widget_nozzle', array('widget_nozzle.widget_id', '=', 'widget.id'))
->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` JOIN `widget_nozzle` ON `widget_nozzle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Multiple join sources", $expected);
ORM::for_table('widget')->table_alias('w')->find_many();
$expected = "SELECT * FROM `widget` `w`";
Tester::check_equal("Main table alias", $expected);
ORM::for_table('widget')->join('widget_handle', array('wh.widget_id', '=', 'widget.id'), 'wh')->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `widget`.`id`";
Tester::check_equal("Join with alias", $expected);
ORM::for_table('widget')->join('widget_handle', "widget_handle.widget_id = widget.id")->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON widget_handle.widget_id = widget.id";
Tester::check_equal("Join with string constraint", $expected);
$widget = ORM::for_table('widget')->create();
$widget->name = "Fred";
$widget->age = 10;
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')";
Tester::check_equal("Insert data", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->name = "Fred";
$widget->age = 10;
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'";
Tester::check_equal("Update data", $expected);
$widget = ORM::for_table('widget')->find_one(1);
$widget->delete();
$expected = "DELETE FROM `widget` WHERE `id` = '1'";
Tester::check_equal("Delete data", $expected);
ORM::configure('id_column', 'primary_key');
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `primary_key` = '5' LIMIT 1";
Tester::check_equal("Setting: id_column", $expected);
ORM::configure('id_column_overrides', array(
'widget' => 'widget_id',
'widget_handle' => 'widget_handle_id',
));
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `widget_id` = '5' LIMIT 1";
Tester::check_equal("Setting: id_column_overrides, first test", $expected);
ORM::for_table('widget_handle')->find_one(5);
$expected = "SELECT * FROM `widget_handle` WHERE `widget_handle_id` = '5' LIMIT 1";
Tester::check_equal("Setting: id_column_overrides, second test", $expected);
ORM::for_table('widget_nozzle')->find_one(5);
$expected = "SELECT * FROM `widget_nozzle` WHERE `primary_key` = '5' LIMIT 1";
Tester::check_equal("Setting: id_column_overrides, third test", $expected);
ORM::for_table('widget')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `new_id` = '5' LIMIT 1";
Tester::check_equal("Instance ID column, first test", $expected);
ORM::for_table('widget_handle')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget_handle` WHERE `new_id` = '5' LIMIT 1";
Tester::check_equal("Instance ID column, second test", $expected);
ORM::for_table('widget_nozzle')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget_nozzle` WHERE `new_id` = '5' LIMIT 1";
Tester::check_equal("Instance ID column, third test", $expected);
Tester::report();
?>
|