add some starting pdo glue

This commit is contained in:
Andrew Dolgov 2017-11-30 10:47:42 +03:00
parent 1a4ff852de
commit 99bda9cc12
2 changed files with 13 additions and 105 deletions

View File

@ -3,15 +3,13 @@ class Db implements IDb {
private static $instance; private static $instance;
private $adapter; private $adapter;
private $link; private $link;
private $pdo;
private function __construct() { private function __construct() {
$er = error_reporting(E_ALL); $er = error_reporting(E_ALL);
if (defined('_ENABLE_PDO') && _ENABLE_PDO && class_exists("PDO")) { switch (DB_TYPE) {
$this->adapter = new Db_PDO();
} else {
switch (DB_TYPE) {
case "mysql": case "mysql":
$this->adapter = new Db_Mysqli(); $this->adapter = new Db_Mysqli();
break; break;
@ -20,7 +18,6 @@ class Db implements IDb {
break; break;
default: default:
die("Unknown DB_TYPE: " . DB_TYPE); die("Unknown DB_TYPE: " . DB_TYPE);
}
} }
if (!$this->adapter) { if (!$this->adapter) {
@ -28,6 +25,17 @@ class Db implements IDb {
exit(100); exit(100);
} }
$db_port = defined(DB_PORT) ? ';port='.DB_PORT : '';
$this->pdo = new PDO(DB_TYPE . ':dbname='.DB_NAME.';host='.DB_HOST.$db_port,
DB_USER,
DB_PASS);
if (!$this->pdo) {
print("Error connecting via PDO.");
exit(101);
}
$this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : ""); $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : "");
if (!$this->link) { if (!$this->link) {

View File

@ -1,100 +0,0 @@
<?php
class Db_PDO implements IDb {
private $pdo;
function connect($host, $user, $pass, $db, $port) {
$connstr = DB_TYPE . ":host=$host;dbname=$db";
if (DB_TYPE == "mysql") $connstr .= ";charset=utf8";
try {
$this->pdo = new PDO($connstr, $user, $pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->init();
} catch (PDOException $e) {
die($e->getMessage());
}
return $this->pdo;
}
function escape_string($s, $strip_tags = true) {
if ($strip_tags) $s = strip_tags($s);
$qs = $this->pdo->quote($s);
return mb_substr($qs, 1, mb_strlen($qs)-2);
}
function query($query, $die_on_error = true) {
try {
return new Db_Stmt($this->pdo->query($query));
} catch (PDOException $e) {
user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING);
}
}
function fetch_assoc($result) {
try {
if ($result) {
return $result->fetch();
} else {
return null;
}
} catch (PDOException $e) {
user_error($e->getMessage(), E_USER_WARNING);
}
}
function num_rows($result) {
try {
if ($result) {
return $result->rowCount();
} else {
return false;
}
} catch (PDOException $e) {
user_error($e->getMessage(), E_USER_WARNING);
}
}
function fetch_result($result, $row, $param) {
return $result->fetch_result($row, $param);
}
function close() {
$this->pdo = null;
}
function affected_rows($result) {
try {
if ($result) {
return $result->rowCount();
} else {
return null;
}
} catch (PDOException $e) {
user_error($e->getMessage(), E_USER_WARNING);
}
}
function last_error() {
return join(" ", $this->pdo->errorInfo());
}
function init() {
switch (DB_TYPE) {
case "pgsql":
$this->query("set client_encoding = 'UTF-8'");
$this->query("set datestyle = 'ISO, european'");
$this->query("set TIME ZONE 0");
return;
case "mysql":
$this->query("SET time_zone = '+0:0'");
return;
}
return true;
}
}