/
Mysql.php
718 lines (635 loc) 路 20.4 KB
/
Mysql.php
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php namespace ClanCats\Hydrahon\Test;
/**
* Hydrahon builder test
**
*
* @package Hydrahon
* @copyright Mario D枚ring
*
* @group Hydrahon
* @group Hydrahon_Translator
* @group Hydrahon_Translator_Mysql
*/
use ClanCats\Hydrahon\Query\Sql\BaseSql;
use ClanCats\Hydrahon\Query\Expression;
use ClanCats\Hydrahon\Query\Sql\Func;
class Translator_Mysql_Test extends TranslatorCase
{
protected $grammar = 'mysql';
/**
* mysql grammar tests
*/
public function testSelectSimple()
{
$this->assertQueryTranslation('select * from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select();
});
// distinct
$this->assertQueryTranslation('select distinct * from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->distinct();
});
// with database
$this->assertQueryTranslation('select distinct * from `db_phpunit`.`users` as `u`', array(), function($q)
{
return $q->table('db_phpunit.users as u')->select()->distinct();
});
}
/**
* mysql grammar tests
*/
public function testSelectFields()
{
$this->assertQueryTranslation('select `id` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select('id');
});
// comma seperated fields
$this->assertQueryTranslation('select `foo`, `bar` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select('foo, bar');
});
// with alias as string
$this->assertQueryTranslation('select `foo`, `bar` as `b` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select('foo, bar as b');
});
// with array
$this->assertQueryTranslation('select `name`, `age` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select(array('name', 'age'));
});
// with array with alias
$this->assertQueryTranslation('select `name` as `n`, `age` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select(array('name' => 'n', 'age'));
});
// with raw
$this->assertQueryTranslation('select count(id) as count from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select(array(new Expression('count(id) as count')));
});
// with raw outside of the array
$this->assertQueryTranslation('select count(id) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select(new Expression('count(id)'));
});
// with raw function
$this->assertQueryTranslation('select count(`id`) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select(new Func('count', 'id'));
});
}
/**
* mysql grammar tests
*/
public function testSelectAddField()
{
$this->assertQueryTranslation('select `foo` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addField('foo');
});
// test with alias
$this->assertQueryTranslation('select `foo` as `bar` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addField('foo', 'bar');
});
// add field with function
$this->assertQueryTranslation('select max(`views`) as `max_views` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addField(new Func('max', 'views'), 'max_views');
});
}
/**
* mysql grammar tests
*/
public function testSelectAddFieldCount()
{
$this->assertQueryTranslation('select count(`id`) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldCount('id');
});
// with alias
$this->assertQueryTranslation('select count(`id`) as `count` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldCount('id', 'count');
});
// with raw
$this->assertQueryTranslation('select count(*) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldCount(new Expression('*'));
});
}
/**
* mysql grammar tests
*/
public function testSelectAddFieldMax()
{
$this->assertQueryTranslation('select max(`views`) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldMax('views');
});
// with alias
$this->assertQueryTranslation('select max(`views`) as `max_views` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldMax('views', 'max_views');
});
}
/**
* mysql grammar tests
*/
public function testSelectAddFieldMin()
{
$this->assertQueryTranslation('select min(`views`) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldMin('views');
});
// with alias
$this->assertQueryTranslation('select min(`views`) as `min_views` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldMin('views', 'min_views');
});
}
/**
* mysql grammar tests
*/
public function testSelectAddFieldSum()
{
$this->assertQueryTranslation('select sum(`views`) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldSum('views');
});
// with alias
$this->assertQueryTranslation('select sum(`views`) as `total_views` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldSum('views', 'total_views');
});
}
/**
* mysql grammar tests
*/
public function testSelectAddFieldAvg()
{
$this->assertQueryTranslation('select avg(`views`) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldAvg('views');
});
// with alias
$this->assertQueryTranslation('select avg(`views`) as `avarage_views` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldAvg('views', 'avarage_views');
});
}
/**
* mysql grammar tests
*/
public function testSelectAddFieldRound()
{
$this->assertQueryTranslation('select round(`price`, 0) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldRound('price');
});
// with custom decimal
$this->assertQueryTranslation('select round(`price`, 2) from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldRound('price', 2);
});
// with alias
$this->assertQueryTranslation('select round(`price`, 2) as `rounded_price` from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->addFieldRound('price', 2, 'rounded_price');
});
}
/**
* mysql grammar tests
*/
public function testWhere()
{
// simple
$this->assertQueryTranslation('select * from `phpunit` where `id` = ?', array(42), function($q)
{
return $q->table('phpunit')->select()->where('id', 42);
});
// diffrent expression
$this->assertQueryTranslation('select * from `phpunit` where `id` != ?', array(42), function($q)
{
return $q->table('phpunit')->select()->where('id', '!=', 42);
});
// raw value
$this->assertQueryTranslation('select * from `phpunit` where `id` != 42', array(), function($q)
{
return $q->table('phpunit')->select()->where('id', '!=', new Expression('42'));
});
// 2 wheres
$this->assertQueryTranslation('select * from `phpunit` where `id` = ? and `active` = ?', array(42, 1), function($q)
{
return $q->table('phpunit')->select()
->where('id', 42 )
->where('active', 1);
});
// 2 wheres or
$this->assertQueryTranslation('select * from `phpunit` where `id` = ? or `active` = ?', array(42, 1), function($q)
{
return $q->table('phpunit')->select()
->where('id', 42 )
->orWhere('active', 1);
});
// nesting
$this->assertQueryTranslation('select * from `phpunit` where ( `a` = ? or `c` = ? )', array('b', 'd'), function($q)
{
return $q->table('phpunit')->select()
->where(function( $q )
{
$q->where('a', 'b');
$q->orWhere('c', 'd');
});
});
// arrays
$this->assertQueryTranslation('select * from `phpunit` where ( `name` = ? and `age` = ? )', array('foo', 18), function($q)
{
return $q->table('phpunit')->select()
->where(array( 'name' => 'foo', 'age' => 18 ));
});
// where in
$this->assertQueryTranslation('select * from `phpunit` where `id` in (?, ?, ?)', array(23, 213, 53), function($q)
{
return $q->table('phpunit')->select()
->whereIn('id', array(23, 213, 53));
});
// where null
$this->assertQueryTranslation('select * from `phpunit` where `user`.`updated` is NULL', array(), function($q)
{
return $q->table('phpunit')->select()
->whereNull('user.updated');
});
}
/**
* mysql grammar tests
*/
public function testLimit()
{
// simple
$this->assertQueryTranslation('select * from `phpunit` limit 0, 1', array(), function($q)
{
return $q->table('phpunit')->select()->limit(1);
});
// with offset
$this->assertQueryTranslation('select * from `phpunit` limit 10, 20', array(), function($q)
{
return $q->table('phpunit')->select()->limit(10, 20);
});
// invalid stuff
$this->assertQueryTranslation('select * from `phpunit`', array(), function($q)
{
return $q->table('phpunit')->select()->limit('ignore');
});
}
/**
* mysql grammar tests
*/
public function testSelectOrderBy()
{
// simple
$this->assertQueryTranslation('select * from `phpunit` order by `id` asc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy('id');
});
// other direction
$this->assertQueryTranslation('select * from `phpunit` order by `id` desc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy('id', 'desc');
});
// more keys comma seperated
$this->assertQueryTranslation('select * from `phpunit` order by `u`.`firstname` desc, `u`.`lastname` desc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy('u.firstname, u.lastname', 'desc');
});
// column array
$this->assertQueryTranslation('select * from `phpunit` order by `u`.`firstname` desc, `u`.`lastname` desc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy(array('u.firstname', 'u.lastname'), 'desc');
});
// multipe sortings diffrent direction
$this->assertQueryTranslation('select * from `phpunit` order by `u`.`firstname` asc, `u`.`lastname` desc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy(array('u.firstname' => 'asc', 'u.lastname' => 'desc'));
});
// raw sorting
$this->assertQueryTranslation('select * from `phpunit` order by firstname <> nick asc', array(), function($q)
{
return $q->table('phpunit')->select()->orderBy(new Expression("firstname <> nick"));
});
}
/**
* mysql grammar tests
*/
public function testSelectGroupBy()
{
// simple
$this->assertQueryTranslation('select * from `phpunit` group by `user`.`group`', array(), function($q)
{
return $q->table('phpunit')->select()->groupBy('user.group');
});
// comma seperated
$this->assertQueryTranslation('select * from `phpunit` group by `user`.`group`, `group`.`key`', array(), function($q)
{
return $q->table('phpunit')->select()->groupBy('user.group, group.key');
});
// array
$this->assertQueryTranslation('select * from `phpunit` group by `foo`, `bar`', array(), function($q)
{
return $q->table('phpunit')->select()->groupBy(array('foo', 'bar'));
});
}
/**
* mysql grammar tests
*/
public function testSelectJoins()
{
// simple
$this->assertQueryTranslation('select * from `db1`.`users` as `u` left join `db1`.`groups` as `g` on `u`.`id` = `g`.`user_id`', array(), function($q)
{
return $q->table('db1.users as u')->select()->join('db1.groups as g', 'u.id', '=', 'g.user_id');
});
// other types
$this->assertQueryTranslation('select * from `db1`.`users` as `u` left join `db1`.`groups` as `g` on `u`.`id` = `g`.`user_id` right join `db1`.`orders` as `o` on `u`.`id` = `o`.`user_id` inner join `profiles` as `p` on `u`.`id` = `p`.`user_id`', array(), function($q)
{
return $q->table('db1.users as u')->select()
->join('db1.groups as g', 'u.id', '=', 'g.user_id')
->rightJoin('db1.orders as o', 'u.id', '=', 'o.user_id')
->innerJoin('profiles as p', 'u.id', '=', 'p.user_id');
});
// with raw values
$this->assertQueryTranslation('select * from `db1`.`users` as `u` left join `db1`.`groups` as `g` on `u`.`id` = `g`.`user_id` AND `g`.active = 1', array(), function($q)
{
return $q->table('db1.users as u')->select()->join('db1.groups as g', 'u.id', '=', new Expression('`g`.`user_id` AND `g`.active = 1'));
});
}
/**
* mysql grammar tests
*/
public function testSelectJoinMulticonditions()
{
// simple
$this->assertQueryTranslation('select * from `users` left join `avatars` on `avatars`.`user_id` = `users`.`id`', array(), function($q)
{
return $q->table('users')->select()
->join('avatars', function($q)
{
$q->on('avatars.user_id', '=', 'users.id');
});
});
// multiple
$this->assertQueryTranslation('select * from `users` left join `avatars` on `avatars`.`user_id` = `users`.`id` or `avatars`.`other_user_id` = `users`.`id`', array(), function($q)
{
return $q->table('users')->select()
->join('avatars', function($q)
{
$q->on('avatars.user_id', '=', 'users.id');
$q->orOn('avatars.other_user_id', '=', 'users.id');
});
});
// and
$this->assertQueryTranslation('select * from `users` left join `avatars` on `avatars`.`user_id` = `users`.`id` and `avatars`.`other_user_id` = `users`.`id`', array(), function($q)
{
return $q->table('users')->select()
->join('avatars', function($q)
{
$q->on('avatars.user_id', '=', 'users.id');
$q->on('avatars.other_user_id', '=', 'users.id');
});
});
// with wheres
$this->assertQueryTranslation('select * from `users` left join `avatars` on `avatars`.`user_id` = `users`.`id` and `avatars`.`other_user_id` = `users`.`id` and `avatars`.`active` = ?', array(1), function($q)
{
return $q->table('users')->select()
->join('avatars', function($q)
{
$q->on('avatars.user_id', '=', 'users.id');
$q->on('avatars.other_user_id', '=', 'users.id');
$q->where('avatars.active', 1);
});
});
// with or wheres
$this->assertQueryTranslation('select * from `users` left join `avatars` on `avatars`.`user_id` = `users`.`id` and `avatars`.`active` = ? or `avatar`.`public` = ?', array(1, 1), function($q)
{
return $q->table('users')->select()
->join('avatars', function($q)
{
$q->on('avatars.user_id', '=', 'users.id');
$q->where('avatars.active', 1);
$q->orWhere('avatar.public', 1);
});
});
}
/**
* mysql grammar tests
*/
public function testInsertSimple()
{
// simple
$this->assertQueryTranslation('insert into `test` (`foo`) values (?)', array('bar'), function($q)
{
return $q->table('test')->insert()->values(array('foo' => 'bar'));
});
// some more complexity
$this->assertQueryTranslation('insert into `test` (`bar`, `foo`) values (?, ?)', array('foo','bar'), function($q)
{
return $q->table('test')->insert()->values(array('foo' => 'bar', 'bar' => 'foo'));
});
}
/**
* mysql grammar tests
*/
public function testInsertWithUnwantedAlias()
{
// simple
$this->assertQueryTranslation('insert into `test` (`foo`) values (?)', array('bar'), function($q)
{
return $q->table('test as foo')->insert()->values(array('foo' => 'bar'));
});
// some more complexity
$this->assertQueryTranslation('insert into `test` (`bar`, `foo`) values (?, ?)', array('foo','bar'), function($q)
{
return $q->table(array('test' => 'nope'))->insert()->values(array('foo' => 'bar', 'bar' => 'foo'));
});
}
/**
* mysql grammar tests
*/
public function testInsertIgnore()
{
// ignore
$this->assertQueryTranslation('insert ignore into `test` (`foo`) values (?)', array('bar'), function($q)
{
return $q->table('test')->insert()->ignore()->values(array('foo' => 'bar'));
});
}
/**
* mysql grammar tests
*/
public function testInsertBulk()
{
$this->assertQueryTranslation('insert into `test` (`foo`) values (?), (?)', array('bar', 'bar'), function($q)
{
return $q->table('test')->insert()->values(array(array('foo' => 'bar'), array('foo' => 'bar')));
});
// 2x add valies
$this->assertQueryTranslation('insert into `test` (`foo`) values (?), (?)', array('bar', 'bar'), function($q)
{
return $q->table('test')->insert()->values(array('foo' => 'bar'))->values(array('foo' => 'bar'));
});
}
/**
* mysql grammar tests
*/
public function testUpdateSimple()
{
// simple
$this->assertQueryTranslation('update `test` set `foo` = ?', array('bar'), function($q)
{
return $q->table('test')->update()->set(array('foo' => 'bar'));
});
$this->assertQueryTranslation('update `test` set `foo` = ?, `bar` = ?', array('bar', 'foo'), function($q)
{
return $q->table('test')->update()->set(array('foo' => 'bar', 'bar' => 'foo'));
});
}
/**
* mysql grammar tests
*/
public function testUpdateWithWhereAndLimit()
{
// simple
$this->assertQueryTranslation('update `test` set `foo` = ? where `id` = ? limit 1', array('bar', 1), function($q)
{
return $q->table('test')->update()->set(array('foo' => 'bar'))->where('id', 1)->limit(1);
});
}
/**
* mysql grammar tests
*/
public function testDelete()
{
// simple
$this->assertQueryTranslation('delete from `test` where `id` = ? limit 1', array(1), function($q)
{
return $q->table('test')->delete()->where('id', 1)->limit(1);
});
// simple with db
$this->assertQueryTranslation('delete from `db`.`test` where `id` = ? limit 1', array(1), function($q)
{
return $q->table('db.test')->delete()->where('id', 1)->limit(1);
});
// simple with alias which is invalid and should not be set
$this->assertQueryTranslation('delete from `db`.`test` where `id` = ? limit 1', array(1), function($q)
{
return $q->table('db.test as test')->delete()->where('id', 1)->limit(1);
});
}
/**
* mysql grammar tests
*/
public function testDrop()
{
// simple
$this->assertQueryTranslation('drop table `test`;', array(), function($q)
{
return $q->table('test')->drop();
});
}
/**
* mysql grammar tests
*/
public function testTruncate()
{
// simple
$this->assertQueryTranslation('truncate table `test`;', array(), function($q)
{
return $q->table('test')->truncate();
});
}
/**
* mysql grammar tests
*/
public function testCount()
{
$this->assertQueryExecution(array(array('count(*)' => 42)), 42, 'select count(*) from `test` limit 0, 1', array(), function($q)
{
return $q->table('test')->select()->count();
});
// with diffrent field
$this->assertQueryExecution(array(array('count(`id`)' => 42)), 42, 'select count(`id`) from `test` limit 0, 1', array(), function($q)
{
return $q->table('test')->select()->count('id');
});
}
/**
* mysql grammar tests
*/
public function testSum()
{
$this->assertQueryExecution(array(array('sum(`views`)' => 123)), 123, 'select sum(`views`) from `test` limit 0, 1', array(), function($q)
{
return $q->table('test')->select()->sum('views');
});
}
/**
* mysql grammar tests
*/
public function testMax()
{
$this->assertQueryExecution(array(array('max(`views`)' => 2343)), 2343, 'select max(`views`) from `test` limit 0, 1', array(), function($q)
{
return $q->table('test')->select()->max('views');
});
}
/**
* mysql grammar tests
*/
public function testMin()
{
$this->assertQueryExecution(array(array('min(`views`)' => 12)), 12, 'select min(`views`) from `test` limit 0, 1', array(), function($q)
{
return $q->table('test')->select()->min('views');
});
}
/**
* mysql grammar tests
*/
public function testAvg()
{
$this->assertQueryExecution(array(array('avg(`views`)' => 2342.324)), 2342.324, 'select avg(`views`) from `test` limit 0, 1', array(), function($q)
{
return $q->table('test')->select()->avg('views');
});
}
/**
* mysql grammar tests
*/
public function testExists()
{
$this->assertQueryExecution(array(array('exists' => 1)), true, 'select exists(select * from `test`) as `exists`', array(), function($q)
{
return $q->table('test')->select()->exists();
});
// no results
$this->assertQueryExecution(array(array('exists' => 0)), false, 'select exists(select * from `test`) as `exists`', array(), function($q)
{
return $q->table('test')->select()->exists();
});
// with some statements
$this->assertQueryExecution(array(array('exists' => 0)), false, 'select exists(select * from `test` where ( `a` = ? or `c` = ? )) as `exists`', array('b', 'd'), function($q)
{
return $q->table('test')
->select()
->where(function( $q )
{
$q->where('a', 'b');
$q->orWhere('c', 'd');
})
->exists();
});
}
}