2013-04-17 17:19:00 +00:00
|
|
|
<?php
|
|
|
|
class Db_PDO implements IDb {
|
|
|
|
private $pdo;
|
|
|
|
|
|
|
|
function connect($host, $user, $pass, $db, $port) {
|
2013-04-18 04:28:03 +00:00
|
|
|
$connstr = DB_TYPE . ":host=$host;dbname=$db";
|
|
|
|
|
|
|
|
if (DB_TYPE == "mysql") $connstr .= ";charset=utf8";
|
2013-04-17 17:19:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->pdo = new PDO($connstr, $user, $pass);
|
2013-04-18 04:28:03 +00:00
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$this->init();
|
2013-04-17 17:19:00 +00:00
|
|
|
} 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 {
|
2013-04-18 04:20:45 +00:00
|
|
|
return new Db_Stmt($this->pdo->query($query));
|
2013-04-17 17:19:00 +00:00
|
|
|
} 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) {
|
2013-04-18 04:20:45 +00:00
|
|
|
return $result->fetch_result($row, $param);
|
2013-04-17 17:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2013-05-07 07:35:10 +00:00
|
|
|
return join(" ", $this->pdo->errorInfo());
|
2013-04-17 17:19:00 +00:00
|
|
|
}
|
2013-04-18 04:28:03 +00:00
|
|
|
|
|
|
|
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");
|
2015-06-07 15:41:18 +00:00
|
|
|
return;
|
2013-04-18 04:28:03 +00:00
|
|
|
case "mysql":
|
|
|
|
$this->query("SET time_zone = '+0:0'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-17 17:19:00 +00:00
|
|
|
}
|
|
|
|
?>
|