@ 2016-01-08T18:25:41Z <?php
//==============================================
// exception
//==============================================
class PDOException extends Exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": SQLSTATE [{$this->code}]: {$this->message}\n";
}
}
//==============================================
// class
//==============================================
abstract class PDOWrapper {
//==============================================
// variables
//==============================================
private $exception = Null;
private $username = Null;
private $password = Null;
private $dsn = Null;
private $client = Null;
private $pdoStatement = Null;
//==============================================
// constructor
//==============================================
final public function __construct() {
$this->reset();
}
//==============================================
// overrides
//==============================================
abstract protected function getConfigFile();
//==============================================
// main
//==============================================
final public function prepare($query) {
if ($this->ready()) {
$this->pdoStatement = $this->client->prepare($query);
}
return $this->checkError();
}
final public function execute() {
if ($this->ready() && $this->pdoStatement) {
return $this->pdoStatement->execute();
}
return $this->checkError();
}
final public function fetch() {
if ($this->ready() && $this->pdoStatement) {
return $this->pdoStatement->fetch();
}
return False;
}
final public function fetchAll() {
if ($this->ready() && $this->pdoStatement) {
return $this->pdoStatement->fetchAll();
}
return False;
}
final public function query($query) {
if ($this->ready()) {
$this->pdoStatement = $this->client->query($query);
}
return $this->checkError();
}
final public function ready() {
return !$this->hasException();
}
final public function reset() {
$exception = Null;
try {
$this->loadConfigFile($this->getConfigFile()); //may throw error (see __get())
$this->client = new PDO($this->dsn, $this->username, $this->password);
} catch (Exception $e) {
$this->exception = $e;
}
}
//==============================================
// accessing
//==============================================
final public function hasException() {
return isset($this->exception);
}
final public function getException() {
return $this->exception;
}
final public function getRowCount() {
if ($this->pdoStatement && !empty($this->pdoStatement->errorInfo()[2])) {
return $this->pdoStatement->rowCount();
}
return False;
}
final protected function setException($e) {
$this->exception = $e;
}
//==============================================
// utility
//==============================================
final private function checkError() { //pdoStatement->errorInfo() always returns array
if ($this->pdoStatement == False) {
$this->exception = new PDOException('Connection Error - Please Check Config File and Network');
return False;
}
if (!empty($this->pdoStatement->errorInfo()[2])) {
$msg = $this->client->errorInfo()[2];
$errCode = $this->client->errorCode();
$this->exception = new PDOException($msg, $code = $errCode);
return False;
}
return True;
}
final private function loadConfigFile($filepath) {
$string = @file_get_contents($filepath);
if ($string === False) {
throw new FileIOException($filepath);
}
$json = @json_decode($string, true);
if ($json === False || !$json['USERNAME'] || !$json['PASSWORD'] || !$json['DSN']) {
throw new InvalidFileFormatException($filepath . ' : ' . json_last_error_msg(), $code = json_last_error());
}
$this->username = $json['USERNAME'];
$this->password = $json['PASSWORD'];
$this->dsn = $json['DSN'];
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Warning: Private methods cannot be final as they are never overridden by other classes in /in/g99DI on line 146
Warning: Private methods cannot be final as they are never overridden by other classes in /in/g99DI on line 165
Fatal error: Cannot declare class PDOException, because the name is already in use in /in/g99DI on line 7
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
56.37 ms | 401 KiB | 8 Q