1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace PhpBoot\DB; |
3
|
|
|
|
4
|
|
|
use PhpBoot\Application; |
5
|
|
|
use PhpBoot\DB\rules\select\SelectRule; |
6
|
|
|
use PhpBoot\DB\rules\insert\InsertRule; |
7
|
|
|
use PhpBoot\DB\rules\update\UpdateRule; |
8
|
|
|
use PhpBoot\DB\rules\delete\DeleteRule; |
9
|
|
|
use PhpBoot\DB\rules\replace\ReplaceIntoRule; |
10
|
|
|
|
11
|
1 |
|
require_once __DIR__.'/rules/select.php'; |
12
|
1 |
|
require_once __DIR__.'/rules/insert.php'; |
13
|
1 |
|
require_once __DIR__.'/rules/update.php'; |
14
|
1 |
|
require_once __DIR__.'/rules/delete.php'; |
15
|
1 |
|
require_once __DIR__.'/rules/replace.php'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* How-to-use: |
20
|
|
|
* |
21
|
|
|
* $db = new DB(...); |
22
|
|
|
* // 1. select |
23
|
|
|
* $res = $db->select('a, b') |
24
|
|
|
* ->from('table') |
25
|
|
|
* ->leftJoin('table1')->on('table.id=table1.id') |
26
|
|
|
* ->where('a=?',1) |
27
|
|
|
* ->groupBy('b')->having('sum(b)=?', 2) |
28
|
|
|
* ->orderBy('c', Sql::ORDER_BY_ASC) |
29
|
|
|
* ->limit(0,1) |
30
|
|
|
* ->forUpdate()->of('d') |
31
|
|
|
* ->get(); |
32
|
|
|
* |
33
|
|
|
* // 2. update |
34
|
|
|
* $rows = $db->update('table') |
35
|
|
|
* ->set('a', 1) |
36
|
|
|
* ->where('b=?', 2) |
37
|
|
|
* ->orderBy('c', Sql::ORDER_BY_ASC) |
38
|
|
|
* ->limit(1) |
39
|
|
|
* ->exec($db) |
40
|
|
|
* ->rows |
41
|
|
|
* |
42
|
|
|
* // 3. insert |
43
|
|
|
* $newId = $db->insertInto('table') |
44
|
|
|
* ->values(['a'=>1]) |
45
|
|
|
* ->exec($db) |
46
|
|
|
* ->lastInsertId() |
47
|
|
|
* |
48
|
|
|
* //4. delete |
49
|
|
|
* $rows = $db->deleteFrom('table') |
50
|
|
|
* ->where('b=?', 2) |
51
|
|
|
* ->orderBy('c', Sql::ORDER_BY_ASC) |
52
|
|
|
* ->limit(1) |
53
|
|
|
* ->exec($db) |
54
|
|
|
* ->rows |
55
|
|
|
* |
56
|
|
|
* @author caoym <[email protected]> |
57
|
|
|
*/ |
58
|
|
|
class DB{ |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* DB constructor. |
62
|
|
|
* @param Application $app |
63
|
|
|
* @param string $dsn @see \PDO |
64
|
|
|
* @param string $username @see \PDO |
65
|
|
|
* @param string $password @see \PDO |
66
|
|
|
* @param array $options @see \PDO |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
static public function connect(Application $app, |
|
|
|
|
70
|
|
|
$dsn, |
71
|
|
|
$username, |
72
|
|
|
$password, |
73
|
|
|
$options = []) |
74
|
|
|
{ |
75
|
|
|
$options += [ |
76
|
|
|
\PDO::ATTR_ERRMODE =>\PDO::ERRMODE_EXCEPTION, |
77
|
|
|
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';", |
78
|
|
|
\PDO::MYSQL_ATTR_FOUND_ROWS => true |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$connection = new \PDO($dsn, $username, $password, $options); |
82
|
|
|
return new DB($app, $connection); |
83
|
|
|
} |
84
|
|
|
|
85
|
42 |
|
public function __construct(Application $app, $connection) |
86
|
|
|
{ |
87
|
42 |
|
$this->app = $app; |
88
|
42 |
|
$this->connection = $connection; |
89
|
42 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* select('column0,column1') => "SELECT column0,column1" |
93
|
|
|
* |
94
|
|
|
* select('column0', 'column1') => "SELECT column0,column1" |
95
|
|
|
* |
96
|
|
|
* @param string $column0 |
97
|
|
|
* @return \PhpBoot\DB\rules\select\FromRule |
98
|
|
|
*/ |
99
|
20 |
|
function select($column0=null, $_=null){ |
|
|
|
|
100
|
20 |
|
$obj = new SelectRule(new Context($this->connection)); |
101
|
20 |
|
if($column0 == null){ |
|
|
|
|
102
|
1 |
|
$args = ['*']; |
103
|
20 |
|
}elseif(is_array($column0)){ |
104
|
3 |
|
$args = $column0; |
105
|
3 |
|
}else{ |
106
|
17 |
|
$args = func_get_args(); |
107
|
|
|
} |
108
|
20 |
|
foreach ($args as &$arg){ |
109
|
20 |
|
$arg = DB::wrap($arg); |
110
|
20 |
|
if($arg == '*'){ |
111
|
8 |
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
20 |
|
} |
115
|
20 |
|
return $obj->select(implode(',', $args)); |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* insertInto('table') => "INSERT INTO table" |
119
|
|
|
* |
120
|
|
|
* @param string $table |
121
|
|
|
* @return \PhpBoot\DB\rules\insert\ValuesRule |
122
|
|
|
*/ |
123
|
5 |
|
public function insertInto($table) { |
124
|
5 |
|
$obj = new InsertRule(new Context($this->connection)); |
125
|
5 |
|
return $obj->insertInto($table); |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* update('table') => "UPDATE table" |
129
|
|
|
* @param string $table |
130
|
|
|
* @return \PhpBoot\DB\rules\update\UpdateSetRule |
131
|
|
|
*/ |
132
|
8 |
|
public function update($table) { |
133
|
8 |
|
$obj = new UpdateRule(new Context($this->connection)); |
134
|
8 |
|
return $obj->update($table); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* deleteFrom('table') => "DELETE FROM table" |
139
|
|
|
* @param string $table |
140
|
|
|
* @return \PhpBoot\DB\rules\basic\WhereRule |
141
|
|
|
*/ |
142
|
7 |
|
public function deleteFrom($table){ |
143
|
7 |
|
$obj = new DeleteRule(new Context($this->connection)); |
144
|
7 |
|
return $obj->deleteFrom($table); |
145
|
|
|
} |
146
|
|
|
/** |
147
|
|
|
* replaceInto('table') => "REPLACE INTO table" |
148
|
|
|
* @param string $table |
149
|
|
|
* @return \PhpBoot\DB\rules\replace\ValuesRule |
150
|
|
|
*/ |
151
|
2 |
|
public function replaceInto($table){ |
152
|
2 |
|
$obj = new ReplaceIntoRule(new Context($this->connection)); |
153
|
2 |
|
return $obj->replaceInto($table); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \PDO |
158
|
|
|
*/ |
159
|
|
|
public function getConnection() |
160
|
|
|
{ |
161
|
|
|
return $this->connection; |
162
|
|
|
} |
163
|
|
|
/** |
164
|
|
|
* Splice sql use raw string(without escaping) |
165
|
|
|
* for example: |
166
|
|
|
* where('time>?', 'now()') => " WHERE time > 'now()' " |
167
|
|
|
* where('time>?', Sql::raw('now()')) => " WHERE time > now() " |
168
|
|
|
* @param string $str |
169
|
|
|
* @return Raw |
170
|
|
|
*/ |
171
|
9 |
|
static public function raw($str){ |
|
|
|
|
172
|
9 |
|
return new Raw($str); |
173
|
|
|
} |
174
|
42 |
|
static public function wrap($value) |
|
|
|
|
175
|
|
|
{ |
176
|
42 |
|
if ($value === '*') { |
177
|
8 |
|
return $value; |
178
|
|
|
} |
179
|
42 |
|
if($value instanceof Raw){ |
180
|
3 |
|
return $value->get(); |
181
|
|
|
} |
182
|
41 |
|
return '`'.str_replace('`', '``', $value).'`'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return Application |
187
|
|
|
*/ |
188
|
10 |
|
public function getApp() |
189
|
|
|
{ |
190
|
10 |
|
return $this->app; |
191
|
|
|
} |
192
|
|
|
const ORDER_BY_ASC ='ASC'; |
193
|
|
|
const ORDER_BY_DESC ='DESC'; |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @var \PDO |
197
|
|
|
*/ |
198
|
|
|
private $connection; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @var Application |
202
|
|
|
*/ |
203
|
|
|
private $app; |
204
|
|
|
} |
205
|
|
|
|
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.