1: <?php
2: namespace codemania\library\database\drivers;
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: defined('CM_ROOT') or exit('Заперт доступа!');
14:
15: class MySQLiDriver extends \mysqli {
16: private $_result;
17: private $_link = null;
18: public function __construct($dsn) {
19: extract(explode(':', $dsn));
20: $this->_link = parent::__construct($host, $user, $password, $database, $port, $socket);
21: if (mysqli_connect_errno()) {
22: throw new \codemania\library\database\DataBaseException(
23: printf('Ошибка подключения к базе данных: %s', mysqli_connect_error()));
24: }
25: }
26: public function query($query, array $param) {
27: $stmt = $this->prepare((string)$query);
28: $stmt->bind_param($this->_prepare_param_str($param), $param);
29: $this->_result = $stmt->execute();
30: $stmt->close();
31: return $this->_result;
32: }
33: public function multi_query($query) {
34: $this->_result = $this->multi_query($query);
35: $this->_result->free();
36: return $this->_result;
37: }
38: public function num_rows($query = false, array $param = null) {
39: if ($query) {
40: $this->query($query, $param);
41: }
42: return $this->_result->num_rows;
43: }
44: public function get_one_result($query = false, array $param = null) {
45: if ($query) {
46: $this->query($query, $param);
47: }
48: $result = $this->_result->fetch_array();
49: return $result[0];
50: }
51: public function fetch_array($query = false, array $param = null) {
52: if ($query) {
53: $this->query($query, $param);
54: }
55: return $this->_result->fetch_array();
56: }
57: public function fetch_assoc($query = false, array $param = null) {
58: if ($query) {
59: $this->query($query, $param);
60: }
61: return $this->_result->fetch_array();
62: }
63: public function fetch_object($query = false, array $param = null) {
64: if ($query) {
65: $this->query($query, $param);
66: }
67: return $this->_result->fetch_object();
68: }
69: public function fetch_row($query = false, array $param = null) {
70: if ($query) {
71: $this->query($query, $param);
72: }
73: return $this->_result->fetch_row();
74: }
75: private function _prepare_param_str($param) {
76: if (is_array($param)) {
77: foreach ($param as $value) {
78: $str .= gettype($value);
79: }
80: return $str;
81: }
82: return gettype($str);
83: }
84: }