2015-01-23 12:38:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Db;
|
|
|
|
|
2016-09-27 06:20:02 +00:00
|
|
|
use Vn\Lib\Type;
|
2015-01-23 12:38:29 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
class Connection {
|
2015-01-23 12:38:29 +00:00
|
|
|
private $conn = NULL;
|
|
|
|
private $isOpen = FALSE;
|
2018-05-23 09:08:11 +00:00
|
|
|
var $enableDebug = FALSE;
|
2015-01-23 12:38:29 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
function __construct() {
|
|
|
|
$this->initHandler();
|
2016-08-31 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 12:38:29 +00:00
|
|
|
/**
|
|
|
|
* Opens a new connection to the database.
|
|
|
|
*
|
|
|
|
* @param string $host The host name
|
|
|
|
* @param string $user The user name to authenticate
|
|
|
|
* @param string $pass The user password
|
|
|
|
* @param string $name The default schema name
|
|
|
|
*
|
|
|
|
* @return boolean %TRUE on success, %FALSE otherwise
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function open($host, $user, $pass, $name, $port = NULL) {
|
|
|
|
$conn = $this->initHandler();
|
|
|
|
$conn->options(MYSQLI_OPT_LOCAL_INFILE, TRUE);
|
|
|
|
$conn->real_connect($host, $user, $pass, $name, $port);
|
2016-08-23 13:55:33 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (mysqli_connect_errno()) {
|
|
|
|
sleep(3);
|
|
|
|
throw new Exception(mysqli_connect_errno(), mysqli_connect_error());
|
2015-01-23 12:38:29 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->isOpen = TRUE;
|
2018-05-23 09:08:11 +00:00
|
|
|
$this->query('SET CHARACTER SET utf8');
|
2015-01-23 12:38:29 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the current connection, if it's closed does nothing.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function close() {
|
|
|
|
if ($this->isOpen) {
|
|
|
|
$this->conn->close();
|
2016-08-31 11:54:12 +00:00
|
|
|
$this->conn = NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-23 12:38:29 +00:00
|
|
|
$this->isOpen = FALSE;
|
|
|
|
}
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2016-08-31 11:54:12 +00:00
|
|
|
/**
|
|
|
|
* Initializes the internal database connection handler.
|
|
|
|
*
|
|
|
|
* @return Objetct The connection handler
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function initHandler() {
|
2016-08-31 11:54:12 +00:00
|
|
|
if (!$this->conn)
|
2018-05-23 09:08:11 +00:00
|
|
|
$this->conn = new \mysqli();
|
2016-08-31 11:54:12 +00:00
|
|
|
|
|
|
|
return $this->conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the internal database connection handler. This function shoud be
|
|
|
|
* used to set options for specific databases that could not be set using
|
2016-09-27 06:20:02 +00:00
|
|
|
* the Connection class methods.
|
2016-08-31 11:54:12 +00:00
|
|
|
*
|
|
|
|
* @return Objetct The connection handler
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getHandler() {
|
2016-08-31 11:54:12 +00:00
|
|
|
return $this->conn;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:40:46 +00:00
|
|
|
/**
|
|
|
|
* Changes the default schema for the current connection.
|
|
|
|
*
|
|
|
|
* @param string $schema The schema name
|
|
|
|
* @return boolean %TRUE if success, %FALSE otherwise
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function selectDb($dbName) {
|
|
|
|
return $this->conn->select_db($dbName);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the query and gets the result.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed The value or %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function query($query, $params = NULL) {
|
|
|
|
$result = $this->conn->query($this->renderDebug($query, $params));
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
if (!$result)
|
2018-05-23 09:08:11 +00:00
|
|
|
$this->checkError();
|
2016-05-09 07:40:46 +00:00
|
|
|
else
|
2018-05-23 09:08:11 +00:00
|
|
|
while ($this->moreResults())
|
|
|
|
$this->nextResult();
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the connection is open.
|
|
|
|
*
|
|
|
|
* @return boolean %TRUE if connection is open, %FALSE otherwise
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function isOpen() {
|
2015-01-23 12:38:29 +00:00
|
|
|
return $this->isOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-09 07:40:46 +00:00
|
|
|
* Gets the first row value of the first column from a result.
|
2015-01-23 12:38:29 +00:00
|
|
|
*
|
2016-05-09 07:40:46 +00:00
|
|
|
* @param resource $result The database result
|
2015-01-23 12:38:29 +00:00
|
|
|
*
|
|
|
|
* @return mixed[] An associative array with the first row, %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getRowFromResult($result) {
|
|
|
|
if ($result) {
|
|
|
|
$row = $result->fetch_assoc();
|
|
|
|
$result->free();
|
2015-01-23 12:38:29 +00:00
|
|
|
return $row;
|
2015-08-26 12:24:45 +00:00
|
|
|
}
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-30 13:27:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the first row value of the first column from a result.
|
|
|
|
*
|
|
|
|
* @param resource $result The database result
|
|
|
|
*
|
|
|
|
* @return object An object with the first row, %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getObjectFromResult($result) {
|
|
|
|
if ($result) {
|
|
|
|
$row = $result->fetch_object();
|
|
|
|
$result->free();
|
2017-05-30 13:27:25 +00:00
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2015-01-23 12:38:29 +00:00
|
|
|
/**
|
2016-05-09 07:40:46 +00:00
|
|
|
* Gets the first row value of the first column from a result.
|
2015-01-23 12:38:29 +00:00
|
|
|
*
|
2016-05-09 07:40:46 +00:00
|
|
|
* @param resource $result The database result
|
2015-01-23 12:38:29 +00:00
|
|
|
*
|
2017-05-30 13:27:25 +00:00
|
|
|
* @return mixed The value or %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getValueFromResult($result) {
|
2015-08-26 12:24:45 +00:00
|
|
|
$value = NULL;
|
2015-01-23 12:38:29 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if ($result) {
|
|
|
|
$row = $result->fetch_row();
|
2015-08-26 12:24:45 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if ($row && count($row) > 0)
|
2015-08-26 12:24:45 +00:00
|
|
|
$value = $row[0];
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
$result->free();
|
2015-08-26 12:24:45 +00:00
|
|
|
}
|
2015-01-23 12:38:29 +00:00
|
|
|
|
2016-05-09 07:40:46 +00:00
|
|
|
return $value;
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-09 07:40:46 +00:00
|
|
|
* Executes the query and gets it's first row.
|
2015-01-23 12:38:29 +00:00
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
2016-05-09 07:40:46 +00:00
|
|
|
* @return mixed[] An associative array with the first row, %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getRow($query, $params = NULL) {
|
|
|
|
$result = $this->query($query, $params);
|
|
|
|
return $this->getRowFromResult($result);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 13:27:25 +00:00
|
|
|
/**
|
|
|
|
* Executes the query and gets it's first row.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return object An object with the first row, %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getObject($query, $params = NULL) {
|
|
|
|
$result = $this->query($query, $params);
|
|
|
|
return $this->getObjectFromResult($result);
|
2017-05-30 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 07:40:46 +00:00
|
|
|
/**
|
|
|
|
* Executes the query and gets the first value of the first row.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed The value or %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getValue($query, $params = NULL) {
|
|
|
|
$result = $this->query($query, $params);
|
|
|
|
return $this->getValueFromResult($result);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a query from a file and renders it.
|
|
|
|
*
|
|
|
|
* @param string $file The file path
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed The query string
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function loadFromFile($file, $params = NULL) {
|
|
|
|
$query = file_get_contents($file .'.sql');
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
if ($query === FALSE)
|
2018-05-23 09:08:11 +00:00
|
|
|
throw new Exception(NULL, 'Can not load query from file');
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
return $this->render($query, $params);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the query stored in the specified file and gets the result.
|
|
|
|
*
|
|
|
|
* @param string $file The file path
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
2015-01-23 12:38:29 +00:00
|
|
|
* @return mixed The value or %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function queryFromFile($file, $params = NULL) {
|
|
|
|
$query = $this->loadFromFile($file, $params);
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
if ($query)
|
2018-05-23 09:08:11 +00:00
|
|
|
return $this->query($query);
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-09 07:40:46 +00:00
|
|
|
* Executes the file query and gets it's first row.
|
2015-01-23 12:38:29 +00:00
|
|
|
*
|
2016-05-09 07:40:46 +00:00
|
|
|
* @param string $file The file path
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed[] An associative array with the first row, %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getRowFromFile($file, $params = NULL) {
|
|
|
|
$result = $this->queryFromFile($file, $params);
|
|
|
|
return $this->getRowFromResult($result);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the file query and gets the first value of the first row.
|
|
|
|
*
|
|
|
|
* @param string $file The file path
|
2015-01-23 12:38:29 +00:00
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed The value or %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getValueFromFile($file, $params = NULL) {
|
|
|
|
$result = $this->queryFromFile($file, $params);
|
|
|
|
return $this->getValueFromResult($result);
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute multiple queries separated by semicolons.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed The value or %NULL if error
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function multiQuery($query, $params = NULL) {
|
|
|
|
$success = $this->conn->multi_query($this->renderDebug($query, $params));
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
if (!$success)
|
2018-05-23 09:08:11 +00:00
|
|
|
$this->checkError();
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2016-01-08 10:53:44 +00:00
|
|
|
/**
|
|
|
|
* Prepare an SQL statement for execution.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
*
|
|
|
|
* @return mixed The statement object or %FALSE if an error occurred
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function prepare($query) {
|
|
|
|
return $this->conn->prepare($query);
|
2016-01-08 10:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
function storeResult() {
|
|
|
|
$result = $this->conn->store_result();
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
if (!$result)
|
2018-05-23 09:08:11 +00:00
|
|
|
$this->checkError();
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
function moreResults() {
|
|
|
|
return $this->conn->more_results();
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
function nextResult() {
|
|
|
|
$hasNext = $this->conn->next_result();
|
|
|
|
$this->checkError();
|
2016-05-09 07:40:46 +00:00
|
|
|
return $hasNext;
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 18:04:47 +00:00
|
|
|
/**
|
|
|
|
* Check if there has been an error in the last query or multiquery,
|
2016-09-27 06:20:02 +00:00
|
|
|
* throwing a @Exception exception if any.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function checkError() {
|
2021-03-31 10:50:17 +00:00
|
|
|
if ($this->conn->errno) {
|
|
|
|
if ($this->conn->errno != 1644)
|
|
|
|
throw new Exception($this->conn->errno, $this->conn->error);
|
|
|
|
else
|
2021-03-31 10:56:45 +00:00
|
|
|
throw new \Vn\Lib\UserException($this->conn->error, $this->conn->errno);
|
2021-03-31 10:50:17 +00:00
|
|
|
}
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
2021-03-31 10:50:17 +00:00
|
|
|
|
2015-08-17 18:04:47 +00:00
|
|
|
/**
|
|
|
|
* Check if there have been warnings in the last query.
|
|
|
|
*
|
|
|
|
* @return boolean %TRUE if there have been warnings %FALSE otherwise
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function checkWarnings() {
|
2015-08-17 18:04:47 +00:00
|
|
|
return $this->conn->warning_count > 0;
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
2015-02-18 16:13:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an SQL string using the given parameters.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL string
|
|
|
|
* @param mixed[] $paramsMap The query parameters
|
|
|
|
*
|
2016-05-09 07:40:46 +00:00
|
|
|
* @return string The rendered SQL string
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function render($query, $paramsMap = NULL) {
|
|
|
|
if (isset($paramsMap) && is_array($paramsMap) && count($paramsMap) > 0) {
|
2015-02-18 16:13:23 +00:00
|
|
|
$i = 0;
|
|
|
|
$params = [];
|
|
|
|
|
2018-06-06 11:11:06 +00:00
|
|
|
foreach ($paramsMap as $key => $value)
|
2018-05-23 09:08:11 +00:00
|
|
|
$params[$key] = $this->renderValue($value);
|
2015-02-18 16:13:23 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
$replaceFunc = function($matches) use(&$params, &$i) {
|
|
|
|
$key = substr($matches[0], 1);
|
2015-02-18 16:13:23 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (strlen($key) == 0)
|
2015-02-18 16:13:23 +00:00
|
|
|
$key = $i++;
|
2018-05-23 09:08:11 +00:00
|
|
|
if (isset($params[$key]))
|
2015-02-18 16:13:23 +00:00
|
|
|
return $params[$key];
|
|
|
|
|
|
|
|
return '#'. $key;
|
|
|
|
};
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
return preg_replace_callback('/#\w*/', $replaceFunc, $query);
|
2015-02-18 16:13:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:40:46 +00:00
|
|
|
/**
|
|
|
|
* Gets an SQL respresentation from a PHP value.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value
|
|
|
|
*
|
|
|
|
* @return string The SQL value
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function renderValue($value) {
|
2015-02-18 16:13:23 +00:00
|
|
|
if ($value !== NULL)
|
2018-05-23 09:08:11 +00:00
|
|
|
switch (Type::get($value)) {
|
2016-09-27 06:20:02 +00:00
|
|
|
case Type::BOOLEAN:
|
2018-05-23 09:08:11 +00:00
|
|
|
return($value) ? 'TRUE' : 'FALSE';
|
2016-09-27 06:20:02 +00:00
|
|
|
case Type::STRING:
|
2018-05-23 09:08:11 +00:00
|
|
|
return '\'' . $this->escapeString($value) . '\'';
|
2016-09-27 06:20:02 +00:00
|
|
|
case Type::DATE:
|
2018-05-23 09:08:11 +00:00
|
|
|
return strftime('\'%Y-%m-%d\'', $value->getTimestamp());
|
2016-09-27 06:20:02 +00:00
|
|
|
case Type::TIME:
|
2018-05-23 09:08:11 +00:00
|
|
|
return strftime('\'%T\'', $value->getTimestamp());
|
2016-09-27 06:20:02 +00:00
|
|
|
case Type::DATE_TIME:
|
2018-05-23 09:08:11 +00:00
|
|
|
return strftime('\'%Y-%m-%d %T\'', $value->getTimestamp());
|
2015-02-18 16:13:23 +00:00
|
|
|
default:
|
2018-05-23 09:08:11 +00:00
|
|
|
return '\'' . $this->escapeString($value) . '\'';
|
2015-02-18 16:13:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 'NULL';
|
|
|
|
}
|
2018-05-23 09:08:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes an string, escaping special characters when necessary.
|
|
|
|
*
|
|
|
|
* @param string $string The string
|
|
|
|
* @return string The escaped string
|
|
|
|
*/
|
|
|
|
function escapeString($string) {
|
|
|
|
return $this->conn->real_escape_string($string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quotes an identifier, escaping special characters when necessary.
|
|
|
|
*
|
|
|
|
* @param string $identifier The identifier without quotes
|
|
|
|
* @return string The quoted identifier
|
|
|
|
*/
|
|
|
|
function quote($identifier) {
|
|
|
|
return "`". str_replace("`", "``", $identifier) ."`";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an SQL string using the given parameters, also debugs the
|
|
|
|
* rendered string if $enableDebug property is set to %true.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL string
|
|
|
|
* @param mixed[] $paramsMap The query parameters
|
|
|
|
*
|
|
|
|
* @return string The rendered SQL string
|
|
|
|
*/
|
|
|
|
function renderDebug($query, $params = NULL) {
|
|
|
|
$renderedQuery = $this->render($query, $params);
|
|
|
|
|
|
|
|
if ($this->enableDebug)
|
|
|
|
error_log($renderedQuery);
|
|
|
|
|
|
|
|
return $renderedQuery;
|
|
|
|
}
|
2022-07-07 09:15:53 +00:00
|
|
|
|
|
|
|
function commit() {
|
|
|
|
return $this->query('COMMIT');
|
|
|
|
}
|
|
|
|
|
|
|
|
function rollback() {
|
|
|
|
return $this->query('ROLLBACK');
|
|
|
|
}
|
|
|
|
|
|
|
|
function startTransaction() {
|
|
|
|
return $this->query('START TRANSACTION');
|
|
|
|
}
|
|
|
|
|
|
|
|
function lastInsertId() {
|
|
|
|
return $this->getValue('SELECT LAST_INSERT_ID()');
|
|
|
|
}
|
|
|
|
|
|
|
|
function call($procName, $values) {
|
|
|
|
return $this->query(
|
|
|
|
"CALL {$this->quote($procName)}({$this->renderList($values)})"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insert($table, $values) {
|
|
|
|
return $this->query("INSERT INTO {$this->quote($table)}
|
|
|
|
SET {$this->renderFields($values)}");
|
|
|
|
}
|
|
|
|
|
|
|
|
function update($table, $values, $where) {
|
|
|
|
return $this->query("UPDATE {$this->quote($table)}
|
|
|
|
SET {$this->renderFields($values)}
|
|
|
|
WHERE {$this->renderWhere($where)}");
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderFields($values) {
|
|
|
|
return $this->renderAssoc($values, ', ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderWhere($operands) {
|
|
|
|
return $this->renderAssoc($operands, ' AND ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderAssoc($list, $separator) {
|
|
|
|
$fields = '';
|
|
|
|
$isFirst = true;
|
|
|
|
|
|
|
|
foreach ($list as $field => $value) {
|
|
|
|
if ($isFirst)
|
|
|
|
$isFirst = false;
|
|
|
|
else
|
|
|
|
$fields .= $separator;
|
|
|
|
|
|
|
|
$fields .= $this->quote($field) .' = '. $this->renderValue($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderList($list, $separator = ', ') {
|
|
|
|
$values = '';
|
|
|
|
$isFirst = true;
|
|
|
|
|
|
|
|
foreach ($list as $value) {
|
|
|
|
if ($isFirst)
|
|
|
|
$isFirst = false;
|
|
|
|
else
|
|
|
|
$values .= $separator;
|
|
|
|
|
|
|
|
$values .= $this->renderValue($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|