-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
291 lines (263 loc) · 6.36 KB
/
Database.php
File metadata and controls
291 lines (263 loc) · 6.36 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
290
291
<?php
/**
* Devbr\Database
* PHP version 7
*
* @category Database
* @package Data
* @author Bill Rocha <prbr@ymail.com>
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
* @license <https://opensource.org/licenses/MIT> MIT
* @version GIT: 0.0.1
* @link http://dbrasil.tk/devbr
*/
namespace Devbr;
use PDO;
/**
* Database Class
*
* @category Database
* @package Data
* @author Bill Rocha <prbr@ymail.com>
* @license <https://opensource.org/licenses/MIT> MIT
* @link http://dbrasil.tk/devbr
*/
class Database
{
private $config = null;
private $conn = null;
private $sql = null;
private $result = null;
private $rows = 0;
private $error = [];
/**
* Constructor
*
* @param array $config configurations
*/
function __construct($config = null)
{
if (is_array($config)) {
$this->config = $config;
} elseif (method_exists('\Config\Devbr\Database', 'get')) {
$this->config = \Config\Devbr\Database::get($config);
} else {
trigger_error('DataBase configurations not found!');
}
}
/**
* Connector
*
* @param string $alias Alias for database connection
*
* @return object This PDO resource
*/
function connect($alias = null)
{
if ($this->conn == null) {
try {
$this->conn = new PDO(
$this->config['dsn'],
$this->config['user'],
$this->config['passw']
);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
trigger_error('Data base not connected!');
}
}
if (!is_object($this->conn)) {
trigger_error('I can not connect to the database', E_USER_ERROR);
}
return $this->conn;
}
/**
* Query
*
* @param string $sql SQL query
* @param array $parms Array of params to "prepare" statment
* @param string $alias Alias of the database connection
*
* @return bool|object False or Row class of results
*/
function query($sql, $parms = array(), $alias = null)
{
$this->sql = $sql;
$sth = $this->connect($alias)->prepare($sql);
$sth->execute($parms);
$this->rows = $sth->rowCount();
$this->error[$sql] = $sth->errorInfo();
if ($sth->columnCount() > 0) {
return $this->result = $sth->fetchAll(PDO::FETCH_CLASS, __NAMESPACE__.'\Row', [$this->sql, $parms]);
} else {
$this->result = false;
return $this->rows;
}
}
/**
* Get results
*
* @return object ROW object
*/
function result()
{
if ($this->result == null || count($this->result) == 0) {
return false;
}
return $this->result;
}
/**
* Clear data
*
* @return void
*/
function clear()
{
$this->result = new Row;
}
/**
* Get error
*
* @return string Error description
*/
function getError()
{
return $this->error;
}
/**
* Get rows
*
* @return integer number of rows affected by the last DELETE, INSERT or UPDATE
*/
function getRows()
{
return $this->rows;
}
/**
* Get SQL
*
* @return string return last SQL string
*/
function getSql()
{
return $this->sql;
}
}
/**
* Row Class
*
* @category Database
* @package Data
* @author Bill Rocha <prbr@ymail.com>
* @license <https://opensource.org/licenses/MIT> MIT
* @link http://dbrasil.tk/devbr
*/
class Row
{
private $__columns = [];
private $__rowParms = ['table'=>null,
'where'=>null,
'sql'=>null,
'parms'=>null,
'id'=>null
];
/**
* Constructor
*
* @param string $sql [description]
* @param array $parms [description]
*/
function __construct($sql, $parms)
{
$this->__rowParms['sql'] = $sql;
$this->__rowParms['parms'] = $parms;
foreach ($this as $n => $v) {
if ($n == '__rowParms' || $n == '__columns') {
continue;
}
$this->__columns[$n] = $v;
unset($this->{$n});
}
}
/**
* Save data
*
* @return bool Salva os dados no banco de dados [insert/update]
*/
function save()
{
//if($this->id == null) //INSERT INTO
//else //UPDATE
/* ex.: INSERT INTO ($this->__table) SET ($this->$key) = ($this->$value)
* UPDATE FROM ($this->__table) VALUES(($this->$key) = ($this->$value)) WHARE ($this->__where)
*
* in foreach: bypass $__table and $__whare !!
*/
}
/**
* Get parameter value
*
* @param string $parm name of param
*
* @return bool|array False or array data
*/
function get($parm = false)
{
if (isset($this->__columns[$parm])) {
return $this->__columns[$parm];
}
return false;
}
/**
* First row
*
* @return array [description]
*/
function first()
{
return reset($this->__columns);
}
/**
* Next row
*
* @return array [description]
*/
function next()
{
return next($this->__columns);
}
/**
* Get all data row
*
* @return array return all data
*/
function getAll()
{
foreach ($this->__columns as $k => $v) {
$a[$k] = $v;
}
return $a;
}
/**
* Set parameter
*
* @param string|array $parm Name of parameter or array of parameter name and value
* @param mixed $value Value of parameter
*
* @return boolean
*/
function set($parm, $value = null)
{
if (is_array($parm)) {
foreach ($parm as $k => $v) {
$this->__columns[$k] = $v;
}
return $this;
} elseif (isset($this->__columns[$parm])) {
$this->__columns[$parm] = $value;
return $this;
} else {
return false;
}
}
}