Overview
  • Class

Classes

  • DSNHelper
  • GxDBConnect

Exceptions

  • GxDBException
  1 <?php
  2 /**
  3  * @author Leandro Silva
  4  * @copyright 2012, 2016 Leandro Silva (http://grafluxe.com)
  5  * @license MIT
  6  *
  7  * @classdesc Data Source Name (DSN) helper. This class is filled with static
  8  * methods that return DSN strings. Use it to simplify the database connection
  9  * process. Note that the 'db' method parameters can be set to an empty string
 10  * if you plan to assign the database later.
 11  */
 12 
 13 class DSNHelper {
 14   /**
 15    * DSN for CUBRID.
 16    * @param  string $db
 17    * @param  string $host="localhost"
 18    * @param  string $port=""
 19    * @return string
 20    */
 21   public static function cubrid($db, $host = "localhost", $port = "") {
 22     $dsn = "cubrid:";
 23 
 24     if ($db) { $dsn .= "dbname=$db;"; }
 25     if ($host) { $dsn .= "host=$host;"; }
 26     if ($port) { $dsn .= "port=$port;"; }
 27 
 28     return $dsn;
 29   }
 30 
 31   /**
 32    * DSN for Microsoft SQL Server and Sybase.
 33    * @param  string $db
 34    * @param  string $host="localhost"
 35    * @param  string $charset=""
 36    * @param  string $appname=""
 37    * @return string
 38    */
 39   public static function sybase($db, $host = "localhost", $charset = "", $appname = "") {
 40     $dsn = "sybase:";
 41 
 42     if ($db) { $dsn .= "dbname=$db;"; }
 43     if ($host) { $dsn .= "host=$host;"; }
 44     if ($charset) { $dsn .= "charset=$charset;"; }
 45     if ($appname) { $dsn .= "appname=$appname;"; }
 46 
 47     return $dsn;
 48   }
 49 
 50   /**
 51    * DSN for Firebird.
 52    * @param  string $db
 53    * @param  string $charset=""
 54    * @param  string $role=""
 55    * @return string
 56    */
 57   public static function firebird($db, $charset = "", $role = "") {
 58     $dsn = "firebird:";
 59 
 60     if ($db) { $dsn .= "dbname=$db;"; }
 61     if ($charset) { $dsn .= "charset=$charset;"; }
 62     if ($role) { $dsn .= "role=$role;"; }
 63 
 64     return $dsn;
 65   }
 66 
 67   /**
 68    * DSN for IBM via config file.
 69    * @param  string $ini
 70    * @return string
 71    */
 72   public static function ibm_ini($ini) {
 73     return "ibm:dsn=$ini";
 74   }
 75 
 76   /**
 77    * DSN for IBM.
 78    * @param  String $db
 79    * @param  String $host="localhost"
 80    * @param  String $port=""
 81    * @param  String $usr=""
 82    * @param  String $pwd=""
 83    * @return string
 84    */
 85   public static function ibm($db, $host = "localhost", $port = "", $usr = "", $pwd = "") {
 86     $dsn = "ibm:driver={ibm db2 odbc driver};protocol=tcpip;";
 87 
 88     if ($db) { $dsn .= "database=$db;"; }
 89     if ($host) { $dsn .= "hostname=$host;"; }
 90     if ($port) { $dsn .= "port=$port;"; }
 91     if ($usr) { $dsn .= "uid=$usr;"; }
 92     if ($pwd) { $dsn .= "pwd=$pwd;"; }
 93 
 94     return $dsn;
 95   }
 96 
 97   /**
 98    * DSN for Informix via config file.
 99    * @param  string $ini
100    * @return string
101    */
102   public static function informix_ini($ini) {
103     return "informix:dsn=$ini";
104   }
105 
106   /**
107    * DSN for Informix.
108    * @param  string $db
109    * @param  string $server
110    * @param  string $app
111    * @param  boolean $connection_pooling
112    * @param  boolean $encrypt
113    * @param  string $failover_partner
114    * @param  integer $login_timeout
115    * @param  string $multiple_active_result_sets
116    * @param  boolean $quoted_id
117    * @param  string $trace_file
118    * @param  boolean $trace_on
119    * @param  string $transaction_isolation
120    * @param  boolean $trust_server_certificate
121    * @param  string $wsid
122    * @return string
123    */
124   public static function informix($db, $server, $app, $connection_pooling, $encrypt, $failover_partner, $login_timeout, $multiple_active_result_sets, $quoted_id, $trace_file, $trace_on, $transaction_isolation, $trust_server_certificate, $wsid) {
125     $dsn = "sqlsrv:";
126 
127     if ($db) { $dsn .= "database=$db;"; }
128     if ($server) { $dsn .= "server=$server;"; }
129     if ($app) { $dsn .= "app=$app;"; }
130     if ($connection_pooling) { $dsn .= "connectionpooling=$connection_pooling;"; }
131     if ($encrypt) { $dsn .= "encrypt=$encrypt;"; }
132     if ($failover_partner) { $dsn .= "failover_partner=$failover_partner;"; }
133     if ($login_timeout) { $dsn .= "logintimeout=$login_timeout;"; }
134     if ($multiple_active_result_sets) { $dsn .= "multipleactiveresultsets=$multiple_active_result_sets;"; }
135     if ($quoted_id) { $dsn .= "quotedid=$quoted_id;"; }
136     if ($trace_file) { $dsn .= "tracefile=$trace_file;"; }
137     if ($trace_on) { $dsn .= "traceon=$trace_on;"; }
138     if ($transaction_isolation) { $dsn .= "transactionisolation=$transaction_isolation;"; }
139     if ($trust_server_certificate) { $dsn .= "trustservercertificate=$trust_server_certificate;"; }
140     if ($wsid) { $dsn .= "wsid=$wsid;"; }
141 
142     return $dsn;
143   }
144 
145   /**
146    * DSN for MySQL (versions 3-5).
147    * @param  string $db
148    * @param  string $host="localhost"
149    * @param  string $port=""
150    * @param  string $charset=""
151    * @return string
152    */
153   public static function mysql($db, $host = "localhost", $port = "", $charset = "") {
154     $dsn = "mysql:";
155 
156     if ($db) { $dsn .= "dbname=$db;"; }
157     if ($host) { $dsn .= "host=$host;"; }
158     if ($port) { $dsn .= "port=$port;"; }
159     if ($charset) { $dsn .= "charset=$charset;"; }
160 
161     return $dsn;
162   }
163 
164   /**
165    * DSN for MySQL Unix Socket (versions 3-5).
166    * @param  string $db
167    * @param  string $unix_socket
168    * @param  string $charset=""
169    * @return string
170    */
171   public static function mysql_socket($db, $unix_socket, $charset = "") {
172     $dsn = "mysql:";
173 
174     if ($db) { $dsn .= "dbname=$db;"; }
175     if ($unix_socket) { $dsn .= "unix_socket=$unix_socket;"; }
176     if ($charset) { $dsn .= "charset=$charset;"; }
177 
178     return $dsn;
179   }
180 
181   /**
182    * DSN for OCI.
183    * @param  string $db
184    * @return string
185    */
186   public static function oci($db) {
187     return "oci:dbname=$db";
188   }
189 
190   /**
191    * DSN for ODBC and DB2.
192    * @param  string $db
193    * @return string
194    */
195   public static function odbc($db) {
196     return "odbc:$db";
197   }
198 
199   /**
200    * DSN for ODBC and DB2 with full syntax.
201    * @param  string $db
202    * @param  string $host="localhost"
203    * @param  string $port=""
204    * @param  string $protocol=""
205    * @param  string $usr=""
206    * @param  string $pwd=""
207    * @return string
208    */
209   public static function odbc_full($db, $host = "localhost", $port = "", $protocol = "", $usr = "", $pwd = "") {
210     $dsn = "odbc:driver={ibm db2 odbc driver};";
211 
212     if ($db) { $dsn .= "database=$db;"; }
213     if ($host) { $dsn .= "hostname=$host;"; }
214     if ($port) { $dsn .= "port=$port;"; }
215     if ($protocol) { $dsn .= "protocol=$protocol;"; }
216     if ($usr) { $dsn .= "uid=$usr;"; }
217     if ($pwd) { $dsn .= "pwd=$pwd;"; }
218 
219     return $dsn;
220   }
221 
222   /**
223    * DSN for ODBC MS Access.
224    * @param  string $mdb
225    * @param  string $usr
226    * @return string
227    */
228   public static function odbc_msaccess($mdb, $usr) {
229     return "odbc:Driver={Microsoft Access Driver (*.mdb)};dbq=$mdb;uid=$usr";
230   }
231 
232   /**
233    * DSN for PostgreSQL.
234    * @param  string $db
235    * @param  string $host="localhost"
236    * @param  string $port=""
237    * @param  string $usr=""
238    * @param  string $pwd=""
239    * @return string
240    */
241   public static function pgsql($db, $host = "localhost", $port = "", $usr = "", $pwd = "") {
242     return "pgsql:";
243 
244     if ($db) { $dsn .= "dbname=$db;"; }
245     if ($host) { $dsn .= "host=$host;"; }
246     if ($port) { $dsn .= "port=$port;"; }
247     if ($usr) { $dsn .= "user=$usr;"; }
248     if ($pwd) { $dsn .= "password=$pwd;"; }
249 
250     return $dsn;
251   }
252 
253   /**
254    * DSN for sqlite.
255    * @param  string $db
256    * @return string
257    */
258   public static function sqlite($db) {
259     return "sqlite:$db";
260   }
261 
262   /**
263    * DSN for sqlite in memory.
264    * @return string
265    */
266   public static function sqlite_in_memory() {
267     return "sqlite::memory:";
268   }
269 
270   /**
271    * DSN for sqlite2.
272    * @param  string $db
273    * @return string
274    */
275   public static function sqlite2($db) {
276     return "sqlite2:$db";
277   }
278 
279   /**
280    * DSN for sqlite2 in memory.
281    * @return string
282    */
283   public static function sqlite2_in_memory() {
284     return "sqlite2::memory:";
285   }
286 
287 }
288 
289 ?>
290 
API documentation generated by ApiGen