1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace PhpBoot\DB\rules\basic; |
3
|
|
|
|
4
|
|
|
use PhpBoot\DB\Context; |
5
|
|
|
use PhpBoot\DB\impls\ExecImpl; |
6
|
|
|
use PhpBoot\DB\impls\LimitImpl; |
7
|
|
|
use PhpBoot\DB\impls\OrderByImpl; |
8
|
|
|
use PhpBoot\DB\impls\ExecResult; |
9
|
|
|
use PhpBoot\DB\impls\WhereImpl; |
10
|
1 |
|
require_once dirname(__DIR__).'/impls.php'; |
11
|
|
|
|
12
|
|
|
class BasicRule |
13
|
|
|
{ |
14
|
42 |
|
public function __construct(Context $context){ |
15
|
42 |
|
$this->context = $context; |
16
|
42 |
|
} |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Context |
21
|
|
|
*/ |
22
|
|
|
public $context; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
class ExecRule extends BasicRule |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Execute sql |
29
|
|
|
* @return ExecResult |
30
|
|
|
*/ |
31
|
22 |
|
public function exec() { |
32
|
22 |
|
return ExecImpl::exec($this->context); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
class LimitRule extends ExecRule |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* limit(1) => "LIMIT 1" |
40
|
|
|
* @param int $size |
41
|
|
|
* @return \PhpBoot\DB\rules\basic\ExecRule |
42
|
|
|
*/ |
43
|
3 |
|
public function limit($size) { |
44
|
3 |
|
LimitImpl::limit($this->context, $size); |
45
|
3 |
|
return new ExecRule($this->context); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
class OrderByRule extends LimitRule |
|
|
|
|
50
|
|
|
{ |
51
|
15 |
|
public function __construct($context){ |
52
|
15 |
|
parent::__construct($context); |
53
|
15 |
|
$this->impl = new OrderByImpl(); |
54
|
15 |
|
} |
55
|
|
|
// /** |
|
|
|
|
56
|
|
|
// * orderByArgs(['column0', 'column1'=>Sql::ORDER_BY_ASC]) => "ORDER BY column0,column1 ASC" |
57
|
|
|
// * @param array $orders |
58
|
|
|
// * @return \PhpBoot\DB\rules\basic\LimitRule |
59
|
|
|
// */ |
60
|
|
|
// public function orderByArgs($orders) { |
61
|
|
|
// $this->impl->orderByArgs($this->context, $orders); |
62
|
|
|
// return new LimitRule($this->context); |
63
|
|
|
// } |
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* orderBy('column') => "ORDER BY column" |
67
|
|
|
* orderBy('column', Sql::ORDER_BY_ASC) => "ORDER BY column ASC" |
68
|
|
|
* orderBy('column0')->orderBy('column1') => "ORDER BY column0, column1" |
69
|
|
|
* orderBy(['column0', 'column1'=>Sql::ORDER_BY_ASC]) => "ORDER BY column0,column1 ASC" |
70
|
|
|
* @param string $column |
71
|
|
|
* @param string $order Sql::ORDER_BY_ASC or Sql::ORDER_BY_DESC |
72
|
|
|
* |
73
|
|
|
* @return \PhpBoot\DB\rules\basic\LimitRule |
74
|
|
|
*/ |
75
|
4 |
|
public function orderBy($column, $order=null) { |
76
|
4 |
|
$this->impl->orderBy($this->context, $column, $order); |
77
|
4 |
|
return new LimitRule($this->context); |
78
|
|
|
} |
79
|
|
|
private $impl; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
class WhereRule extends OrderByRule |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* where('a=?', 1) => "WHERE a=1" |
87
|
|
|
* where('a=?', Sql::raw('now()')) => "WHERE a=now()" |
88
|
|
|
* where('a IN (?)', [1, 2]) => "WHERE a IN (1,2)" |
89
|
|
|
* |
90
|
|
|
* where([ |
91
|
|
|
* 'a'=>1, |
92
|
|
|
* 'b'=>['IN'=>[1,2]] |
93
|
|
|
* 'c'=>['BETWEEN'=>[1,2]] |
94
|
|
|
* 'd'=>['<>'=>1] |
95
|
|
|
* ]) |
96
|
|
|
* => |
97
|
|
|
* "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
98
|
|
|
* |
99
|
|
|
* @param string|array $expr |
100
|
|
|
* @param mixed $_ |
101
|
|
|
* @return \PhpBoot\DB\rules\basic\OrderByRule |
102
|
|
|
*/ |
103
|
13 |
|
public function where($expr, $_= null) { |
|
|
|
|
104
|
13 |
|
WhereImpl::where($this->context, $expr, array_slice(func_get_args(), 1)); |
105
|
13 |
|
return new OrderByRule($this->context); |
106
|
|
|
} |
107
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.