merge from 1.5.3 (~r10943)

git-svn-id: http://xe-core.googlecode.com/svn/trunk@10951 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
flyskyko 2012-07-27 02:47:10 +00:00
parent 7aa4798373
commit 54e3a72065
334 changed files with 13011 additions and 5561 deletions

View file

@ -1,18 +1,4 @@
<?php
/**
* @class DB
* @author NHN (developers@xpressengine.com)
* @brief base class of db* classes
* @version 0.1
*
* usage of db in XE is via xml
* there are 2 types of xml - query xml, schema xml
* in case of query xml, DB::executeQuery() method compiles xml file into php code and then execute it
* query xml has unique query id, and will be created in module
*
* queryid = module_name.query_name
**/
if(!defined('__XE_LOADED_DB_CLASS__')){
define('__XE_LOADED_DB_CLASS__', 1);
@ -45,11 +31,31 @@
require(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
}
/**
* - DB parent class
* - usage of db in XE is via xml
* - there are 2 types of xml - query xml, schema xml
* - in case of query xml, DB::executeQuery() method compiles xml file into php code and then execute it
* - query xml has unique query id, and will be created in module
* - queryid = module_name.query_name
*
* @author NHN (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
*/
class DB {
/**
* count cache path
* @var string
*/
var $count_cache_path = 'files/cache/db';
var $cond_operation = array( ///< operations for condition
/**
* operations for condition
* @var array
*/
var $cond_operation = array(
'equal' => '=',
'more' => '>=',
'excess' => '>',
@ -60,35 +66,84 @@
'null' => 'is null',
);
var $master_db = NULL; // master database connection string
var $slave_db = NULL; // array of slave databases connection strings
/**
* master database connection string
* @var array
*/
var $master_db = NULL;
/**
* array of slave databases connection strings
* @var array
*/
var $slave_db = NULL;
var $result = NULL; ///< result
var $result = NULL;
var $errno = 0; ///< error code (0 means no error)
var $errstr = ''; ///< error message
var $query = ''; ///< query string of latest executed query
/**
* error code (0 means no error)
* @var int
*/
var $errno = 0;
/**
* error message
* @var string
*/
var $errstr = '';
/**
* query string of latest executed query
* @var string
*/
var $query = '';
var $connection = '';
var $elapsed_time = 0; ///< elapsed time of latest executed query
var $elapsed_dbclass_time = 0; ///< elapsed time of latest executed query
/**
* elapsed time of latest executed query
* @var int
*/
var $elapsed_time = 0;
/**
* elapsed time of latest executed DB class
* @var int
*/
var $elapsed_dbclass_time = 0;
var $transaction_started = false; ///< transaction flag
/**
* transaction flag
* @var boolean
*/
var $transaction_started = false;
var $is_connected = false; ///< is db connected
var $is_connected = false;
var $supported_list = array(); ///< list of supported db, (will be written by classes/DB/DB***.class.php)
/**
* returns enable list in supported dbms list
* will be written by classes/DB/DB***.class.php
* @var array
*/
var $supported_list = array();
var $cache_file = 'files/cache/queries/'; ///< location of query cache
/**
* location of query cache
* @var string
*/
var $cache_file = 'files/cache/queries/';
var $db_type; ///< stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set
/**
* stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set
* @var string
*/
var $db_type;
var $use_prepared_statements; ///< flag to decide if class prepared statements or not (when supported); can be changed from db.config.info
/**
* flag to decide if class prepared statements or not (when supported); can be changed from db.config.info
* @var string
*/
var $use_prepared_statements;
/**
* @brief returns instance of certain db type
* @param[in] $db_type type of db
* @return instance
**/
/**
* returns instance of certain db type
* @param string $db_type type of db
* @return DB return DB object instance
*/
function &getInstance($db_type = NULL) {
if(!$db_type) $db_type = Context::getDBType();
if(!$db_type && Context::isInstalled()) return new Object(-1, 'msg_db_not_setted');
@ -108,32 +163,39 @@
return $GLOBALS['__DB__'][$db_type];
}
/**
* returns instance of db
* @return DB return DB object instance
*/
function create() {
return new DB;
}
/**
* @brief constructor
* @return none
**/
* constructor
* @return void
*/
function DB() {
$this->count_cache_path = _XE_PATH_.$this->count_cache_path;
$this->cache_file = _XE_PATH_.$this->cache_file;
}
/**
* @brief returns list of supported db
* @return list of supported db
**/
* returns list of supported dbms list
* this list return by directory list
* check by instance can creatable
* @return array return supported DBMS list
*/
function getSupportedList() {
$oDB = new DB();
return $oDB->_getSupportedList();
}
/**
* @brief returns list of enable in supported db
* @return list of enable in supported db
**/
* returns enable list in supported dbms list
* this list return by child class
* @return array return enable DBMS list in supported dbms list
*/
function getEnableList()
{
if(!$this->supported_list)
@ -152,9 +214,10 @@
}
/**
* @brief returns list of disable in supported db
* @return list of disable in supported db
**/
* returns list of disable in supported dbms list
* this list return by child class
* @return array return disable DBMS list in supported dbms list
*/
function getDisableList()
{
if(!$this->supported_list)
@ -173,10 +236,17 @@
}
/**
* @brief returns list of supported db
* @return list of supported db
**/
* returns list of supported dbms list
* this method is private
* @return array return supported DBMS list
*/
function _getSupportedList() {
static $get_supported_list = '';
if(is_array($get_supported_list)) {
$this->supported_list = $get_supported_list;
return $this->supported_list;
}
$get_supported_list = array();
$db_classes_path = _XE_PATH_."classes/db/";
$filter = "/^DB([^\.]+)\.class\.php/i";
$supported_list = FileHandler::readDir($db_classes_path, $filter, true);
@ -203,34 +273,37 @@
$obj->db_type = $db_type;
$obj->enable = $oDB->isSupported() ? true : false;
$this->supported_list[] = $obj;
$get_supported_list[] = $obj;
}
$this->supported_list = $get_supported_list;
return $this->supported_list;
}
/**
* @brief check if the db_type is supported
* @param[in] $db_type type of db to check
* @return true: is supported, false: is not supported
**/
* Return dbms supportable status
* The value is set in the child class
* @return boolean true: is supported, false: is not supported
*/
function isSupported() {
return FALSE;
}
/**
* @brief check if is connected
* @return true: connected, false: not connected
**/
* Return connected status
* @param string $type master or slave
* @param int $indx key of server list
* @return boolean true: connected, false: not connected
*/
function isConnected($type = 'master', $indx = 0) {
if($type == 'master') return $this->master_db["is_connected"] ? true : false;
else return $this->slave_db[$indx]["is_connected"] ? true : false;
}
/**
* @brief start recording log
* @return none
**/
* start recording log
* @param string $query query string
* @return void
*/
function actStart($query) {
$this->setError(0, 'success');
$this->query = $query;
@ -239,9 +312,9 @@
}
/**
* @brief finish recording log
* @return none
**/
* finish recording log
* @return void
*/
function actFinish() {
if(!$this->query) return;
$this->act_finish = getMicroTime();
@ -297,41 +370,43 @@
}
/**
* @brief set error
* @param[in] $errno error code
* @param[in] $errstr error message
* @return none
**/
* set error
* @param int $errno error code
* @param string $errstr error message
* @return void
*/
function setError($errno = 0, $errstr = 'success') {
$this->errno = $errno;
$this->errstr = $errstr;
}
/**
* @brief check if an error occured
* @return true: error, false: no error
**/
* Return error status
* @return boolean true: error, false: no error
*/
function isError() {
return $this->errno === 0 ? false : true;
}
/**
* @brief returns object of error info
* @return object of error
**/
* Returns object of error info
* @return object object of error
*/
function getError() {
$this->errstr = Context::convertEncodingStr($this->errstr);
return new Object($this->errno, $this->errstr);
}
/**
* @brief Run the result of the query xml file
* @param[in] $query_id query id (module.queryname
* @param[in] $args arguments for query
* @return result of query
* @remarks this function finds xml file or cache file of $query_id, compiles it and then execute it
**/
* Execute Query that result of the query xml file
* This function finds xml file or cache file of $query_id, compiles it and then execute it
* @param string $query_id query id (module.queryname)
* @param array|object $args arguments for query
* @param array $arg_columns column list. if you want get specific colums from executed result, add column list to $arg_columns
* @return object result of query
*/
function executeQuery($query_id, $args = NULL, $arg_columns = NULL) {
static $cache_file = array();
if(!$query_id) return new Object(-1, 'msg_invalid_queryid');
if(!$this->db_type) return;
@ -339,49 +414,51 @@
$this->query_id = $query_id;
$id_args = explode('.', $query_id);
if(count($id_args) == 2) {
$target = 'modules';
$module = $id_args[0];
$id = $id_args[1];
} elseif(count($id_args) == 3) {
$target = $id_args[0];
if(!in_array($target, array('addons','widgets'))){
$this->actDBClassFinish();
return;
}
$module = $id_args[1];
$id = $id_args[2];
}
if(!$target || !$module || !$id){
$this->actDBClassFinish();
return new Object(-1, 'msg_invalid_queryid');
}
if(!isset($cache_file[$query_id])) {
$id_args = explode('.', $query_id);
if(count($id_args) == 2) {
$target = 'modules';
$module = $id_args[0];
$id = $id_args[1];
} elseif(count($id_args) == 3) {
$target = $id_args[0];
$typeList = array('addons'=>1, 'widgets'=>1);
if(!isset($typeList[$target])){
$this->actDBClassFinish();
return;
}
$module = $id_args[1];
$id = $id_args[2];
}
if(!$target || !$module || !$id){
$this->actDBClassFinish();
return new Object(-1, 'msg_invalid_queryid');
}
$xml_file = sprintf('%s%s/%s/queries/%s.xml', _XE_PATH_, $target, $module, $id);
if(!file_exists($xml_file)){
$this->actDBClassFinish();
return new Object(-1, 'msg_invalid_queryid');
}
$xml_file = sprintf('%s%s/%s/queries/%s.xml', _XE_PATH_, $target, $module, $id);
if(!file_exists($xml_file)){
$this->actDBClassFinish();
return new Object(-1, 'msg_invalid_queryid');
}
// look for cache file
$cache_file = $this->checkQueryCacheFile($query_id, $xml_file);
$result = $this->_executeQuery($cache_file, $args, $query_id, $arg_columns);
// look for cache file
$cache_file[$query_id] = $this->checkQueryCacheFile($query_id, $xml_file);
}
$result = $this->_executeQuery($cache_file[$query_id], $args, $query_id, $arg_columns);
$this->actDBClassFinish();
// execute query
return $result;
$this->actDBClassFinish();
// execute query
return $result;
}
/**
* @brief look for cache file
* @param[in] $query_id query id for finding
* @param[in] $xml_file original xml query file
* @return cache file
**/
* Look for query cache file
* @param string $query_id query id for finding
* @param string $xml_file original xml query file
* @return string cache file
*/
function checkQueryCacheFile($query_id,$xml_file){
// first try finding cache file
$cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type);
@ -400,12 +477,13 @@
/**
* @brief execute query and return the result
* @param[in] $cache_file cache file of query
* @param[in] $source_args arguments for query
* @param[in] $query_id query id
* @return result of query
**/
* Execute query and return the result
* @param string $cache_file cache file of query
* @param array|object $source_args arguments for query
* @param string $query_id query id
* @param array $arg_columns column list. if you want get specific colums from executed result, add column list to $arg_columns
* @return object result of query
*/
function _executeQuery($cache_file, $source_args, $query_id, $arg_columns) {
global $lang;
@ -420,6 +498,7 @@
// execute appropriate query
switch($output->getAction()) {
case 'insert' :
case 'insert-select' :
$this->resetCountCache($output->tables);
$output = $this->_executeInsertAct($output);
break;
@ -449,11 +528,11 @@
/**
* @brief returns counter cache data
* @param[in] $tables tables to get data
* @param[in] $condition condition to get data
* @return count of cache data
**/
* Returns counter cache data
* @param array|string $tables tables to get data
* @param string $condition condition to get data
* @return int count of cache data
*/
function getCountCache($tables, $condition) {
return false;
if(!$tables) return false;
@ -483,12 +562,12 @@
}
/**
* @brief save counter cache data
* @param[in] $tables tables to save data
* @param[in] $condition condition to save data
* @param[in] $count count of cache data to save
* @return none
**/
* Save counter cache data
* @param array|string $tables tables to save data
* @param string $condition condition to save data
* @param int $count count of cache data to save
* @return void
*/
function putCountCache($tables, $condition, $count = 0) {
return false;
if(!$tables) return false;
@ -508,10 +587,10 @@
}
/**
* @brief reset counter cache data
* @param[in] $tables tables to reset cache data
* @return true: success, false: failed
**/
* Reset counter cache data
* @param array|string $tables tables to reset cache data
* @return boolean true: success, false: failed
*/
function resetCountCache($tables) {
return false;
if(!$tables) return false;
@ -528,9 +607,9 @@
}
/**
* @brief returns supported database list
* @return list of supported database
**/
* Returns supported database list
* @return array list of supported database
*/
function getSupportedDatabase(){
$result = array();
@ -545,12 +624,23 @@
return $result;
}
/**
* Drop tables
* @param string $table_name
* @return void
*/
function dropTable($table_name){
if(!$table_name) return;
$query = sprintf("drop table %s%s", $this->prefix, $table_name);
$this->_query($query);
}
/**
* Return select query string
* @param object $query
* @param boolean $with_values
* @return string
*/
function getSelectSql($query, $with_values = true){
$select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query");
@ -585,6 +675,13 @@
return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
}
/**
* Return delete query string
* @param object $query
* @param boolean $with_values
* @param boolean $with_priority
* @return string
*/
function getDeleteSql($query, $with_values = true, $with_priority = false){
$sql = 'DELETE ';
@ -603,6 +700,13 @@
return $sql;
}
/**
* Return update query string
* @param object $query
* @param boolean $with_values
* @param boolean $with_priority
* @return string
*/
function getUpdateSql($query, $with_values = true, $with_priority = false){
$columnsList = $query->getUpdateString($with_values);
if($columnsList == '') return new Object(-1, "Invalid query");
@ -618,6 +722,13 @@
return "UPDATE $priority $tables SET $columnsList ".$where;
}
/**
* Return insert query string
* @param object $query
* @param boolean $with_values
* @param boolean $with_priority
* @return string
*/
function getInsertSql($query, $with_values = true, $with_priority = false){
$tableName = $query->getFirstTableName();
$values = $query->getInsertString($with_values);
@ -626,12 +737,22 @@
return "INSERT $priority INTO $tableName \n $values";
}
/**
* Return index from slave server list
* @return int
*/
function _getSlaveConnectionStringIndex() {
$max = count($this->slave_db);
$indx = rand(0, $max - 1);
return $indx;
}
/**
* Return connection resource
* @param string $type use 'master' or 'slave'. default value is 'master'
* @param int $indx if indx value is NULL, return rand number in slave server list
* @return resource
*/
function _getConnection($type = 'master', $indx = NULL){
if($type == 'master'){
if(!$this->master_db['is_connected'])
@ -650,6 +771,10 @@
return $this->slave_db[$indx]["resource"];
}
/**
* check db information exists
* @return boolean
*/
function _dbInfoExists() {
if (!$this->master_db)
return false;
@ -658,13 +783,22 @@
return true;
}
/**
* DB disconnection
* this method is protected
* @param resource $connection
* @return void
*/
function _close($connection){
}
/**
* @brief DB disconnection
* */
/**
* DB disconnection
* @param string $type 'master' or 'slave'
* @param int $indx number in slave dbms server list
* @return void
*/
function close($type = 'master', $indx = 0) {
if (!$this->isConnected($type, $indx))
return;
@ -679,12 +813,19 @@
$connection["is_connected"] = false;
}
/**
* DB transaction start
* this method is protected
* @return boolean
*/
function _begin(){
return true;
}
/**
* @brief Begin transaction
* */
/**
* DB transaction start
* @return void
*/
function begin() {
if (!$this->isConnected() || $this->transaction_started)
return;
@ -693,13 +834,19 @@
$this->transaction_started = true;
}
/**
* DB transaction rollback
* this method is protected
* @return boolean
*/
function _rollback(){
return true;
}
/**
* @brief Rollback
* */
/**
* DB transaction rollback
* @return void
*/
function rollback() {
if (!$this->isConnected() || !$this->transaction_started)
return;
@ -707,12 +854,20 @@
$this->transaction_started = false;
}
/**
* DB transaction commit
* this method is protected
* @return boolean
*/
function _commit(){
return true;
}
/**
* @brief Commits
* */
/**
* DB transaction commit
* @param boolean $force regardless transaction start status or connect status, forced to commit
* @return void
*/
function commit($force = false) {
if (!$force && (!$this->isConnected() || !$this->transaction_started))
return;
@ -720,18 +875,24 @@
$this->transaction_started = false;
}
/**
* Execute the query
* this method is protected
* @param string $query
* @param resource $connection
* @return void
*/
function __query($query, $connection){
}
/**
* @brief : Run a query and fetch the result
*
* query: run a query and return the result \n
* fetch: NULL if no value is returned \n
* array object if rows are returned \n
* object if a row is returned \n
* return\n
* */
/**
* Execute the query
* this method is protected
* @param string $query
* @param resource $connection
* @return resource
*/
function _query($query, $connection = null) {
if($connection == null)
$connection = $this->_getConnection('master');
@ -747,9 +908,11 @@
return $result;
}
/**
* @brief DB settings and connect/close
* */
/**
* DB info settings
* this method is protected
* @return void
*/
function _setDBInfo(){
$db_info = Context::getDBInfo();
$this->master_db = $db_info->master_db;
@ -766,16 +929,33 @@
$this->use_prepared_statements = $db_info->use_prepared_statements;
}
/**
* DB Connect
* this method is protected
* @param array $connection
* @return void
*/
function __connect($connection){
}
/**
* If have a task after connection, add a taks in this method
* this method is protected
* @param resource $connection
* @return void
*/
function _afterConnect($connection){
}
/**
* @brief DB Connection
* */
/**
* DB Connect
* this method is protected
* @param string $type 'master' or 'slave'
* @param int $indx number in slave dbms server list
* @return void
*/
function _connect($type = 'master', $indx = 0) {
if ($this->isConnected($type, $indx))
return;
@ -804,20 +984,21 @@
$this->_afterConnect($result);
}
/**
* @brief start recording DBClass log
* @return none
**/
/**
* Start recording DBClass log
* @return void
*/
function actDBClassStart() {
$this->setError(0, 'success');
$this->act_dbclass_start = getMicroTime();
$this->elapsed_dbclass_time = 0;
}
/**
* @brief finish recording DBClass log
* @return none
**/
/**
* Finish recording DBClass log
* @return void
*/
function actDBClassFinish() {
if(!$this->query) return;
$this->act_dbclass_finish = getMicroTime();
@ -826,14 +1007,16 @@
$GLOBALS['__dbclass_elapsed_time__'] += $elapsed_dbclass_time;
}
/**
* Returns a database specific parser class
* used for escaping expressions and table/column identifiers
*
* Requires an implementation of the DB class (won't work if database is not set)
*
* @remarks singleton
*/
/**
* Returns a database specific parser instance
* used for escaping expressions and table/column identifiers
*
* Requires an implementation of the DB class (won't work if database is not set)
* this method is singleton
*
* @param boolean $force force load DBParser instance
* @return DBParser
*/
function &getParser($force = false){
static $dbParser = null;
if(!$dbParser || $force) {

View file

@ -1,28 +1,34 @@
<?php
/**
* @class DBCubrid
* @author NHN (developers@xpressengine.com)
* @brief Cubrid DBMS to use the class
* @version 1.0
* - DB child class
* - Cubrid DBMS to use the class
* - Works with CUBRID up to 8.4.1
*
* Works with CUBRID up to 8.4.1
**/
* @author NHN (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
*/
class DBCubrid extends DB
{
/**
* @brief CUBRID DB connection information
**/
var $prefix = 'xe_'; // / <prefix of XE tables(One more XE can be installed on a single DB)
var $cutlen = 12000; // /< max size of constant in CUBRID(if string is larger than this, '...'+'...' should be used)
* prefix of XE tables(One more XE can be installed on a single DB)
* @var string
*/
var $prefix = 'xe_';
/**
* max size of constant in CUBRID(if string is larger than this, '...'+'...' should be used)
* @var int
*/
var $cutlen = 12000;
var $comment_syntax = '/* %s */';
/**
* @brief column type used in CUBRID
* column type used in CUBRID
*
* column_type should be replaced for each DBMS's type
* becasue it uses commonly defined type in the schema/query xml
* @var array
**/
var $column_type = array(
'bignumber' => 'numeric(20)',
@ -37,8 +43,9 @@
);
/**
* @brief constructor
**/
* constructor
* @return void
*/
function DBCubrid()
{
$this->_setDBInfo();
@ -46,7 +53,8 @@
}
/**
* @brief create an instance of this class
* Create an instance of this class
* @return DBCubrid return DBCubrid object instance
*/
function create()
{
@ -54,8 +62,10 @@
}
/**
* @brief Return if installable
**/
* Return if supportable
* Check 'cubrid_connect' function exists.
* @return boolean
*/
function isSupported()
{
if (!function_exists('cubrid_connect')) return false;
@ -63,8 +73,11 @@
}
/**
* @brief DB Connection
**/
* 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)
{
// attempts to connect
@ -90,8 +103,11 @@
}
/**
* @brief DB disconnect
**/
* DB disconnection
* this method is private
* @param resource $connection
* @return void
*/
function _close($connection)
{
@cubrid_commit ($connection);
@ -100,8 +116,10 @@
}
/**
* @brief handles quatation of the string variables from the query
**/
* Handles quatation of the string variables from the query
* @param string $string
* @return string
*/
function addQuotes($string)
{
if (version_compare (PHP_VERSION, "5.9.0", "<") &&
@ -126,8 +144,10 @@
}
/**
* @brief Begin transaction
**/
* DB transaction start
* this method is private
* @return boolean
*/
function _begin()
{
if(__CUBRID_VERSION__ >= '8.4.0')
@ -139,8 +159,10 @@
}
/**
* @brief Rollback
**/
* DB transaction rollback
* this method is private
* @return boolean
*/
function _rollback()
{
$connection = $this->_getConnection('master');
@ -149,8 +171,10 @@
}
/**
* @brief Commit
**/
* DB transaction commit
* this method is private
* @return boolean
*/
function _commit()
{
$connection = $this->_getConnection('master');
@ -159,14 +183,12 @@
}
/**
* @brief : executing the query and fetching the result
*
* query: run a query and return the result\n
* fetch: NULL if no value returned \n
* array object if rows returned \n
* object if a row returned \n
* return\n
**/
* Execute the query
* this method is private
* @param string $query
* @param resource $connection
* @return resource
*/
function __query($query, $connection)
{
if($this->use_prepared_statements == 'Y')
@ -232,8 +254,11 @@
}
/**
* @brief Fetch the result
**/
* Fetch the result
* @param resource $result
* @param int|NULL $arrayIndexEndValue
* @return array
*/
function _fetch($result, $arrayIndexEndValue = NULL)
{
$output = array();
@ -281,8 +306,10 @@
}
/**
* @brief return the sequence value incremented by 1(auto_increment column only used in the CUBRID sequence table)
**/
* Return the sequence value incremented by 1
* Auto_increment column only used in the CUBRID sequence table
* @return int
*/
function getNextSequence()
{
$this->_makeSequence();
@ -295,8 +322,9 @@
}
/**
* @brief return if the table already exists
**/
* if the table already exists, set the status to GLOBALS
* @return void
*/
function _makeSequence()
{
if($_GLOBALS['XE_EXISTS_SEQUENCE']) return;
@ -337,8 +365,10 @@
/**
* brief return a table if exists
**/
* Check a table exists status
* @param string $target_name
* @return boolean
*/
function isTableExists ($target_name)
{
if($target_name == 'sequence') {
@ -362,8 +392,15 @@
}
/**
* @brief add a column to the table
**/
* Add a column to the table
* @param string $table_name table name
* @param string $column_name column name
* @param string $type column type, default value is 'number'
* @param int $size column size
* @param string|int $default default value
* @param boolean $notnull not null status, default value is false
* @return void
*/
function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
{
$type = strtoupper($this->column_type[$type]);
@ -397,8 +434,11 @@
}
/**
* @brief drop a column from the table
**/
* Drop a column from the table
* @param string $table_name table name
* @param string $column_name column name
* @return void
*/
function dropColumn ($table_name, $column_name)
{
$query = sprintf ("alter class \"%s%s\" drop \"%s\" ", $this->prefix, $table_name, $column_name);
@ -407,8 +447,11 @@
}
/**
* @brief return column information of the table
**/
* Check column exist status of the table
* @param string $table_name table name
* @param string $column_name column name
* @return boolean
*/
function isColumnExists ($table_name, $column_name)
{
$query = sprintf ("select \"attr_name\" from \"db_attribute\" where ". "\"attr_name\" ='%s' and \"class_name\" = '%s%s'", $column_name, $this->prefix, $table_name);
@ -423,10 +466,15 @@
}
/**
* @brief add an index to the table
* Add an index to the table
* $target_columns = array(col1, col2)
* $is_unique? unique : none
**/
* @param string $table_name table name
* @param string $index_name index name
* @param string|array $target_columns target column or columns
* @param boolean $is_unique
* @return void
*/
function addIndex ($table_name, $index_name, $target_columns, $is_unique = false)
{
if (!is_array ($target_columns)) {
@ -439,8 +487,12 @@
}
/**
* @brief drop an index from the table
**/
* Drop an index from the table
* @param string $table_name table name
* @param string $index_name index name
* @param boolean $is_unique
* @return void
*/
function dropIndex ($table_name, $index_name, $is_unique = false)
{
$query = sprintf ("drop %s index \"%s\" on \"%s%s\"", $is_unique?'unique':'', $this->prefix .$index_name, $this->prefix, $table_name);
@ -449,8 +501,11 @@
}
/**
* @brief return index information of the table
**/
* Check index status of the table
* @param string $table_name table name
* @param string $index_name index name
* @return boolean
*/
function isIndexExists ($table_name, $index_name)
{
$query = sprintf ("select \"index_name\" from \"db_index\" where ". "\"class_name\" = '%s%s' and \"index_name\" = '%s' ", $this->prefix, $table_name, $this->prefix .$index_name);
@ -464,6 +519,10 @@
return true;
}
/**
* Delete duplicated index of the table
* @return boolean
*/
function deleteDuplicateIndexes()
{
$query = sprintf("
@ -507,16 +566,20 @@
}
/**
* @brief creates a table by using xml file
**/
* Creates a table by using xml contents
* @param string $xml_doc xml schema contents
* @return void|object
*/
function createTableByXml ($xml_doc)
{
return $this->_createTable ($xml_doc);
}
/**
* @brief creates a table by using xml file
**/
* Creates a table by using xml file path
* @param string $file_name xml schema file path
* @return void|object
*/
function createTableByXmlFile ($file_name)
{
if (!file_exists ($file_name)) return;
@ -526,12 +589,14 @@
}
/**
* @brief create table by using the schema xml
* Create table by using the schema xml
*
* type : number, varchar, tinytext, text, bigtext, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
**/
* @param string $xml_doc xml schema contents
* @return void|object
*/
function _createTable ($xml_doc)
{
// xml parsing
@ -645,8 +710,11 @@
/**
* @brief handles insertAct
**/
* Handles insertAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeInsertAct($queryObject, $with_values = true)
{
if($this->use_prepared_statements == 'Y')
@ -668,8 +736,11 @@
}
/**
* @brief handles updateAct
**/
* Handles updateAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeUpdateAct($queryObject, $with_values = true)
{
if($this->use_prepared_statements == 'Y')
@ -689,8 +760,11 @@
/**
* @brief handles deleteAct
**/
* Handles deleteAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeDeleteAct($queryObject, $with_values = true)
{
if($this->use_prepared_statements == 'Y')
@ -710,11 +784,14 @@
}
/**
* @brief Handle selectAct
*
* to get a specific page list easily in select statement,\n
* Handle selectAct
* To get a specific page list easily in select statement,
* a method, navigation, is used
**/
* @param Object $queryObject
* @param resource $connection
* @param boolean $with_values
* @return Object
*/
function _executeSelectAct($queryObject, $connection = null, $with_values = true) {
if ($this->use_prepared_statements == 'Y') {
$this->param = $queryObject->getArguments();
@ -745,6 +822,11 @@
}
}
/**
* If have a error, return error object
* @param Object $queryObject
* @return Object
*/
function queryError($queryObject){
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()){
@ -759,6 +841,13 @@
return;
}
/**
* If select query execute, return page info
* @param Object $queryObject
* @param resource $connection
* @param boolean $with_values
* @return Object Object with page info containing
*/
function queryPageLimit($queryObject, $connection, $with_values){
$limit = $queryObject->getLimit();
// Total count
@ -834,11 +923,23 @@
return $buff;
}
function &getParser($force = FALSE){
$dbParser = new DBParser('"', '"', $this->prefix);
return $dbParser;
/**
* Return the DBParser
* @param boolean $force
* @return DBParser
*/
function getParser($force = FALSE){
return new DBParser('"', '"', $this->prefix);
}
/**
* If select query execute, return paging sql
* @param object $query
* @param boolean $with_values
* @param int $start_count
* @param int $list_count
* @return string select paging sql
*/
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
$select = $query->getSelectString($with_values);

View file

@ -1,27 +1,30 @@
<?php
/**
* @class DBMSSQL
* - DBMSSQL
* - Modified to use MSSQL driver by sol (sol@ngleader.com)
*
* @author NHN (developers@xpressengine.com)
* @brief Modified to use MSSQL driver by sol (sol@ngleader.com)
* @package /classes/db
* @version 0.1
**/
class DBMssql extends DB {
/**
* information to connect to DB
**/
var $prefix = 'xe'; // / <prefix of XE tables(One more XE can be installed on a single DB)
* prefix of XE tables(One more XE can be installed on a single DB)
* @var string
*/
var $prefix = 'xe';
var $param = array();
var $comment_syntax = '/* %s */';
/**
* @brief column type used in mssql
* column type used in mssql
*
* column_type should be replaced for each DBMS's type
* becasue it uses commonly defined type in the schema/query xml
**/
* @var array
*/
var $column_type = array(
'bignumber' => 'bigint',
'number' => 'int',
@ -34,32 +37,39 @@
);
/**
* @brief constructor
**/
* Constructor
* @return void
*/
function DBMssql() {
$this->_setDBInfo();
$this->_connect();
}
/**
* @brief create an instance of this class
* Create an instance of this class
* @return DBMssql return DBMssql object instance
*/
function create()
{
return new DBMssql;
}
/**
* @brief Return if installable
**/
/**
* Return if supportable
* Check 'sqlsrv' extension loaded.
* @return boolean
*/
function isSupported() {
if (!extension_loaded("sqlsrv")) return false;
return true;
}
/**
* @brief DB Connection
**/
/**
* DB Connect
* this method is private
* @param array $connection connection's value is db_hostname, db_database, db_userid, db_password
* @return resource
*/
function __connect($connection) {
//sqlsrv_configure( 'WarningsReturnAsErrors', 0 );
//sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
@ -75,18 +85,23 @@
return $result;
}
/**
* @brief DB disconnect
**/
/**
* DB disconnection
* this method is private
* @param resource $connection
* @return void
*/
function _close($connection) {
$this->commit();
sqlsrv_close($connection);
}
/**
* @brief handles quatation of the string variables from the query
**/
// TODO See what to do about this
/**
* Handles quatation of the string variables from the query
* @todo See what to do about this
* @param string $string
* @return string
*/
function addQuotes($string) {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
//if(!is_numeric($string)) $string = str_replace("'","''",$string);
@ -94,42 +109,46 @@
return $string;
}
/**
* @brief Begin transaction
**/
/**
* DB transaction start
* this method is private
* @return boolean
*/
function _begin() {
$connection = $this->_getConnection('master');
if(sqlsrv_begin_transaction($connection) === false) return;
return true;
}
/**
* @brief Rollback
**/
/**
* DB transaction rollback
* this method is private
* @return boolean
*/
function _rollback() {
$connection = $this->_getConnection('master');
sqlsrv_rollback($connection);
return true;
}
/**
* @brief Commit
**/
/**
* DB transaction commit
* this method is private
* @return boolean
*/
function _commit() {
$connection = $this->_getConnection('master');
sqlsrv_commit($connection);
return true;
}
/**
* @brief : executing the query and fetching the result
*
* query: run a query and return the result\n
* fetch: NULL if no value returned \n
* array object if rows returned \n
* object if a row returned \n
* return\n
**/
/**
* Execute the query
* this method is private
* @param string $query
* @param resource $connection
* @return resource|boolean Returns a statement resource on success and FALSE if an error occurred.
*/
function __query($query, $connection) {
$_param = array();
@ -183,6 +202,8 @@
* Parameters are sent as an array, where each parameter can be:
* - a PHP variable (by reference)
* - a PHP array (containng param value, type and direction) -> also needs to be sent by reference
* @param array $_param
* @return array
*/
function _getParametersByReference($_param)
{
@ -206,9 +227,12 @@
return $args;
}
/**
* @brief Fetch results
**/
/**
* Fetch results
* @param resource $result
* @param int|NULL $arrayIndexEndValue
* @return array
*/
function _fetch($result, $arrayIndexEndValue = NULL) {
$output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output;
@ -234,9 +258,11 @@
}
/**
* @brief Return sequence value incremented by 1(auto_increment is usd in the sequence table only)
**/
/**
* Return the sequence value incremented by 1
* Auto_increment column only used in the sequence table
* @return int
*/
function getNextSequence() {
$query = sprintf("insert into %ssequence (seq) values (ident_incr('%ssequence'))", $this->prefix, $this->prefix);
$this->_query($query);
@ -249,9 +275,11 @@
return $tmp->sequence;
}
/**
* @brief Return if a table already exists
**/
/**
* Check a table exists status
* @param string $target_name
* @return boolean
*/
function isTableExists($target_name) {
$query = sprintf("select name from sysobjects where name = '%s%s' and xtype='U'", $this->prefix, $this->addQuotes($target_name));
$result = $this->_query($query);
@ -261,9 +289,16 @@
return true;
}
/**
* @brief Add a column to a table
**/
/**
* Add a column to the table
* @param string $table_name table name
* @param string $column_name column name
* @param string $type column type, default value is 'number'
* @param int $size column size
* @param string|int $default default value
* @param boolean $notnull not null status, default value is false
* @return void
*/
function addColumn($table_name, $column_name, $type='number', $size='', $default = '', $notnull=false) {
if($this->isColumnExists($table_name, $column_name)) return;
$type = $this->column_type[$type];
@ -278,18 +313,24 @@
$this->_query($query);
}
/**
* @brief Delete a column from a table
**/
/**
* Drop a column from the table
* @param string $table_name table name
* @param string $column_name column name
* @return void
*/
function dropColumn($table_name, $column_name) {
if(!$this->isColumnExists($table_name, $column_name)) return;
$query = sprintf("alter table %s%s drop %s ", $this->prefix, $table_name, $column_name);
$this->_query($query);
}
/**
* @brief Return column information of a table
**/
/**
* Check column exist status of the table
* @param string $table_name table name
* @param string $column_name column name
* @return boolean
*/
function isColumnExists($table_name, $column_name) {
$query = sprintf("select syscolumns.name as name from syscolumns, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = syscolumns.id and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name);
$result = $this->_query($query);
@ -299,11 +340,16 @@
return true;
}
/**
* @brief Add an index to a table
* $target_columns = array(col1, col2)
* $is_unique? unique : none
**/
/**
* Add an index to the table
* $target_columns = array(col1, col2)
* $is_unique? unique : none
* @param string $table_name table name
* @param string $index_name index name
* @param string|array $target_columns target column or columns
* @param boolean $is_unique
* @return void
*/
function addIndex($table_name, $index_name, $target_columns, $is_unique = false) {
if($this->isIndexExists($table_name, $index_name)) return;
if(!is_array($target_columns)) $target_columns = array($target_columns);
@ -312,18 +358,25 @@
$this->_query($query);
}
/**
* @brief Drop an index from a table
**/
/**
* Drop an index from the table
* @param string $table_name table name
* @param string $index_name index name
* @param boolean $is_unique
* @return void
*/
function dropIndex($table_name, $index_name, $is_unique = false) {
if(!$this->isIndexExists($table_name, $index_name)) return;
$query = sprintf("drop index %s%s.%s", $this->prefix, $table_name, $index_name);
$this->_query($query);
}
/**
* @brief Return index information of a table
**/
/**
* Check index status of the table
* @param string $table_name table name
* @param string $index_name index name
* @return boolean
*/
function isIndexExists($table_name, $index_name) {
$query = sprintf("select sysindexes.name as name from sysindexes, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = sysindexes.id and sysindexes.name = '%s'", $this->prefix, $table_name, $index_name);
@ -335,16 +388,20 @@
return true;
}
/**
* @brief Create a table by using xml file
**/
/**
* Creates a table by using xml contents
* @param string $xml_doc xml schema contents
* @return void|object
*/
function createTableByXml($xml_doc) {
return $this->_createTable($xml_doc);
}
/**
* @brief Create a table by using xml file
**/
/**
* Creates a table by using xml file path
* @param string $file_name xml schema file path
* @return void|object
*/
function createTableByXmlFile($file_name) {
if(!file_exists($file_name)) return;
// read xml file
@ -352,13 +409,15 @@
return $this->_createTable($buff);
}
/**
* @brief generate a query statement to create a table by using schema xml
*
* type : number, varchar, text, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
**/
/**
* Create table by using the schema xml
*
* type : number, varchar, tinytext, text, bigtext, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
* @param string $xml_doc xml schema contents
* @return void|object
*/
function _createTable($xml_doc) {
// xml parsing
$oXml = new XmlParser();
@ -381,6 +440,7 @@
$unique_list = array();
$index_list = array();
$typeList = array('number'=>1, 'text'=>1);
foreach($columns as $column) {
$name = $column->attrs->name;
$type = $column->attrs->type;
@ -395,7 +455,7 @@
$column_schema[] = sprintf('[%s] %s%s %s %s %s',
$name,
$this->column_type[$type],
!in_array($type,array('number','text'))&&$size?'('.$size.')':'',
!isset($typeList[$type])&&$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'null',
$auto_increment?'identity(1,1)':''
@ -433,25 +493,37 @@
}
/**
* @brief Handle the insertAct
**/
// TODO Lookup _filterNumber against sql injection - see if it is still needed and how to integrate
/**
* Handles insertAct
* @todo Lookup _filterNumber against sql injection - see if it is still needed and how to integrate
* @param Object $queryObject
* @return resource
*/
function _executeInsertAct($queryObject) {
$query = $this->getInsertSql($queryObject, false);
$this->param = $queryObject->getArguments();
return $this->_query($query);
}
/**
* @brief Handle updateAct
**/
/**
* Handles updateAct
* @param Object $queryObject
* @return resource
*/
function _executeUpdateAct($queryObject) {
$query = $this->getUpdateSql($queryObject, false);
$this->param = $queryObject->getArguments();
return $this->_query($query);
}
/**
* Return update query string
* @param object $query
* @param boolean $with_values
* @param boolean $with_priority
* @return string
*/
function getUpdateSql($query, $with_values = true, $with_priority = false){
$columnsList = $query->getUpdateString($with_values);
if($columnsList == '') return new Object(-1, "Invalid query");
@ -473,15 +545,23 @@
return "UPDATE $priority $alias_list SET $columnsList FROM ".$from.$where;
}
/**
* @brief Handle deleteAct
**/
/**
* Handles deleteAct
* @param Object $queryObject
* @return resource
*/
function _executeDeleteAct($queryObject) {
$query = $this->getDeleteSql($queryObject, false);
$this->param = $queryObject->getArguments();
return $this->_query($query);
}
/**
* Return select query string
* @param object $query
* @param boolean $with_values
* @return string
*/
function getSelectSql($query, $with_values = TRUE){
$with_values = false;
@ -519,12 +599,14 @@
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
}
/**
* @brief Handle selectAct
*
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
**/
/**
* Handle selectAct
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
* @param Object $queryObject
* @param resource $connection
* @return Object
*/
function _executeSelectAct($queryObject, $connection = null) {
$query = $this->getSelectSql($queryObject);
@ -540,11 +622,20 @@
else return $this->queryPageLimit($queryObject, $result, $connection);
}
function &getParser($force = FALSE){
$dbParser = new DBParser("[", "]", $this->prefix);
return $dbParser;
/**
* Return the DBParser
* @param boolean $force
* @return DBParser
*/
function getParser($force = FALSE){
return new DBParser("[", "]", $this->prefix);
}
/**
* If have a error, return error object
* @param Object $queryObject
* @return Object
*/
function queryError($queryObject){
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()){
@ -559,6 +650,13 @@
return;
}
/**
* If select query execute, return page info
* @param Object $queryObject
* @param resource $result
* @param resource $connection
* @return Object Object with page info containing
*/
function queryPageLimit($queryObject, $result, $connection) {
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) {
@ -615,7 +713,7 @@
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
return $buff;
}
$start_count = ($page - 1) * $list_count;
$this->param = $queryObject->getArguments();
$virtual_no = $total_count - $start_count;

View file

@ -1,29 +1,30 @@
<?php
/**
* @class DBMysql
* @author NHN (developers@xpressengine.com)
* @brief Class to use MySQL DBMS
* @version 0.1
*
* Class to use MySQL DBMS
* mysql handling class
*
* Does not use prepared statements, since mysql driver does not support them
**/
*
* @author NHN (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
*/
class DBMysql extends DB {
/**
* @brief Connection information for Mysql DB
**/
var $prefix = 'xe_'; // / <prefix of a tablename (One or more XEs can be installed in a single DB)
* prefix of a tablename (One or more XEs can be installed in a single DB)
* @var string
*/
var $prefix = 'xe_'; // / <
var $comment_syntax = '/* %s */';
/**
* @brief Column type used in MySQL
* Column type used in MySQL
*
* Becasue a common column type in schema/query xml is used for colum_type,
* it should be replaced properly for each DBMS
**/
* @var array
*/
var $column_type = array(
'bignumber' => 'bigint',
'number' => 'bigint',
@ -36,28 +37,38 @@ class DBMysql extends DB {
);
/**
* @brief constructor
**/
* Constructor
* @return void
*/
function DBMysql() {
$this->_setDBInfo();
$this->_connect();
}
/**
* Create an instance of this class
* @return DBMysql return DBMysql object instance
*/
function create() {
return new DBMysql;
}
/**
* @brief Return if it is installable
**/
/**
* Return if supportable
* Check 'mysql_connect' function exists.
* @return boolean
*/
function isSupported() {
if(!function_exists('mysql_connect')) return false;
return true;
}
/**
* @brief DB Connection
**/
/**
* 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) {
// Ignore if no DB information exists
if (strpos($connection["db_hostname"], ':') === false && $connection["db_port"])
@ -85,57 +96,72 @@ class DBMysql extends DB {
return $result;
}
/**
* If have a task after connection, add a taks in this method
* this method is private
* @param resource $connection
* @return void
*/
function _afterConnect($connection){
// Set utf8 if a database is MySQL
$this->_query("set names 'utf8'", $connection);
}
/**
* @brief DB disconnection
**/
/**
* DB disconnection
* this method is private
* @param resource $connection
* @return void
*/
function _close($connection) {
@mysql_close($connection);
}
/**
* @brief Add quotes on the string variables in a query
**/
/**
* Handles quatation of the string variables from the query
* @param string $string
* @return string
*/
function addQuotes($string) {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(!is_numeric($string)) $string = @mysql_real_escape_string($string);
return $string;
}
/**
* @brief Begin transaction
**/
/**
* DB transaction start
* this method is private
* @return boolean
*/
function _begin() {
return true;
}
/**
* @brief Rollback
**/
/**
* DB transaction rollback
* this method is private
* @return boolean
*/
function _rollback() {
return true;
}
/**
* @brief Commits
**/
/**
* DB transaction commit
* this method is private
* @return boolean
*/
function _commit() {
return true;
}
/**
* @brief : Run a query and fetch the result
*
* query: run a query and return the result \n
* fetch: NULL if no value is returned \n
* array object if rows are returned \n
* object if a row is returned \n
* return\n
**/
/**
* Execute the query
* this method is private
* @param string $query
* @param resource $connection
* @return resource
*/
function __query($query, $connection) {
// Run the query statement
$result = mysql_query($query, $connection);
@ -145,9 +171,12 @@ class DBMysql extends DB {
return $result;
}
/**
* @brief Fetch results
**/
/**
* Fetch the result
* @param resource $result
* @param int|NULL $arrayIndexEndValue
* @return array
*/
function _fetch($result, $arrayIndexEndValue = NULL) {
$output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output;
@ -163,9 +192,11 @@ class DBMysql extends DB {
return $output;
}
/**
* @brief Return sequence value incremented by 1(auto_increment is used in sequence table only in MySQL)
**/
/**
* Return the sequence value incremented by 1
* Auto_increment column only used in the sequence table
* @return int
*/
function getNextSequence() {
$query = sprintf("insert into `%ssequence` (seq) values ('0')", $this->prefix);
$this->_query($query);
@ -178,9 +209,12 @@ class DBMysql extends DB {
return $sequence;
}
/**
* @brief Function to obtain mysql old password(mysql only)
**/
/**
* Function to obtain mysql old password(mysql only)
* @param string $password input password
* @param string $saved_password saved password in DBMS
* @return boolean
*/
function isValidOldPassword($password, $saved_password) {
$query = sprintf("select password('%s') as password, old_password('%s') as old_password", $this->addQuotes($password), $this->addQuotes($password));
$result = $this->_query($query);
@ -189,9 +223,11 @@ class DBMysql extends DB {
return false;
}
/**
* @brief Return if a table already exists
**/
/**
* Check a table exists status
* @param string $target_name
* @return boolean
*/
function isTableExists($target_name) {
$query = sprintf("show tables like '%s%s'", $this->prefix, $this->addQuotes($target_name));
$result = $this->_query($query);
@ -200,9 +236,16 @@ class DBMysql extends DB {
return true;
}
/**
* @brief Add a column to a table
**/
/**
* Add a column to the table
* @param string $table_name table name
* @param string $column_name column name
* @param string $type column type, default value is 'number'
* @param int $size column size
* @param string|int $default default value
* @param boolean $notnull not null status, default value is false
* @return void
*/
function addColumn($table_name, $column_name, $type='number', $size='', $default = '', $notnull=false) {
$type = $this->column_type[$type];
if(strtoupper($type)=='INTEGER') $size = '';
@ -216,17 +259,23 @@ class DBMysql extends DB {
$this->_query($query);
}
/**
* @brief Delete a column from a table
**/
/**
* Drop a column from the table
* @param string $table_name table name
* @param string $column_name column name
* @return void
*/
function dropColumn($table_name, $column_name) {
$query = sprintf("alter table `%s%s` drop `%s` ", $this->prefix, $table_name, $column_name);
$this->_query($query);
}
/**
* @brief Return column information of a table
**/
/**
* Check column exist status of the table
* @param string $table_name table name
* @param string $column_name column name
* @return boolean
*/
function isColumnExists($table_name, $column_name) {
$query = sprintf("show fields from `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query);
@ -242,11 +291,16 @@ class DBMysql extends DB {
return false;
}
/**
* @brief Add an index to a table
* $target_columns = array(col1, col2)
* $is_unique? unique : none
**/
/**
* Add an index to the table
* $target_columns = array(col1, col2)
* $is_unique? unique : none
* @param string $table_name table name
* @param string $index_name index name
* @param string|array $target_columns target column or columns
* @param boolean $is_unique
* @return void
*/
function addIndex($table_name, $index_name, $target_columns, $is_unique = false) {
if(!is_array($target_columns)) $target_columns = array($target_columns);
@ -254,18 +308,25 @@ class DBMysql extends DB {
$this->_query($query);
}
/**
* @brief Drop an index from a table
**/
/**
* Drop an index from the table
* @param string $table_name table name
* @param string $index_name index name
* @param boolean $is_unique
* @return void
*/
function dropIndex($table_name, $index_name, $is_unique = false) {
$query = sprintf("alter table `%s%s` drop index `%s`", $this->prefix, $table_name, $index_name);
$this->_query($query);
}
/**
* @brief Return index information of a table
**/
/**
* Check index status of the table
* @param string $table_name table name
* @param string $index_name index name
* @return boolean
*/
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);
@ -281,16 +342,20 @@ class DBMysql extends DB {
return false;
}
/**
* @brief Create a table by using xml file
**/
/**
* Creates a table by using xml contents
* @param string $xml_doc xml schema contents
* @return void|object
*/
function createTableByXml($xml_doc) {
return $this->_createTable($xml_doc);
}
/**
* @brief Create a table by using xml file
**/
/**
* Creates a table by using xml file path
* @param string $file_name xml schema file path
* @return void|object
*/
function createTableByXmlFile($file_name) {
if(!file_exists($file_name)) return;
// read xml file
@ -298,13 +363,15 @@ class DBMysql extends DB {
return $this->_createTable($buff);
}
/**
* @brief generate a query statement to create a table by using schema xml
*
* type : number, varchar, text, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
**/
/**
* Create table by using the schema xml
*
* type : number, varchar, tinytext, text, bigtext, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
* @param string $xml_doc xml schema contents
* @return void|object
*/
function _createTable($xml_doc) {
// xml parsing
$oXml = new XmlParser();
@ -368,39 +435,51 @@ class DBMysql extends DB {
if(!$output) return false;
}
/**
* @brief Handle the insertAct
**/
/**
* Handles insertAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeInsertAct($queryObject, $with_values = true) {
$query = $this->getInsertSql($queryObject, $with_values, true);
if(is_a($query, 'Object')) return;
return $this->_query($query);
}
/**
* @brief Handle updateAct
**/
/**
* Handles updateAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeUpdateAct($queryObject, $with_values = true) {
$query = $this->getUpdateSql($queryObject, $with_values, true);
if(is_a($query, 'Object')) return;
return $this->_query($query);
}
/**
* @brief Handle deleteAct
**/
/**
* Handles deleteAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeDeleteAct($queryObject, $with_values = true) {
$query = $this->getDeleteSql($queryObject, $with_values, true);
if(is_a($query, 'Object')) return;
return $this->_query($query);
}
/**
* @brief Handle selectAct
*
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
**/
/**
* Handle selectAct
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
* @param Object $queryObject
* @param resource $connection
* @param boolean $with_values
* @return Object
*/
function _executeSelectAct($queryObject, $connection = null, $with_values = true) {
$limit = $queryObject->getLimit();
$result = NULL;
@ -423,26 +502,52 @@ class DBMysql extends DB {
}
}
/**
* Get the ID generated in the last query
* Return next sequence from sequence table
* This method use only mysql
* @return int
*/
function db_insert_id()
{
$connection = $this->_getConnection('master');
return mysql_insert_id($connection);
}
/**
* Fetch a result row as an object
* @param resource $result
* @return object
*/
function db_fetch_object(&$result)
{
return mysql_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 mysql_free_result($result);
}
/**
* Return the DBParser
* @param boolean $force
* @return DBParser
*/
function &getParser($force = FALSE){
$dbParser = new DBParser('`', '`', $this->prefix);
return $dbParser;
}
/**
* If have a error, return error object
* @param Object $queryObject
* @return Object
*/
function queryError($queryObject){
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()){
@ -457,6 +562,14 @@ class DBMysql extends DB {
return;
}
/**
* If select query execute, return page info
* @param Object $queryObject
* @param resource $result
* @param resource $connection
* @param boolean $with_values
* @return Object Object with page info containing
*/
function queryPageLimit($queryObject, $result, $connection, $with_values = true){
$limit = $queryObject->getLimit();
// Total count
@ -469,7 +582,7 @@ class DBMysql extends DB {
$uses_groupby = $queryObject->getGroupByString() != '';
if($uses_distinct || $uses_groupby) {
$count_query = sprintf('select %s %s %s %s'
, $temp_select
, $temp_select == '*' ? '1' : $temp_select
, 'FROM ' . $queryObject->getFromString($with_values)
, ($temp_where === '' ? '' : ' WHERE '. $temp_where)
, ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '')
@ -529,6 +642,14 @@ class DBMysql extends DB {
return $buff;
}
/**
* If select query execute, return paging sql
* @param object $query
* @param boolean $with_values
* @param int $start_count
* @param int $list_count
* @return string select paging sql
*/
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
$select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query");

View file

@ -1,79 +1,87 @@
<?php
require_once('DBMysql.class.php');
/**
* @class DBMysql_innodb
* @author NHN (developers@xpressengine.com)
* @brief class to use MySQL DBMS
* @version 0.1
*
* mysql innodb handling class
*
* Does not use prepared statements since the mysql driver does not support them
**/
/**
* Class to use MySQL innoDB DBMS
* mysql innodb handling class
*
* Does not use prepared statements, since mysql driver does not support them
*
* @author NHN (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
**/
class DBMysql_innodb extends DBMysql {
/**
* @brief constructor
**/
/**
* Constructor
* @return void
**/
function DBMysql_innodb() {
$this->_setDBInfo();
$this->_connect();
}
/**
* @brief create an instance of this class
* Create an instance of this class
* @return DBMysql_innodb return DBMysql_innodb object instance
*/
function create()
{
return new DBMysql_innodb;
}
/**
* @brief DB disconnection
**/
/**
* DB disconnection
* this method is private
* @param resource $connection
* @return void
*/
function _close($connection) {
$this->_query("commit", $connection);
@mysql_close($connection);
}
/**
* @brief Begin transaction
**/
/**
* DB transaction start
* this method is private
* @return boolean
*/
function _begin() {
$connection = $this->_getConnection('master');
$this->_query("begin", $connection);
return true;
}
/**
* @brief Rollback
**/
/**
* DB transaction rollback
* this method is private
* @return boolean
*/
function _rollback() {
$connection = $this->_getConnection('master');
$this->_query("rollback", $connection);
return true;
}
/**
* @brief Commits
**/
/**
* DB transaction commit
* this method is private
* @return boolean
*/
function _commit() {
$connection = $this->_getConnection('master');
$this->_query("commit", $connection);
return true;
}
/**
* @brief : Run a query and fetch the result
*
* query: run a query and return the result \n
* fetch: NULL if no value is returned \n
* array object if rows are returned \n
* object if a row is returned \n
* return\n
**/
/**
* Execute the query
* this method is private
* @param string $query
* @param resource $connection
* @return resource
*/
function __query($query, $connection) {
// Run the query statement
$result = @mysql_query($query, $connection);
@ -83,13 +91,15 @@
return $result;
}
/**
* @brief generate a query statement to create a table by using schema xml
*
* type : number, varchar, text, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
**/
/**
* Create table by using the schema xml
*
* type : number, varchar, tinytext, text, bigtext, char, date, \n
* opt : notnull, default, size\n
* index : primary key, index, unique\n
* @param string $xml_doc xml schema contents
* @return void|object
*/
function _createTable($xml_doc) {
// xml parsing
$oXml = new XmlParser();

View file

@ -1,44 +1,52 @@
<?php
require_once('DBMysql.class.php');
/**
* @class DBMysqli
* @author NHN (developers@xpressengine.com)
* @brief Class to use MySQL DBMS as mysqli_*
* @version 0.1
*
* mysql handling class
**/
/**
* Class to use MySQLi DBMS as mysqli_*
* mysql handling class
*
* Does not use prepared statements, since mysql driver does not support them
*
* @author NHN (developers@xpressengine.com)
* @package /classes/db
* @version 0.1
**/
class DBMysqli extends DBMysql {
/**
* @brief constructor
**/
/**
* Constructor
* @return void
**/
function DBMysqli() {
$this->_setDBInfo();
$this->_connect();
}
/**
* @brief Return if it is installable
**/
/**
* Return if supportable
* Check 'mysqli_connect' function exists.
* @return boolean
*/
function isSupported() {
if(!function_exists('mysqli_connect')) return false;
return true;
}
/**
* @brief create an instance of this class
* Create an instance of this class
* @return DBMysqli return DBMysqli object instance
*/
function create()
{
return new DBMysqli;
}
/**
* @brief DB Connection
**/
/**
* 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["db_port"]) {
@ -62,16 +70,21 @@
return $result;
}
/**
* @brief DB disconnection
**/
/**
* DB disconnection
* this method is private
* @param resource $connection
* @return void
*/
function _close($connection) {
mysqli_close($connection);
}
/**
* @brief Add quotes on the string variables in a query
**/
/**
* Handles quatation of the string variables from the query
* @param string $string
* @return string
*/
function addQuotes($string) {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(!is_numeric($string)){
@ -81,15 +94,13 @@
return $string;
}
/**
* @brief : Run a query and fetch the result
*
* query: run a query and return the result \n
* fetch: NULL if no value is returned \n
* array object if rows are returned \n
* object if a row is returned \n
* return\n
**/
/**
* Execute the query
* this method is private
* @param string $query
* @param resource $connection
* @return resource
*/
function __query($query, $connection) {
if($this->use_prepared_statements == 'Y')
{
@ -140,6 +151,13 @@
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();
@ -182,9 +200,12 @@
}
}
/**
* @brief Fetch results
**/
/**
* 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);
@ -247,6 +268,12 @@
return $output;
}
/**
* Handles insertAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeInsertAct($queryObject, $with_values = false){
if($this->use_prepared_statements != 'Y')
{
@ -258,6 +285,12 @@
return $result;
}
/**
* Handles updateAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeUpdateAct($queryObject, $with_values = false) {
if($this->use_prepared_statements != 'Y')
{
@ -269,6 +302,12 @@
return $result;
}
/**
* Handles deleteAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeDeleteAct($queryObject, $with_values = false) {
if($this->use_prepared_statements != 'Y')
{
@ -280,6 +319,15 @@
return $result;
}
/**
* Handle selectAct
* In order to get a list of pages easily when selecting \n
* it supports a method as navigation
* @param Object $queryObject
* @param resource $connection
* @param boolean $with_values
* @return Object
*/
function _executeSelectAct($queryObject, $connection = null, $with_values = false) {
if($this->use_prepared_statements != 'Y')
{
@ -291,17 +339,33 @@
return $result;
}
/**
* Get the ID generated in the last query
* Return next sequence from sequence table
* This method use only mysql
* @return int
*/
function db_insert_id()
{
$connection = $this->_getConnection('master');
return mysqli_insert_id($connection);
}
/**
* Fetch a result row as an object
* @param resource $result
* @return object
*/
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);
}

View file

@ -1,343 +1,497 @@
<?php
class Query extends Object {
var $queryID;
var $action;
var $priority;
var $columns;
var $tables;
var $conditions;
var $groups;
var $orderby;
var $limit;
var $arguments = null;
var $columnList = null;
var $_orderByString;
function Query($queryID = null
, $action = null
, $columns = null
, $tables = null
, $conditions = null
, $groups = null
, $orderby = null
, $limit = null
, $priority = null){
$this->queryID = $queryID;
$this->action = $action;
$this->priority = $priority;
if(!isset($tables)) return;
$this->columns = $this->setColumns($columns);
$this->tables = $this->setTables($tables);
$this->conditions = $this->setConditions($conditions);
$this->groups = $this->setGroups($groups);
$this->orderby = $this->setOrder($orderby);
$this->limit = $this->setLimit($limit);
}
function show(){
return true;
}
function setQueryId($queryID){
$this->queryID = $queryID;
}
function setAction($action){
$this->action = $action;
}
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
* @version 0.1
*/
class Query extends Object {
/**
* Query id, defined in query xml file
* @var string
*/
var $queryID;
/**
* DML type, ex) INSERT, DELETE, UPDATE, SELECT
* @var string
*/
var $action;
/**
* priority level ex)LOW_PRIORITY, HIGHT_PRIORITY
* @var string
*/
var $priority;
/**
* column list
* @var string|array
*/
var $columns;
/**
* table list
* @var string|array
*/
var $tables;
/**
* condition list
* @var string|array
*/
var $conditions;
/**
* group list
* @var string|array
*/
var $groups;
/**
* order list
* @var array
*/
var $orderby;
/**
* limit count
* @var int
*/
var $limit;
/**
* argument list
* @var array
*/
var $arguments = null;
/**
* column list
* @var array
*/
var $columnList = null;
/**
* order by text
* @var string
*/
var $_orderByString;
/**
* constructor
* @param string $queryID
* @param string $action
* @param string|array $columns
* @param string|array $tables
* @param string|array $conditions
* @param string|array $groups
* @param string|array $orderby
* @param int $limit
* @param string $priority
* @return void
*/
function Query($queryID = null
, $action = null
, $columns = null
, $tables = null
, $conditions = null
, $groups = null
, $orderby = null
, $limit = null
, $priority = null){
$this->queryID = $queryID;
$this->action = $action;
$this->priority = $priority;
if(!isset($tables)) return;
$this->columns = $this->setColumns($columns);
$this->tables = $this->setTables($tables);
$this->conditions = $this->setConditions($conditions);
$this->groups = $this->setGroups($groups);
$this->orderby = $this->setOrder($orderby);
$this->limit = $this->setLimit($limit);
}
function show(){
return true;
}
function setQueryId($queryID){
$this->queryID = $queryID;
}
function setAction($action){
$this->action = $action;
}
function setPriority($priority){
$this->priority = $priority;
}
function setColumnList($columnList){
$this->columnList = $columnList;
if(count($this->columnList) > 0) {
$selectColumns = array();
$dbParser = DB::getParser();
foreach($this->columnList as $columnName){
$columnName = $dbParser->escapeColumn($columnName);
$selectColumns[] = new SelectExpression($columnName);
}
unset($this->columns);
$this->columns = $selectColumns;
}
}
function setColumns($columns){
if(!isset($columns) || count($columns) === 0){
$this->columns = array(new StarExpression());
return;
}
if(!is_array($columns)) $columns = array($columns);
$this->columns = $columns;
}
function setTables($tables){
if(!isset($tables) || count($tables) === 0){
$this->setError(true);
$this->setMessage("You must provide at least one table for the query.");
return;
}
if(!is_array($tables)) $tables = array($tables);
$this->tables = $tables;
}
function setPriority($priority){
$this->priority = $priority;
function setSubquery($subquery){
$this->subquery = $subquery;
}
function setColumnList($columnList){
$this->columnList = $columnList;
if(count($this->columnList) > 0) {
$selectColumns = array();
$dbParser = DB::getParser();
foreach($this->columnList as $columnName){
$columnName = $dbParser->escapeColumn($columnName);
$selectColumns[] = new SelectExpression($columnName);
}
unset($this->columns);
$this->columns = $selectColumns;
}
}
function setColumns($columns){
if(!isset($columns) || count($columns) === 0){
$this->columns = array(new StarExpression());
return;
}
if(!is_array($columns)) $columns = array($columns);
$this->columns = $columns;
}
function setTables($tables){
if(!isset($tables) || count($tables) === 0){
$this->setError(true);
$this->setMessage("You must provide at least one table for the query.");
return;
}
if(!is_array($tables)) $tables = array($tables);
$this->tables = $tables;
}
function setConditions($conditions){
$this->conditions = array();
if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions);
foreach($conditions as $conditionGroup){
if($conditionGroup->show()) $this->conditions[] = $conditionGroup;
}
}
function setGroups($groups){
if(!isset($groups) || count($groups) === 0) return;
if(!is_array($groups)) $groups = array($groups);
$this->groups = $groups;
}
function setOrder($order){
if(!isset($order) || count($order) === 0) return;
if(!is_array($order)) $order = array($order);
$this->orderby = $order;
}
function setLimit($limit = NULL){
if(!isset($limit)) return;
$this->limit = $limit;
}
// START Fluent interface
function select($columns= null){
$this->action = 'select';
$this->setColumns($columns);
return $this;
}
function from($tables){
$this->setTables($tables);
return $this;
}
function where($conditions){
$this->setConditions($conditions);
return $this;
}
function groupBy($groups){
$this->setGroups($groups);
return $this;
}
function orderBy($order){
$this->setOrder($order);
return $this;
}
function limit($limit){
$this->setLimit($limit);
return $this;
}
// END Fluent interface
function getAction(){
return $this->action;
}
function getPriority(){
return $this->priority?'LOW_PRIORITY':'';
}
function getSelectString($with_values = true){
foreach($this->columns as $column){
if($column->show())
if($column->isSubquery()){
$select[] = $column->toString($with_values) . ' as '. $column->getAlias();
}
else
$select[] = $column->getExpression($with_values);
}
return trim(implode($select, ', '));
}
function getUpdateString($with_values = true){
foreach($this->columns as $column){
if($column->show())
$update[] = $column->getExpression($with_values);
}
return trim(implode($update, ', '));
}
function getInsertString($with_values = true){
$columnsList = '';
$valuesList = '';
foreach($this->columns as $column){
if($column->show()){
$columnsList .= $column->getColumnName() . ', ';
$valuesList .= $column->getValue($with_values) . ', ';
}
}
$columnsList = substr($columnsList, 0, -2);
$valuesList = substr($valuesList, 0, -2);
return "($columnsList) \n VALUES ($valuesList)";
}
function getTables(){
return $this->tables;
}
// from table_a
// from table_a inner join table_b on x=y
// from (select * from table a) as x
// from (select * from table t) as x inner join table y on y.x
function getFromString($with_values = true){
$from = '';
$simple_table_count = 0;
foreach($this->tables as $table){
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString($with_values) . ' ';
if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
$simple_table_count++;
}
if(trim($from) == '') return '';
return $from;
}
function getWhereString($with_values = true, $with_optimization = true){
$where = '';
$condition_count = 0;
foreach ($this->conditions as $conditionGroup) {
if ($condition_count === 0) {
$conditionGroup->setPipe("");
}
$condition_string = $conditionGroup->toString($with_values);
$where .= $condition_string;
$condition_count++;
}
if ($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order'))) {
if ($condition_count !== 0)
$where = '(' . $where . ') ';
foreach ($this->orderby as $order) {
$colName = $order->getColumnName();
if (strstr($colName, 'list_order') || strstr($colName, 'update_order')) {
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if ($condition_count === 0)
$opt_condition->setPipe("");
$where .= $opt_condition->toString($with_values) . ' ';
$condition_count++;
}
}
}
return trim($where);
}
function getGroupByString(){
$groupBy = '';
if($this->groups) if($this->groups[0] !== "")
$groupBy = implode(', ', $this->groups);
return $groupBy;
}
function getOrderByString(){
if(!$this->_orderByString){
if(count($this->orderby) === 0) return '';
$orderBy = '';
foreach($this->orderby as $order){
$orderBy .= $order->toString() .', ';
}
$orderBy = substr($orderBy, 0, -2);
$this->_orderByString = $orderBy;
}
return $this->_orderByString;
}
function getLimit(){
return $this->limit;
}
function getLimitString(){
$limit = '';
if(count($this->limit) > 0){
$limit = '';
$limit .= $this->limit->toString();
}
return $limit;
}
function getFirstTableName(){
return $this->tables[0]->getName();
}
function getArguments(){
if(!isset($this->arguments)){
$this->arguments = array();
// Join table arguments
if(count($this->tables) > 0)
{
foreach($this->tables as $table)
{
if($table->isJoinTable())
{
$args = $table->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
}
}
}
function setConditions($conditions){
$this->conditions = array();
if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions);
foreach($conditions as $conditionGroup){
if($conditionGroup->show()) $this->conditions[] = $conditionGroup;
}
}
function setGroups($groups){
if(!isset($groups) || count($groups) === 0) return;
if(!is_array($groups)) $groups = array($groups);
$this->groups = $groups;
}
function setOrder($order){
if(!isset($order) || count($order) === 0) return;
if(!is_array($order)) $order = array($order);
$this->orderby = $order;
}
function setLimit($limit = NULL){
if(!isset($limit)) return;
$this->limit = $limit;
}
// START Fluent interface
/**
* seleect set
* @param string|array $columns
* @return Query return Query instance
*/
function select($columns= null){
$this->action = 'select';
$this->setColumns($columns);
return $this;
}
/**
* from set
* @param string|array $tables
* @return Query return Query instance
*/
function from($tables){
$this->setTables($tables);
return $this;
}
/**
* where set
* @param string|array $conditions
* @return Query return Query instance
*/
function where($conditions){
$this->setConditions($conditions);
return $this;
}
/**
* groupBy set
* @param string|array $groups
* @return Query return Query instance
*/
function groupBy($groups){
$this->setGroups($groups);
return $this;
}
/**
* orderBy set
* @param string|array $order
* @return Query return Query instance
*/
function orderBy($order){
$this->setOrder($order);
return $this;
}
/**
* limit set
* @param int $limit
* @return Query return Query instance
*/
function limit($limit){
$this->setLimit($limit);
return $this;
}
// END Fluent interface
function getAction(){
return $this->action;
}
function getPriority(){
return $this->priority?'LOW_PRIORITY':'';
}
/**
* Return select sql
* @param boolean $with_values
* @return string
*/
function getSelectString($with_values = true){
foreach($this->columns as $column){
if($column->show())
if($column->isSubquery()){
$select[] = $column->toString($with_values) . ' as '. $column->getAlias();
}
else
$select[] = $column->getExpression($with_values);
}
return trim(implode($select, ', '));
}
/**
* Return update sql
* @param boolean $with_values
* @return string
*/
function getUpdateString($with_values = true){
foreach($this->columns as $column){
if($column->show())
$update[] = $column->getExpression($with_values);
}
return trim(implode($update, ', '));
}
/**
* Return insert sql
* @param boolean $with_values
* @return string
*/
function getInsertString($with_values = true){
$columnsList = '';
if($this->subquery){ // means we have insert-select
// Column arguments
if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns
foreach($this->columns as $column){
if($column->show()){
$args = $column->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
}
}
foreach($this->columns as $column){
$columnsList .= $column->getColumnName() . ', ';
}
// Condition arguments
if(count($this->conditions) > 0)
foreach($this->conditions as $conditionGroup){
$args = $conditionGroup->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
}
// Navigation arguments
if(count($this->orderby) > 0)
foreach($this->orderby as $order){
$args = $order->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
}
$columnsList = substr($columnsList, 0, -2);
$selectStatement = $this->subquery->toString($with_values);
$selectStatement = substr($selectStatement, 1, -1);
return "($columnsList) \n $selectStatement";
}
return $this->arguments;
}
}
?>
$valuesList = '';
foreach($this->columns as $column){
if($column->show()){
$columnsList .= $column->getColumnName() . ', ';
$valuesList .= $column->getValue($with_values) . ', ';
}
}
$columnsList = substr($columnsList, 0, -2);
$valuesList = substr($valuesList, 0, -2);
return "($columnsList) \n VALUES ($valuesList)";
}
function getTables(){
return $this->tables;
}
/**
* from table_a
* from table_a inner join table_b on x=y
* from (select * from table a) as x
* from (select * from table t) as x inner join table y on y.x
* @param boolean $with_values
* @return string
*/
function getFromString($with_values = true){
$from = '';
$simple_table_count = 0;
foreach($this->tables as $table){
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString($with_values) . ' ';
if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
$simple_table_count++;
}
if(trim($from) == '') return '';
return $from;
}
/**
* Return where sql
* @param boolean $with_values
* @param boolean $with_optimization
* @return string
*/
function getWhereString($with_values = true, $with_optimization = true){
$where = '';
$condition_count = 0;
foreach ($this->conditions as $conditionGroup) {
if ($condition_count === 0) {
$conditionGroup->setPipe("");
}
$condition_string = $conditionGroup->toString($with_values);
$where .= $condition_string;
$condition_count++;
}
if ($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order'))) {
if ($condition_count !== 0)
$where = '(' . $where . ') ';
foreach ($this->orderby as $order) {
$colName = $order->getColumnName();
if (strstr($colName, 'list_order') || strstr($colName, 'update_order')) {
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if ($condition_count === 0)
$opt_condition->setPipe("");
$where .= $opt_condition->toString($with_values) . ' ';
$condition_count++;
}
}
}
return trim($where);
}
/**
* Return groupby sql
* @return string
*/
function getGroupByString(){
$groupBy = '';
if($this->groups) if($this->groups[0] !== "")
$groupBy = implode(', ', $this->groups);
return $groupBy;
}
/**
* Return orderby sql
* @return string
*/
function getOrderByString(){
if(!$this->_orderByString){
if(count($this->orderby) === 0) return '';
$orderBy = '';
foreach($this->orderby as $order){
$orderBy .= $order->toString() .', ';
}
$orderBy = substr($orderBy, 0, -2);
$this->_orderByString = $orderBy;
}
return $this->_orderByString;
}
function getLimit(){
return $this->limit;
}
/**
* Return limit sql
* @return string
*/
function getLimitString(){
$limit = '';
if(count($this->limit) > 0){
$limit = '';
$limit .= $this->limit->toString();
}
return $limit;
}
function getFirstTableName(){
return $this->tables[0]->getName();
}
/**
* Return argument list
* @return array
*/
function getArguments(){
if(!isset($this->arguments)){
$this->arguments = array();
// Join table arguments
if(count($this->tables) > 0)
{
foreach($this->tables as $table)
{
if($table->isJoinTable() || is_a($table, 'Subquery'))
{
$args = $table->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
}
}
}
// Column arguments
if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns
foreach($this->columns as $column){
if($column->show()){
$args = $column->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
}
}
}
// Condition arguments
if(count($this->conditions) > 0)
foreach($this->conditions as $conditionGroup){
$args = $conditionGroup->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
}
// Navigation arguments
if(count($this->orderby) > 0)
foreach($this->orderby as $order){
$args = $order->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
}
}
return $this->arguments;
}
}
?>

View file

@ -1,9 +1,33 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
* @version 0.1
*/
class Subquery extends Query {
/**
* table alias
* @var string
*/
var $alias;
/**
* join type
* @var string
*/
var $join_type;
/**
* constructor
* @param string $alias
* @param string|array $columns
* @param string|array $tables
* @param string|array $conditions
* @param string|array $groups
* @param string|array $orderby
* @param int $limit
* @param string $join_type
* @return void
*/
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null){
$this->alias = $alias;

View file

@ -1,9 +1,27 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class Condition {
/**
* column name
* @var string
*/
var $column_name;
var $argument;
/**
* operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail',
* 'notlike_prefix', 'notlike', 'in', 'notin', 'not_in', 'and', 'or', 'xor', 'not', 'notequal', 'between'
* 'null', 'notnull'
* @var string
*/
var $operation;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_value;
@ -11,6 +29,14 @@
var $_show;
var $_value_to_string;
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function Condition($column_name, $argument, $operation, $pipe){
$this->column_name = $column_name;
$this->argument = $argument;
@ -23,6 +49,11 @@
return null;
}
/**
* value to string
* @param boolean $withValue
* @return string
*/
function toString($withValue = true){
if (!isset($this->_value_to_string)) {
if (!$this->show())
@ -41,10 +72,18 @@
return $this->_value_to_string;
}
/**
* change string without value
* @return string
*/
function toStringWithoutValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
/**
* change string with value
* @return string
*/
function toStringWithValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
@ -53,6 +92,9 @@
$this->pipe = $pipe;
}
/**
* @return boolean
*/
function show(){
if(!isset($this->_show)){
if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') {
@ -74,6 +116,7 @@
case 'notlike' :
case 'in' :
case 'notin' :
case 'not_in' :
case 'and':
case 'or':
case 'xor':
@ -82,17 +125,32 @@
// if variable is not set or is not string or number, return
if(!isset($this->_value)) { $this->_show = false; break;}
if($this->_value === '') { $this->_show = false; break; }
if(!in_array(gettype($this->_value), array('string', 'integer'))) {$this->_show = false; break; }
$tmpArray = array('string'=>1, 'integer'=>1);
if(!isset($tmpArray[gettype($this->_value)]))
{
$this->_show = false; break;
}
break;
case 'between' :
if(!is_array($this->_value)) { $this->_show = false; break;}
if(count($this->_value)!=2) {$this->_show = false; break;}
case 'null':
case 'notnull':
break;
default:
// If operation is not one of the above, means the condition is invalid
$this->_show = false;
}
}
}
return $this->_show;
}
/**
* Return condition string
* @param int|string|array $value
* @return string
*/
function getConditionPart($value) {
$name = $this->column_name;
$operation = $this->operation;
@ -131,6 +189,7 @@
return $name.' in '.$value;
break;
case 'notin' :
case 'not_in' :
return $name.' not in '.$value;
break;
case 'notequal' :

View file

@ -1,12 +1,30 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionGroup {
/**
* condition list
* @var array
*/
var $conditions;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_group;
var $_show;
/**
* constructor
* @param array $conditions
* @param string $pipe
* @return void
*/
function ConditionGroup($conditions, $pipe = "") {
$this->conditions = array();
foreach($conditions as $condition){
@ -28,6 +46,11 @@
$this->pipe = $pipe;
}
/**
* value to string
* @param boolean $with_value
* @return string
*/
function toString($with_value = true){
if(!isset($this->_group)){
$cond_indx = 0;
@ -48,6 +71,10 @@
return $this->_group;
}
/**
* return argument list
* @return array
*/
function getArguments(){
$args = array();
foreach($this->conditions as $condition){
@ -57,4 +84,4 @@
return $args;
}
}
?>
?>

View file

@ -1,7 +1,19 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionSubquery extends Condition {
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function ConditionSubquery($column_name, $argument, $operation, $pipe = ""){
parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $this->argument->toString();

View file

@ -1,7 +1,19 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithArgument extends Condition {
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function ConditionWithArgument($column_name, $argument, $operation, $pipe = ""){
if($argument === null) { $this->_show = false; return; }
parent::Condition($column_name, $argument, $operation, $pipe);
@ -13,6 +25,10 @@
return $this->argument;
}
/**
* change string without value
* @return string
*/
function toStringWithoutValue(){
$value = $this->argument->getUnescapedValue();
@ -37,6 +53,9 @@
return $this->pipe . ' ' . $this->getConditionPart($q);
}
/**
* @return boolean
*/
function show(){
if(!isset($this->_show)){
if(!$this->argument->isValid()) $this->_show = false;

View file

@ -1,9 +1,22 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithoutArgument extends Condition {
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function ConditionWithoutArgument($column_name, $argument, $operation, $pipe = ""){
parent::Condition($column_name, $argument, $operation, $pipe);
if(in_array($operation, array('in', 'notin'))){
$tmpArray = array('in'=>1, 'notin'=>1, 'not_in'=>1);
if(isset($tmpArray[$operation])){
if(is_array($argument)) $argument = implode($argument, ',');
$this->_value = '('. $argument .')';
}

View file

@ -1,15 +1,24 @@
<?php
/**
* @class ClickCountExpression
* ClickCountExpression
* @author Arnia Software
* @brief
*
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class ClickCountExpression extends SelectExpression {
/**
* click count
* @var bool
*/
var $click_count;
/**
* constructor
* @param string $column_name
* @param string $alias
* @param bool $click_count
* @return void
*/
function ClickCountExpression($column_name, $alias = NULL, $click_count = false){
parent::SelectExpression($column_name, $alias);
@ -25,9 +34,13 @@
return $this->click_count;
}
/**
* Return column expression, ex) column = column + 1
* @return string
*/
function getExpression(){
return "$this->column_name = $this->column_name + 1";
}
}
?>
?>

View file

@ -1,20 +1,35 @@
<?php
/**
* @class DeleteExpression
* @author Arnia Software
* @brief
* DeleteExpression
*
* @author Arnia Software
* @package /classes/db/queryparts/expression
* @version 0.1
* @todo Fix this class
*/
// TODO Fix this class
class DeleteExpression extends Expression {
/**
* column value
* @var mixed
*/
var $value;
/**
* constructor
* @param string $column_name
* @param mixed $value
* @return void
*/
function DeleteExpression($column_name, $value){
parent::Expression($column_name);
$this->value = $value;
}
/**
* Return column expression, ex) column = value
* @return string
*/
function getExpression(){
return "$this->column_name = $this->value";
}
@ -32,4 +47,4 @@
}
?>
?>

View file

@ -1,18 +1,28 @@
<?php
/**
* @class Expression
* @author Corina
* @brief Represents an expression used in select/update/insert/delete statements
* Expression
* Represents an expression used in select/update/insert/delete statements
*
* Examples (expressions are inside double square brackets):
* select [[columnA]], [[columnB as aliasB]] from tableA
* update tableA set [[columnA = valueA]] where columnB = something
*
* @author Corina
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class Expression {
/**
* column name
* @var string
*/
var $column_name;
/**
* constructor
* @param string $column_name
* @return void
*/
function Expression($column_name){
$this->column_name = $column_name;
}
@ -25,6 +35,10 @@
return false;
}
/**
* Return column expression, ex) column as alias
* @return string
*/
function getExpression() {
}
}
}

View file

@ -1,15 +1,24 @@
<?php
/**
* @class InsertExpression
* @author Arnia Software
* @brief
* InsertExpression
*
* @author Arnia Software
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class InsertExpression extends Expression {
/**
* argument
* @var object
*/
var $argument;
/**
* constructor
* @param string $column_name
* @param object $argument
* @return void
*/
function InsertExpression($column_name, $argument){
parent::Expression($column_name);
$this->argument = $argument;

View file

@ -1,26 +1,40 @@
<?php
/**
* @class SelectExpression
* @author Arnia Software
* @brief Represents an expresion that appears in the select clause
* SelectExpression
* Represents an expresion that appears in the select clause
*
* @remarks
* $column_name can be:
* - a table column name
* - an sql function - like count(*)
* - an sql expression - substr(column_name, 1, 8) or score1 + score2
* $column_name is already escaped
*
* @author Arnia Software
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class SelectExpression extends Expression {
/**
* column alias name
* @var string
*/
var $column_alias;
/**
* constructor
* @param string $column_name
* @param string $alias
* @return void
*/
function SelectExpression($column_name, $alias = NULL){
parent::Expression($column_name);
$this->column_alias = $alias;
}
/**
* Return column expression, ex) column as alias
* @return string
*/
function getExpression() {
return sprintf("%s%s", $this->column_name, $this->column_alias ? " as ".$this->column_alias : "");
}

View file

@ -1,14 +1,17 @@
<?php
/**
* @class StarExpression
* @author Corina
* @brief Represents the * in 'select * from ...' statements
* StarExpression
* Represents the * in 'select * from ...' statements
*
* @author Corina
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class StarExpression extends SelectExpression {
/**
* constructor, set the column to asterisk
* @return void
*/
function StarExpression(){
parent::SelectExpression("*");
}

View file

@ -1,25 +1,43 @@
<?php
/**
* @class UpdateExpression
* @author Arnia Software
* @brief
* UpdateExpression
*
* @author Arnia Software
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class UpdateExpression extends Expression {
/**
* argument
* @var object
*/
var $argument;
/**
* constructor
* @param string $column_name
* @param object $argument
* @return void
*/
function UpdateExpression($column_name, $argument){
parent::Expression($column_name);
$this->argument = $argument;
}
/**
* Return column expression, ex) column = value
* @return string
*/
function getExpression($with_value = true){
if($with_value)
return $this->getExpressionWithValue();
return $this->getExpressionWithoutValue();
}
/**
* Return column expression, ex) column = value
* @return string
*/
function getExpressionWithValue(){
$value = $this->argument->getValue();
$operation = $this->argument->getColumnOperation();
@ -28,6 +46,11 @@
return "$this->column_name = $value";
}
/**
* Return column expression, ex) column = ?
* Can use prepare statement
* @return string
*/
function getExpressionWithoutValue(){
$operation = $this->argument->getColumnOperation();
if(isset($operation))

View file

@ -1,14 +1,24 @@
<?php
/**
* @class UpdateExpression
* @author Arnia Software
* @brief
* UpdateExpression
*
* @author Arnia Software
* @package /classes/db/queryparts/expression
* @version 0.1
*/
class UpdateExpressionWithoutArgument extends UpdateExpression {
/**
* argument
* @var object
*/
var $argument;
/**
* constructor
* @param string $column_name
* @param object $argument
* @return void
*/
function UpdateExpressionWithoutArgument($column_name, $argument){
parent::Expression($column_name);
$this->argument = $argument;
@ -35,11 +45,11 @@
function getArgument(){
return null;
}
function getArguments(){
return array();
}
}
?>
?>

View file

@ -1,10 +1,38 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/limit
* @version 0.1
*/
class Limit {
/**
* start number
* @var int
*/
var $start;
/**
* list count
* @var int
*/
var $list_count;
/**
* page count
* @var int
*/
var $page_count;
/**
* current page
* @var int
*/
var $page;
/**
* constructor
* @param int $list_count
* @param int $page
* @param int $page_count
* @return void
*/
function Limit($list_count, $page= NULL, $page_count= NULL){
$this->list_count = $list_count;
if ($page){
@ -16,7 +44,11 @@
}
}
function isPageHandler(){//in case you choose to use query limit in other cases than page select
/**
* In case you choose to use query limit in other cases than page select
* @return boolean
*/
function isPageHandler(){
if ($this->page)return true;
else return false;
}
@ -34,4 +66,4 @@
else return $this->list_count->getValue();
}
}
?>
?>

View file

@ -1,8 +1,27 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/order
* @version 0.1
*/
class OrderByColumn {
/**
* column name
* @var string
*/
var $column_name;
/**
* sort order
* @var string
*/
var $sort_order;
/**
* constructor
* @param string $column_name
* @param string $sort_order
* @return void
*/
function OrderByColumn($column_name, $sort_order){
$this->column_name = $column_name;
$this->sort_order = $sort_order;
@ -28,4 +47,4 @@
}
}
?>
?>

View file

@ -1,15 +1,42 @@
<?php
/**
* @author NHN (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 CubridTableWithHint($name, $alias = NULL, $index_hints_list){
parent::Table($name, $alias);
$this->index_hints_list = $index_hints_list;
}
/**
* Return index hint string
* @return string
*/
function getIndexHintString(){
$result = '';
@ -32,4 +59,4 @@
}
}
?>
?>

View file

@ -1,9 +1,27 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class IndexHint {
/**
* index name
* @var string
*/
var $index_name;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var string
*/
var $index_hint_type;
/**
* constructor
* @param string $index_name
* @param string $index_hint_type
* @return void
*/
function IndexHint($index_name, $index_hint_type){
$this->index_name = $index_name;
$this->index_hint_type = $index_hint_type;
@ -18,4 +36,4 @@
}
}
?>
?>

View file

@ -1,19 +1,32 @@
<?php
/**
* @class JoinTable
* @author Arnia Software
* @brief
*
* @remarks
* class JoinTable
* $conditions in an array of Condition objects
*
*
* @author Arnia Software
* @package /classes/db/queryparts/table
* @version 0.1
*/
class JoinTable extends Table {
/**
* join type
* @var string
*/
var $join_type;
/**
* condition list
* @var array
*/
var $conditions;
/**
* constructor
* @param string $name
* @param string $alias
* @param string $join_type
* @param array $conditions
* @return void
*/
function JoinTable($name, $alias, $join_type, $conditions){
parent::Table($name, $alias);
$this->join_type = $join_type;
@ -43,4 +56,4 @@
}
?>
?>

View file

@ -1,10 +1,33 @@
<?php
/**
* @author NHN (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 MssqlTableWithHint($name, $alias = NULL, $index_hints_list){
parent::Table($name, $alias);
$this->index_hints_list = $index_hints_list;
@ -14,9 +37,10 @@
$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(in_array($index_hint_type, array('USE', 'FORCE')))
if(isset($indexTypeList[$index_hint_type]))
$index_hint_string .= 'INDEX(' . $index_hint->getIndexName() . '), ';
}
if($index_hint_string != ''){
@ -26,4 +50,4 @@
}
}
?>
?>

View file

@ -1,10 +1,33 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class MysqlTableWithHint 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 MysqlTableWithHint($name, $alias = NULL, $index_hints_list){
parent::Table($name, $alias);
$this->index_hints_list = $index_hints_list;
@ -33,4 +56,4 @@
}
}
?>
?>

View file

@ -1,9 +1,27 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class Table {
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
*/
var $alias;
/**
* constructor
* @param string $name
* @param string $alias
* @return void
*/
function Table($name, $alias = NULL){
$this->name = $name;
$this->alias = $alias;
@ -27,4 +45,4 @@
}
}
?>
?>