1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php namespace System\DB;
- class Connector {
-
- public static $options = array(
- \PDO::ATTR_CASE => \PDO::CASE_LOWER,
- \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
- \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
- \PDO::ATTR_STRINGIFY_FETCHES => false,
- );
-
- public static function connect($config)
- {
-
-
-
- if ($config->driver == 'sqlite')
- {
-
-
-
-
-
-
- if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite'))
- {
- return new \PDO('sqlite:'.$path, null, null, static::$options);
- }
- elseif (file_exists($config->database))
- {
- return new \PDO('sqlite:'.$config->database, null, null, static::$options);
- }
- else
- {
- throw new \Exception("SQLite database [".$config->database."] could not be found.");
- }
- }
-
-
-
- elseif ($config->driver == 'mysql' or $config->driver == 'pgsql')
- {
-
-
-
- $dsn = $config->driver.':host='.$config->host.';dbname='.$config->database;
- if (isset($config->port))
- {
- $dsn .= ';port='.$config->port;
- }
- $connection = new \PDO($dsn, $config->username, $config->password, static::$options);
-
-
-
- if (isset($config->charset))
- {
- $connection->prepare("SET NAMES '".$config->charset."'")->execute();
- }
- return $connection;
- }
- throw new \Exception('Database driver '.$config->driver.' is not supported.');
- }
- }
|