forked from lexxpavlov/angular-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect.php
More file actions
289 lines (270 loc) · 9.03 KB
/
Copy pathConnect.php
File metadata and controls
289 lines (270 loc) · 9.03 KB
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
<?php
/**
* Используйте константу DBSIMPLE_SKIP в качестве подстановочного значения чтобы пропустить опцональный SQL блок.
*/
define('DBSIMPLE_SKIP', log(0));
/**
* Имена специализированных колонок в резальтате,
* которые используются как ключи в результирующем массиве
*/
define('DBSIMPLE_ARRAY_KEY', 'ARRAY_KEY'); // hash-based resultset support
define('DBSIMPLE_PARENT_KEY', 'PARENT_KEY'); // forrest-based resultset support
/**
* Класс обертка для DbSimple
*
* <br>нужен для ленивой инициализации коннекта к базе
*
* @package DbSimple
* @method mixed transaction(string $mode=null)
* @method mixed commit()
* @method mixed rollback()
* @method mixed select(string $query [, $arg1] [,$arg2] ...)
* @method mixed selectRow(string $query [, $arg1] [,$arg2] ...)
* @method array selectCol(string $query [, $arg1] [,$arg2] ...)
* @method string selectCell(string $query [, $arg1] [,$arg2] ...)
* @method mixed query(string $query [, $arg1] [,$arg2] ...)
* @method string escape(mixed $s, bool $isIdent=false)
* @method DbSimple_SubQuery subquery(string $query [, $arg1] [,$arg2] ...)
* @method callback setLogger(callback $logger)
* @method callback setCacher(callback $cacher)
* @method string setIdentPrefix($prx)
* @method string setCachePrefix($prx)
*/
class DbSimple_Connect
{
/** @var DbSimple_Generic_Database База данных */
protected $DbSimple;
/** @var string DSN подключения */
protected $DSN;
/** @var string Тип базы данных */
protected $shema;
/** @var array Что выставить при коннекте */
protected $init;
/** @var integer код ошибки */
public $error = null;
/** @var string сообщение об ошибке */
public $errmsg = null;
/**
* Конструктор только запоминает переданный DSN
* создание класса и коннект происходит позже
*
* @param string $dsn DSN строка БД
*/
public function __construct($dsn)
{
$this->DbSimple = null;
$this->DSN = $dsn;
$this->init = array();
$this->shema = ucfirst(substr($dsn, 0, strpos($dsn, ':')));
}
/**
* Взять базу из пула коннектов
*
* @param string $dsn DSN строка БД
* @return DbSimple_Connect
*/
public static function get($dsn)
{
static $pool = array();
return isset($pool[$dsn]) ? $pool[$dsn] : $pool[$dsn] = new self($dsn);
}
/**
* Возвращает тип базы данных
*
* @return string имя типа БД
*/
public function getShema()
{
return $this->shema;
}
/**
* Коннект при первом запросе к базе данных
*/
public function __call($method, $params)
{
if ($this->DbSimple === null)
$this->connect($this->DSN);
return call_user_func_array(array(&$this->DbSimple, $method), $params);
}
/**
* mixed selectPage(int &$total, string $query [, $arg1] [,$arg2] ...)
* Функцию нужно вызвать отдельно из-за передачи по ссылке
*/
public function selectPage(&$total, $query)
{
if ($this->DbSimple === null)
$this->connect($this->DSN);
$args = func_get_args();
$args[0] = &$total;
return call_user_func_array(array(&$this->DbSimple, 'selectPage'), $args);
}
/**
* Подключение к базе данных
* @param string $dsn DSN строка БД
*/
protected function connect($dsn)
{
$parsed = $this->parseDSN($dsn);
if (!$parsed)
$this->errorHandler('Ошибка разбора строки DSN', $dsn);
if (!isset($parsed['scheme']))
$this->errorHandler('Невозможно загрузить драйвер базы данных', $parsed);
$this->shema = ucfirst($parsed['scheme']);
require_once dirname(__FILE__).'/'.$this->shema.'.php';
$class = 'DbSimple_'.$this->shema;
$this->DbSimple = new $class($parsed);
$this->errmsg = &$this->DbSimple->errmsg;
$this->error = &$this->DbSimple->error;
$prefix = isset($parsed['prefix']) ? $parsed['prefix'] : ($this->_identPrefix ? $this->_identPrefix : false);
if ($prefix)
$this->DbSimple->setIdentPrefix($prefix);
if ($this->_cachePrefix) $this->DbSimple->setCachePrefix($this->_cachePrefix);
if ($this->_cacher) $this->DbSimple->setCacher($this->_cacher);
if ($this->_logger) $this->DbSimple->setLogger($this->_logger);
$this->DbSimple->setErrorHandler($this->errorHandler!==null ? $this->errorHandler : array(&$this, 'errorHandler'));
//выставление переменных
foreach($this->init as $query)
call_user_func_array(array(&$this->DbSimple, 'query'), $query);
$this->init = array();
}
/**
* Функция обработки ошибок - стандартный обработчик
* Все вызовы без @ прекращают выполнение скрипта
*
* @param string $msg Сообщение об ошибке
* @param array $info Подробная информация о контексте ошибки
*/
public function errorHandler($msg, $info)
{
// Если использовалась @, ничего не делать.
if (!error_reporting()) return;
// Выводим подробную информацию об ошибке.
echo "SQL Error: $msg<br><pre>";
print_r($info);
echo "</pre>";
exit();
}
/**
* Выставляет запрос для инициализации
*
* @param string $query запрос
*/
public function addInit($query)
{
$args = func_get_args();
if ($this->DbSimple !== null)
return call_user_func_array(array(&$this->DbSimple, 'query'), $args);
$this->init[] = $args;
}
/**
* Устанавливает новый обработчик ошибок
* Обработчик получает 2 аргумента:
* - сообщение об ошибке
* - массив (код, сообщение, запрос, контекст)
*
* @param callback|null|false $handler обработчик ошибок
* <br> null - по умолчанию
* <br> false - отключен
* @return callback|null|false предыдущий обработчик
*/
public function setErrorHandler($handler)
{
$prev = $this->errorHandler;
$this->errorHandler = $handler;
if ($this->DbSimple)
$this->DbSimple->setErrorHandler($handler);
return $prev;
}
/** @var callback обработчик ошибок */
private $errorHandler = null;
private $_cachePrefix = '';
private $_identPrefix = null;
private $_logger = null;
private $_cacher = null;
/**
* callback setLogger(callback $logger)
* Set query logger called before each query is executed.
* Returns previous logger.
*/
public function setLogger($logger)
{
$prev = $this->_logger;
$this->_logger = $logger;
if ($this->DbSimple)
$this->DbSimple->setLogger($logger);
return $prev;
}
/**
* callback setCacher(callback $cacher)
* Set cache mechanism called during each query if specified.
* Returns previous handler.
*/
public function setCacher(Zend_Cache_Backend_Interface $cacher=null)
{
$prev = $this->_cacher;
$this->_cacher = $cacher;
if ($this->DbSimple)
$this->DbSimple->setCacher($cacher);
return $prev;
}
/**
* string setIdentPrefix($prx)
* Set identifier prefix used for $_ placeholder.
*/
public function setIdentPrefix($prx)
{
$old = $this->_identPrefix;
if ($prx !== null) $this->_identPrefix = $prx;
if ($this->DbSimple)
$this->DbSimple->setIdentPrefix($prx);
return $old;
}
/**
* string setCachePrefix($prx)
* Set cache prefix used in key caclulation.
*/
public function setCachePrefix($prx)
{
$old = $this->_cachePrefix;
if ($prx !== null) $this->_cachePrefix = $prx;
if ($this->DbSimple)
$this->DbSimple->setCachePrefix($prx);
return $old;
}
/**
* Разбирает строку DSN в массив параметров подключения к базе
*
* @param string $dsn строка DSN для разбора
* @return array Параметры коннекта
*/
protected function parseDSN($dsn)
{
$parsed = parse_url($dsn);
if (!$parsed)
return null;
$params = null;
if (!empty($parsed['query']))
{
parse_str($parsed['query'], $params);
$parsed += $params;
}
$parsed['dsn'] = $dsn;
return $parsed;
}
/**
* Проверяет значение $value на присутствие в поле $field таблицы $table
*
* @param string $table таблица
* @param string $field поле
* @param string $value проверяемое значение
* @return bool Присутствие значения в таблице
*/
public function check_value($table, $field, $value)
{
if ($this->DbSimple === null)
$this->connect($this->DSN);
$result = $this->DbSimple->selectRow("SELECT 1 AS `check`,count(*) AS `count` FROM ?# WHERE ?#=?", $table, $field, $value);
return $result['check'] && $result['count'];
}
}
?>