From 9594791782bb9adbf29018c444ea427fbaeb5ee4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 17 Apr 2013 13:08:36 +0400 Subject: [PATCH] experimental singleton-based Db connection --- classes/db.php | 73 ++++++++++++++++++++++++++++++++++++++++++ classes/db/mysql.php | 59 ++++++++++++++++++++++++++++++++++ classes/db/pgsql.php | 73 ++++++++++++++++++++++++++++++++++++++++++ classes/idb.php | 13 ++++++++ classes/logger/sql.php | 20 ++++-------- 5 files changed, 225 insertions(+), 13 deletions(-) create mode 100644 classes/db.php create mode 100644 classes/db/mysql.php create mode 100644 classes/db/pgsql.php create mode 100644 classes/idb.php diff --git a/classes/db.php b/classes/db.php new file mode 100644 index 00000000..403cbc93 --- /dev/null +++ b/classes/db.php @@ -0,0 +1,73 @@ +adapter = new Db_Mysql(); + break; + case "pgsql": + $this->adapter = new Db_Pgsql(); + break; + default: + die("Unknown DB_TYPE: " . DB_TYPE); + } + + $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); + } + + private function __clone() { + // + } + + public static function get() { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance; + } + + static function quote($str){ + return("'$str'"); + } + + function connect($host, $user, $pass, $db, $port) { + //return $this->adapter->connect($host, $user, $pass, $db, $port); + } + + function escape_string($s, $strip_tags = true) { + return $this->adapter->escape_string($s, $strip_tags); + } + + function query($query, $die_on_error = true) { + return $this->adapter->query($query, $die_on_error); + } + + function fetch_assoc($result) { + return $this->adapter->fetch_assoc($result); + } + + function num_rows($result) { + return $this->adapter->num_rows($result); + } + + function fetch_result($result, $row, $param) { + return $this->adapter->fetch_result($result, $row, $param); + } + + function close() { + return $this->adapter->close(); + } + + function affected_rows($result) { + return $this->adapter->affected_rows($result); + } + + function last_error() { + return $this->adapter->last_error(); + } + +} +?> diff --git a/classes/db/mysql.php b/classes/db/mysql.php new file mode 100644 index 00000000..512ea389 --- /dev/null +++ b/classes/db/mysql.php @@ -0,0 +1,59 @@ +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 $query 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); + } + +} +?> diff --git a/classes/db/pgsql.php b/classes/db/pgsql.php new file mode 100644 index 00000000..87c2abef --- /dev/null +++ b/classes/db/pgsql.php @@ -0,0 +1,73 @@ + 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 $query 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); + } + +} +?> diff --git a/classes/idb.php b/classes/idb.php new file mode 100644 index 00000000..16f760bf --- /dev/null +++ b/classes/idb.php @@ -0,0 +1,13 @@ + diff --git a/classes/logger/sql.php b/classes/logger/sql.php index a478e892..c4584160 100644 --- a/classes/logger/sql.php +++ b/classes/logger/sql.php @@ -1,32 +1,26 @@ link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); - } - function log_error($errno, $errstr, $file, $line, $context) { if ($errno == E_NOTICE) return false; - if ($this->link) { - $errno = db_escape_string($this->link, $errno); - $errstr = db_escape_string($this->link, $errstr); - $file = db_escape_string($this->link, $file); - $line = db_escape_string($this->link, $line); + if (Db::get()) { + $errno = Db::get()->escape_string($errno); + $errstr = Db::get()->escape_string($errstr); + $file = Db::get()->escape_string($file); + $line = Db::get()->escape_string($line); $context = ''; // backtrace is a lot of data which is not really critical to store //$context = db_escape_string($this->link, serialize($context)); $owner_uid = $_SESSION["uid"] ? $_SESSION["uid"] : "NULL"; - $result = db_query($this->link, + $result = Db::get()->query( "INSERT INTO ttrss_error_log (errno, errstr, filename, lineno, context, owner_uid, created_at) VALUES ($errno, '$errstr', '$file', '$line', '$context', $owner_uid, NOW())"); - return db_affected_rows($this->link, $result) != 0; + return Db::get()->affected_rows($result) != 0; } return false; -- 2.39.5