/** * Creates connection to database. * @param array $config Configuration options * @throws DriverException */ public function connect(array $config) { Connection::alias($config, 'database', 'file'); Connection::alias($config, 'updateLimit', 'update_limit'); $defaults = array('memory' => false, 'resource' => null, 'updateLimit' => false, 'charset' => 'UTF-8', 'dbcharset' => 'UTF-8', 'persistent' => false, 'unbuffered' => false); $config += $defaults; if ($config['memory']) { $config['database'] = ':memory:'; } // Connect if (is_resource($config['resource'])) { $connection = $config['resource']; } elseif (!isset($config['database'])) { throw new DriverException("No database file selected."); } elseif ($config['persistent']) { $connection = @sqlite_popen($config['database'], 0666, $error); } else { $connection = @sqlite_open($config['database'], 0666, $error); } if (!is_resource($connection)) { throw new DriverException("Opening database file '{$config['database']}' failed."); } $this->resource = $connection; $this->updateLimit = (bool) $config['updateLimit']; // Set charset $this->dbCharset = $config['dbcharset']; $this->charset = $config['charset']; if (strcasecmp($this->dbCharset, $this->charset) === 0) { $this->dbCharset = $this->charset = null; } $this->unbuffered = $config['unbuffered']; $this->persistent = $config['persistent']; }
/** * Creates connection to database. * @param array $config Configuration options * @throws DriverException */ public function connect(array $config) { Connection::alias($config, 'database', 'file'); Connection::alias($config, 'updateLimit', 'update_limit'); $defaults = array('memory' => false, 'resource' => null, 'updateLimit' => false, 'charset' => 'UTF-8', 'dbcharset' => 'UTF-8'); $config += $defaults; if ($config['memory']) { $config['database'] = ':memory:'; } // Connect if ($config['resource'] instanceof SQLite3) { $connection = $config['resource']; } elseif (!isset($config['database'])) { throw new DriverException("No database file selected."); } else { try { $connection = new SQLite3($config['database']); } catch (Exception $e) { throw new DriverException($e->getMessage(), $e->getCode()); } } if (!$connection instanceof SQLite3) { throw new DriverException("Opening database file '{$config['database']}' failed."); } $this->resource = $connection; $this->updateLimit = (bool) $config['updateLimit']; // Set charset $this->dbCharset = $config['dbcharset']; $this->charset = $config['charset']; if (strcasecmp($this->dbCharset, $this->charset) === 0) { $this->dbCharset = $this->charset = null; } }
/** * Creates connection to database. * @param array $config Configuration options * @throws DriverException */ public function connect(array $config) { Connection::alias($config, 'resource', 'pdo'); // Defaults $defaults = array('dsn' => null, 'resource' => null, 'username' => null, 'password' => null, 'options' => array()); $config += $defaults; // Connect if ($config['resource'] instanceof PDO) { $this->resource = $config['resource']; } else { try { $this->resource = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']); } catch (PDOException $e) { throw new DriverException($e->getMessage(), $e->getCode()); } } if (!$this->resource) { throw new DriverException('Connection failed.'); } $this->driverName = $this->resource->getAttribute(PDO::ATTR_DRIVER_NAME); }
public function testAlias() { $config = array('alias' => 'value'); Connection::alias($config, 'key', 'alias'); $this->assertEquals('value', $config['key']); }