tony-landis (owner)

Revisions

gist: 31464 Download_button fork
public
Description:
PHP PDO Singleton Class
Public Clone URL: git://gist.github.com/31464.git
Embed All Files: show embed
pdo-singleton-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
<?php
 
/**
* PDO SINGLETON CLASS
*
* @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;
     
   /**
* 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);
} catch (PDOException $e) {
die("PDO CONNECTION ERROR: " . $e->getMessage() . "<br/>");
}
     }
       return self::$PDOInstance;
    }
 
   /**
* Initiates a transaction
*
* @return bool
*/
public function beginTransaction() {
return self::$PDOInstance->beginTransaction();
}
        
/**
* Commits a transaction
*
* @return bool
*/
public function commit() {
return self::$PDOInstance->commit();
}
 
/**
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @return string
*/
    public function errorCode() {
     return self::$PDOInstance->errorCode();
    }
    
    /**
* Fetch extended error information associated with the last operation on the database handle
*
* @return array
*/
    public function errorInfo() {
     return self::$PDOInstance->errorInfo();
    }
    
    /**
* Execute an SQL statement and return the number of affected rows
*
* @param string $statement
*/
    public function exec($statement) {
     return self::$PDOInstance->exec($statement);
    }
    
    /**
* Retrieve a database connection attribute
*
* @param int $attribute
* @return mixed
*/
    public function getAttribute($attribute) {
     return self::$PDOInstance->getAttribute($attribute);
    }
 
    /**
* Return an array of available PDO drivers
*
* @return array
*/
    public 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 function lastInsertId($name) {
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 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 function query($statement) {
     return self::$PDOInstance->query($statement);
    }
    
    /**
* Execute query and return all rows in assoc array
*
* @param string $statement
* @return array
*/
    public 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 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 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 function quote ($input, $parameter_type=0) {
     return self::$PDOInstance->quote($input, $parameter_type);
    }
    
    /**
* Rolls back a transaction
*
* @return bool
*/
    public function rollBack() {
     return self::$PDOInstance->rollBack();
    }
    
    /**
* Set an attribute
*
* @param int $attribute
* @param mixed $value
* @return bool
*/
    public function setAttribute($attribute, $value ) {
     return self::$PDOInstance->setAttribute($attribute, $value);
    }
}
?>