134 lines
4.9 KiB
PHP
Raw Normal View History

2017-09-12 11:13:43 +02:00
<?php
class MySQL extends MySQLCore
{
const REDIS_CONTROLLER_KEY_MAX_LENGTH = 16;
const DEFAULT_SLICE_QUERY_STEP = 1000;
2017-11-15 17:31:44 +01:00
/*
* Simpler version of MySQL::ExecuteS that always returns
* an array even if there is an error, so it doesn't raise
* a notice when used directly in a foreach.
*/
public function ExecuteQ($query, $use_cache = FALSE, $controller = FALSE, $cache_expire = null)
{
$this->_result = FALSE;
$this->_lastQuery = $query;
if ($controller) {
$controller = strtoupper(substr($controller, 0, self::REDIS_CONTROLLER_KEY_MAX_LENGTH));
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
if (FALSE !== ($result = CacheRedis::getInstance()->getQuery($query, $controller))) {
return $result;
}
}
if ($this->_link && $this->_result = mysql_query($query, $this->_link)) {
$this->_lastCached = FALSE;
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
$resultArray = array();
// Only SELECT queries and a few others return a valid resource usable with mysql_fetch_assoc
if ($this->_result !== TRUE) {
while ($row = mysql_fetch_assoc($this->_result)) {
$resultArray[] = $row;
}
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
CacheRedis::getInstance()->setQuery($query, $controller, $resultArray, $cache_expire);
}
return $resultArray;
}
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
return array();
}
2017-09-12 11:13:43 +02:00
/**
* ExecuteS return the result of $query as array,
* or as mysqli_result if $array set to FALSE
*
* @param string $query query to execute
* @param boolean $array return an array instead of a mysql_result object
* @param bool|int $use_cache if query has been already executed, use its result
* @param string|bool $controller name used to limit cache collision
* @return array or result object
*/
public function ExecuteS($query, $array = TRUE, $use_cache = FALSE, $controller = FALSE, $cache_expire = null)
{
$this->_result = FALSE;
$this->_lastQuery = $query;
if ($controller) {
$controller = strtoupper(substr($controller, 0, self::REDIS_CONTROLLER_KEY_MAX_LENGTH));
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
if (FALSE !== ($result = CacheRedis::getInstance()->getQuery($query, $controller))) {
return $result;
}
}
if ($this->_link && $this->_result = mysql_query($query, $this->_link)) {
$this->_lastCached = FALSE;
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
if (!$array) {
return $this->_result;
}
$resultArray = array();
// Only SELECT queries and a few others return a valid resource usable with mysql_fetch_assoc
if ($this->_result !== TRUE) {
while ($row = mysql_fetch_assoc($this->_result)) {
$resultArray[] = $row;
}
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
CacheRedis::getInstance()->setQuery($query, $controller, $resultArray, $cache_expire);
}
return $resultArray;
}
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
return FALSE;
}
public function getRow($query, $use_cache = FALSE, $controller = FALSE, $cache_expire = null)
{
$query .= ' LIMIT 1';
$this->_result = FALSE;
$this->_lastQuery = $query;
if ($controller) {
$controller = strtoupper(substr($controller, 0, self::REDIS_CONTROLLER_KEY_MAX_LENGTH));
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
if (FALSE !== ($result = CacheRedis::getInstance()->getQuery($query, $controller))) {
return $result;
}
}
if ($this->_link) {
if ($this->_result = mysql_query($query, $this->_link)) {
$this->_lastCached = FALSE;
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
$result = mysql_fetch_assoc($this->_result);
if ($use_cache && $controller && class_exists('CacheRedis')) {
CacheRedis::getInstance()->setQuery($query, $controller, $result, $cache_expire);
}
return $result;
}
}
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
return FALSE;
}
}