Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
- class DatabaseMySQL {
- protected $link;
- private $hostname;
- private $username;
- private $password;
- private $database;
- private $onInit = array();
- private $queries = array();
- public $debug = true;
- // {{{ __construct()
- public function __construct($hostname, $username, $password, $database = null, array $onInit = array())
- {
- /** store values for lazy connect */
- $this->hostname = $hostname;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->onInit = $onInit;
- }
- // }}}
- // {{{ lazyConnect()
- private function lazyConnect()
- {
- /** use stored values to connect */
- $this->connect(
- $this->hostname
- , $this->username
- , $this->password
- , $this->database
- );
- return $this;
- }
- // }}}
- // {{{ connect()
- public function connect($hostname, $username, $password, $database = null)
- {
- $mysqli = @new mysqli($hostname, $username, $password, $database);
- if (mysqli_connect_errno()) {
- throw new DatabaseException(
- 'Could not connect to database on: ' . $hostname . PHP_EOL
- . 'Server returned: ' . mysqli_connect_error() . ' (Errno. ' . mysqli_connect_errno() . ')'
- );
- }
- $this->link = $mysqli;
- /** execute init statements if any */
- if (count($this->onInit)) {
- foreach ($this->onInit as $statement) {
- $this->doExec($statement);
- }
- }
- return $this;
- }
- // }}}
- // {{{ checkLink()
- public function checkLink()
- {
- if (is_null($this->link)) {
- return false;
- }
- return true;
- }
- // }}}
- // {{{ selectDatabase()
- public function selectDatabase($database)
- {
- if ($this->checkLink() === false) {
- $this->lazyConnect();
- }
- if ($this->link->select_db($database) === false) {
- throw new DatabaseException(
- 'Could not select database: ' . $database . PHP_EOL
- . 'Server returned: ' . $this->link->error . ' (Errno. ' . $this->link->errno . ')'
- );
- }
- $this->database = $database;
- return $this;
- }
- // }}}
- // {{{ getResult()
- private function getResult($statement, $database = null)
- {
- /** check if we have no link */
- if ($this->checkLink() === false) {
- /** lazy connect */
- $this->lazyConnect();
- }
- /** check if we are switching database */
- if (!is_null($database)) {
- /** store name of old one and switch to new */
- $old = $this->database;
- $this->selectDatabase($database);
- }
- /** check if we debug mode is on */
- if ($this->debug === true) {
- /** get result set within timers */
- $start = microtime(true);
- $result = $this->link->query($statement);
- $end = microtime(true);
- /** add to queries array */
- $this->queries[] = array(
- $this->clean($statement)
- , sprintf('%.8F', $end - $start)
- );
- /** if not, just get the result set */
- } else {
- $result = $this->link->query($statement);
- }
- /** switch back to old database if applicable */
- if (isset($old)) {
- $this->selectDatabase($old);
- }
- /** if result is boolean false then query failed */
- if ($result === false) {
- throw new DatabaseException(
- 'Invalid query: ' . $statement . PHP_EOL
- . 'Server returned: ' . $this->link->error . ' (Errno. ' . $this->link->errno . ')'
- );
- }
- /** return result set */
- return $result;
- }
- // }}}
- // {{{ doSelect()
- public function doSelect($statement, $database = null)
- {
- return $this->getResult($statement, $database);
- }
- // }}}
- // {{{ doInsert()
- public function doInsert($statement, $database = null)
- {
- $this->getResult($statement, $database);
- return $this->link->insert_id;
- }
- // }}}
- // {{{ doAlter()
- public function doAlter($statement, $database = null)
- {
- $this->getResult($statement, $database);
- return $this->link->affected_rows;
- }
- // }}}
- // {{{ doExec()
- public function doExec($statement, $database = null)
- {
- $this->getResult($statement, $database);
- return $this;
- }
- // }}}
- // {{{ escape()
- public function escape($string)
- {
- /** check if we have no link */
- if ($this->checkLink() === false) {
- /** lazy connect */
- $this->lazyConnect();
- }
- /** return the value properly escaped */
- return $this->link->real_escape_string($string);
- }
- // }}}
- // {{{ clean()
- public function clean($string)
- {
- /** strip all linebreaks and tabs, replace with spaces */
- $string = preg_replace('/[\r|\n|\t]/m', ' ', $string);
- /** strip all excess whitespace */
- $string = preg_replace('/ +/m', ' ', $string);
- /** return string */
- return trim($string);
- }
- // }}}
- // {{{ getQueries()
- public function getQueries()
- {
- return $this->queries;
- }
- // }}}
- }
Add Comment
Please, Sign In to add comment