/** * Constructs a new connection * @param zibo\library\database\Dsn $dsn connection parameters * @return null */ public function __construct(Dsn $dsn) { if ($dsn->getProtocol() != self::PROTOCOL) { throw new SqliteException('Provided dsn does not have the sqlite protocol'); } parent::__construct($dsn); }
/** * Constructs a new connection * @param zibo\library\database\Dsn $dsn connection parameters * @return null */ public function __construct(Dsn $dsn) { if ($dsn->getProtocol() != self::PROTOCOL) { throw new MysqlException('Provided dsn does not have the mysql protocol'); } if (!function_exists('mysql_connect')) { throw new MysqlException('Could not create a MysqlDriver instance. Your PHP installation does not support MySQL, please install the MySQL extension.'); } parent::__construct($dsn); $this->isTransactionStarted = false; }
public function testConstructWithSqliteDatabase() { $dsn = new Dsn('sqlite:///var/lib/sqlite/file.db'); $this->assertEquals('sqlite', $dsn->getProtocol()); $this->assertEquals('', $dsn->getHost()); $this->assertEquals('', $dsn->getPort()); $this->assertEquals('', $dsn->getUsername()); $this->assertEquals('', $dsn->getPassword()); $this->assertEquals('file.db', $dsn->getDatabase()); $this->assertEquals('/var/lib/sqlite/file.db', $dsn->getPath()); }
/** * Registers a connection in the manager * @param string $name Name of the connection * @param Dsn $dsn DSN connection properties * @return null * @throws zibo\library\database\exception\DatabaseException when the name is invalid or already registered and connected * @throws zibo\library\database\exception\DatabaseException when the protocol has no driver available */ public function registerConnection($name, Dsn $dsn) { if (String::isEmpty($name)) { throw new DatabaseException('Provided database name is empty'); } $protocol = $dsn->getProtocol(); if (!isset($this->drivers[$protocol])) { throw new DatabaseException('Protocol ' . $protocol . ' has no database driver available'); } if (isset($this->connections[$name]) && $this->connections[$name]->isConnected()) { throw new DatabaseException('Database ' . $name . ' is already registered and connected. Disconnect the connection first'); } $this->connections[$name] = new $this->drivers[$protocol]($dsn); if ($this->defaultConnectionName == null) { $this->setDefaultConnectionName($name); } }