Skip to content

Commit 9b97ed5

Browse files
authored
Orange Summer
1 parent 03226cf commit 9b97ed5

File tree

1 file changed

+152
-81
lines changed

1 file changed

+152
-81
lines changed

Lib/Db.php

Lines changed: 152 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
<?php
22
/**
3-
* Description of Database
3+
* Lib\Db
4+
* PHP version 7
45
*
5-
* @author http://plus.google.com/+BillRocha
6-
* @copyright Bill Rocha - http://plus.google.com/+BillRocha
7-
* @license MIT & GLP2
8-
* @version 0.0.1
9-
* @package Lib
10-
* @access public
11-
* @since 0.0.2
12-
*
13-
* Database Configuration
14-
* ex.:
15-
*
16-
* echo 'Iniciou';
17-
*
18-
* $users = ['dsn'=>'sqlite:qzumba_users.db', 'user'=>null, 'passw'=>null];
19-
* $msg = ['dsn'=>'sqlite:qzumba_msg.db', 'user'=>null, 'passw'=>null];
20-
*
21-
* //conectando ----------------------------
22-
* $users = new Db($users);
23-
* $msg = new Db($msg);
24-
*
25-
* echo '<pre>';
26-
* $users->query('SELECT * FROM USERS');
27-
* print_r($users->result());
28-
*
29-
* $msg->query('SELECT * FROM MSG');
30-
* print_r($msg->result());
31-
*
32-
*
33-
* print_r($users->result()[0]->get('NAME'));
34-
*/
6+
* @category Database
7+
* @package Data
8+
* @author Bill Rocha <[email protected]>
9+
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
10+
* @license <https://opensource.org/licenses/MIT> MIT
11+
* @version GIT: 0.0.1
12+
* @link http://paulorocha.tk/github/devbr
13+
*/
3514

3615
namespace Lib;
16+
3717
use PDO;
3818

