|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lepton\Core; |
|
4
|
|
|
|
|
5
|
|
|
class Database |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
protected $connection; |
|
9
|
|
|
protected $query; |
|
10
|
|
|
protected $show_errors = TRUE; |
|
11
|
|
|
protected $query_closed = TRUE; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Database constructor. |
|
15
|
|
|
* @param string $dbhost |
|
16
|
|
|
* @param string $dbuser |
|
17
|
|
|
* @param string $dbpass |
|
18
|
|
|
* @param string $dbname |
|
19
|
|
|
* @param string $charset |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($dbhost = 'localhost', $dbuser = 'root', $dbpass = '', $dbname = '', $charset = 'utf8') |
|
22
|
|
|
{ |
|
23
|
|
|
$this->connection = new \mysqli($dbhost, $dbuser, $dbpass, $dbname); |
|
24
|
|
|
if ($this->connection->connect_error) { |
|
25
|
|
|
$this->error('Failed to connect to MySQL - ' . $this->connection->connect_error); |
|
26
|
|
|
} |
|
27
|
|
|
$this->connection->set_charset($charset); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/* |
|
31
|
|
|
* Execute an SQL query on the database |
|
32
|
|
|
* @param string $query |
|
33
|
|
|
* @param array $args |
|
34
|
|
|
* @return array|bool |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
public function query($query, &...$args) |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$this->query_closed) { |
|
40
|
|
|
$this->query_closed = TRUE; |
|
41
|
|
|
$this->query->close(); |
|
42
|
|
|
} |
|
43
|
|
|
if ($this->query = $this->connection->prepare($query)) { |
|
44
|
|
|
if (count($args)) { |
|
45
|
|
|
$types = implode(array_map(array($this, '_gettype'), $args)); |
|
46
|
|
|
$this->query->bind_param($types, ...$args); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->query->execute(); |
|
50
|
|
|
if ($this->query->errno) { |
|
51
|
|
|
$this->error('Unable to process MySQL query (check your params) - ' . $this->query->error); |
|
52
|
|
|
} |
|
53
|
|
|
$this->query_closed = FALSE; |
|
54
|
|
|
} else { |
|
55
|
|
|
$this->error('Unable to prepare MySQL statement (check your syntax) - ' . $this->connection->error); |
|
56
|
|
|
} |
|
57
|
|
|
return $this; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function fetch_all($callback = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$params = array(); |
|
64
|
|
|
$row = array(); |
|
65
|
|
|
$meta = $this->query->result_metadata(); |
|
66
|
|
|
while ($field = $meta->fetch_field()) { |
|
67
|
|
|
$params[] = &$row[$field->name]; |
|
68
|
|
|
} |
|
69
|
|
|
$this->query->bind_result(...$params); |
|
70
|
|
|
|
|
71
|
|
|
$result = array(); |
|
72
|
|
|
while ($this->query->fetch()) { |
|
73
|
|
|
$r = array(); |
|
74
|
|
|
foreach ($row as $key => $val) { |
|
75
|
|
|
$r[$key] = $val; |
|
76
|
|
|
} |
|
77
|
|
|
if ($callback != null && is_callable($callback)) { |
|
78
|
|
|
$value = $callback($r); //call_user_func($callback, $r); |
|
79
|
|
|
if ($value == 'break') break; |
|
80
|
|
|
} else { |
|
81
|
|
|
$result[] = $r; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
$this->query_closed = TRUE; |
|
85
|
|
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function fetch_array() |
|
89
|
|
|
{ |
|
90
|
|
|
$params = array(); |
|
91
|
|
|
$row = array(); |
|
92
|
|
|
$meta = $this->query->result_metadata(); |
|
93
|
|
|
while ($field = $meta->fetch_field()) { |
|
94
|
|
|
$params[] = &$row[$field->name]; |
|
95
|
|
|
} |
|
96
|
|
|
$this->query->bind_result(...$params); |
|
97
|
|
|
$result = array(); |
|
98
|
|
|
while ($this->query->fetch()) { |
|
99
|
|
|
foreach ($row as $key => $val) { |
|
100
|
|
|
$result[$key] = $val; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function fetch_result() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->query->get_result(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function close() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->connection->close(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function num_rows() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->query->store_result(); |
|
122
|
|
|
return $this->query->num_rows; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function affected_rows() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->query->affected_rows; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function last_insert_id() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->connection->insert_id; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function error($error) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->show_errors) { |
|
138
|
|
|
die($error); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function _gettype($var) |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_string($var)) return 's'; |
|
145
|
|
|
if (is_float($var)) return 'd'; |
|
146
|
|
|
if (is_int($var)) return 'i'; |
|
147
|
|
|
if (is_null($var)) return 's'; |
|
148
|
|
|
return 'b'; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|