diff options
Diffstat (limited to 'public/system/library/db')
-rw-r--r-- | public/system/library/db/mpdo.php | 111 | ||||
-rw-r--r-- | public/system/library/db/mssql.php | 79 | ||||
-rw-r--r-- | public/system/library/db/mysql.php | 90 | ||||
-rw-r--r-- | public/system/library/db/mysqli.php | 63 | ||||
-rw-r--r-- | public/system/library/db/postgre.php | 68 |
5 files changed, 411 insertions, 0 deletions
diff --git a/public/system/library/db/mpdo.php b/public/system/library/db/mpdo.php new file mode 100644 index 0000000..1cdb0ea --- /dev/null +++ b/public/system/library/db/mpdo.php @@ -0,0 +1,111 @@ +<?php +namespace DB; +final class mPDO { + private $connection = null; + private $statement = null; + + public function __construct($hostname, $username, $password, $database, $port = '3306') { + try { + $this->connection = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true)); + } catch(\PDOException $e) { + throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); + } + + $this->connection->exec("SET NAMES 'utf8'"); + $this->connection->exec("SET CHARACTER SET utf8"); + $this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8"); + $this->connection->exec("SET SQL_MODE = ''"); + } + + public function prepare($sql) { + $this->statement = $this->connection->prepare($sql); + } + + public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) { + if ($length) { + $this->statement->bindParam($parameter, $variable, $data_type, $length); + } else { + $this->statement->bindParam($parameter, $variable, $data_type); + } + } + + public function execute() { + try { + if ($this->statement && $this->statement->execute()) { + $data = array(); + + while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { + $data[] = $row; + } + + $result = new \stdClass(); + $result->row = (isset($data[0])) ? $data[0] : array(); + $result->rows = $data; + $result->num_rows = $this->statement->rowCount(); + } + } catch(\PDOException $e) { + throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); + } + } + + public function query($sql, $params = array()) { + $this->statement = $this->connection->prepare($sql); + + $result = false; + + try { + if ($this->statement && $this->statement->execute($params)) { + $data = array(); + + while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { + $data[] = $row; + } + + $result = new \stdClass(); + $result->row = (isset($data[0]) ? $data[0] : array()); + $result->rows = $data; + $result->num_rows = $this->statement->rowCount(); + } + } catch (\PDOException $e) { + throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql); + } + + if ($result) { + return $result; + } else { + $result = new \stdClass(); + $result->row = array(); + $result->rows = array(); + $result->num_rows = 0; + return $result; + } + } + + public function escape($value) { + return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value); + } + + public function countAffected() { + if ($this->statement) { + return $this->statement->rowCount(); + } else { + return 0; + } + } + + public function getLastId() { + return $this->connection->lastInsertId(); + } + + public function isConnected() { + if ($this->connection) { + return true; + } else { + return false; + } + } + + public function __destruct() { + $this->connection = null; + } +} diff --git a/public/system/library/db/mssql.php b/public/system/library/db/mssql.php new file mode 100644 index 0000000..06d3c78 --- /dev/null +++ b/public/system/library/db/mssql.php @@ -0,0 +1,79 @@ +<?php +namespace DB; +final class MSSQL { + private $connection; + + public function __construct($hostname, $username, $password, $database, $port = '1433') { + if (!$this->connection = mssql_connect($hostname. ':' . $port, $username, $password)) { + throw new \Exception('Error: Could not make a database connection using ' . $username . '@' . $hostname); + } + + if (!mssql_select_db($database, $this->link)) { + throw new \Exception('Error: Could not connect to database ' . $database); + } + + mssql_query("SET NAMES 'utf8'", $this->connection); + mssql_query("SET CHARACTER SET utf8", $this->connection); + } + + public function query($sql) { + $resource = mssql_query($sql, $this->connection); + + if ($resource) { + if (is_resource($resource)) { + $i = 0; + + $data = array(); + + while ($result = mssql_fetch_assoc($resource)) { + $data[$i] = $result; + + $i++; + } + + mssql_free_result($resource); + + $query = new \stdClass(); + $query->row = isset($data[0]) ? $data[0] : array(); + $query->rows = $data; + $query->num_rows = $i; + + unset($data); + + return $query; + } else { + return true; + } + } else { + throw new \Exception('Error: ' . mssql_get_last_message($this->connection) . '<br />' . $sql); + } + } + + public function escape($value) { + $unpacked = unpack('H*hex', $value); + + return '0x' . $unpacked['hex']; + } + + public function countAffected() { + return mssql_rows_affected($this->connection); + } + + public function getLastId() { + $last_id = false; + + $resource = mssql_query("SELECT @@identity AS id", $this->connection); + + if ($row = mssql_fetch_row($resource)) { + $last_id = trim($row[0]); + } + + mssql_free_result($resource); + + return $last_id; + } + + public function __destruct() { + mssql_close($this->connection); + } +}
\ No newline at end of file diff --git a/public/system/library/db/mysql.php b/public/system/library/db/mysql.php new file mode 100644 index 0000000..8f6c259 --- /dev/null +++ b/public/system/library/db/mysql.php @@ -0,0 +1,90 @@ +<?php +namespace DB; +final class MySQL { + private $connection; + + public function __construct($hostname, $username, $password, $database, $port = '3306') { + if (!$this->connection = mysql_connect($hostname . ':' . $port, $username, $password)) { + trigger_error('Error: Could not make a database link using ' . $username . '@' . $hostname); + exit(); + } + + if (!mysql_select_db($database, $this->connection)) { + throw new \Exception('Error: Could not connect to database ' . $database); + } + + mysql_query("SET NAMES 'utf8'", $this->connection); + mysql_query("SET CHARACTER SET utf8", $this->connection); + mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection); + mysql_query("SET SQL_MODE = ''", $this->connection); + } + + public function query($sql) { + if ($this->connection) { + $resource = mysql_query($sql, $this->connection); + + if ($resource) { + if (is_resource($resource)) { + $i = 0; + + $data = array(); + + while ($result = mysql_fetch_assoc($resource)) { + $data[$i] = $result; + + $i++; + } + + mysql_free_result($resource); + + $query = new \stdClass(); + $query->row = isset($data[0]) ? $data[0] : array(); + $query->rows = $data; + $query->num_rows = $i; + + unset($data); + + return $query; + } else { + return true; + } + } else { + $trace = debug_backtrace(); + + throw new \Exception('Error: ' . mysql_error($this->connection) . '<br />Error No: ' . mysql_errno($this->connection) . '<br /> Error in: <b>' . $trace[1]['file'] . '</b> line <b>' . $trace[1]['line'] . '</b><br />' . $sql); + } + } + } + + public function escape($value) { + if ($this->connection) { + return mysql_real_escape_string($value, $this->connection); + } + } + + public function countAffected() { + if ($this->connection) { + return mysql_affected_rows($this->connection); + } + } + + public function getLastId() { + if ($this->connection) { + return mysql_insert_id($this->connection); + } + } + + public function isConnected() { + if ($this->connection) { + return true; + } else { + return false; + } + } + + public function __destruct() { + if ($this->connection) { + mysql_close($this->connection); + } + } +}
\ No newline at end of file diff --git a/public/system/library/db/mysqli.php b/public/system/library/db/mysqli.php new file mode 100644 index 0000000..2e40109 --- /dev/null +++ b/public/system/library/db/mysqli.php @@ -0,0 +1,63 @@ +<?php +namespace DB; +final class MySQLi { + private $connection; + + public function __construct($hostname, $username, $password, $database, $port = '3306') { + $this->connection = new \mysqli($hostname, $username, $password, $database, $port); + + if ($this->connection->connect_error) { + throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno); + } + + $this->connection->set_charset("utf8"); + $this->connection->query("SET SQL_MODE = ''"); + } + + public function query($sql) { + $query = $this->connection->query($sql); + + if (!$this->connection->errno) { + if ($query instanceof \mysqli_result) { + $data = array(); + + while ($row = $query->fetch_assoc()) { + $data[] = $row; + } + + $result = new \stdClass(); + $result->num_rows = $query->num_rows; + $result->row = isset($data[0]) ? $data[0] : array(); + $result->rows = $data; + + $query->close(); + + return $result; + } else { + return true; + } + } else { + throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno . '<br />' . $sql); + } + } + + public function escape($value) { + return $this->connection->real_escape_string($value); + } + + public function countAffected() { + return $this->connection->affected_rows; + } + + public function getLastId() { + return $this->connection->insert_id; + } + + public function connected() { + return $this->connection->ping(); + } + + public function __destruct() { + $this->connection->close(); + } +} diff --git a/public/system/library/db/postgre.php b/public/system/library/db/postgre.php new file mode 100644 index 0000000..9d86b1e --- /dev/null +++ b/public/system/library/db/postgre.php @@ -0,0 +1,68 @@ +<?php +namespace DB; +final class Postgre { + private $link; + + public function __construct($hostname, $username, $password, $database, $port = '5432') { + if (!$this->link = pg_connect('hostname=' . $hostname . ' port=' . $port . ' username=' . $username . ' password=' . $password . ' database=' . $database)) { + throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname); + } + + if (!mysql_select_db($database, $this->link)) { + throw new \Exception('Error: Could not connect to database ' . $database); + } + + pg_query($this->link, "SET CLIENT_ENCODING TO 'UTF8'"); + } + + public function query($sql) { + $resource = pg_query($this->link, $sql); + + if ($resource) { + if (is_resource($resource)) { + $i = 0; + + $data = array(); + + while ($result = pg_fetch_assoc($resource)) { + $data[$i] = $result; + + $i++; + } + + pg_free_result($resource); + + $query = new \stdClass(); + $query->row = isset($data[0]) ? $data[0] : array(); + $query->rows = $data; + $query->num_rows = $i; + + unset($data); + + return $query; + } else { + return true; + } + } else { + throw new \Exception('Error: ' . pg_result_error($this->link) . '<br />' . $sql); + } + } + + public function escape($value) { + return pg_escape_string($this->link, $value); + } + + public function countAffected() { + return pg_affected_rows($this->link); + } + + public function getLastId() { + $query = $this->query("SELECT LASTVAL() AS `id`"); + + return $query->row['id']; + } + + public function __destruct() { + pg_close($this->link); + } +}
\ No newline at end of file |