summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2013-04-17 21:19:00 +0400
committerAndrew Dolgov <[email protected]>2013-04-17 21:19:00 +0400
commite441b5837b84f8313e506a3d1b087f269f4a9fb3 (patch)
tree5d7c131538588eebb3813a9f002542e7db072a55 /classes
parentd9c85e0f112034ca3e3f4d34213f6dcccf9d54e1 (diff)
initial
Diffstat (limited to 'classes')
-rw-r--r--classes/db.php39
-rw-r--r--classes/db/pdo.php87
2 files changed, 114 insertions, 12 deletions
diff --git a/classes/db.php b/classes/db.php
index c9d9ad5ea..86d2ab897 100644
--- a/classes/db.php
+++ b/classes/db.php
@@ -5,22 +5,37 @@ class Db implements IDb {
private $link;
private function __construct() {
- switch (DB_TYPE) {
- case "mysql":
- if (function_exists("mysqli_connect")) {
- $this->adapter = new Db_Mysqli();
- } else {
- $this->adapter = new Db_Mysql();
+
+ $er = error_reporting(E_ALL);
+
+ if (class_exists("PDO")) {
+ $this->adapter = new Db_PDO();
+ } else {
+ switch (DB_TYPE) {
+ case "mysql":
+ if (function_exists("mysqli_connect")) {
+ $this->adapter = new Db_Mysqli();
+ } else {
+ $this->adapter = new Db_Mysql();
+ }
+ break;
+ case "pgsql":
+ $this->adapter = new Db_Pgsql();
+ break;
+ default:
+ die("Unknown DB_TYPE: " . DB_TYPE);
}
- break;
- case "pgsql":
- $this->adapter = new Db_Pgsql();
- break;
- default:
- die("Unknown DB_TYPE: " . DB_TYPE);
}
+ if (!$this->adapter) die("Error initializing database adapter for " . DB_TYPE);
+
$this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : false);
+
+ if (!$this->link) {
+ die("Error connecting through adapter: " . $this->adapter->last_error());
+ }
+
+ error_reporting($er);
}
private function __clone() {
diff --git a/classes/db/pdo.php b/classes/db/pdo.php
new file mode 100644
index 000000000..aaac892c6
--- /dev/null
+++ b/classes/db/pdo.php
@@ -0,0 +1,87 @@
+<?php
+class Db_PDO implements IDb {
+ private $pdo;
+
+ function connect($host, $user, $pass, $db, $port) {
+ $connstr = DB_TYPE . ":host=$host;dbname=$db;charset=utf8";
+
+ try {
+ $this->pdo = new PDO($connstr, $user, $pass);
+ } 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 $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) {
+ $line = $this->fetch_assoc($result);
+
+ if ($line)
+ return $line[$param];
+ else
+ return null;
+
+ }
+
+ 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(" ", $pdo->errorInfo());
+ }
+}
+?>