connector.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php namespace System\DB;
  2. class Connector {
  3. /**
  4. * The PDO connection options.
  5. *
  6. * @var array
  7. */
  8. public static $options = array(
  9. \PDO::ATTR_CASE => \PDO::CASE_LOWER,
  10. \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  11. \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
  12. \PDO::ATTR_STRINGIFY_FETCHES => false,
  13. );
  14. /**
  15. * Establish a PDO database connection.
  16. *
  17. * @param object $config
  18. * @return PDO
  19. */
  20. public static function connect($config)
  21. {
  22. // ---------------------------------------------------
  23. // Establish a SQLite PDO connection.
  24. // ---------------------------------------------------
  25. if ($config->driver == 'sqlite')
  26. {
  27. return new \PDO('sqlite:'.APP_PATH.'db/'.$config->database.'.sqlite', null, null, static::$options);
  28. }
  29. // ---------------------------------------------------
  30. // Establish a MySQL or Postgres PDO connection.
  31. // ---------------------------------------------------
  32. elseif ($config->driver == 'mysql' or $config->driver == 'pgsql')
  33. {
  34. $connection = new \PDO($config->driver.':host='.$config->host.';dbname='.$config->database, $config->username, $config->password, static::$options);
  35. if (isset($config->charset))
  36. {
  37. $connection->prepare("SET NAMES '".$config->charset."'")->execute();
  38. }
  39. return $connection;
  40. }
  41. else
  42. {
  43. throw new \Exception('Database driver '.$config->driver.' is not supported.');
  44. }
  45. }
  46. }