/** * Establish a new connection to SQLite server. */ private function establishConnection() { try { if (!($this->link = new \SQLite3($this->config['database'], SQLITE3_OPEN_READWRITE))) { throw new RunException('Unable connect to database in <strong>' . $this->connection . '</strong> connection.'); } } catch (RunException $e) { RunException::outputError($e->getMessage()); } }
/** * Select the databse. */ private function selectDb($dbname) { if (is_null($this->link)) { $this->init(); } try { if (!@mysql_select_db($dbname, $this->link)) { throw new RunException('Unable to select database in <strong>' . $this->connection . '</strong> connection.'); } } catch (RunException $e) { RunException::outputError($e->getMessage()); } }
/** * Inital for all process * * @return void */ private function init() { if (is_null($this->link)) { $this->link = $this->establishConnection(); } try { if (!$this->link) { throw new RunException('Unable connect to database in <strong>' . $this->connection . '</strong> connection.'); } } catch (RunException $e) { RunException::outputError($e->getMessage()); } }
public function __construct($config, $connectionName) { /** * Makesure Mongo extension is enabled */ if (!class_exists('Mongo')) { throw new RunException('Mongo PECL extension that required by Mongodb Driver is not available.'); } $this->config = $config; $this->connection = $connectionName; /** * $this->config['options'] is the mongodb connection option. Eg: array('replicaSet' => true, 'connect' => false) */ try { parent::__construct($this->config['host'], $this->config['options']); } catch (\MongoConnectionException $e) { RunException::outputError('Unable connect to database in <strong>' . $connectionName . '</strong> connection.'); } }
public function __construct($config) { if (!extension_loaded('redis')) { die('Redis extension that required by Driver Redis is not available.'); } parent::__construct(); foreach ($config['server'] as $server) { if ($server['persistent']) { $this->pconnect($server['host'], $server['port'], $server['timeout']); } else { $this->connect($server['host'], $server['port'], $server['timeout']); } } try { $this->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); } catch (\RedisException $e) { Resources\RunException::outputError($e->getMessage()); } }
/** * Print the error * * @return string */ protected function printError() { if ($caller = \Resources\RunException::getErrorCaller(5)) { $error_str = sprintf('Database error %1$s for query %2$s made by %3$s', $this->lastError, $this->lastQuery, $caller); } else { $error_str = sprintf('Database error %1$s for query %2$s', $this->lastError, $this->lastQuery); } \Resources\RunException::outputError($error_str); }