39-
class Db
19+
/**
20+
* Db Class
21+
*
22+
* @category Database
23+
* @package Data
24+
* @author Bill Rocha <[email protected]>
25+
* @license <https://opensource.org/licenses/MIT> MIT
26+
* @link http://paulorocha.tk/github/devbr
27+
*/
28+
class Db
4029
{
4130
private $config = null;
4231

@@ -48,34 +37,59 @@ class Db
4837

4938
private $error = [];
5039

51-
40+
/**
41+
* Constructor
42+
*
43+
* @param array $config configurations
44+
*/
5245
function __construct($config = null)
5346
{
54-
if(is_array($config)){
47+
if (is_array($config)) {
5548
$this->config = $config;
56-
} elseif(method_exists('Config\Database', 'get')){
49+
} elseif (method_exists('Config\Database', 'get')) {
5750
$this->config = \Config\Database::get($config);
58-
}else{
59-
trigger_error('DataBase configurations not found!');
51+
} else {
52+
trigger_error('DataBase configurations not found!');
6053
}
6154
}
6255

56+
/**
57+
* Connector
58+
*
59+
* @param string $alias Alias for database connection
60+
*
61+
* @return object This PDO resource
62+
*/
6363
function connect($alias = null)
64-
{
65-
if($this->conn == null)
66-
try{
67-
$this->conn = new PDO( $this->config['dsn'],
68-
$this->config['user'],
69-
$this->config['passw']);
70-
71-
$this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
72-
} catch (PDOException $e){
64+
{
65+
if ($this->conn == null) {
66+
try {
67+
$this->conn = new PDO(
68+
$this->config['dsn'],
69+
$this->config['user'],
70+
$this->config['passw']
71+
);
72+
73+
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
74+
}
75+
} catch (PDOException $e) {
7376
trigger_error('Data base not connected!');
7477
}
75-
if(!is_object($this->conn)) trigger_error('I can not connect to the database',E_USER_ERROR);
78+
if (!is_object($this->conn)) {
79+
trigger_error('I can not connect to the database', E_USER_ERROR);
80+
}
7681
return $this->conn;
7782
}
7883

84+
/**
85+
* Query
86+
*
87+
* @param string $sql SQL query
88+
* @param array $parms Array of params to "prepare" statment
89+
* @param string $alias Alias of the database connection
90+
*
91+
* @return bool|object False or Row class of results
92+
*/
7993
function query($sql, $parms = array(), $alias = null)
8094
{
8195
$this->sql = $sql;
@@ -84,52 +98,78 @@ function query($sql, $parms = array(), $alias = null)
8498
$this->rows = $sth->rowCount();
8599
$this->error[$sql] = $sth->errorInfo();
86100

87-
if($sth->columnCount() > 0) {
88-
return $this->result = $sth->fetchAll(PDO::FETCH_CLASS,"Lib\Row", [$this->sql, $parms]);
101+
if ($sth->columnCount() > 0) {
102+
return $this->result = $sth->fetchAll(PDO::FETCH_CLASS, "Lib\Row", [$this->sql, $parms]);
89103
} else {
90104
$this->result = false;
91105
return $this->rows;
92106
}
93107
}
94108

95-
//Result Object
109+
/**
110+
* Get results
111+
*
112+
* @return object ROW object
113+
*/
96114
function result()
97115
{
98-
if($this->result == null || count($this->result) == 0) return false;
116+
if ($this->result == null || count($this->result) == 0) {
117+
return false;
118+
}
99119
return $this->result;
100120
}
101-
//Liba os resultados
121+
122+
/**
123+
* Clear data
124+
*
125+
* @return void
126+
*/
102127
function clear()
103128
{
104129
$this->result = new Row;
105130
}
106131

107-
//Pegando erros
132+
/**
133+
* Get error
134+
*
135+
* @return string Error description
136+
*/
108137
function getError()
109138
{
110139
return $this->error;
111140
}
112141

113-
//return the number of rows affected by the last DELETE, INSERT or UPDATE
142+
/**
143+
* Get rows
144+
*
145+
* @return integer number of rows affected by the last DELETE, INSERT or UPDATE
146+
*/
114147
function getRows()
115148
{
116149
return $this->rows;
117150
}
118151

119-
//return last sql string
152+
/**
153+
* Get SQL
154+
*
155+
* @return string return last SQL string
156+
*/
120157
function getSql()
121158
{
122159
return $this->sql;
123160
}
124-
125161
}
126162

127163
/**
128-
* Description of Row
164+
* Row Class
129165
*
130-
* @author http://plus.google.com/+BillRocha
166+
* @category Database
167+
* @package Data
168+
* @author Bill Rocha <[email protected]>
169+
* @license <https://opensource.org/licenses/MIT> MIT
170+
* @link http://paulorocha.tk/github/devbr
131171
*/
132-
class Row
172+
class Row
133173
{
134174
private $__columns = [];
135175
private $__rowParms = ['table'=>null,
@@ -140,21 +180,32 @@ class Row
140180
];
141181
public $teste = 'este é um teste';
142182

143-
183+
/**
184+
* Constructor
185+
*
186+
* @param string $sql [description]
187+
* @param array $parms [description]
188+
*/
144189
function __construct($sql, $parms)
145-
{
190+
{
146191
$this->__rowParms['sql'] = $sql;
147192
$this->__rowParms['parms'] = $parms;
148193

149-
foreach($this as $n=>$v){
150-
if($n == '__rowParms' || $n == '__columns') continue;
194+
foreach ($this as $n => $v) {
195+
if ($n == '__rowParms' || $n == '__columns') {
196+
continue;
197+
}
151198
$this->__columns[$n] = $v;
152199
unset($this->{$n});
153200
}
154201
}
155202

156203

157-
//Salva os dados no banco de dados [insert/update]
204+
/**
205+
* Save data
206+
*
207+
* @return bool Salva os dados no banco de dados [insert/update]
208+
*/
158209
function save()
159210
{
160211
//if($this->id == null) //INSERT INTO
@@ -168,54 +219,74 @@ function save()
168219
}
169220

170221

171-
/** GET
222+
/**
172223
* Get parameter value
173-
* @param string $parm
174-
* @return boolean
224+
*
225+
* @param string $parm name of param
226+
*
227+
* @return bool|array False or array data
175228
*/
176229
function get($parm = false)
177230
{
178-
if(isset($this->__columns[$parm])) return $this->__columns[$parm];
231+
if (isset($this->__columns[$parm])) {
232+
return $this->__columns[$parm];
233+
}
179234
return false;
180235
}
181236

182-
237+
/**
238+
* First row
239+
*
240+
* @return array [description]
241+
*/
183242
function first()
184243
{
185244
return reset($this->__columns);
186245
}
187246

247+
/**
248+
* Next row
249+
*
250+
* @return array [description]
251+
*/
188252
function next()
189253
{
190254
return next($this->__columns);
191255
}
192256

193-
//Return $this as array
257+
/**
258+
* Get all data row
259+
*
260+
* @return array return all data
261+
*/
194262
function getAll()
195263
{
196-
foreach ($this->__columns as $k=>$v) {
264+
foreach ($this->__columns as $k => $v) {
197265
$a[$k] = $v;
198266
}
199267
return $a;
200268
}
201269

202-
/** SET
270+
/**
203271
* Set parameter
204-
* @param string|array $parm Name of parameter or array of parameter name and value
205-
* @param mixed $value Value of parameter
272+
*
273+
* @param string|array $parm Name of parameter or array of parameter name and value
274+
* @param mixed $value Value of parameter
275+
*
206276
* @return boolean
207277
*/
208278
function set($parm, $value = null)
209279
{
210-
if(is_array($parm)){
211-
foreach($parm as $k=>$v){ $this->__columns[$k] = $v; }
280+
if (is_array($parm)) {
281+
foreach ($parm as $k => $v) {
282+
$this->__columns[$k] = $v;
283+
}
212284
return $this;
213-
}
214-
elseif(isset($this->__columns[$parm])) {
285+
} elseif (isset($this->__columns[$parm])) {
215286
$this->__columns[$parm] = $value;
216287
return $this;
288+
} else {
289+
return false;
217290
}
218-
else return false;
219291
}
220-
221-
}
292+
}

0 commit comments

Comments
 (0)