/** * Connects to a database. * @return void * @throws Dibi\Exception */ public function connect(array &$config) { Dibi\Helpers::alias($config, 'database', 'file'); $this->fmtDate = isset($config['formatDate']) ? $config['formatDate'] : 'U'; $this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U'; if (isset($config['resource']) && $config['resource'] instanceof SQLite3) { $this->connection = $config['resource']; } else { try { $this->connection = new SQLite3($config['database']); } catch (\Exception $e) { throw new Dibi\DriverException($e->getMessage(), $e->getCode()); } } $this->dbcharset = empty($config['dbcharset']) ? 'UTF-8' : $config['dbcharset']; $this->charset = empty($config['charset']) ? 'UTF-8' : $config['charset']; if (strcasecmp($this->dbcharset, $this->charset) === 0) { $this->dbcharset = $this->charset = NULL; } // enable foreign keys support (defaultly disabled; if disabled then foreign key constraints are not enforced) $version = SQLite3::version(); if ($version['versionNumber'] >= '3006019') { $this->query('PRAGMA foreign_keys = ON'); } }
/** * Connects to a database. * @return void * @throws Dibi\Exception */ public function connect(array &$config) { Helpers::alias($config, 'options|UID', 'username'); Helpers::alias($config, 'options|PWD', 'password'); Helpers::alias($config, 'options|Database', 'database'); Helpers::alias($config, 'options|CharacterSet', 'charset'); if (isset($config['resource'])) { $this->connection = $config['resource']; } else { $options =& $config['options']; // Default values if (!isset($options['CharacterSet'])) { $options['CharacterSet'] = 'UTF-8'; } $options['PWD'] = (string) $options['PWD']; $options['UID'] = (string) $options['UID']; $options['Database'] = (string) $options['Database']; $this->connection = sqlsrv_connect($config['host'], $options); } if (!is_resource($this->connection)) { $info = sqlsrv_errors(); throw new Dibi\DriverException($info[0]['message'], $info[0]['code']); } $this->version = sqlsrv_server_info($this->connection)['SQLServerVersion']; }
/** * Connects to a database. * @return void * @throws Dibi\Exception */ public function connect(array &$config) { if (isset($config['resource'])) { $this->connection = $config['resource']; } else { // default values Dibi\Helpers::alias($config, 'flags', 'options'); $config += ['charset' => 'utf8', 'timezone' => date('P'), 'username' => ini_get('mysql.default_user'), 'password' => ini_get('mysql.default_password')]; if (!isset($config['host'])) { $host = ini_get('mysql.default_host'); if ($host) { $config['host'] = $host; $config['port'] = ini_get('mysql.default_port'); } else { if (!isset($config['socket'])) { $config['socket'] = ini_get('mysql.default_socket'); } $config['host'] = NULL; } } if (empty($config['socket'])) { $host = $config['host'] . (empty($config['port']) ? '' : ':' . $config['port']); } else { $host = ':' . $config['socket']; } if (empty($config['persistent'])) { $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['flags']); // intentionally @ } else { $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['flags']); // intentionally @ } } if (!is_resource($this->connection)) { throw new Dibi\DriverException(mysql_error(), mysql_errno()); } if (isset($config['charset'])) { if (!@mysql_set_charset($config['charset'], $this->connection)) { // intentionally @ $this->query("SET NAMES '{$config['charset']}'"); } } if (isset($config['database'])) { if (!@mysql_select_db($config['database'], $this->connection)) { // intentionally @ throw new Dibi\DriverException(mysql_error($this->connection), mysql_errno($this->connection)); } } if (isset($config['sqlmode'])) { $this->query("SET sql_mode='{$config['sqlmode']}'"); } if (isset($config['timezone'])) { $this->query("SET time_zone='{$config['timezone']}'"); } $this->buffered = empty($config['unbuffered']); }
/** * Connects to a database. * @return void * @throws Dibi\Exception */ public function connect(array &$config) { Dibi\Helpers::alias($config, 'database', 'db'); if (isset($config['resource'])) { $this->connection = $config['resource']; } else { // default values $config += ['username' => ini_get('ibase.default_password'), 'password' => ini_get('ibase.default_user'), 'database' => ini_get('ibase.default_db'), 'charset' => ini_get('ibase.default_charset'), 'buffers' => 0]; if (empty($config['persistent'])) { $this->connection = @ibase_connect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @ } else { $this->connection = @ibase_pconnect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @ } if (!is_resource($this->connection)) { throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode()); } } }
/** * Connects to a database. * @return void * @throws Dibi\Exception */ public function connect(array &$config) { $foo =& $config['dsn']; $foo =& $config['options']; Dibi\Helpers::alias($config, 'resource', 'pdo'); if ($config['resource'] instanceof PDO) { $this->connection = $config['resource']; } else { try { $this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']); } catch (\PDOException $e) { if ($e->getMessage() === 'could not find driver') { throw new Dibi\NotSupportedException('PHP extension for PDO is not loaded.'); } throw new Dibi\DriverException($e->getMessage(), $e->getCode()); } } $this->driverName = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); $this->serverVersion = isset($config['version']) ? $config['version'] : @$this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); // @ - may be not supported }
/** * Connects to a database. * @return void * @throws Dibi\Exception */ public function connect(array &$config) { $error = NULL; if (isset($config['resource'])) { $this->connection = $config['resource']; } else { $config += ['charset' => 'utf8']; if (isset($config['string'])) { $string = $config['string']; } else { $string = ''; Dibi\Helpers::alias($config, 'user', 'username'); Dibi\Helpers::alias($config, 'dbname', 'database'); foreach (['host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'] as $key) { if (isset($config[$key])) { $string .= $key . '=' . $config[$key] . ' '; } } } set_error_handler(function ($severity, $message) use(&$error) { $error = $message; }); if (empty($config['persistent'])) { $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); } else { $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); } restore_error_handler(); } if (!is_resource($this->connection)) { throw new Dibi\DriverException($error ?: 'Connecting error.'); } pg_set_error_verbosity($this->connection, PGSQL_ERRORS_VERBOSE); if (isset($config['charset']) && pg_set_client_encoding($this->connection, $config['charset'])) { throw self::createException(pg_last_error($this->connection)); } if (isset($config['schema'])) { $this->query('SET search_path TO "' . $config['schema'] . '"'); } }
/** @deprecated */ public static function alias(&$config, $key, $alias) { trigger_error(__METHOD__ . '() is deprecated, use Helpers::alias().', E_USER_DEPRECATED); Helpers::alias($config, $key, $alias); }