Merge pull request #1045 from kijin/pr/mysqli-only

MySQL을 제외한 모든 DB 타입 지원 중단
This commit is contained in:
Kijin Sung 2018-07-02 15:46:54 +09:00 committed by GitHub
commit 712287d564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 447 additions and 3401 deletions

View file

@ -23,24 +23,7 @@ class DB
* @var array
*/
protected static $priority_dbms = array(
'mysqli' => 6,
'cubrid' => 2,
'mssql' => 1
);
/**
* operations for condition
* @var array
*/
protected static $cond_operation = array(
'equal' => '=',
'more' => '>=',
'excess' => '>',
'less' => '<=',
'below' => '<',
'notequal' => '<>',
'notnull' => 'is not null',
'null' => 'is null',
'mysql' => 1,
);
/**
@ -108,7 +91,7 @@ class DB
protected $cache_file = 'files/cache/queries/';
/**
* stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set
* stores database type, e.g. mysql
* @var string
*/
public $db_type;
@ -136,15 +119,15 @@ class DB
if(!$db_type)
{
$db_type = config('db.master.type');
if (config('db.master.engine') === 'innodb')
{
$db_type .= '_innodb';
}
}
if(!$db_type && Context::isInstalled())
{
return new BaseObject(-1, 'msg_db_not_setted');
}
if(!strncmp($db_type, 'mysql', 5))
{
$db_type = 'mysql';
}
if(!isset($GLOBALS['__DB__']))
{
@ -271,7 +254,7 @@ class DB
// after creating instance of class, check is supported
foreach ($supported_list as $db_type)
{
if (strncasecmp($db_type, 'mysql', 5) === 0 && strtolower($db_type) !== 'mysqli')
if (strtolower($db_type) !== 'mysql')
{
continue;
}
@ -707,6 +690,7 @@ class DB
$tableObjects = $query->getTables();
$index_hint_list = '';
/*
foreach($tableObjects as $tableObject)
{
if(is_a($tableObject, 'CubridTableWithHint'))
@ -719,6 +703,7 @@ class DB
{
$index_hint_list = 'USING INDEX ' . $index_hint_list;
}
*/
$groupBy = $query->getGroupByString();
if($groupBy != '')

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -2,24 +2,17 @@
/* Copyright (C) NAVER <http://www.navercorp.com> */
/**
* Class to use MySQL DBMS
* mysql handling class
*
* Does not use prepared statements, since mysql driver does not support them
*
* @author NAVER (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
* Merged class for MySQL and MySQLi, with or without InnoDB
*/
class DBMysql extends DB
class DBMySQL extends DB
{
/**
* prefix of a tablename (One or more Rhymix can be installed in a single DB)
* @var string
*/
var $prefix = 'rx_'; // / <
var $prefix = 'rx_';
var $comment_syntax = '/* %s */';
var $charset = 'utf8';
var $charset = 'utf8mb4';
/**
* Column type used in MySQL
@ -43,6 +36,11 @@ class DBMysql extends DB
* Last statement executed
*/
var $last_stmt;
/**
* Query parameters for prepared statement
*/
var $params = array();
/**
* Constructor
@ -58,62 +56,54 @@ class DBMysql extends DB
* DB Connect
* this method is private
* @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password
* @return resource
* @return mysqli
*/
function __connect($connection)
protected function __connect($connection)
{
// Ignore if no DB information exists
if(strpos($connection['host'], ':') === false && $connection['port'])
{
$connection['host'] .= ':' . $connection['port'];
}
// Attempt to connect
$result = @mysql_connect($connection['host'], $connection['user'], $connection['pass']);
if(!$result)
if($connection['port'])
{
$this->setError(-1, 'Unable to connect to DB.');
$mysqli = new mysqli($connection['host'], $connection['user'], $connection['pass'], $connection['database'], $connection['port']);
}
else
{
$mysqli = new mysqli($connection['host'], $connection['user'], $connection['pass'], $connection['database']);
}
// Check connection error
if($mysqli->connect_errno)
{
$this->setError($mysqli->connect_errno, $mysqli->connect_error());
return;
}
if(mysql_error())
{
$this->setError(mysql_errno(), mysql_error());
return;
}
// Error appears if the version is lower than 5.0.7
$this->db_version = mysql_get_server_info($result);
if(version_compare($this->db_version, '5.0.7', '<'))
// Check DB version
$this->db_version = $mysqli->server_info;
if (version_compare($this->db_version, '5.0.7', '<'))
{
$this->setError(-1, 'Rhymix requires MySQL 5.0.7 or later. Current MySQL version is ' . $this->db_version);
return;
}
// Set charset
// Set DB charset
$this->charset = isset($connection['charset']) ? $connection['charset'] : 'utf8';
mysql_set_charset($this->charset, $result);
// select db
@mysql_select_db($connection['database'], $result);
if(mysql_error())
{
$this->setError(mysql_errno(), mysql_error());
return;
}
return $result;
$mysqli->set_charset($this->charset);
return $mysqli;
}
/**
* DB disconnection
* this method is private
* @param resource $connection
* @param mysqli $connection
* @return void
*/
function _close($connection)
protected function _close($connection)
{
@mysql_close($connection);
if ($connection instanceof mysqli)
{
$connection->close();
}
}
/**
@ -125,7 +115,8 @@ class DBMysql extends DB
{
if(!is_numeric($string))
{
$string = @mysql_real_escape_string($string);
$connection = $this->_getConnection('master');
$string = $connection->real_escape_string($string);
}
return $string;
}
@ -135,8 +126,19 @@ class DBMysql extends DB
* this method is private
* @return boolean
*/
function _begin($transactionLevel = 0)
protected function _begin($transactionLevel = 0)
{
$connection = $this->_getConnection('master');
if(!$transactionLevel)
{
$connection->begin_transaction();
$this->setQueryLog(array('query' => 'START TRANSACTION'));
}
else
{
$this->_query("SAVEPOINT SP" . $transactionLevel, $connection);
}
return true;
}
@ -145,8 +147,20 @@ class DBMysql extends DB
* this method is private
* @return boolean
*/
function _rollback($transactionLevel = 0)
protected function _rollback($transactionLevel = 0)
{
$connection = $this->_getConnection('master');
$point = $transactionLevel - 1;
if($point)
{
$this->_query("ROLLBACK TO SP" . $point, $connection);
}
else
{
$connection->rollback();
$this->setQueryLog(array('query' => 'ROLLBACK'));
}
return true;
}
@ -155,8 +169,11 @@ class DBMysql extends DB
* this method is private
* @return boolean
*/
function _commit()
protected function _commit()
{
$connection = $this->_getConnection('master');
$connection->commit();
$this->setQueryLog(array('query' => 'COMMIT'));
return true;
}
@ -169,21 +186,105 @@ class DBMysql extends DB
*/
function __query($query, $connection)
{
if(!$connection)
if (!($connection instanceof mysqli) || $connection->connection_errno)
{
$this->setError(-1, 'Unable to connect to DB.');
return false;
}
// Run the query statement
$result = @mysql_query($query, $connection);
// Error Check
if(mysql_error($connection))
if($this->use_prepared_statements == 'Y')
{
$this->setError(mysql_errno($connection), mysql_error($connection));
// 1. Prepare query
$stmt = $connection->prepare($query);
if(!$stmt)
{
$this->setError($connection->errno, $connection->error);
return $this->last_stmt = $result;
}
// 2. Bind parameters
if ($this->params)
{
$types = '';
$params = array();
foreach($this->params as $k => $o)
{
$value = $o->getUnescapedValue();
$type = $o->getType();
// Skip column names -> this should be concatenated to query string
if($o->isColumnName())
{
continue;
}
switch($type)
{
case 'number' :
$type = 'i';
break;
case 'varchar' :
$type = 's';
break;
default:
$type = 's';
}
if(is_array($value))
{
foreach($value as $v)
{
$params[] = $v;
$types .= $type;
}
}
else
{
$params[] = $value;
$types .= $type;
}
}
// 2. Bind parameters
$args = array();
$args[0] = $stmt;
$args[1] = $types;
$i = 2;
foreach($params as $key => $param)
{
$copy[$key] = $param;
$args[$i++] = &$copy[$key];
}
$status = call_user_func_array('mysqli_stmt_bind_param', $args);
if(!$status)
{
$this->setError(-1, "Invalid arguments: " . $connection->error);
return $this->last_stmt = $stmt;
}
}
// 3. Execute query
$status = $stmt->execute();
if(!$status)
{
$this->setError(-1, "Prepared statement failed: " . $connection->error);
return $this->last_stmt = $stmt;
}
// Return stmt for other processing
return $this->last_stmt = $stmt;
}
else
{
$result = $connection->query($query);
if($connection->errno)
{
$this->setError($connection->errno, $connection->error);
}
return $this->last_stmt = $result;
}
// Return result
$this->last_stmt = $result;
return $result;
}
/**
@ -199,17 +300,92 @@ class DBMysql extends DB
{
return $output;
}
while($tmp = $this->db_fetch_object($result))
// No prepared statements
if($this->use_prepared_statements != 'Y')
{
if($arrayIndexEndValue)
while($tmp = $this->db_fetch_object($result))
{
$output[$arrayIndexEndValue--] = $tmp;
}
else
{
$output[] = $tmp;
if($arrayIndexEndValue)
{
$output[$arrayIndexEndValue--] = $tmp;
}
else
{
$output[] = $tmp;
}
}
$result->free_result();
}
// Prepared stements: bind result variable and fetch data
else
{
$stmt = $result;
$fields = $stmt->result_metadata()->fetch_fields();
$row = array();
$resultArray = array();
/**
* Mysqli has a bug that causes LONGTEXT columns not to get loaded
* Unless store_result is called before
* MYSQLI_TYPE for longtext is 252
*/
$longtext_exists = false;
foreach($fields as $field)
{
// When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
if(isset($resultArray[$field->name]))
{
$field->name = 'repeat_' . $field->name;
}
// Array passed needs to contain references, not values
$row[$field->name] = '';
$resultArray[$field->name] = &$row[$field->name];
if($field->type == 252)
{
$longtext_exists = true;
}
}
$resultArray = array_merge(array($stmt), $resultArray);
if($longtext_exists)
{
$stmt->store_result();
}
call_user_func_array('mysqli_stmt_bind_result', $resultArray);
array_shift($resultArray);
while($stmt->fetch())
{
$resultObject = new stdClass;
foreach($resultArray as $key => $value)
{
if(strpos($key, 'repeat_'))
{
$key = substr($key, 6);
}
$resultObject->$key = $value;
}
if($arrayIndexEndValue)
{
$output[$arrayIndexEndValue--] = $resultObject;
}
else
{
$output[] = $resultObject;
}
}
$stmt->free_result();
$stmt->close();
}
// Return object if there is only 1 result.
if(count($output) == 1)
{
if(isset($arrayIndexEndValue))
@ -221,8 +397,10 @@ class DBMysql extends DB
return $output[0];
}
}
$this->db_free_result($result);
return $output;
else
{
return $output;
}
}
/**
@ -232,12 +410,12 @@ class DBMysql extends DB
*/
function getNextSequence()
{
$query = sprintf("insert into `%ssequence` (seq) values ('0')", $this->prefix);
$query = sprintf("INSERT INTO `%ssequence` (seq) VALUES ('0')", $this->prefix);
$this->_query($query);
$sequence = $this->getInsertID();
if($sequence % 10000 == 0)
{
$query = sprintf("delete from `%ssequence` where seq < %d", $this->prefix, $sequence);
$query = sprintf("DELETE FROM `%ssequence` WHERE seq < %d", $this->prefix, $sequence);
$this->_query($query);
}
@ -252,7 +430,7 @@ class DBMysql extends DB
*/
function isValidOldPassword($password, $saved_password)
{
$query = sprintf("select password('%s') as password, old_password('%s') as old_password", $this->addQuotes($password), $this->addQuotes($password));
$query = sprintf("SELECT PASSWORD('%s') AS password, OLD_PASSWORD('%s') AS old_password", $this->addQuotes($password), $this->addQuotes($password));
$result = $this->_query($query);
$tmp = $this->_fetch($result);
if($tmp->password === $saved_password || $tmp->old_password === $saved_password)
@ -269,7 +447,7 @@ class DBMysql extends DB
*/
function isTableExists($target_name)
{
$query = sprintf("show tables like '%s%s'", $this->prefix, $this->addQuotes($target_name));
$query = sprintf("SHOW TABLES LIKE '%s%s'", $this->prefix, $this->addQuotes($target_name));
$result = $this->_query($query);
$tmp = $this->_fetch($result);
if(!$tmp)
@ -297,7 +475,7 @@ class DBMysql extends DB
$size = '';
}
$query = sprintf("alter table `%s%s` add `%s` ", $this->prefix, $table_name, $column_name);
$query = sprintf("ALTER TABLE `%s%s` ADD `%s` ", $this->prefix, $table_name, $column_name);
if($size)
{
$query .= sprintf(" %s(%s) ", $type, $size);
@ -308,11 +486,11 @@ class DBMysql extends DB
}
if(isset($default))
{
$query .= sprintf(" default '%s' ", $default);
$query .= sprintf(" DEFAULT '%s' ", $default);
}
if($notnull)
{
$query .= " not null ";
$query .= " NOT NULL ";
}
return $this->_query($query);
@ -326,7 +504,7 @@ class DBMysql extends DB
*/
function dropColumn($table_name, $column_name)
{
$query = sprintf("alter table `%s%s` drop `%s` ", $this->prefix, $table_name, $column_name);
$query = sprintf("ALTER TABLE `%s%s` DROP `%s` ", $this->prefix, $table_name, $column_name);
$this->_query($query);
}
@ -348,7 +526,7 @@ class DBMysql extends DB
$size = '';
}
$query = sprintf("alter table `%s%s` modify `%s` ", $this->prefix, $table_name, $column_name);
$query = sprintf("ALTER TABLE `%s%s` MODIFY `%s` ", $this->prefix, $table_name, $column_name);
if($size)
{
$query .= sprintf(" %s(%s) ", $type, $size);
@ -359,11 +537,11 @@ class DBMysql extends DB
}
if($default)
{
$query .= sprintf(" default '%s' ", $default);
$query .= sprintf(" DEFAULT '%s' ", $default);
}
if($notnull)
{
$query .= " not null ";
$query .= " NOT NULL ";
}
return $this->_query($query) ? true : false;
@ -377,7 +555,7 @@ class DBMysql extends DB
*/
function isColumnExists($table_name, $column_name)
{
$query = sprintf("show fields from `%s%s`", $this->prefix, $table_name);
$query = sprintf("SHOW FIELDS FROM `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query);
if($this->isError())
{
@ -407,7 +585,7 @@ class DBMysql extends DB
*/
function getColumnInfo($table_name, $column_name)
{
$query = sprintf("show fields from `%s%s` where `Field` = '%s'", $this->prefix, $table_name, $column_name);
$query = sprintf("SHOW FIELDS FROM `%s%s` WHERE `Field` = '%s'", $this->prefix, $table_name, $column_name);
$result = $this->_query($query);
if($this->isError())
{
@ -471,7 +649,7 @@ class DBMysql extends DB
$target_columns = array($target_columns);
}
$query = sprintf("alter table `%s%s` add %s index `%s` (%s);", $this->prefix, $table_name, $is_unique ? 'unique' : '', $index_name, implode(',', $target_columns));
$query = sprintf("ALTER TABLE `%s%s` ADD %s INDEX `%s` (%s);", $this->prefix, $table_name, $is_unique ? 'UNIQUE' : '', $index_name, implode(',', $target_columns));
$this->_query($query);
}
@ -484,7 +662,7 @@ class DBMysql extends DB
*/
function dropIndex($table_name, $index_name, $is_unique = false)
{
$query = sprintf("alter table `%s%s` drop index `%s`", $this->prefix, $table_name, $index_name);
$query = sprintf("ALTER TABLE `%s%s` DROP INDEX `%s`", $this->prefix, $table_name, $index_name);
$this->_query($query);
}
@ -496,8 +674,7 @@ class DBMysql extends DB
*/
function isIndexExists($table_name, $index_name)
{
//$query = sprintf("show indexes from %s%s where key_name = '%s' ", $this->prefix, $table_name, $index_name);
$query = sprintf("show indexes from `%s%s`", $this->prefix, $table_name);
$query = sprintf("SHOW INDEXES FROM `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query);
if($this->isError())
{
@ -668,7 +845,7 @@ class DBMysql extends DB
}
// Generate table schema
$engine = stripos(get_class($this), 'innodb') === false ? 'MYISAM' : 'INNODB';
$engine = config('db.master.engine') === 'innodb' ? 'InnoDB' : 'MyISAM';
$charset = $this->charset ?: 'utf8';
$collation = $charset . '_unicode_ci';
$schema = sprintf("CREATE TABLE `%s` (%s) %s",
@ -716,49 +893,84 @@ class DBMysql extends DB
* Handles insertAct
* @param BaseObject $queryObject
* @param boolean $with_values
* @return resource
* @return mixed
*/
function _executeInsertAct($queryObject, $with_values = true)
{
$query = $this->getInsertSql($queryObject, $with_values, true);
if($query instanceof BaseObject)
if ($this->use_prepared_statements == 'Y')
{
return;
$this->params = $queryObject->getArguments();
$with_values = false;
}
$query = $this->getInsertSql($queryObject, $with_values, true);
if ($query instanceof BaseObject)
{
$this->params = array();
return $query;
}
else
{
$output = $this->_query($query);
$this->params = array();
return $output;
}
return $this->_query($query);
}
/**
* Handles updateAct
* @param BaseObject $queryObject
* @param boolean $with_values
* @return resource
* @return mixed
*/
function _executeUpdateAct($queryObject, $with_values = true)
{
$query = $this->getUpdateSql($queryObject, $with_values, true);
if($query instanceof BaseObject)
if ($this->use_prepared_statements == 'Y')
{
if(!$query->toBool()) return $query;
else return;
$this->params = $queryObject->getArguments();
$with_values = false;
}
$query = $this->getUpdateSql($queryObject, $with_values, true);
if ($query instanceof BaseObject)
{
$this->params = array();
return $query;
}
else
{
$output = $this->_query($query);
$this->params = array();
return $output;
}
return $this->_query($query);
}
/**
* Handles deleteAct
* @param BaseObject $queryObject
* @param boolean $with_values
* @return resource
* @return mixed
*/
function _executeDeleteAct($queryObject, $with_values = true)
{
$query = $this->getDeleteSql($queryObject, $with_values, true);
if($query instanceof BaseObject)
if ($this->use_prepared_statements == 'Y')
{
return;
$this->params = $queryObject->getArguments();
$with_values = false;
}
$query = $this->getDeleteSql($queryObject, $with_values, true);
if ($query instanceof BaseObject)
{
$this->params = array();
return $query;
}
else
{
$output = $this->_query($query);
$this->params = array();
return $output;
}
return $this->_query($query);
}
/**
@ -772,23 +984,33 @@ class DBMysql extends DB
*/
function _executeSelectAct($queryObject, $connection = null, $with_values = true)
{
if ($this->use_prepared_statements == 'Y')
{
$this->params = $queryObject->getArguments();
$with_values = false;
}
$result = null;
$limit = $queryObject->getLimit();
$result = NULL;
if($limit && $limit->isPageHandler())
{
return $this->queryPageLimit($queryObject, $result, $connection, $with_values);
$output = $this->queryPageLimit($queryObject, $result, $connection, $with_values);
$this->params = array();
return $output;
}
else
{
$query = $this->getSelectSql($queryObject, $with_values);
if($query instanceof BaseObject)
{
return;
$this->params = array();
return $query;
}
$result = $this->_query($query, $connection);
if($this->isError())
{
$this->params = array();
return $this->queryError($queryObject);
}
@ -802,6 +1024,7 @@ class DBMysql extends DB
$this->_executeUpdateAct($update_query, $with_values);
}
$this->params = array();
return $buff;
}
}
@ -812,8 +1035,8 @@ class DBMysql extends DB
*/
function getAffectedRows()
{
$connection = $this->_getConnection('master');
return mysql_affected_rows($connection);
$stmt = $this->last_stmt;
return $stmt ? $stmt->affected_rows : -1;
}
/**
@ -823,7 +1046,7 @@ class DBMysql extends DB
function getInsertID()
{
$connection = $this->_getConnection('master');
return mysql_insert_id($connection);
return $connection->insert_id;
}
/**
@ -840,9 +1063,9 @@ class DBMysql extends DB
* @param resource $result
* @return BaseObject
*/
function db_fetch_object(&$result)
function db_fetch_object($result)
{
return mysql_fetch_object($result);
return $result->fetch_object();
}
/**
@ -850,9 +1073,9 @@ class DBMysql extends DB
* @param resource $result
* @return boolean Returns TRUE on success or FALSE on failure.
*/
function db_free_result(&$result)
function db_free_result($result)
{
return mysql_free_result($result);
return $result->free_result();
}
/**
@ -873,7 +1096,7 @@ class DBMysql extends DB
*/
function queryError($queryObject)
{
$limit = $queryObject->getLimit();
$limit = method_exists($queryObject, 'getLimit') ? $queryObject->getLimit() : false;
if($limit && $limit->isPageHandler())
{
$buff = new BaseObject;
@ -1063,7 +1286,4 @@ class DBMysql extends DB
}
}
DBMysql::$isSupported = function_exists('mysql_connect');
/* End of file DBMysql.class.php */
/* Location: ./classes/db/DBMysql.class.php */
DBMysql::$isSupported = class_exists('mysqli');

View file

@ -1,76 +0,0 @@
<?php
/* Copyright (C) NAVER <http://www.navercorp.com> */
require_once('DBMysql.class.php');
/**
* Class to use MySQL innoDB DBMS
* mysql innodb handling class
*
* Does not use prepared statements, since mysql driver does not support them
*
* @author NAVER (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
*/
class DBMysql_innodb extends DBMysql
{
/**
* DB transaction start
* this method is private
* @return boolean
*/
function _begin($transactionLevel = 0)
{
$connection = $this->_getConnection('master');
if(!$transactionLevel)
{
$this->_query("START TRANSACTION", $connection);
}
else
{
$this->_query("SAVEPOINT SP" . $transactionLevel, $connection);
}
return true;
}
/**
* DB transaction rollback
* this method is private
* @return boolean
*/
function _rollback($transactionLevel = 0)
{
$connection = $this->_getConnection('master');
$point = $transactionLevel - 1;
if($point)
{
$this->_query("ROLLBACK TO SP" . $point, $connection);
}
else
{
$this->_query("ROLLBACK", $connection);
}
return true;
}
/**
* DB transaction commit
* this method is private
* @return boolean
*/
function _commit()
{
$connection = $this->_getConnection('master');
$this->_query("COMMIT", $connection);
return true;
}
}
DBMysql_innodb::$isSupported = function_exists('mysql_connect');
/* End of file DBMysql_innodb.class.php */
/* Location: ./classes/db/DBMysql_innodb.class.php */

View file

@ -1,422 +0,0 @@
<?php
/* Copyright (C) NAVER <http://www.navercorp.com> */
require_once('DBMysql.class.php');
/**
* Class to use MySQLi DBMS as mysqli_*
* mysql handling class
*
* Does not use prepared statements, since mysql driver does not support them
*
* @author NAVER (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
*/
class DBMysqli extends DBMysql
{
/**
* DB Connect
* this method is private
* @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password
* @return resource
*/
function __connect($connection)
{
// Attempt to connect
if($connection['port'])
{
$result = @mysqli_connect($connection['host'], $connection['user'], $connection['pass'], $connection['database'], $connection['port']);
}
else
{
$result = @mysqli_connect($connection['host'], $connection['user'], $connection['pass'], $connection['database']);
}
$error = mysqli_connect_errno();
if($error)
{
$this->setError($error, mysqli_connect_error());
return;
}
$this->charset = isset($connection['charset']) ? $connection['charset'] : 'utf8';
$this->db_version = $result->server_info;
mysqli_set_charset($result, $this->charset);
return $result;
}
/**
* DB disconnection
* this method is private
* @param resource $connection
* @return void
*/
function _close($connection)
{
mysqli_close($connection);
}
/**
* Handles quatation of the string variables from the query
* @param string $string
* @return string
*/
function addQuotes($string)
{
if(!is_numeric($string))
{
$connection = $this->_getConnection('master');
$string = mysqli_escape_string($connection, $string);
}
return $string;
}
/**
* Execute the query
* this method is private
* @param string $query
* @param resource $connection
* @return resource
*/
function __query($query, $connection)
{
if ($connection === null)
{
$this->setError(-1, 'Unable to connect to DB.');
return false;
}
if($this->use_prepared_statements == 'Y')
{
// 1. Prepare query
$stmt = mysqli_prepare($connection, $query);
if($stmt)
{
$types = '';
$params = array();
$this->_prepareQueryParameters($types, $params);
if(!empty($params))
{
$args[0] = $stmt;
$args[1] = $types;
$i = 2;
foreach($params as $key => $param)
{
$copy[$key] = $param;
$args[$i++] = &$copy[$key];
}
// 2. Bind parameters
$status = call_user_func_array('mysqli_stmt_bind_param', $args);
if(!$status)
{
$this->setError(-1, "Invalid arguments: " . mysqli_error($connection));
}
}
// 3. Execute query
$status = mysqli_stmt_execute($stmt);
if(!$status)
{
$this->setError(-1, "Prepared statement failed: " . mysqli_error($connection));
}
// Return stmt for other processing - like retrieving resultset (_fetch)
$this->last_stmt = $stmt;
return $stmt;
}
}
// Run the query statement
$result = mysqli_query($connection, $query);
// Error Check
$error = mysqli_error($connection);
if($error)
{
$this->setError(mysqli_errno($connection), $error);
}
// Return result
$this->last_stmt = $result;
return $result;
}
/**
* Before execute query, prepare statement
* this method is private
* @param string $types
* @param array $params
* @return void
*/
function _prepareQueryParameters(&$types, &$params)
{
$types = '';
$params = array();
if(!$this->param)
{
return;
}
foreach($this->param as $k => $o)
{
$value = $o->getUnescapedValue();
$type = $o->getType();
// Skip column names -> this should be concatenated to query string
if($o->isColumnName())
{
continue;
}
switch($type)
{
case 'number' :
$type = 'i';
break;
case 'varchar' :
$type = 's';
break;
default:
$type = 's';
}
if(is_array($value))
{
foreach($value as $v)
{
$params[] = $v;
$types .= $type;
}
}
else
{
$params[] = $value;
$types .= $type;
}
}
}
/**
* Fetch the result
* @param resource $result
* @param int|NULL $arrayIndexEndValue
* @return array
*/
function _fetch($result, $arrayIndexEndValue = NULL)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_fetch($result, $arrayIndexEndValue);
}
$output = array();
if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
// Prepared stements: bind result variable and fetch data
$stmt = $result;
$meta = mysqli_stmt_result_metadata($stmt);
$fields = mysqli_fetch_fields($meta);
/**
* Mysqli has a bug that causes LONGTEXT columns not to get loaded
* Unless store_result is called before
* MYSQLI_TYPE for longtext is 252
*/
$longtext_exists = false;
foreach($fields as $field)
{
if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
{
$field->name = 'repeat_' . $field->name;
}
// Array passed needs to contain references, not values
$row[$field->name] = "";
$resultArray[$field->name] = &$row[$field->name];
if($field->type == 252)
{
$longtext_exists = true;
}
}
$resultArray = array_merge(array($stmt), $resultArray);
if($longtext_exists)
{
mysqli_stmt_store_result($stmt);
}
call_user_func_array('mysqli_stmt_bind_result', $resultArray);
$rows = array();
while(mysqli_stmt_fetch($stmt))
{
$resultObject = new stdClass();
foreach($resultArray as $key => $value)
{
if($key === 0)
{
continue; // Skip stmt object
}
if(strpos($key, 'repeat_'))
{
$key = substr($key, 6);
}
$resultObject->$key = $value;
}
$rows[] = $resultObject;
}
mysqli_stmt_close($stmt);
if($arrayIndexEndValue)
{
foreach($rows as $row)
{
$output[$arrayIndexEndValue--] = $row;
}
}
else
{
$output = $rows;
}
if(count($output) == 1)
{
if(isset($arrayIndexEndValue))
{
return $output;
}
else
{
return $output[0];
}
}
return $output;
}
/**
* Handles insertAct
* @param BaseObject $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeInsertAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeInsertAct($queryObject);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeInsertAct($queryObject, $with_values);
unset($this->param);
return $result;
}
/**
* Handles updateAct
* @param BaseObject $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeUpdateAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeUpdateAct($queryObject);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeUpdateAct($queryObject, $with_values);
unset($this->param);
return $result;
}
/**
* Handles deleteAct
* @param BaseObject $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeDeleteAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeDeleteAct($queryObject);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeDeleteAct($queryObject, $with_values);
unset($this->param);
return $result;
}
/**
* Handle selectAct
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
* @param BaseObject $queryObject
* @param resource $connection
* @param boolean $with_values
* @return BaseObject
*/
function _executeSelectAct($queryObject, $connection = null, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeSelectAct($queryObject, $connection);
}
$this->param = $queryObject->getArguments();
$result = parent::_executeSelectAct($queryObject, $connection, $with_values);
unset($this->param);
return $result;
}
/**
* Get the number of rows affected by the last query
* @return int
*/
function getAffectedRows()
{
$stmt = $this->last_stmt;
return $stmt ? $stmt->affected_rows : -1;
}
/**
* Get the ID generated in the last query
* @return int
*/
function getInsertID()
{
$connection = $this->_getConnection('master');
return mysqli_insert_id($connection);
}
/**
* Fetch a result row as an object
* @param resource $result
* @return BaseObject
*/
function db_fetch_object(&$result)
{
return mysqli_fetch_object($result);
}
/**
* Free result memory
* @param resource $result
* @return boolean Returns TRUE on success or FALSE on failure.
*/
function db_free_result(&$result)
{
return mysqli_free_result($result);
}
}
DBMysqli::$isSupported = function_exists('mysqli_connect');
/* End of file DBMysqli.class.php */
/* Location: ./classes/db/DBMysqli.class.php */

View file

@ -1,86 +0,0 @@
<?php
/* Copyright (C) NAVER <http://www.navercorp.com> */
require_once('DBMysql.class.php');
require_once('DBMysqli.class.php');
/**
* Class to use MySQLi innoDB DBMS as mysqli_*
* mysql innodb handling class
*
* Does not use prepared statements, since mysql driver does not support them
*
* @author NAVER (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
*/
class DBMysqli_innodb extends DBMysqli
{
/**
* DB transaction start
* this method is private
* @return boolean
*/
function _begin($transactionLevel = 0)
{
$connection = $this->_getConnection('master');
if(!$transactionLevel)
{
if(function_exists('mysqli_begin_transaction'))
{
mysqli_begin_transaction($connection);
$this->setQueryLog(array('query' => 'START TRANSACTION'));
}
else
{
$this->_query("START TRANSACTION" . $point, $connection);
}
}
else
{
$this->_query("SAVEPOINT SP" . $transactionLevel, $connection);
}
return true;
}
/**
* DB transaction rollback
* this method is private
* @return boolean
*/
function _rollback($transactionLevel = 0)
{
$connection = $this->_getConnection('master');
$point = $transactionLevel - 1;
if($point)
{
$this->_query("ROLLBACK TO SP" . $point, $connection);
}
else
{
mysqli_rollback($connection);
$this->setQueryLog(array('query' => 'ROLLBACK'));
}
return true;
}
/**
* DB transaction commit
* this method is private
* @return boolean
*/
function _commit()
{
$connection = $this->_getConnection('master');
mysqli_commit($connection);
$this->setQueryLog(array('query' => 'COMMIT'));
return true;
}
}
DBMysqli_innodb::$isSupported = function_exists('mysqli_connect');
/* End of file DBMysqli.class.php */
/* Location: ./classes/db/DBMysqli.class.php */

View file

@ -41,7 +41,7 @@ class Condition
* @param string $pipe
* @return void
*/
function __construct($column_name, $argument, $operation, $pipe)
function __construct($column_name, $argument, $operation, $pipe = 'and')
{
$this->column_name = $column_name;
$this->argument = $argument;
@ -85,7 +85,7 @@ class Condition
*/
function toStringWithoutValue()
{
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
return strtoupper($this->pipe) . ' ' . $this->getConditionPart($this->_value);
}
/**
@ -94,7 +94,7 @@ class Condition
*/
function toStringWithValue()
{
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
return strtoupper($this->pipe) . ' ' . $this->getConditionPart($this->_value);
}
function setPipe($pipe)
@ -122,13 +122,18 @@ class Condition
case 'more' :
case 'excess' :
case 'less' :
case 'below' :
case 'below' :
case 'gte' :
case 'gt' :
case 'lte' :
case 'lt' :
case 'like_tail' :
case 'like_prefix' :
case 'like' :
case 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
case 'not_like' :
case 'in' :
case 'notin' :
case 'not_in' :
@ -137,6 +142,7 @@ class Condition
case 'xor':
case 'not':
case 'notequal' :
case 'not_equal' :
// if variable is not set or is not string or number, return
if(!isset($this->_value))
{
@ -167,7 +173,8 @@ class Condition
break;
}
case 'null':
case 'notnull':
case 'notnull':
case 'not_null':
break;
default:
// If operation is not one of the above, means the condition is invalid
@ -190,50 +197,52 @@ class Condition
switch($operation)
{
case 'equal' :
case 'equal' :
return $name . ' = ' . $value;
break;
case 'more' :
case 'more' :
case 'gte' :
return $name . ' >= ' . $value;
break;
case 'excess' :
case 'excess' :
case 'gt' :
return $name . ' > ' . $value;
break;
case 'less' :
case 'less' :
case 'lte' :
return $name . ' <= ' . $value;
break;
case 'below' :
case 'below' :
case 'lt' :
return $name . ' < ' . $value;
break;
case 'like_tail' :
case 'like_prefix' :
case 'like' :
if(defined('__CUBRID_VERSION__')
&& __CUBRID_VERSION__ >= '8.4.1')
return $name . ' rlike ' . $value;
else
return $name . ' like ' . $value;
break;
return $name . ' LIKE ' . $value;
case 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
return $name . ' not like ' . $value;
case 'not_like' :
return $name . ' NOT LIKE ' . $value;
break;
case 'in' :
return $name . ' in ' . $value;
return $name . ' IN ' . $value;
break;
case 'notin' :
case 'not_in' :
return $name . ' not in ' . $value;
return $name . ' NOT IN ' . $value;
break;
case 'notequal' :
case 'notequal' :
case 'not_equal' :
return $name . ' <> ' . $value;
break;
case 'notnull' :
return $name . ' is not null';
case 'notnull' :
case 'not_null' :
return $name . ' IS NOT NULL ';
break;
case 'null' :
return $name . ' is null';
return $name . ' IS NULL ';
break;
case 'and' :
return $name . ' & ' . $value;
@ -248,7 +257,7 @@ class Condition
return $name . ' ~ ' . $value;
break;
case 'between' :
return $name . ' between ' . $value[0] . ' and ' . $value[1];
return $name . ' BETWEEN ' . $value[0] . ' AND ' . $value[1];
break;
}
}

View file

@ -29,7 +29,7 @@ class ConditionGroup
* @param string $pipe
* @return void
*/
function __construct($conditions, $pipe = "")
function __construct($conditions, $pipe = 'and')
{
$this->conditions = array();
foreach($conditions as $condition)
@ -89,7 +89,7 @@ class ConditionGroup
if($this->pipe !== "" && trim($group) !== '')
{
$group = $this->pipe . ' (' . $group . ')';
$group = strtoupper($this->pipe) . ' (' . $group . ')';
}
$this->_group = $group;
@ -105,11 +105,24 @@ class ConditionGroup
{
$args = array();
foreach($this->conditions as $condition)
{
$arg = $condition->getArgument();
if($arg)
{
if($condition instanceof ConditionGroup)
{
foreach($condition->getArguments() as $arg)
{
if($arg)
{
$args[] = $arg;
}
}
}
else
{
$args[] = $arg;
$arg = $condition->getArgument();
if($arg)
{
$args[] = $arg;
}
}
}
return $args;

View file

@ -68,7 +68,7 @@ class ConditionWithArgument extends Condition
$q = '?';
}
}
return $this->pipe . ' ' . $this->getConditionPart($q);
return strtoupper($this->pipe) . ' ' . $this->getConditionPart($q);
}
/**

View file

@ -42,7 +42,7 @@ class SelectExpression extends Expression
*/
function getExpression()
{
return sprintf("%s%s", $this->column_name, $this->column_alias ? " as " . $this->column_alias : "");
return sprintf("%s%s", $this->column_name, $this->column_alias ? (' AS ' . $this->column_alias) : "");
}
function show()

View file

@ -37,7 +37,7 @@ class OrderByColumn
{
$result = $this->getColumnName();
$result .= ' ';
$result .= is_a($this->sort_order, 'Argument') ? $this->sort_order->getValue() : $this->sort_order;
$result .= is_a($this->sort_order, 'Argument') ? $this->sort_order->getValue() : strtoupper($this->sort_order);
return $result;
}

View file

@ -1,71 +0,0 @@
<?php
/* Copyright (C) NAVER <http://www.navercorp.com> */
/**
* @author NAVER (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class CubridTableWithHint extends Table
{
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
*/
var $alias;
/**
* index hint list
* @var array
*/
var $index_hints_list;
/**
* constructor
* @param string $name
* @param string $alias
* @param array $index_hints_list
* @return void
*/
function __construct($name, $alias = NULL, $index_hints_list)
{
parent::__construct($name, $alias);
$this->index_hints_list = $index_hints_list;
}
/**
* Return index hint string
* @return string
*/
function getIndexHintString()
{
$result = '';
// Retrieve table prefix, to add it to index name
$prefix = config('db.master.prefix');
foreach($this->index_hints_list as $index_hint)
{
$index_hint_type = $index_hint->getIndexHintType();
if($index_hint_type !== 'IGNORE')
{
$result .= $this->alias . '.'
. '"' . $prefix . substr($index_hint->getIndexName(), 1)
. ($index_hint_type == 'FORCE' ? '(+)' : '')
. ', ';
}
}
$result = substr($result, 0, -2);
return $result;
}
}
/* End of file CubridTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/CubridTableWithHint.class.php */

View file

@ -41,12 +41,19 @@ class JoinTable extends Table
function toString($with_value = true)
{
$part = $this->join_type . ' ' . $this->name;
$part .= $this->alias ? ' as ' . $this->alias : '';
$part .= ' on ';
$part = strtoupper($this->join_type) . ' ' . $this->name;
$part .= $this->alias ? (' AS ' . $this->alias) : '';
$part .= ' ON ';
$condition_count = 0;
foreach($this->conditions as $conditionGroup)
{
$part .= $conditionGroup->toString($with_value);
if($condition_count === 0)
{
$conditionGroup->setPipe("");
}
$part .= $conditionGroup->toString($with_value);
$condition_count++;
}
return $part;
}

View file

@ -1,66 +0,0 @@
<?php
/* Copyright (C) NAVER <http://www.navercorp.com> */
/**
* @author NAVER (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class MssqlTableWithHint extends Table
{
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
*/
var $alias;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var array
*/
var $index_hints_list;
/**
* constructor
* @param string $name
* @param string $alias
* @param string $index_hints_list
* @return void
*/
function __construct($name, $alias = NULL, $index_hints_list)
{
parent::__construct($name, $alias);
$this->index_hints_list = $index_hints_list;
}
function toString()
{
$result = parent::toString();
$index_hint_string = '';
$indexTypeList = array('USE' => 1, 'FORCE' => 1);
foreach($this->index_hints_list as $index_hint)
{
$index_hint_type = $index_hint->getIndexHintType();
if(isset($indexTypeList[$index_hint_type]))
{
$index_hint_string .= 'INDEX(' . $index_hint->getIndexName() . '), ';
}
}
if($index_hint_string != '')
{
$result .= ' WITH(' . substr($index_hint_string, 0, -2) . ') ';
}
return $result;
}
}
/* End of file MssqlTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/MssqlTableWithHint.class.php */

View file

@ -36,7 +36,7 @@ class Table
function toString()
{
//return $this->name;
return sprintf("%s%s", $this->name, $this->alias ? ' as ' . $this->alias : '');
return sprintf("%s%s", $this->name, $this->alias ? (' AS ' . $this->alias) : '');
}
function getName()

View file

@ -16,7 +16,7 @@ class ConditionGroupTag
* @var string|array value is ConditionTag object
*/
var $conditions;
/**
* pipe
* @var string
@ -29,7 +29,7 @@ class ConditionGroupTag
* @param string $pipe
* @return void
*/
function __construct($conditions, $pipe = "")
function __construct($conditions, $pipe = 'and')
{
$this->pipe = $pipe;
@ -37,11 +37,22 @@ class ConditionGroupTag
{
$conditions = array($conditions);
}
//var_dump($conditions);
foreach($conditions as $condition)
{
//if($condition->node_name === 'query') $this->conditions[] = new QueryTag($condition, true);
$this->conditions[] = new ConditionTag($condition);
if($condition->node_name === 'group')
{
$subconditions = $condition->condition;
$subgroups = $condition->group;
$subconditions = $subconditions ? (is_array($subconditions) ? $subconditions : [$subconditions]) : [];
$subgroups = $subgroups ? (is_array($subgroups) ? $subgroups : [$subgroups]) : [];
$this->conditions[] = new ConditionGroupTag(array_merge($subconditions, $subgroups), $condition->attrs->pipe);
}
else
{
$this->conditions[] = new ConditionTag($condition);
}
}
}
@ -58,8 +69,15 @@ class ConditionGroupTag
{
$conditions_string = 'array(' . PHP_EOL;
foreach($this->conditions as $condition)
{
$conditions_string .= $condition->getConditionString() . PHP_EOL . ',';
{
if($condition instanceof ConditionGroupTag)
{
$conditions_string .= $condition->getConditionGroupString() . PHP_EOL . ',';
}
else
{
$conditions_string .= $condition->getConditionString() . PHP_EOL . ',';
}
}
$conditions_string = substr($conditions_string, 0, -2); //remove ','
$conditions_string .= ')';

View file

@ -62,7 +62,7 @@ class ConditionTag
function __construct($condition)
{
$this->operation = $condition->attrs->operation;
$this->pipe = $condition->attrs->pipe;
$this->pipe = $condition->attrs->pipe ?: 'and';
$dbParser = DB::getParser();
$this->column_name = $dbParser->parseExpression($condition->attrs->column);

View file

@ -61,8 +61,12 @@ class ConditionsTag
$xml_groups = array($xml_groups);
}
foreach($xml_groups as $group)
{
$this->condition_groups[] = new ConditionGroupTag($group->condition, $group->attrs->pipe);
{
$subconditions = $group->condition;
$subgroups = $group->group;
$subconditions = $subconditions ? (is_array($subconditions) ? $subconditions : [$subconditions]) : [];
$subgroups = $subgroups ? (is_array($subgroups) ? $subgroups : [$subgroups]) : [];
$this->condition_groups[] = new ConditionGroupTag(array_merge($subconditions, $subgroups), $group->attrs->pipe);
}
}
}

View file

@ -66,10 +66,8 @@ $GLOBALS['RX_AUTOLOAD_FILE_MAP'] = array_change_key_case(array(
'UpdateExpressionWithoutArgument' => 'classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php',
'Limit' => 'classes/db/queryparts/limit/Limit.class.php',
'OrderByColumn' => 'classes/db/queryparts/order/OrderByColumn.class.php',
'CubridTableWithHint' => 'classes/db/queryparts/table/CubridTableWithHint.class.php',
'IndexHint' => 'classes/db/queryparts/table/IndexHint.class.php',
'JoinTable' => 'classes/db/queryparts/table/JoinTable.class.php',
'MssqlTableWithHint' => 'classes/db/queryparts/table/MssqlTableWithHint.class.php',
'MysqlTableWithHint' => 'classes/db/queryparts/table/MysqlTableWithHint.class.php',
'Table' => 'classes/db/queryparts/table/Table.class.php',
'DisplayHandler' => 'classes/display/DisplayHandler.class.php',