Some identation and documentation
This commit is contained in:
+65
-17
@@ -3,29 +3,72 @@
|
|||||||
namespace ORM;
|
namespace ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \PDO generator class
|
* \PDO generator class
|
||||||
*
|
*
|
||||||
* This code is based on the TConnection.php class on the Adianti Framework
|
* This code is based on the TConnection.php class on the Adianti Framework
|
||||||
*
|
*
|
||||||
* @license http://www.adianti.com.br/framework-license
|
* @license http://www.adianti.com.br/framework-license
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Connection {
|
class Connection
|
||||||
|
{
|
||||||
|
|
||||||
private function __construct() {
|
/**
|
||||||
|
* Available Database Drivers
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
const array drivers = ["pgsql", "mysql", "sqlite", "ibase", "oci8", "mssq"];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor
|
||||||
|
* This class will not exist, it only encapsulate logic
|
||||||
|
*/
|
||||||
|
private function __construct(){ }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a new connection
|
||||||
|
*
|
||||||
|
* You need to pass a connection configuration in a Object/Array
|
||||||
|
*
|
||||||
|
* user
|
||||||
|
* pass
|
||||||
|
* name
|
||||||
|
* host
|
||||||
|
* driver
|
||||||
|
* - pgsql (Postgres)
|
||||||
|
* - mysql (MySQL)
|
||||||
|
* - sqlite (SQLite)
|
||||||
|
* - ibase (Firebird)
|
||||||
|
* - oci8 (Oracle)
|
||||||
|
* - mssql SQLServer
|
||||||
|
* port
|
||||||
|
* prefix
|
||||||
|
*
|
||||||
|
* @param mixed $connectionInfo
|
||||||
|
*
|
||||||
|
* @return \PDO
|
||||||
|
*/
|
||||||
|
public static function open(mixed $connectionInfo = []): \PDO
|
||||||
|
{
|
||||||
|
|
||||||
|
if (is_array($connectionInfo)) {
|
||||||
|
$connectionInfo = (object) $connectionInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function open($connection_info = null) {
|
if(!isset($connectionInfo->driver)){
|
||||||
|
throw new \Exception("Driver not defined");
|
||||||
|
}
|
||||||
|
|
||||||
$user = isset($connection_info['user']) ? $connection_info['user'] : NULL;
|
$user = isset($connectionInfo->user) ? $connectionInfo->user : NULL;
|
||||||
$pass = isset($connection_info['pass']) ? $connection_info['pass'] : NULL;
|
$pass = isset($connectionInfo->pass) ? $connectionInfo->pass : NULL;
|
||||||
$name = isset($connection_info['name']) ? $connection_info['name'] : NULL;
|
$name = isset($connectionInfo->name) ? $connectionInfo->name : NULL;
|
||||||
$host = isset($connection_info['host']) ? $connection_info['host'] : NULL;
|
$host = isset($connectionInfo->host) ? $connectionInfo->host : NULL;
|
||||||
$driver = isset($connection_info['driver']) ? $connection_info['driver'] : NULL;
|
$driver = isset($connectionInfo->driver) ? $connectionInfo->driver : NULL;
|
||||||
$port = isset($connection_info['port']) ? $connection_info['port'] : NULL;
|
$port = isset($connectionInfo->port) ? $connectionInfo->port : NULL;
|
||||||
$prefix = isset($connection_info['prefix']) ? $connection_info['prefix'] : '';
|
$prefix = isset($connectionInfo->prefix) ? $connectionInfo->prefix : '';
|
||||||
|
$traceSQL = isset($connectionInfo->traceSQL) ? $connectionInfo->traceSQL : false;
|
||||||
|
|
||||||
switch ($driver) {
|
switch ($driver) {
|
||||||
case 'pgsql':
|
case 'pgsql':
|
||||||
@@ -49,8 +92,13 @@ class Connection {
|
|||||||
$conn = new \PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
|
$conn = new \PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$conn->prefix = $prefix;
|
$conn->prefix = $prefix;
|
||||||
|
$conn->driver = $driver;
|
||||||
|
$conn->traceSQL = $traceSQL;
|
||||||
|
|
||||||
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
return $conn;
|
return $conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user