forked from surebert/surebert-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDO.php
More file actions
178 lines (149 loc) · 5.12 KB
/
PDO.php
File metadata and controls
178 lines (149 loc) · 5.12 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
<?php
/**
* Extends native PDO class for logging and debugging
*
* @author Paul Visco
* @version 1.9 09/27/2007 04/01/2009
* @package sb_PDO
*/
class sb_PDO extends PDO{
/**
* This is an array of stored prepared sql statements for use with prepare_and_store
*
* @var array
*/
private $prepared_and_stored = Array();
/**
* Creates am extended PDO object
*
* @param string $connection The pdo connection string
* @param string $user Username if required
* @param string $pass Password for connection if required
*
* <code>
* $db=new sb_PDO("mysql:dbname=xxx;host=xxx", 'username', 'pass');
* $db=new sb_PDO("sqlite:myfile.db3');
* </code>
*
*/
function __construct($connection, $user='', $pass=''){
parent::__construct($connection, $user, $pass);
//sets default mode to fetch obj
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
//silence all errors in production
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}
/**
* Turn a sql query into an array of objects representing each row returned
*
* @param string $sql A sql string, can be used with :prop to create a prepared statement
* @param Array params An array hash of values to match with properties inside prepared statement
* @param Object params An object whose properties match with properties inside prepared statement.
* @param Array class_name Here you can specify a class_name, so that rows returned are instances of that class_name instead of standard objects
* @return array An array of objects representing rows returned
* <code>
*
* //without prepared statements
* $results = App::$db->s2o("SELECT * FROM pages WHERE pid =1");
*
* //with prepared statement and params array
* $results = App::$db->s2o("SELECT * FROM pages WHERE pid =:pid", Array(":pid"=>1));
*
* //with prepared statement and params object
* $params = new stdClass();
* $params->pid = 1;
* $results = App::$db->s2o("SELECT * FROM pages WHERE pid =:pid", $params);
*
* //with prepared statement and params array plus class_name
* $results = App::$db->s2o("SELECT * FROM pages WHERE pid =:pid", Array(":pid" => 1), 'Page');
*
* * //without prepared statement and plus class_name, fetches rows from the pages table into Page instances
* $results = App::$db->s2o("SELECT * FROM pages", null, 'Page');
*
* </code>
*
*/
public function s2o($sql, $params=null, $class_name=''){
//if it has parameters then prepare the statement and execute with the parameters
//stmts that have already been prepared will reuse the prepared statement
if(is_array($params) || is_object($params)){
//convert object parameters
$param = is_array($params) ? $params : self::paramify($params);
$stmt = $this->prepare($sql);
$result = $stmt->execute($params);
if(!$result){
return Array();
}
//if there
} else {
$result = $this->query($sql);
if(!is_object($result)){
return Array();
} else {
$stmt = $result;
}
}
//if the class_name is set return instances
if(substr(ltrim($sql), 0, 1) != 'S'){
return $stmt;
} else if(!empty($class_name)){
if(class_exists($class_name)){
return $stmt->fetchAll(PDO::FETCH_CLASS, $class_name);
}
} else {
//otherwise return standard objects
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
}
/**
* Converts an array into a bound params hash
*
* @param array/object $data The object/hash to convert to a bindParams compatible hash with the colon in front of each key so that it can be used as a bid param array
* @param array $omit An simple array of key names to omit from the array
* @return array The input data as an array designed for passing to pdo's execute or bindParams
* @author Paul Visco
* @version 1.0
* <code>
* $question = new Question();
* $question->qid = 1;
* $question->answer = 'grape';
*
* $params = sb_PDO::paramify($question);
* //returns $params as Array ( [:qid] => 1 [:answer] => 'grape' )
*
* $params = sb_PDO::paramify(Array('qid' => 1, 'answer' => 'grape'));
* //returns $params as Array ( [:qid] => 1 [:answer] => 'grape' )
* </code>
*
*/
public static function paramify($data, $omit=Array()){
$params = Array();
if(is_object($data)){
$data = get_object_vars($data);
}
if(is_array($data)){
foreach($data as $key=>$val){
if(!in_array($key, $omit)){
$params[':'.$key] = $val;
}
}
}
return $params;
}
/**
* Used to prepare and store sql statements for value binding
*
* @param string $sql
* @return PDOStatement A PDO_statment instance
*/
public function prepare($sql, $driver_options=array()){
$md5 = md5($sql);
if(isset($this->prepared_sql[$md5])){
return $this->prepared_sql[$md5];
}
$stmt = parent::prepare($sql, $driver_options);
$this->prepared_sql[$md5] = $stmt;
return $stmt;
}
}
?>