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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
<?php
class QueryBuilderTest extends PHPUnit_Framework_TestCase {
public function setUp() {
// Enable logging
ORM::configure('logging', true);
// Set up the dummy database connection
$db = new MockPDO('sqlite::memory:');
ORM::set_db($db);
}
public function tearDown() {
ORM::reset_config();
ORM::reset_db();
}
public function testFindMany() {
ORM::for_table('widget')->find_many();
$expected = "SELECT * FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testFindOne() {
ORM::for_table('widget')->find_one();
$expected = "SELECT * FROM `widget` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testFindOneWithPrimaryKeyFilter() {
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereIdIs() {
ORM::for_table('widget')->where_id_is(5)->find_one();
$expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSingleWhereClause() {
ORM::for_table('widget')->where('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMultipleWhereClauses() {
ORM::for_table('widget')->where('name', 'Fred')->where('age', 10)->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND `age` = '10' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereNotEqual() {
ORM::for_table('widget')->where_not_equal('name', 'Fred')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` != 'Fred'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereLike() {
ORM::for_table('widget')->where_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` LIKE '%Fred%' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereNotLike() {
ORM::for_table('widget')->where_not_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` NOT LIKE '%Fred%' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereIn() {
ORM::for_table('widget')->where_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IN ('Fred', 'Joe')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereNotIn() {
ORM::for_table('widget')->where_not_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` NOT IN ('Fred', 'Joe')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testLimit() {
ORM::for_table('widget')->limit(5)->find_many();
$expected = "SELECT * FROM `widget` LIMIT 5";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testLimitAndOffset() {
ORM::for_table('widget')->limit(5)->offset(5)->find_many();
$expected = "SELECT * FROM `widget` LIMIT 5 OFFSET 5";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testOrderByDesc() {
ORM::for_table('widget')->order_by_desc('name')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` DESC LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testOrderByAsc() {
ORM::for_table('widget')->order_by_asc('name')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testOrderByExpression() {
ORM::for_table('widget')->order_by_expr('SOUNDEX(`name`)')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY SOUNDEX(`name`) LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMultipleOrderBy() {
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";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testGroupBy() {
ORM::for_table('widget')->group_by('name')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMultipleGroupBy() {
ORM::for_table('widget')->group_by('name')->group_by('age')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name`, `age`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testGroupByExpression() {
ORM::for_table('widget')->group_by_expr("FROM_UNIXTIME(`time`, '%Y-%m')")->find_many();
$expected = "SELECT * FROM `widget` GROUP BY FROM_UNIXTIME(`time`, '%Y-%m')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHaving() {
ORM::for_table('widget')->group_by('name')->having('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMultipleHaving() {
ORM::for_table('widget')->group_by('name')->having('name', 'Fred')->having('age', 10)->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' AND `age` = '10' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingNotEqual() {
ORM::for_table('widget')->group_by('name')->having_not_equal('name', 'Fred')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` != 'Fred'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingLike() {
ORM::for_table('widget')->group_by('name')->having_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` LIKE '%Fred%' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingNotLike() {
ORM::for_table('widget')->group_by('name')->having_not_like('name', '%Fred%')->find_one();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` NOT LIKE '%Fred%' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingIn() {
ORM::for_table('widget')->group_by('name')->having_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IN ('Fred', 'Joe')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingNotIn() {
ORM::for_table('widget')->group_by('name')->having_not_in('name', array('Fred', 'Joe'))->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` NOT IN ('Fred', 'Joe')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingLessThan() {
ORM::for_table('widget')->group_by('name')->having_lt('age', 10)->having_gt('age', 5)->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `age` < '10' AND `age` > '5'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingLessThanOrEqualAndGreaterThanOrEqual() {
ORM::for_table('widget')->group_by('name')->having_lte('age', 10)->having_gte('age', 5)->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `age` <= '10' AND `age` >= '5'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingNull() {
ORM::for_table('widget')->group_by('name')->having_null('name')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IS NULL";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testHavingNotNull() {
ORM::for_table('widget')->group_by('name')->having_not_null('name')->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IS NOT NULL";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawHaving() {
ORM::for_table('widget')->group_by('name')->having_raw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->find_many();
$expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' AND (`age` = '5' OR `age` = '10')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testComplexQuery() {
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";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereLessThanAndGreaterThan() {
ORM::for_table('widget')->where_lt('age', 10)->where_gt('age', 5)->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` < '10' AND `age` > '5'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereLessThanAndEqualAndGreaterThanAndEqual() {
ORM::for_table('widget')->where_lte('age', 10)->where_gte('age', 5)->find_many();
$expected = "SELECT * FROM `widget` WHERE `age` <= '10' AND `age` >= '5'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereNull() {
ORM::for_table('widget')->where_null('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IS NULL";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testWhereNotNull() {
ORM::for_table('widget')->where_not_null('name')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` IS NOT NULL";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawWhereClause() {
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')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawWhereClauseWithPercentSign() {
ORM::for_table('widget')->where_raw('STRFTIME("%Y", "now") = ?', array(2012))->find_many();
$expected = "SELECT * FROM `widget` WHERE STRFTIME(\"%Y\", \"now\") = '2012'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawWhereClauseWithNoParameters() {
ORM::for_table('widget')->where_raw('`name` = "Fred"')->find_many();
$expected = "SELECT * FROM `widget` WHERE `name` = \"Fred\"";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawWhereClauseInMethodChain() {
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'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawQuery() {
ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w')->find_many();
$expected = "SELECT `w`.* FROM `widget` w";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawQueryWithParameters() {
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'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSimpleResultColumn() {
ORM::for_table('widget')->select('name')->find_many();
$expected = "SELECT `name` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMultipleSimpleResultColumns() {
ORM::for_table('widget')->select('name')->select('age')->find_many();
$expected = "SELECT `name`, `age` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSpecifyTableNameAndColumnInResultColumns() {
ORM::for_table('widget')->select('widget.name')->find_many();
$expected = "SELECT `widget`.`name` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMainTableAlias() {
ORM::for_table('widget')->table_alias('w')->find_many();
$expected = "SELECT * FROM `widget` `w`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testAliasesInResultColumns() {
ORM::for_table('widget')->select('widget.name', 'widget_name')->find_many();
$expected = "SELECT `widget`.`name` AS `widget_name` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testAliasesInSelectManyResults() {
ORM::for_table('widget')->select_many(array('widget_name' => 'widget.name'), 'widget_handle')->find_many();
$expected = "SELECT `widget`.`name` AS `widget_name`, `widget_handle` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testLiteralExpressionInResultColumn() {
ORM::for_table('widget')->select_expr('COUNT(*)', 'count')->find_many();
$expected = "SELECT COUNT(*) AS `count` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testLiteralExpressionInSelectManyResultColumns() {
ORM::for_table('widget')->select_many_expr(array('count' => 'COUNT(*)'), 'SUM(widget_order)')->find_many();
$expected = "SELECT COUNT(*) AS `count`, SUM(widget_order) FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSimpleJoin() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSimpleJoinWithWhereIdIsMethod() {
ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_one(5);
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` WHERE `widget`.`id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testInnerJoin() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testLeftOuterJoin() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRightOuterJoin() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testFullOuterJoin() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMultipleJoinSources() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testJoinWithAliases() {
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`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testJoinWithAliasesAndWhere() {
ORM::for_table('widget')->table_alias('w')->join('widget_handle', array('wh.widget_id', '=', 'w.id'), 'wh')->where_equal('id', 1)->find_many();
$expected = "SELECT * FROM `widget` `w` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `w`.`id` WHERE `w`.`id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testJoinWithStringConstraint() {
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";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawJoin() {
ORM::for_table('widget')->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` )', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle')->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` ) `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawJoinWithParameters() {
ORM::for_table('widget')->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2))->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testRawJoinAndRawWhereWithParameters() {
ORM::for_table('widget')
->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2))
->raw_join('INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE ?)', array('person.id', '=', 'widget.person_id'), 'person', array('%Fred%'))
->where_raw('`id` > ? AND `id` < ?', array(5, 10))
->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE '%Fred%') `person` ON `person`.`id` = `widget`.`person_id` WHERE `id` > '5' AND `id` < '10'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSelectWithDistinct() {
ORM::for_table('widget')->distinct()->select('name')->find_many();
$expected = "SELECT DISTINCT `name` FROM `widget`";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testInsertData() {
$widget = ORM::for_table('widget')->create();
$widget->name = "Fred";
$widget->age = 10;
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testInsertDataContainingAnExpression() {
$widget = ORM::for_table('widget')->create();
$widget->name = "Fred";
$widget->age = 10;
$widget->set_expr('added', 'NOW()');
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`, `added`) VALUES ('Fred', '10', NOW())";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testInsertDataUsingArrayAccess() {
$widget = ORM::for_table('widget')->create();
$widget['name'] = "Fred";
$widget['age'] = 10;
$widget->save();
$expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testUpdateData() {
$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'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testUpdateDataContainingAnExpression() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->name = "Fred";
$widget->age = 10;
$widget->set_expr('added', 'NOW()');
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW() WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testUpdateMultipleFields() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->set(array("name" => "Fred", "age" => 10));
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testUpdateMultipleFieldsContainingAnExpression() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->set(array("name" => "Fred", "age" => 10));
$widget->set_expr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')"));
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = GeomFromText('POINT(1.2347 2.3436)') WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testUpdateMultipleFieldsContainingAnExpressionAndOverridePreviouslySetExpression() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->set(array("name" => "Fred", "age" => 10));
$widget->set_expr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')"));
$widget->lat_long = 'unknown';
$widget->save();
$expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = 'unknown' WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testDeleteData() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->delete();
$expected = "DELETE FROM `widget` WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testDeleteMany() {
ORM::for_table('widget')->where_equal('age', 10)->delete_many();
$expected = "DELETE FROM `widget` WHERE `age` = '10'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testCount() {
ORM::for_table('widget')->count();
$expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testIgnoreSelectAndCount() {
ORM::for_table('widget')->select('test')->count();
$expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMax() {
ORM::for_table('person')->max('height');
$expected = "SELECT MAX(`height`) AS `max` FROM `person` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testMin() {
ORM::for_table('person')->min('height');
$expected = "SELECT MIN(`height`) AS `min` FROM `person` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testAvg() {
ORM::for_table('person')->avg('height');
$expected = "SELECT AVG(`height`) AS `avg` FROM `person` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testSum() {
ORM::for_table('person')->sum('height');
$expected = "SELECT SUM(`height`) AS `sum` FROM `person` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function test_quote_identifier_part() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->set('added', '2013-01-04');
$widget->save();
$expected = "UPDATE `widget` SET `added` = '2013-01-04' WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function test_quote_multiple_identifiers_part() {
$record = ORM::for_table('widget')->use_id_column(array('id1', 'id2'))->create();
$expected = "`id1`, `id2`";
$this->assertEquals($expected, $record->_quote_identifier($record->_get_id_column_name()));
}
/**
* Compound primary key tests
*/
public function testFindOneWithCompoundPrimaryKey() {
$record = ORM::for_table('widget')->use_id_column(array('id1', 'id2'));
$record->findOne(array('id1' => 10, 'name' => 'Joe', 'id2' => 20));
$expected = "SELECT * FROM `widget` WHERE `id1` = '10' AND `id2` = '20' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testInsertWithCompoundPrimaryKey() {
$record = ORM::for_table('widget')->use_id_column(array('id1', 'id2'))->create();
$record->set('id1', 10);
$record->set('id2', 20);
$record->set('name', 'Joe');
$record->save();
$expected = "INSERT INTO `widget` (`id1`, `id2`, `name`) VALUES ('10', '20', 'Joe')";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testUpdateWithCompoundPrimaryKey() {
$record = ORM::for_table('widget')->use_id_column(array('id1', 'id2'))->create();
$record->set('id1', 10);
$record->set('id2', 20);
$record->set('name', 'Joe');
$record->save();
$record->set('name', 'John');
$record->save();
$expected = "UPDATE `widget` SET `name` = 'John' WHERE `id1` = '10' AND `id2` = '20'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testDeleteWithCompoundPrimaryKey() {
$record = ORM::for_table('widget')->use_id_column(array('id1', 'id2'))->create();
$record->set('id1', 10);
$record->set('id2', 20);
$record->set('name', 'Joe');
$record->save();
$record->delete();
$expected = "DELETE FROM `widget` WHERE `id1` = '10' AND `id2` = '20'";
$this->assertEquals($expected, ORM::get_last_query());
}
/**
* Regression tests
*/
public function testIssue12IncorrectQuotingOfColumnWildcard() {
ORM::for_table('widget')->select('widget.*')->find_one();
$expected = "SELECT `widget`.* FROM `widget` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testIssue57LogQueryRaisesWarningWhenPercentSymbolSupplied() {
ORM::for_table('widget')->where_raw('username LIKE "ben%"')->find_many();
$expected = 'SELECT * FROM `widget` WHERE username LIKE "ben%"';
$this->assertEquals($expected, ORM::get_last_query());
}
public function testIssue57LogQueryRaisesWarningWhenQuestionMarkSupplied() {
ORM::for_table('widget')->where_raw('comments LIKE "has been released?%"')->find_many();
$expected = 'SELECT * FROM `widget` WHERE comments LIKE "has been released?%"';
$this->assertEquals($expected, ORM::get_last_query());
}
public function testIssue74EscapingQuoteMarksIn_quote_identifier_part() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->set('ad`ded', '2013-01-04');
$widget->save();
$expected = "UPDATE `widget` SET `ad``ded` = '2013-01-04' WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testIssue90UsingSetExprAloneDoesTriggerQueryGeneration() {
$widget = ORM::for_table('widget')->find_one(1);
$widget->set_expr('added', 'NOW()');
$widget->save();
$expected = "UPDATE `widget` SET `added` = NOW() WHERE `id` = '1'";
$this->assertEquals($expected, ORM::get_last_query());
}
public function testIssue176LimitDoesntWorkFirstTime() {
ORM::reset_config();
ORM::reset_db();
ORM::configure('logging', true);
ORM::configure('connection_string', 'sqlite::memory:');
ORM::for_table('sqlite_master')->limit(1)->find_array();
$expected = "SELECT * FROM `sqlite_master` LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}
}
|