Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- protected $_db, $_table_prefix;
- public function __construct($config) {
- extract($config);
- @$this->_db = new mysqli($server, $username, $password, $database);
- if ($this->_db->connect_error) {
- die("Cant't connect to db.");
- }
- $this->_table_prefix = $table_prefix;
- $this->_db->set_charset($charset);
- }
- public function rawQuery($sql) {
- // echo $sql;
- if (!(@$result = $this->_db->query($sql))) {
- die('Bad query.');
- }
- return $result;
- }
- public function quote($str) {
- return "'" . $this->_db->real_escape_string($str) . "'";
- }
- public function sanitize($data) {
- $ret = array();
- foreach ($data as $k => $v) {
- switch (gettype($v)) {
- case 'boolean':
- $v = $v ? 'TRUE' : 'FALSE';
- break;
- case 'string':
- $v = $this->quote($v);
- break;
- case 'NULL':
- $v = 'NULL';
- }
- $ret[$k] = $v;
- }
- return $ret;
- }
- public function a($data) {
- $data = $this->sanitize($data);
- $temp = array();
- foreach ($data as $k => $v) {
- $temp[] = "`$k`=$v";
- }
- return implode(',', $temp);
- }
- public function v($data) {
- return implode(',', $this->sanitize($data));
- }
- protected function _query($params) {
- $parts = preg_split('/\?([adfvj_]?)/', $params[0], -1, PREG_SPLIT_DELIM_CAPTURE);
- $p = 1;
- $count = count($parts);
- for ($i = 1; $i < $count; $i += 2) {
- $a = &$parts[$i];
- if ($a == '_') {
- $a = $this->_table_prefix;
- }
- else {
- $b = $params[$p++];
- switch ($a) {
- case 'a':
- $a = $this->a($b);
- break;
- case 'd':
- $a = (int) $b;
- break;
- case 'f':
- $a = (float) $b;
- break;
- case 'v':
- $a = $this->v($b);
- break;
- case 'j':
- $a = implode(',', $b);
- break;
- default:
- $a = $this->quote($b);
- }
- }
- }
- return $this->rawQuery(implode('', $parts));
- }
- /**
- * $db->query($sql, $arg1, $arg2, ...);
- * плейсхолдеры:
- * ? - экранированная строка в кавычках
- * ?d - целое число
- * ?f - плавающее число
- * ?v - значения через запятую(фильтруются)
- * ?a - col1 = val1, col2 = val2, ...(фильтруются)
- * ?j - значения через запятую
- * ?_ - преффикс для таблиц
- *
- * @return mysqli_result
- */
- public function query() {
- return $this->_query(func_get_args());
- }
- public function single() {
- $result = $this->_query(func_get_args());
- $row = $result->fetch_row();
- return $row[0];
- }
- public function get() {
- $result = $this->_query(func_get_args());
- return $result->fetch_assoc();
- }
- public function getAll() {
- $result = $this->_query(func_get_args());
- return $result->fetch_all(MYSQLI_ASSOC);
- }
- /**
- * Возвращает ассоциативный массив, где первое поле является ключом, а второе значением
- *
- * @return array
- */
- public function assoc() {
- $result = $this->_query(func_get_args());
- $ret = array();
- $data = $result->fetch_all(MYSQLI_NUM);
- for ($i = 0, $n = count($data); $i < $n; ++$i) {
- $ret[$data[$i][0]] = $data[$i][1];
- }
- return $ret;
- }
- public function insert($table, $data) {
- $this->rawQuery('INSERT INTO `' . $this->_table_prefix . $table . '` (`' . implode('`,`', array_keys($data)) . '`) VALUES(' . $this->v($data) . ');');
- return $this->_db->insert_id;
- }
- public function update($table, $id, $data) {
- $this->rawQuery('UPDATE `' . $this->_table_prefix . $table . '` SET ' . $this->a($data) . ' WHERE `id`=' . intval($id) . ';');
- return $this->_db->affected_rows;
- }
- public function delete($table, $id) {
- $this->rawQuery('DELETE FROM `' . $this->_table_prefix . $table . '` WHERE `id`=' . intval($id) . ';');
- return $this->_db->affected_rows;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment