2015-01-23 12:38:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Db;
|
|
|
|
|
2015-02-18 16:13:23 +00:00
|
|
|
require_once ('vn/lib/type.php');
|
2015-01-23 12:38:29 +00:00
|
|
|
require_once ('vn/db/exception.php');
|
|
|
|
|
|
|
|
class Conn
|
|
|
|
{
|
|
|
|
private $conn = NULL;
|
|
|
|
private $isOpen = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
**/
|
2015-08-17 18:04:47 +00:00
|
|
|
function open ($host, $user, $pass, $name, $port = NULL)
|
2015-01-23 12:38:29 +00:00
|
|
|
{
|
|
|
|
$conn = $this->conn = mysqli_init ();
|
|
|
|
$conn->options (MYSQLI_READ_DEFAULT_FILE, __DIR__.'/my.cnf');
|
2015-08-17 18:04:47 +00:00
|
|
|
@$conn->real_connect ($host, $user, $pass, $name, $port);
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
if (mysqli_connect_errno ())
|
|
|
|
{
|
|
|
|
sleep (1);
|
|
|
|
throw new Exception (mysqli_connect_errno (), mysqli_connect_error ());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->isOpen = TRUE;
|
|
|
|
$this->query ('SET CHARACTER SET utf8');
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the current connection, if it's closed does nothing.
|
|
|
|
**/
|
|
|
|
function close ()
|
|
|
|
{
|
|
|
|
if ($this->isOpen)
|
|
|
|
$this->conn->close ();
|
|
|
|
|
|
|
|
$this->isOpen = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the connection is open.
|
|
|
|
*
|
|
|
|
* @return boolean %TRUE if connection is open, %FALSE otherwise
|
|
|
|
**/
|
|
|
|
function isOpen ()
|
|
|
|
{
|
|
|
|
return $this->isOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the query and gets it's first row.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed[] An associative array with the first row, %NULL if error
|
|
|
|
**/
|
|
|
|
function getRow ($query, $params = NULL)
|
|
|
|
{
|
|
|
|
$result = $this->query ($query, $params);
|
|
|
|
|
|
|
|
if ($result && ($row = $result->fetch_assoc ()))
|
|
|
|
return $row;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
**/
|
|
|
|
function getValue ($query, $params = NULL)
|
|
|
|
{
|
|
|
|
$result = $this->query ($query, $params);
|
|
|
|
|
|
|
|
if ($result && ($row = $result->fetch_row ()))
|
|
|
|
return $row[0];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the query stored in the specified file and gets the result.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query
|
|
|
|
* @param mixed[] $params The query parameters
|
|
|
|
*
|
|
|
|
* @return mixed The value or %NULL if error
|
|
|
|
**/
|
|
|
|
function queryFromFile ($file, $params = NULL)
|
|
|
|
{
|
|
|
|
$query = file_get_contents ($file);
|
|
|
|
|
|
|
|
if ($query)
|
|
|
|
return $this->query ($query, $params);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
**/
|
|
|
|
function query ($query, $params = NULL)
|
|
|
|
{
|
2015-02-18 16:13:23 +00:00
|
|
|
$result = $this->conn->query ($this->render ($query, $params));
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
$this->checkError ();
|
|
|
|
else
|
|
|
|
while ($this->moreResults ())
|
|
|
|
$this->nextResult ();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
**/
|
|
|
|
function multiQuery ($query, $params = NULL)
|
|
|
|
{
|
2015-02-18 16:13:23 +00:00
|
|
|
$success = $this->conn->multi_query ($this->render ($query, $params));
|
2015-01-23 12:38:29 +00:00
|
|
|
|
|
|
|
if (!$success)
|
|
|
|
$this->checkError ();
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeResult ()
|
|
|
|
{
|
|
|
|
$result = $this->conn->store_result ();
|
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
$this->checkError ();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function moreResults ()
|
|
|
|
{
|
|
|
|
return $this->conn->more_results ();
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextResult ()
|
|
|
|
{
|
|
|
|
return $this->conn->next_result ();
|
|
|
|
}
|
|
|
|
|
2015-08-17 18:04:47 +00:00
|
|
|
/**
|
|
|
|
* Check if there has been an error in the last query or multiquery,
|
|
|
|
* throwing a @Db\Exception exception if any.
|
|
|
|
**/
|
2015-03-27 19:16:37 +00:00
|
|
|
function checkError ()
|
2015-01-23 12:38:29 +00:00
|
|
|
{
|
|
|
|
if ($this->conn->errno)
|
2015-08-17 18:04:47 +00:00
|
|
|
throw new Exception ($this->conn->errno, $this->conn->error);
|
2015-01-23 12:38:29 +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
|
|
|
|
**/
|
2015-01-23 12:38:29 +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
|
|
|
|
*
|
|
|
|
* @return mixed The rendered SQL string
|
|
|
|
**/
|
|
|
|
function render (&$query, &$paramsMap = NULL)
|
|
|
|
{
|
|
|
|
if (isset ($paramsMap) && is_array ($paramsMap) && count ($paramsMap) > 0)
|
|
|
|
{
|
|
|
|
$i = 0;
|
|
|
|
$params = [];
|
|
|
|
|
|
|
|
foreach ($paramsMap as $key => $value)
|
|
|
|
$params[$key] = $this->renderValue ($value);
|
|
|
|
|
|
|
|
$replaceFunc = function ($matches) use (&$params, &$i)
|
|
|
|
{
|
|
|
|
$key = substr ($matches[0], 1);
|
|
|
|
|
|
|
|
if (strlen ($key) == 0)
|
|
|
|
$key = $i++;
|
|
|
|
if (isset ($params[$key]))
|
|
|
|
return $params[$key];
|
|
|
|
|
|
|
|
return '#'. $key;
|
|
|
|
};
|
|
|
|
|
|
|
|
return preg_replace_callback ('/#\w*/', $replaceFunc, $query);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderValue ($value)
|
|
|
|
{
|
|
|
|
if ($value !== NULL)
|
|
|
|
switch (get_type ($value))
|
|
|
|
{
|
|
|
|
case TYPE_BOOLEAN:
|
|
|
|
return ($value) ? 'TRUE' : 'FALSE';
|
|
|
|
case TYPE_STRING:
|
|
|
|
return '\'' . $this->conn->escape_string ($value) . '\'';
|
|
|
|
case TYPE_DATE:
|
|
|
|
return strftime ('\'%Y-%m-%d\'', $value->getTimestamp ());
|
|
|
|
case TYPE_TIME:
|
|
|
|
return strftime ('\'%T\'', $value->getTimestamp ());
|
|
|
|
case TYPE_DATE_TIME:
|
|
|
|
return strftime ('\'%Y-%m-%d %T\'', $value->getTimestamp ());
|
|
|
|
default:
|
2015-03-27 19:16:37 +00:00
|
|
|
return '\'' . $this->conn->escape_string ($value) . '\'';
|
2015-02-18 16:13:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 'NULL';
|
|
|
|
}
|
2015-01-23 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|