<?php
/**
* This file is part of Soloproyectos common library.
*
* @author Gonzalo Chumillas <gchumillas@email.com>
* @license https://github.com/soloproyectos/php.common-libs/blob/master/LICENSE BSD 2-Clause License
* @link https://github.com/soloproyectos/php.common-libs
*/
namespace com\soloproyectos\common\db;
use \Exception;
use \Mysqli;
use com\soloproyectos\common\debug\DebugCapable;
/**
* Class DbConnector.
*
* @package Db
* @author Gonzalo Chumillas <gchumillas@email.com>
* @license https://github.com/soloproyectos/php.common-libs/blob/master/LICENSE BSD 2-Clause License
* @link https://github.com/soloproyectos/php.common-libs
*/
class DbConnector
{
use DebugCapable;
/**
* Database connection.
* @var Mysqli
*/
private $_conn;
/**
* Constructor.
*
* @param string $dbname Database name
* @param string $username User name (not required)
* @param string $password Password (not required)
* @param string $server Server machine (default is 'localhost')
*/
public function __construct($dbname, $username = "", $password = "", $server = "localhost")
{
$this->_conn = new Mysqli($server, $username, $password);
$this->_conn->select_db($dbname);
if ($this->_conn->errno > 0) {
throw new Exception(
"Failed to connect to MySQL: ({$this->_conn->errno}) {$this->_conn->error}"
);
}
}
/**
* Debug handler.
*
* This function implements DebugCapable::debugger.
*
* @param string $text Text to debug
*
* @return void
*/
protected function debugger($text)
{
echo "$text\n";
}
/**
* Escapes and quotes a value to be used in a SQL statement.
*
* @param string|null $value Value
*
* @return string
*/
public function quote($value)
{
return is_null($value) ? "null" : "'" . mysqli_real_escape_string($this->_conn, $value) . "'";
}
/**
* Executes an SQL statement and returns the number of affected rows.
*
* @param string $sql SQL statement
* @param array $arguments List of strings (not required)
*
* @return int
*/
public function exec($sql, $arguments = array())
{
$result = $this->_exec($sql, $arguments);
return $this->_conn->affected_rows;
}
/**
* Executes a DLL statement (select, show, describe, etc...) and returns all rows.
*
* @param string $sql SQL statement
* @param array $arguments List of arguments (not required)
*
* @return array
*/
public function query($sql, $arguments = array())
{
$ret = array();
$result = $this->_exec($sql, $arguments);
// fetches all rows
while ($row = $result->fetch_array()) {
array_push($ret, $row);
}
$result->close();
return $ret;
}
/**
* Gets the last inserted id.
*
* @return string
*/
public function getLastInsertId()
{
$ds = new DbDataSource($this, "select last_insert_id()");
return "" . $ds[0];
}
/**
* Closes the database connection.
*
* @return void
*/
public function close()
{
$this->_conn->close();
}
/**
* Executes an SQL statement.
*
* @param string $sql SQL statement
* @param array $arguments List of arguments (not required)
*
* @return Mysqli_result
*/
private function _exec($sql, $arguments = array())
{
$sql = $this->_replaceArguments($sql, $arguments);
$this->debug($sql);
// executes the statement
$result = $this->_conn->query($sql);
if ($this->_conn->errno > 0) {
throw new Exception(
"Failed to execute the statement: ({$this->_conn->errno}) {$this->_conn->error}"
);
}
return $result;
}
/**
* Replaces arguments in an SQL statement.
*
* @param string $sql SQL statement
* @param array $arguments List of arguments
*
* @return string
*/
private function _replaceArguments($sql, $arguments)
{
$i = 0;
return preg_replace_callback(
'/\?/',
function ($matches) use (&$i, $arguments) {
return $i < count($arguments)
? $this->quote($arguments[$i++])
: $matches[0];
},
$sql
);
}
}
|