tony-landis (owner)

Revisions

gist: 31470 Download_button fork
public
Description:
PHP PDO Singleton w/AdoDB backwards compatable
Public Clone URL: git://gist.github.com/31470.git
Embed All Files: show embed
pdo-adodb-singleton-refactor.class.php #
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* PDO SINGLETON CLASS, ADODB ENABLED
*
* @author Tony Landis
* @link http://www.tonylandis.com
* @license Use how you like it, just please don't remove or alter this PHPDoc
*/
class sdb
{
    /**
* The singleton instance
*/
    static private $PDOInstance;
    public $debug=false;
     
   /**
* Creates a PDO instance representing a connection to a database and makes the instance available as a singleton
*
* @param string $dsn The full DSN, eg: mysql:host=localhost;dbname=testdb
* @param string $username The user name for the DSN string. This parameter is optional for some PDO drivers.
* @param string $password The password for the DSN string. This parameter is optional for some PDO drivers.
* @param array $driver_options A key=>value array of driver-specific connection options
*
* @return PDO
*/
    public function __construct($dsn, $username=false, $password=false, $driver_options=false)
    {
        if(!self::$PDOInstance) {
try {
self::$PDOInstance = new PDO($dsn, $username, $password, $driver_options);
self::debug(false);
} catch (PDOException $e) {
die("PDO CONNECTION ERROR: " . $e->getMessage() . "<br/>");
}
     }
       return self::$PDOInstance;
    }
 
   /**
* Initiates a transaction
*
* @return bool
*/
public static function beginTransaction() {
return self::$PDOInstance->beginTransaction();
}
        
/**
* Commits a transaction
*
* @return bool
*/
public static function commit() {
return self::$PDOInstance->commit();
}
 
/**
* Do debugging?
*
* @param bool $debugging
*/
public static function debug($debug) {
self::$PDOInstance->debug = (bool) $debug;
if(self::$PDOInstance->debug) {
self::$PDOInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} else {
self::$PDOInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}
}
 
/**
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @return string
*/
    public static function errorCode() {
     return self::$PDOInstance->errorCode();
    }
    
    /**
* Fetch extended error information associated with the last operation on the database handle
*
* @return array
*/
    public static function errorInfo() {
     return self::$PDOInstance->errorInfo();
    }
    
    /**
* Execute an SQL statement and return the number of affected rows
*
* @param string $statement
*/
    public static function exec($statement) {
     try {
if(self::$PDOInstance->debug) echo "$statement<hr>";
return self::$PDOInstance->exec($statement);
} catch(PDOException $e) {
die("DB Exception: ".$e->getMessage()."<hr>");
}
    }
    
    /**
* Retrieve a database connection attribute
*
* @param int $attribute
* @return mixed
*/
    public static function getAttribute($attribute) {
     return self::$PDOInstance->getAttribute($attribute);
    }
 
    /**
* Return an array of available PDO drivers
*
* @return array
*/
    public static function getAvailableDrivers(){
     return Self::$PDOInstance->getAvailableDrivers();
    }
    
    /**
* Returns the ID of the last inserted row or sequence value
*
* @param string $name Name of the sequence object from which the ID should be returned.
* @return string
*/
public static function lastInsertId($name=false) {
return self::$PDOInstance->lastInsertId($name);
}
        
    /**
* Prepares a statement for execution and returns a statement object
*
* @param string $statement A valid SQL statement for the target database server
* @param array $driver_options Array of one or more key=>value pairs to set attribute values for the PDOStatement obj returned
* @return PDOStatement
*/
    public static function prepare ($statement, $driver_options=false) {
     if(!$driver_options) $driver_options=array();
     return self::$PDOInstance->prepare($statement, $driver_options);
    }
    
    /**
* Executes an SQL statement, returning a result set as a PDOStatement object
*
* @param string $statement
* @return PDOStatement
*/
    public static function query($statement) {
     try {
if(self::$PDOInstance->debug) echo "$statement<hr>";
return self::$PDOInstance->query($statement);
} catch(PDOException $e) {
die("DB Exception: ".$e->getMessage()."<hr>");
}
    }
    
    /**
* Execute query and return all rows in assoc array
*
* @param string $statement
* @return array
*/
    public static function queryFetchAllAssoc($statement) {
     return self::$PDOInstance->query($statement)->fetchAll(PDO::FETCH_ASSOC);
    }
    
    /**
* Execute query and return one row in assoc array
*
* @param string $statement
* @return array
*/
    public static function queryFetchRowAssoc($statement) {
     return self::$PDOInstance->query($statement)->fetch(PDO::FETCH_ASSOC);
    }
    
    /**
* Execute query and select one column only
*
* @param string $statement
* @return mixed
*/
    public static function queryFetchColAssoc($statement) {
     return self::$PDOInstance->query($statement)->fetchColumn();
    }
    
    /**
* Quotes a string for use in a query
*
* @param string $input
* @param int $parameter_type
* @return string
*/
    public static function quote ($input, $parameter_type=0) {
     return self::$PDOInstance->quote($input, $parameter_type);
    }
    
    /**
* Rolls back a transaction
*
* @return bool
*/
    public static function rollBack() {
     return self::$PDOInstance->rollBack();
    }
    
    /**
* Set an attribute
*
* @param int $attribute
* @param mixed $value
* @return bool
*/
    public static function setAttribute($attribute, $value ) {
     return self::$PDOInstance->setAttribute($attribute, $value);
    }
 
    /**
* Do not call this ever! Only exists for adodb backwards compatibility
*
* @todo Refactor existing code that calls this
*/
    public static function GetOne($statement) { return self::queryFetchColAssoc($statement); }
    
    /**
* Do not call this ever! Only exists for adodb backwards compatibility
*
* @todo Refactor existing code that calls this
*/
    public static function qstr($string) { return self::quote($string); }
    
    /**
* Do not call this ever! Only exists for adodb backwards compatibility
*
* @todo Refactor existing code that calls this
*
* @return ADODOB_PDOStatement
*/
    public static function Execute($statement) {
     if(!$stmt = self::query($statement)) return false;
     return new ADODOB_PDOStatement($stmt);
    }
}
 
/**
* Class for backwards ADODB RecordSet compatibility
*
*/
class ADODOB_PDOStatement implements Iterator
{
/**
* The PDOStatement object
*
* @var PDOStatement
*/
private $handle;
public $fields;
private $row=0;
 
/* init query */
public function __construct($handle) {
$this->handle =& $handle;
$this->fields =& $this->handle->fetch(PDO::FETCH_ASSOC);
}
 
/* get record count */
public function RecordCount() {
return $this->handle->rowCount();
}
 
function MoveNext() {
if (@$this->fields =& $this->handle->fetch(PDO::FETCH_ASSOC)) {
$this->row += 1;
return $this->fields;
}
return false;
}
 
public function & current(){
return $this->fields;
}
 
public function next() {
$this->fields = $this->handle->fetch(PDO::FETCH_ASSOC);
}
 
public function key(){
return $this->row;
}
 
public function valid(){
return ($this->fields === false ? false : true);
}
 
public function rewind(){
return true;
}
 
public function _close() {
$this->handle->closeCursor();
}
}
?>