1
0
mirror of https://git.tt-rss.org/git/tt-rss.git synced 2025-12-13 19:15:56 +00:00

experimental singleton-based Db connection

This commit is contained in:
Andrew Dolgov
2013-04-17 13:08:36 +04:00
parent 857efe49e6
commit 9594791782
5 changed files with 225 additions and 13 deletions

59
classes/db/mysql.php Normal file
View File

@@ -0,0 +1,59 @@
<?php
class Db_Mysql implements IDb {
private $link;
function connect($host, $user, $pass, $db, $port) {
$this->link = mysql_connect($host, $user, $pass);
if ($this->link) {
$result = mysql_select_db($db, $this->link);
if (!$result) {
die("Can't select DB: " . mysql_error($this->link));
}
return $this->link;
} else {
die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
}
}
function escape_string($s, $strip_tags = true) {
return mysql_real_escape_string($s, $this->link);
}
function query($query, $die_on_error = true) {
$result = mysql_query($query, $this->link);
if (!$result) {
$query = htmlspecialchars($query);
if ($die_on_error) {
die("Query <i>$query</i> failed: " . ($this->link ? mysql_error($link) : "No connection"));
}
}
return $result;
}
function fetch_assoc($result) {
return mysql_fetch_assoc($result);
}
function num_rows($result) {
return mysql_num_rows($result);
}
function fetch_result($result, $row, $param) {
return mysql_result($result, $row, $param);
}
function close() {
return mysql_close($this->link);
}
function affected_rows($result) {
return mysql_affected_rows($this->link);
}
function last_error() {
return mysql_affected_rows($this->link);
}
}
?>

73
classes/db/pgsql.php Normal file
View File

@@ -0,0 +1,73 @@
<?php
class Db_Pgsql implements IDb {
private $link;
function connect($host, $user, $pass, $db, $port) {
$string = "dbname=$db user=$user";
if ($pass) {
$string .= " password=$pass";
}
if ($host) {
$string .= " host=$host";
}
if (is_numeric($port) && $port > 0) {
$string = "$string port=" . $port;
}
$this->link = pg_connect($string);
if (!$this->link) {
die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
}
return $this->link;
}
function escape_string($s, $strip_tags = true) {
if ($strip_tags) $s = strip_tags($s);
return pg_escape_string($this->link, $s);
}
function query($query, $die_on_error = true) {
$result = pg_query($this->link, $query);
if (!$result) {
$query = htmlspecialchars($query); // just in case
if ($die_on_error) {
die("Query <i>$query</i> failed [$result]: " . ($this->link ? pg_last_error($this->link) : "No connection"));
}
}
return $result;
}
function fetch_assoc($result) {
return pg_fetch_assoc($result);
}
function num_rows($result) {
return pg_num_rows($result);
}
function fetch_result($result, $row, $param) {
return pg_fetch_result($result, $row, $param);
}
function close() {
return pg_close($this->link);
}
function affected_rows($result) {
return pg_affected_rows($result);
}
function last_error() {
return pg_last_error($this->link);
}
}
?>