|
@@ -0,0 +1,57 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace ORM;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+* \PDO generator class
|
|
|
|
|
+*
|
|
|
|
|
+* This code is based on the TConnection.php class on the Adianti Framework
|
|
|
|
|
+*
|
|
|
|
|
+* @license http://www.adianti.com.br/framework-license
|
|
|
|
|
+*
|
|
|
|
|
+*/
|
|
|
|
|
+
|
|
|
|
|
+class Connection {
|
|
|
|
|
+
|
|
|
|
|
+ private function __construct() {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function open($connection_info = null) {
|
|
|
|
|
+
|
|
|
|
|
+ $user = isset($connection_info['user']) ? $connection_info['user'] : NULL;
|
|
|
|
|
+ $pass = isset($connection_info['pass']) ? $connection_info['pass'] : NULL;
|
|
|
|
|
+ $name = isset($connection_info['name']) ? $connection_info['name'] : NULL;
|
|
|
|
|
+ $host = isset($connection_info['host']) ? $connection_info['host'] : NULL;
|
|
|
|
|
+ $driver = isset($connection_info['driver']) ? $connection_info['driver'] : NULL;
|
|
|
|
|
+ $port = isset($connection_info['port']) ? $connection_info['port'] : NULL;
|
|
|
|
|
+ $prefix = isset($connection_info['prefix']) ? $connection_info['prefix'] : '';
|
|
|
|
|
+
|
|
|
|
|
+ switch ($driver) {
|
|
|
|
|
+ case 'pgsql':
|
|
|
|
|
+ $port = $port ? $port : '5432';
|
|
|
|
|
+ $conn = new \PDO("pgsql:dbname={$name}; user={$user}; password={$pass};host=$host;port={$port}");
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'mysql':
|
|
|
|
|
+ $port = $port ? $port : '3306';
|
|
|
|
|
+ $conn = new \PDO("mysql:host={$host};port={$port};dbname={$name}", $user, $pass);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'sqlite':
|
|
|
|
|
+ $conn = new \PDO("sqlite:{$name}");
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'ibase':
|
|
|
|
|
+ $conn = new \PDO("firebird:dbname={$name}", $user, $pass);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'oci8':
|
|
|
|
|
+ $conn = new \PDO("oci:dbname={$name}", $user, $pass);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'mssql':
|
|
|
|
|
+ $conn = new \PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ $conn->prefix = $prefix;
|
|
|
|
|
+ $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
|
|
|
+ return $conn;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|