/
database.php
280 lines (223 loc) · 6.23 KB
/
database.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
<?php
interface Database_Handler {
function __construct($info);
function disconnect();
function escape($s);
function quote($s);
function quote_ident($s);
function query($SQL);
function insert_id();
function affected_rows();
function table_exists($tbl);
function prepare_table($tbl, $schema);
function table_fields($name, $refresh);
function table_indexes($name, $refresh);
function create_table($tbl, $fields, $indexes, $engine);
function begin_transaction();
function commit();
function rollback();
function snapshot($filename, $tbls);
function empty_database();
function drop_table();
function restore($filename, $tables);
}
interface DBResult {
function rows($mode/*='object'*/);
function row($mode/*='object'*/);
function count();
function value();
}
final class Database {
static $DB = array();
static $query_count = 0;
static $cache_hits = 0;
const TIMEOUT_VAL = 2;
public $timeout = 0;
private $_handle;
private $_url;
private $_info;
private $_name;
static function & factory($name=NULL) {
if(!$name) $name = Config::get('database.default');
if(!isset(self::$DB[$name])){
$url = Config::get('database.'.$name.'.url');
if (!$url) {
$dbname = Config::get('database.'.$name.'.db');
if (!$dbname) $dbname = Config::get('database.prefix') . $name;
$url = strtr(Config::get('database.root'), array('%database' => $dbname));
}
self::$DB[$name] = new Database($url);
self::$DB[$name]->name($name);
}
return self::$DB[$name];
}
static function shutdown($name=NULL) {
if (!$name) $name = Config::get('database.default');
if (isset(self::$DB[$name])) {
self::$DB[$name]->disconnect();
unset(self::$DB[$name]);
}
}
static function reset() {
foreach ((array) self::$DB as $name => $db) {
$db->disconnect();
}
self::$DB = array();
}
static function cleanup_timeout() {
$now = time();
$dbs = (array) self::$DB;
foreach ($dbs as $name => $db) {
if ($db->timeout < $now) {
$db->disconnect();
unset(self::$DB[$name]);
}
}
}
function __construct($url=NULL){
$this->_url = $url;
$url = parse_url($url);
$this->_info['handler'] = $url['scheme'];
$this->_info['host']= urldecode($url['host']);
$this->_info['port'] = (int)$url['port'];
$this->_info['db'] = substr(urldecode($url['path']), 1);
$this->_info['user'] = urldecode($url['user']);
$this->_info['password'] = isset($url['pass']) ? urldecode($url['pass']) : NULL;
$this->timeout = time() + self::TIMEOUT_VAL;
$this->connect();
}
function info() {
return $this->_info;
}
function disconnect() {
$this->_handle->disconnect();
}
function connect() {
$handler = 'Database_'.$this->_info['handler'];
$this->_handle = new $handler($this->_info);
}
function name($name = NULL) { return is_null($name) ? $this->_name : $this->_name = $name; }
function url() { return $this->_url; }
function is_connected() {
return $this->_handle->is_connected();
}
function escape($s) {
return $this->_handle->escape($s);
}
function make_ident() {
$args = func_get_args();
$ident = array();
foreach($args as $arg) {
$ident[] = $this->quote_ident($arg);
}
return implode('.', $ident);
}
function quote_ident($s){
return $this->_handle->quote_ident($s);
}
function quote($s) {
return $this->_handle->quote($s);
}
function rewrite(){
$args=func_get_args();
$SQL=array_shift($args);
foreach($args as $k=>&$v){
if (is_bool($s) && is_numeric($s)){
}
elseif (is_string($v) && !is_numeric($v)) {
$v=$this->escape($v);
}
elseif (is_array($v)){
$v=$this->quote($v);
}
}
return vsprintf($SQL, $args);
}
function query() {
$args=func_get_args();
if(func_num_args()>1){
$SQL=call_user_func_array(array($this, 'rewrite'), $args);
}else{
$SQL=$args[0];
}
//去掉不必要的换行符
$SQL = preg_replace('/[\n\r\t]+/', ' ', $SQL);
if (Config::get('debug.database', FALSE)) {
Log::add($SQL, 'database');
}
self::$query_count++;
$this->timeout = time() + self::TIMEOUT_VAL;
return $this->_handle->query($SQL);
}
function insert_id() {
return $this->_handle->insert_id();
}
function affected_rows() {
return $this->_handle->affected_rows();
}
function value() {
$args=func_get_args();
$result = call_user_func_array(array($this,'query'), $args);
return $result ? $result->value():NULL;
}
function table_exists($tbl_name) {
return $this->_handle->table_exists($tbl_name);
}
function prepare_table($tbl_name, $schema) {
return $this->_handle->prepare_table($tbl_name, $schema);
}
function table_fields($name, $refresh = FALSE) {
return $this->_handle->table_fields($name, $refresh);
}
function table_indexes($name, $refresh = FALSE) {
return $this->_handle->table_indexes($name, $refresh);
}
function create_table($tbl_name, $fields, $indexes, $engine = NULL) {
if ($this->table_exists($tbl_name) || count($fields) <= 0) return FALSE;
return $this->_handle->create_table($tbl_name, $fields, $indexes, $engine);
}
private $_trans_in_progress = FALSE;
function begin_transaction() {
$this->_handle->begin_transaction();
$this->_trans_in_progress = TRUE;
}
function commit() {
if ($this->_trans_in_progress) {
$this->_handle->commit();
$this->_trans_in_progress = FALSE;
}
}
function rollback() {
if ($this->_trans_in_progress) {
$this->_handle->rollback();
$this->_trans_in_progress = FALSE;
}
}
function snapshot($filename, $tables = NULL) {
if (is_string($tables)) $tables = array($tables);
else $tables = (array)$tables;
return $this->_handle->snapshot($filename, $tables);
}
function empty_database() {
return $this->_handle->empty_database();
}
function drop_table() {
$args = func_get_args();
return call_user_func_array(array($this->_handle, 'drop_table'), $args);
}
function restore($filename, &$retore_filename=NULL, $tables=NULL) {
$retore_filename = $filename.'.restore'.uniqid();
if (!$this->snapshot($retore_filename)) return FALSE;
if (is_string($tables)) $tables = array($tables);
else $tables = (array)$tables;
if (count($tables) > 0) {
foreach ($tables as $table) {
$this->drop_table($table);
}
}
else {
$this->empty_database();
}
return $this->_handle->restore($filename, $tables);
}
}