adds comments for phpDoc

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10739 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ovclas 2012-05-25 10:14:48 +00:00
parent 94be154d88
commit c65e9d3071
29 changed files with 1701 additions and 583 deletions

View file

@ -1,18 +1,4 @@
<?php <?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__')){ if(!defined('__XE_LOADED_DB_CLASS__')){
define('__XE_LOADED_DB_CLASS__', 1); define('__XE_LOADED_DB_CLASS__', 1);
@ -45,11 +31,31 @@
require(_XE_PATH_.'classes/db/queryparts/Subquery.class.php'); 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 { class DB {
/**
* count cache path
* @var string
*/
var $count_cache_path = 'files/cache/db'; var $count_cache_path = 'files/cache/db';
var $cond_operation = array( ///< operations for condition /**
* operations for condition
* @var array
*/
var $cond_operation = array(
'equal' => '=', 'equal' => '=',
'more' => '>=', 'more' => '>=',
'excess' => '>', 'excess' => '>',
@ -60,35 +66,84 @@
'null' => 'is null', '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 * error code (0 means no error)
var $query = ''; ///< query string of latest executed query * @var int
*/
var $errno = 0;
/**
* error message
* @var string
*/
var $errstr = '';
/**
* query string of latest executed query
* @var string
*/
var $query = '';
var $connection = ''; 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 * returns instance of certain db type
* @param[in] $db_type type of db * @param string $db_type type of db
* @return instance * @return DB return DB object instance
**/ */
function &getInstance($db_type = NULL) { function &getInstance($db_type = NULL) {
if(!$db_type) $db_type = Context::getDBType(); if(!$db_type) $db_type = Context::getDBType();
if(!$db_type && Context::isInstalled()) return new Object(-1, 'msg_db_not_setted'); if(!$db_type && Context::isInstalled()) return new Object(-1, 'msg_db_not_setted');
@ -108,32 +163,39 @@
return $GLOBALS['__DB__'][$db_type]; return $GLOBALS['__DB__'][$db_type];
} }
/**
* returns instance of db
* @return DB return DB object instance
*/
function create() { function create() {
return new DB; return new DB;
} }
/** /**
* @brief constructor * constructor
* @return none * @return void
**/ */
function DB() { function DB() {
$this->count_cache_path = _XE_PATH_.$this->count_cache_path; $this->count_cache_path = _XE_PATH_.$this->count_cache_path;
$this->cache_file = _XE_PATH_.$this->cache_file; $this->cache_file = _XE_PATH_.$this->cache_file;
} }
/** /**
* @brief returns list of supported db * returns list of supported dbms list
* @return list of supported db * this list return by directory list
**/ * check by instance can creatable
* @return array return supported DBMS list
*/
function getSupportedList() { function getSupportedList() {
$oDB = new DB(); $oDB = new DB();
return $oDB->_getSupportedList(); return $oDB->_getSupportedList();
} }
/** /**
* @brief returns list of enable in supported db * returns enable list in supported dbms list
* @return list of enable in supported db * this list return by child class
**/ * @return array return enable DBMS list in supported dbms list
*/
function getEnableList() function getEnableList()
{ {
if(!$this->supported_list) if(!$this->supported_list)
@ -152,9 +214,10 @@
} }
/** /**
* @brief returns list of disable in supported db * returns list of disable in supported dbms list
* @return list of disable in supported db * this list return by child class
**/ * @return array return disable DBMS list in supported dbms list
*/
function getDisableList() function getDisableList()
{ {
if(!$this->supported_list) if(!$this->supported_list)
@ -173,9 +236,10 @@
} }
/** /**
* @brief returns list of supported db * returns list of supported dbms list
* @return list of supported db * this method is private
**/ * @return array return supported DBMS list
*/
function _getSupportedList() { function _getSupportedList() {
static $get_supported_list = ''; static $get_supported_list = '';
if(is_array($get_supported_list)) { if(is_array($get_supported_list)) {
@ -216,27 +280,30 @@
} }
/** /**
* @brief check if the db_type is supported * Return dbms supportable status
* @param[in] $db_type type of db to check * The value is set in the child class
* @return true: is supported, false: is not supported * @return boolean true: is supported, false: is not supported
**/ */
function isSupported() { function isSupported() {
return FALSE; return FALSE;
} }
/** /**
* @brief check if is connected * Return connected status
* @return true: connected, false: not connected * @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) { function isConnected($type = 'master', $indx = 0) {
if($type == 'master') return $this->master_db["is_connected"] ? true : false; if($type == 'master') return $this->master_db["is_connected"] ? true : false;
else return $this->slave_db[$indx]["is_connected"] ? true : false; else return $this->slave_db[$indx]["is_connected"] ? true : false;
} }
/** /**
* @brief start recording log * start recording log
* @return none * @param string $query query string
**/ * @return void
*/
function actStart($query) { function actStart($query) {
$this->setError(0, 'success'); $this->setError(0, 'success');
$this->query = $query; $this->query = $query;
@ -245,9 +312,9 @@
} }
/** /**
* @brief finish recording log * finish recording log
* @return none * @return void
**/ */
function actFinish() { function actFinish() {
if(!$this->query) return; if(!$this->query) return;
$this->act_finish = getMicroTime(); $this->act_finish = getMicroTime();
@ -303,40 +370,41 @@
} }
/** /**
* @brief set error * set error
* @param[in] $errno error code * @param int $errno error code
* @param[in] $errstr error message * @param string $errstr error message
* @return none * @return void
**/ */
function setError($errno = 0, $errstr = 'success') { function setError($errno = 0, $errstr = 'success') {
$this->errno = $errno; $this->errno = $errno;
$this->errstr = $errstr; $this->errstr = $errstr;
} }
/** /**
* @brief check if an error occured * Return error status
* @return true: error, false: no error * @return boolean true: error, false: no error
**/ */
function isError() { function isError() {
return $this->errno === 0 ? false : true; return $this->errno === 0 ? false : true;
} }
/** /**
* @brief returns object of error info * Returns object of error info
* @return object of error * @return object object of error
**/ */
function getError() { function getError() {
$this->errstr = Context::convertEncodingStr($this->errstr); $this->errstr = Context::convertEncodingStr($this->errstr);
return new Object($this->errno, $this->errstr); return new Object($this->errno, $this->errstr);
} }
/** /**
* @brief Run the result of the query xml file * Execute Query that result of the query xml file
* @param[in] $query_id query id (module.queryname * This function finds xml file or cache file of $query_id, compiles it and then execute it
* @param[in] $args arguments for query * @param string $query_id query id (module.queryname)
* @return result of query * @param array|object $args arguments for query
* @remarks this function finds xml file or cache file of $query_id, compiles it and then execute it * @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) { function executeQuery($query_id, $args = NULL, $arg_columns = NULL) {
static $cache_file = array(); static $cache_file = array();
if(!$query_id) return new Object(-1, 'msg_invalid_queryid'); if(!$query_id) return new Object(-1, 'msg_invalid_queryid');
@ -384,11 +452,11 @@
/** /**
* @brief look for cache file * Look for query cache file
* @param[in] $query_id query id for finding * @param string $query_id query id for finding
* @param[in] $xml_file original xml query file * @param string $xml_file original xml query file
* @return cache file * @return string cache file
**/ */
function checkQueryCacheFile($query_id,$xml_file){ function checkQueryCacheFile($query_id,$xml_file){
// first try finding cache 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); $cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type);
@ -408,12 +476,13 @@
/** /**
* @brief execute query and return the result * Execute query and return the result
* @param[in] $cache_file cache file of query * @param string $cache_file cache file of query
* @param[in] $source_args arguments for query * @param array|object $source_args arguments for query
* @param[in] $query_id query id * @param string $query_id query id
* @return result of 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($cache_file, $source_args, $query_id, $arg_columns) { function _executeQuery($cache_file, $source_args, $query_id, $arg_columns) {
global $lang; global $lang;
@ -458,11 +527,11 @@
/** /**
* @brief returns counter cache data * Returns counter cache data
* @param[in] $tables tables to get data * @param array|string $tables tables to get data
* @param[in] $condition condition to get data * @param string $condition condition to get data
* @return count of cache data * @return int count of cache data
**/ */
function getCountCache($tables, $condition) { function getCountCache($tables, $condition) {
return false; return false;
if(!$tables) return false; if(!$tables) return false;
@ -492,12 +561,12 @@
} }
/** /**
* @brief save counter cache data * Save counter cache data
* @param[in] $tables tables to save data * @param array|string $tables tables to save data
* @param[in] $condition condition to save data * @param string $condition condition to save data
* @param[in] $count count of cache data to save * @param int $count count of cache data to save
* @return none * @return void
**/ */
function putCountCache($tables, $condition, $count = 0) { function putCountCache($tables, $condition, $count = 0) {
return false; return false;
if(!$tables) return false; if(!$tables) return false;
@ -517,10 +586,10 @@
} }
/** /**
* @brief reset counter cache data * Reset counter cache data
* @param[in] $tables tables to reset cache data * @param array|string $tables tables to reset cache data
* @return true: success, false: failed * @return boolean true: success, false: failed
**/ */
function resetCountCache($tables) { function resetCountCache($tables) {
return false; return false;
if(!$tables) return false; if(!$tables) return false;
@ -537,9 +606,9 @@
} }
/** /**
* @brief returns supported database list * Returns supported database list
* @return list of supported database * @return array list of supported database
**/ */
function getSupportedDatabase(){ function getSupportedDatabase(){
$result = array(); $result = array();
@ -554,12 +623,23 @@
return $result; return $result;
} }
/**
* Drop tables
* @param string $table_name
* @return void
*/
function dropTable($table_name){ function dropTable($table_name){
if(!$table_name) return; if(!$table_name) return;
$query = sprintf("drop table %s%s", $this->prefix, $table_name); $query = sprintf("drop table %s%s", $this->prefix, $table_name);
$this->_query($query); $this->_query($query);
} }
/**
* Return select query string
* @param object $query
* @param boolean $with_values
* @return string
*/
function getSelectSql($query, $with_values = true){ function getSelectSql($query, $with_values = true){
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query"); if($select == '') return new Object(-1, "Invalid query");
@ -594,6 +674,13 @@
return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; 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){ function getDeleteSql($query, $with_values = true, $with_priority = false){
$sql = 'DELETE '; $sql = 'DELETE ';
@ -612,6 +699,13 @@
return $sql; 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){ function getUpdateSql($query, $with_values = true, $with_priority = false){
$columnsList = $query->getUpdateString($with_values); $columnsList = $query->getUpdateString($with_values);
if($columnsList == '') return new Object(-1, "Invalid query"); if($columnsList == '') return new Object(-1, "Invalid query");
@ -627,6 +721,13 @@
return "UPDATE $priority $tables SET $columnsList ".$where; 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){ function getInsertSql($query, $with_values = true, $with_priority = false){
$tableName = $query->getFirstTableName(); $tableName = $query->getFirstTableName();
$values = $query->getInsertString($with_values); $values = $query->getInsertString($with_values);
@ -635,12 +736,22 @@
return "INSERT $priority INTO $tableName \n $values"; return "INSERT $priority INTO $tableName \n $values";
} }
/**
* Return index from slave server list
* @return int
*/
function _getSlaveConnectionStringIndex() { function _getSlaveConnectionStringIndex() {
$max = count($this->slave_db); $max = count($this->slave_db);
$indx = rand(0, $max - 1); $indx = rand(0, $max - 1);
return $indx; 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){ function _getConnection($type = 'master', $indx = NULL){
if($type == 'master'){ if($type == 'master'){
if(!$this->master_db['is_connected']) if(!$this->master_db['is_connected'])
@ -659,6 +770,10 @@
return $this->slave_db[$indx]["resource"]; return $this->slave_db[$indx]["resource"];
} }
/**
* check db information exists
* @return boolean
*/
function _dbInfoExists() { function _dbInfoExists() {
if (!$this->master_db) if (!$this->master_db)
return false; return false;
@ -667,13 +782,22 @@
return true; return true;
} }
/**
* DB disconnection
* this method is protected
* @param resource $connection
* @return void
*/
function _close($connection){ 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) { function close($type = 'master', $indx = 0) {
if (!$this->isConnected($type, $indx)) if (!$this->isConnected($type, $indx))
return; return;
@ -688,12 +812,19 @@
$connection["is_connected"] = false; $connection["is_connected"] = false;
} }
/**
* DB transaction start
* this method is protected
* @return boolean
*/
function _begin(){ function _begin(){
return true; return true;
} }
/**
* @brief Begin transaction /**
* */ * DB transaction start
* @return void
*/
function begin() { function begin() {
if (!$this->isConnected() || $this->transaction_started) if (!$this->isConnected() || $this->transaction_started)
return; return;
@ -702,13 +833,19 @@
$this->transaction_started = true; $this->transaction_started = true;
} }
/**
* DB transaction rollback
* this method is protected
* @return boolean
*/
function _rollback(){ function _rollback(){
return true; return true;
} }
/** /**
* @brief Rollback * DB transaction rollback
* */ * @return void
*/
function rollback() { function rollback() {
if (!$this->isConnected() || !$this->transaction_started) if (!$this->isConnected() || !$this->transaction_started)
return; return;
@ -716,12 +853,20 @@
$this->transaction_started = false; $this->transaction_started = false;
} }
/**
* DB transaction commit
* this method is protected
* @return boolean
*/
function _commit(){ function _commit(){
return true; 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) { function commit($force = false) {
if (!$force && (!$this->isConnected() || !$this->transaction_started)) if (!$force && (!$this->isConnected() || !$this->transaction_started))
return; return;
@ -729,18 +874,24 @@
$this->transaction_started = false; $this->transaction_started = false;
} }
/**
* Execute the query
* this method is protected
* @param string $query
* @param resource $connection
* @return void
*/
function __query($query, $connection){ function __query($query, $connection){
} }
/**
* @brief : Run a query and fetch the result /**
* * Execute the query
* query: run a query and return the result \n * this method is protected
* fetch: NULL if no value is returned \n * @param string $query
* array object if rows are returned \n * @param resource $connection
* object if a row is returned \n * @return resource
* return\n */
* */
function _query($query, $connection = null) { function _query($query, $connection = null) {
if($connection == null) if($connection == null)
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
@ -756,9 +907,11 @@
return $result; return $result;
} }
/** /**
* @brief DB settings and connect/close * DB info settings
* */ * this method is protected
* @return void
*/
function _setDBInfo(){ function _setDBInfo(){
$db_info = Context::getDBInfo(); $db_info = Context::getDBInfo();
$this->master_db = $db_info->master_db; $this->master_db = $db_info->master_db;
@ -775,16 +928,33 @@
$this->use_prepared_statements = $db_info->use_prepared_statements; $this->use_prepared_statements = $db_info->use_prepared_statements;
} }
/**
* DB Connect
* this method is protected
* @param array $connection
* @return void
*/
function __connect($connection){ 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){ 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) { function _connect($type = 'master', $indx = 0) {
if ($this->isConnected($type, $indx)) if ($this->isConnected($type, $indx))
return; return;
@ -813,20 +983,21 @@
$this->_afterConnect($result); $this->_afterConnect($result);
} }
/**
* @brief start recording DBClass log /**
* @return none * Start recording DBClass log
**/ * @return void
*/
function actDBClassStart() { function actDBClassStart() {
$this->setError(0, 'success'); $this->setError(0, 'success');
$this->act_dbclass_start = getMicroTime(); $this->act_dbclass_start = getMicroTime();
$this->elapsed_dbclass_time = 0; $this->elapsed_dbclass_time = 0;
} }
/** /**
* @brief finish recording DBClass log * Finish recording DBClass log
* @return none * @return void
**/ */
function actDBClassFinish() { function actDBClassFinish() {
if(!$this->query) return; if(!$this->query) return;
$this->act_dbclass_finish = getMicroTime(); $this->act_dbclass_finish = getMicroTime();
@ -835,14 +1006,16 @@
$GLOBALS['__dbclass_elapsed_time__'] += $elapsed_dbclass_time; $GLOBALS['__dbclass_elapsed_time__'] += $elapsed_dbclass_time;
} }
/** /**
* Returns a database specific parser class * Returns a database specific parser instance
* used for escaping expressions and table/column identifiers * used for escaping expressions and table/column identifiers
* *
* Requires an implementation of the DB class (won't work if database is not set) * Requires an implementation of the DB class (won't work if database is not set)
* * this method is singleton
* @remarks singleton *
*/ * @param boolean $force force load DBParser instance
* @return DBParser
*/
function &getParser($force = false){ function &getParser($force = false){
static $dbParser = null; static $dbParser = null;
if(!$dbParser || $force) { if(!$dbParser || $force) {

View file

@ -1,28 +1,34 @@
<?php <?php
/** /**
* @class DBCubrid * - DB child class
* @author NHN (developers@xpressengine.com) * - Cubrid DBMS to use the class
* @brief Cubrid DBMS to use the class * - Works with CUBRID up to 8.4.1
* @version 1.0
* *
* Works with CUBRID up to 8.4.1 * @author NHN (developers@xpressengine.com)
**/ * @package /classes/db
* @version 0.1
*/
class DBCubrid extends DB class DBCubrid extends DB
{ {
/** /**
* @brief CUBRID DB connection information * prefix of XE tables(One more XE can be installed on a single DB)
**/ * @var string
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) 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 */'; 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 * column_type should be replaced for each DBMS's type
* becasue it uses commonly defined type in the schema/query xml * becasue it uses commonly defined type in the schema/query xml
* @var array
**/ **/
var $column_type = array( var $column_type = array(
'bignumber' => 'numeric(20)', 'bignumber' => 'numeric(20)',
@ -37,8 +43,9 @@
); );
/** /**
* @brief constructor * constructor
**/ * @return void
*/
function DBCubrid() function DBCubrid()
{ {
$this->_setDBInfo(); $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() function create()
{ {
@ -54,8 +62,10 @@
} }
/** /**
* @brief Return if installable * Return if supportable
**/ * Check 'cubrid_connect' function exists.
* @return boolean
*/
function isSupported() function isSupported()
{ {
if (!function_exists('cubrid_connect')) return false; 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) function __connect($connection)
{ {
// attempts to connect // attempts to connect
@ -90,8 +103,11 @@
} }
/** /**
* @brief DB disconnect * DB disconnection
**/ * this method is private
* @param resource $connection
* @return void
*/
function _close($connection) function _close($connection)
{ {
@cubrid_commit ($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) function addQuotes($string)
{ {
if (version_compare (PHP_VERSION, "5.9.0", "<") && 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() function _begin()
{ {
if(__CUBRID_VERSION__ >= '8.4.0') if(__CUBRID_VERSION__ >= '8.4.0')
@ -139,8 +159,10 @@
} }
/** /**
* @brief Rollback * DB transaction rollback
**/ * this method is private
* @return boolean
*/
function _rollback() function _rollback()
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
@ -149,8 +171,10 @@
} }
/** /**
* @brief Commit * DB transaction commit
**/ * this method is private
* @return boolean
*/
function _commit() function _commit()
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
@ -159,14 +183,12 @@
} }
/** /**
* @brief : executing the query and fetching the result * Execute the query
* * this method is private
* query: run a query and return the result\n * @param string $query
* fetch: NULL if no value returned \n * @param resource $connection
* array object if rows returned \n * @return resource
* object if a row returned \n */
* return\n
**/
function __query($query, $connection) function __query($query, $connection)
{ {
if($this->use_prepared_statements == 'Y') 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) function _fetch($result, $arrayIndexEndValue = NULL)
{ {
$output = array(); $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() function getNextSequence()
{ {
$this->_makeSequence(); $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() function _makeSequence()
{ {
if($_GLOBALS['XE_EXISTS_SEQUENCE']) return; 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) function isTableExists ($target_name)
{ {
if($target_name == 'sequence') { 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) function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
{ {
$type = strtoupper($this->column_type[$type]); $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) function dropColumn ($table_name, $column_name)
{ {
$query = sprintf ("alter class \"%s%s\" drop \"%s\" ", $this->prefix, $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) 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); $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) * $target_columns = array(col1, col2)
* $is_unique? unique : none * $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) function addIndex ($table_name, $index_name, $target_columns, $is_unique = false)
{ {
if (!is_array ($target_columns)) { 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) 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); $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) 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); $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; return true;
} }
/**
* Delete duplicated index of the table
* @return boolean
*/
function deleteDuplicateIndexes() function deleteDuplicateIndexes()
{ {
$query = sprintf(" $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) function createTableByXml ($xml_doc)
{ {
return $this->_createTable ($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) function createTableByXmlFile ($file_name)
{ {
if (!file_exists ($file_name)) return; 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 * type : number, varchar, tinytext, text, bigtext, char, date, \n
* opt : notnull, default, size\n * opt : notnull, default, size\n
* index : primary key, index, unique\n * index : primary key, index, unique\n
**/ * @param string $xml_doc xml schema contents
* @return void|object
*/
function _createTable ($xml_doc) function _createTable ($xml_doc)
{ {
// xml parsing // 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) function _executeInsertAct($queryObject, $with_values = true)
{ {
if($this->use_prepared_statements == 'Y') 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) function _executeUpdateAct($queryObject, $with_values = true)
{ {
if($this->use_prepared_statements == 'Y') 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) function _executeDeleteAct($queryObject, $with_values = true)
{ {
if($this->use_prepared_statements == 'Y') if($this->use_prepared_statements == 'Y')
@ -710,11 +784,14 @@
} }
/** /**
* @brief Handle selectAct * Handle selectAct
* * To get a specific page list easily in select statement,
* to get a specific page list easily in select statement,\n
* a method, navigation, is used * 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) { function _executeSelectAct($queryObject, $connection = null, $with_values = true) {
if ($this->use_prepared_statements == 'Y') { if ($this->use_prepared_statements == 'Y') {
$this->param = $queryObject->getArguments(); $this->param = $queryObject->getArguments();
@ -745,6 +822,11 @@
} }
} }
/**
* If have a error, return error object
* @param Object $queryObject
* @return Object
*/
function queryError($queryObject){ function queryError($queryObject){
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()){ if ($limit && $limit->isPageHandler()){
@ -759,6 +841,13 @@
return; 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){ function queryPageLimit($queryObject, $connection, $with_values){
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
// Total count // Total count
@ -834,10 +923,23 @@
return $buff; return $buff;
} }
/**
* Return the DBParser
* @param boolean $force
* @return DBParser
*/
function getParser($force = FALSE){ function getParser($force = FALSE){
return new DBParser('"', '"', $this->prefix); 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) { function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) {
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);

View file

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

View file

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

View file

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

View file

@ -1,44 +1,52 @@
<?php <?php
require_once('DBMysql.class.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 { class DBMysqli extends DBMysql {
/** /**
* @brief constructor * Constructor
**/ * @return void
**/
function DBMysqli() { function DBMysqli() {
$this->_setDBInfo(); $this->_setDBInfo();
$this->_connect(); $this->_connect();
} }
/** /**
* @brief Return if it is installable * Return if supportable
**/ * Check 'mysqli_connect' function exists.
* @return boolean
*/
function isSupported() { function isSupported() {
if(!function_exists('mysqli_connect')) return false; if(!function_exists('mysqli_connect')) return false;
return true; return true;
} }
/** /**
* @brief create an instance of this class * Create an instance of this class
* @return DBMysqli return DBMysqli object instance
*/ */
function create() function create()
{ {
return new DBMysqli; 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) { function __connect($connection) {
// Attempt to connect // Attempt to connect
if ($connection["db_port"]) { if ($connection["db_port"]) {
@ -62,16 +70,21 @@
return $result; return $result;
} }
/** /**
* @brief DB disconnection * DB disconnection
**/ * this method is private
* @param resource $connection
* @return void
*/
function _close($connection) { function _close($connection) {
mysqli_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) { function addQuotes($string) {
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string)); if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(!is_numeric($string)){ if(!is_numeric($string)){
@ -81,15 +94,13 @@
return $string; return $string;
} }
/** /**
* @brief : Run a query and fetch the result * Execute the query
* * this method is private
* query: run a query and return the result \n * @param string $query
* fetch: NULL if no value is returned \n * @param resource $connection
* array object if rows are returned \n * @return resource
* object if a row is returned \n */
* return\n
**/
function __query($query, $connection) { function __query($query, $connection) {
if($this->use_prepared_statements == 'Y') if($this->use_prepared_statements == 'Y')
{ {
@ -140,6 +151,13 @@
return $result; return $result;
} }
/**
* Before execute query, prepare statement
* this method is private
* @param string $types
* @param array $params
* @return void
*/
function _prepareQueryParameters(&$types, &$params){ function _prepareQueryParameters(&$types, &$params){
$types = ''; $types = '';
$params = array(); $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) { function _fetch($result, $arrayIndexEndValue = NULL) {
if($this->use_prepared_statements != 'Y'){ if($this->use_prepared_statements != 'Y'){
return parent::_fetch($result, $arrayIndexEndValue); return parent::_fetch($result, $arrayIndexEndValue);
@ -247,6 +268,12 @@
return $output; return $output;
} }
/**
* Handles insertAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeInsertAct($queryObject, $with_values = false){ function _executeInsertAct($queryObject, $with_values = false){
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
@ -258,6 +285,12 @@
return $result; return $result;
} }
/**
* Handles updateAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeUpdateAct($queryObject, $with_values = false) { function _executeUpdateAct($queryObject, $with_values = false) {
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
@ -269,6 +302,12 @@
return $result; return $result;
} }
/**
* Handles deleteAct
* @param Object $queryObject
* @param boolean $with_values
* @return resource
*/
function _executeDeleteAct($queryObject, $with_values = false) { function _executeDeleteAct($queryObject, $with_values = false) {
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
@ -280,6 +319,15 @@
return $result; 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) { function _executeSelectAct($queryObject, $connection = null, $with_values = false) {
if($this->use_prepared_statements != 'Y') if($this->use_prepared_statements != 'Y')
{ {
@ -291,17 +339,33 @@
return $result; 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() function db_insert_id()
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');
return mysqli_insert_id($connection); return mysqli_insert_id($connection);
} }
/**
* Fetch a result row as an object
* @param resource $result
* @return object
*/
function db_fetch_object(&$result) function db_fetch_object(&$result)
{ {
return mysqli_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){ function db_free_result(&$result){
return mysqli_free_result($result); return mysqli_free_result($result);
} }

View file

@ -1,23 +1,88 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
* @version 0.1
*/
class Query extends Object { class Query extends Object {
/**
* Query id, defined in query xml file
* @var string
*/
var $queryID; var $queryID;
/**
* DML type, ex) INSERT, DELETE, UPDATE, SELECT
* @var string
*/
var $action; var $action;
/**
* priority level ex)LOW_PRIORITY, HIGHT_PRIORITY
* @var string
*/
var $priority; var $priority;
/**
* column list
* @var string|array
*/
var $columns; var $columns;
/**
* table list
* @var string|array
*/
var $tables; var $tables;
/**
* condition list
* @var string|array
*/
var $conditions; var $conditions;
/**
* group list
* @var string|array
*/
var $groups; var $groups;
/**
* order list
* @var array
*/
var $orderby; var $orderby;
/**
* limit count
* @var int
*/
var $limit; var $limit;
/**
* argument list
* @var array
*/
var $arguments = null; var $arguments = null;
/**
* column list
* @var array
*/
var $columnList = null; var $columnList = null;
/**
* order by text
* @var string
*/
var $_orderByString; 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 function Query($queryID = null
, $action = null , $action = null
, $columns = null , $columns = null
@ -128,32 +193,62 @@
} }
// START Fluent interface // START Fluent interface
/**
* seleect set
* @param string|array $columns
* @return Query return Query instance
*/
function select($columns= null){ function select($columns= null){
$this->action = 'select'; $this->action = 'select';
$this->setColumns($columns); $this->setColumns($columns);
return $this; return $this;
} }
/**
* from set
* @param string|array $tables
* @return Query return Query instance
*/
function from($tables){ function from($tables){
$this->setTables($tables); $this->setTables($tables);
return $this; return $this;
} }
/**
* where set
* @param string|array $conditions
* @return Query return Query instance
*/
function where($conditions){ function where($conditions){
$this->setConditions($conditions); $this->setConditions($conditions);
return $this; return $this;
} }
/**
* groupBy set
* @param string|array $groups
* @return Query return Query instance
*/
function groupBy($groups){ function groupBy($groups){
$this->setGroups($groups); $this->setGroups($groups);
return $this; return $this;
} }
/**
* orderBy set
* @param string|array $order
* @return Query return Query instance
*/
function orderBy($order){ function orderBy($order){
$this->setOrder($order); $this->setOrder($order);
return $this; return $this;
} }
/**
* limit set
* @param int $limit
* @return Query return Query instance
*/
function limit($limit){ function limit($limit){
$this->setLimit($limit); $this->setLimit($limit);
return $this; return $this;
@ -168,6 +263,11 @@
return $this->priority?'LOW_PRIORITY':''; return $this->priority?'LOW_PRIORITY':'';
} }
/**
* Return select sql
* @param boolean $with_values
* @return string
*/
function getSelectString($with_values = true){ function getSelectString($with_values = true){
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()) if($column->show())
@ -180,6 +280,11 @@
return trim(implode($select, ', ')); return trim(implode($select, ', '));
} }
/**
* Return update sql
* @param boolean $with_values
* @return string
*/
function getUpdateString($with_values = true){ function getUpdateString($with_values = true){
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()) if($column->show())
@ -188,6 +293,11 @@
return trim(implode($update, ', ')); return trim(implode($update, ', '));
} }
/**
* Return insert sql
* @param boolean $with_values
* @return string
*/
function getInsertString($with_values = true){ function getInsertString($with_values = true){
$columnsList = ''; $columnsList = '';
if($this->subquery){ // means we have insert-select if($this->subquery){ // means we have insert-select
@ -221,10 +331,14 @@
return $this->tables; return $this->tables;
} }
// from table_a /**
// from table_a inner join table_b on x=y * from table_a
// from (select * from table a) as x * from table_a inner join table_b on x=y
// from (select * from table t) as x inner join table y on y.x * 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){ function getFromString($with_values = true){
$from = ''; $from = '';
$simple_table_count = 0; $simple_table_count = 0;
@ -240,6 +354,12 @@
return $from; return $from;
} }
/**
* Return where sql
* @param boolean $with_values
* @param boolean $with_optimization
* @return string
*/
function getWhereString($with_values = true, $with_optimization = true){ function getWhereString($with_values = true, $with_optimization = true){
$where = ''; $where = '';
$condition_count = 0; $condition_count = 0;
@ -274,6 +394,10 @@
return trim($where); return trim($where);
} }
/**
* Return groupby sql
* @return string
*/
function getGroupByString(){ function getGroupByString(){
$groupBy = ''; $groupBy = '';
if($this->groups) if($this->groups[0] !== "") if($this->groups) if($this->groups[0] !== "")
@ -281,6 +405,10 @@
return $groupBy; return $groupBy;
} }
/**
* Return orderby sql
* @return string
*/
function getOrderByString(){ function getOrderByString(){
if(!$this->_orderByString){ if(!$this->_orderByString){
if(count($this->orderby) === 0) return ''; if(count($this->orderby) === 0) return '';
@ -298,6 +426,10 @@
return $this->limit; return $this->limit;
} }
/**
* Return limit sql
* @return string
*/
function getLimitString(){ function getLimitString(){
$limit = ''; $limit = '';
if(count($this->limit) > 0){ if(count($this->limit) > 0){
@ -311,6 +443,10 @@
return $this->tables[0]->getName(); return $this->tables[0]->getName();
} }
/**
* Return argument list
* @return array
*/
function getArguments(){ function getArguments(){
if(!isset($this->arguments)){ if(!isset($this->arguments)){
$this->arguments = array(); $this->arguments = array();

View file

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

View file

@ -1,9 +1,26 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class Condition { class Condition {
/**
* column name
* @var string
*/
var $column_name; var $column_name;
var $argument; var $argument;
/**
* operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail',
* 'notlike_prefix', 'notlike', 'in', 'notin', 'and', 'or', 'xor', 'not', 'notequal', 'between'
* @var string
*/
var $operation; var $operation;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe; var $pipe;
var $_value; var $_value;
@ -11,6 +28,14 @@
var $_show; var $_show;
var $_value_to_string; 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){ function Condition($column_name, $argument, $operation, $pipe){
$this->column_name = $column_name; $this->column_name = $column_name;
$this->argument = $argument; $this->argument = $argument;
@ -23,6 +48,11 @@
return null; return null;
} }
/**
* value to string
* @param boolean $withValue
* @return string
*/
function toString($withValue = true){ function toString($withValue = true){
if (!isset($this->_value_to_string)) { if (!isset($this->_value_to_string)) {
if (!$this->show()) if (!$this->show())
@ -41,10 +71,18 @@
return $this->_value_to_string; return $this->_value_to_string;
} }
/**
* change string without value
* @return string
*/
function toStringWithoutValue(){ function toStringWithoutValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value); return $this->pipe . ' ' . $this->getConditionPart($this->_value);
} }
/**
* change string with value
* @return string
*/
function toStringWithValue(){ function toStringWithValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value); return $this->pipe . ' ' . $this->getConditionPart($this->_value);
} }
@ -53,6 +91,9 @@
$this->pipe = $pipe; $this->pipe = $pipe;
} }
/**
* @return boolean
*/
function show(){ function show(){
if(!isset($this->_show)){ if(!isset($this->_show)){
if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') { if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') {
@ -93,6 +134,11 @@
return $this->_show; return $this->_show;
} }
/**
* Return condition string
* @param int|string|array $value
* @return string
*/
function getConditionPart($value) { function getConditionPart($value) {
$name = $this->column_name; $name = $this->column_name;
$operation = $this->operation; $operation = $this->operation;

View file

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

View file

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

View file

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

View file

@ -1,6 +1,18 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithoutArgument extends Condition { 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 = ""){ function ConditionWithoutArgument($column_name, $argument, $operation, $pipe = ""){
parent::Condition($column_name, $argument, $operation, $pipe); parent::Condition($column_name, $argument, $operation, $pipe);
if(in_array($operation, array('in', 'notin'))){ if(in_array($operation, array('in', 'notin'))){

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,10 +1,38 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/limit
* @version 0.1
*/
class Limit { class Limit {
/**
* start number
* @var int
*/
var $start; var $start;
/**
* list count
* @var int
*/
var $list_count; var $list_count;
/**
* page count
* @var int
*/
var $page_count; var $page_count;
/**
* current page
* @var int
*/
var $page; 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){ function Limit($list_count, $page= NULL, $page_count= NULL){
$this->list_count = $list_count; $this->list_count = $list_count;
if ($page){ 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; if ($this->page)return true;
else return false; else return false;
} }

View file

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

View file

@ -1,15 +1,42 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class CubridTableWithHint extends Table { class CubridTableWithHint extends Table {
/**
* table name
* @var string
*/
var $name; var $name;
/**
* table alias
* @var string
*/
var $alias; var $alias;
/**
* index hint list
* @var array
*/
var $index_hints_list; 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){ function CubridTableWithHint($name, $alias = NULL, $index_hints_list){
parent::Table($name, $alias); parent::Table($name, $alias);
$this->index_hints_list = $index_hints_list; $this->index_hints_list = $index_hints_list;
} }
/**
* Return index hint string
* @return string
*/
function getIndexHintString(){ function getIndexHintString(){
$result = ''; $result = '';

View file

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

View file

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

View file

@ -1,10 +1,33 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class MssqlTableWithHint extends Table { class MssqlTableWithHint extends Table {
/**
* table name
* @var string
*/
var $name; var $name;
/**
* table alias
* @var string
*/
var $alias; var $alias;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var array
*/
var $index_hints_list; 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){ function MssqlTableWithHint($name, $alias = NULL, $index_hints_list){
parent::Table($name, $alias); parent::Table($name, $alias);
$this->index_hints_list = $index_hints_list; $this->index_hints_list = $index_hints_list;

View file

@ -1,10 +1,33 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
* @version 0.1
*/
class MysqlTableWithHint extends Table { class MysqlTableWithHint extends Table {
/**
* table name
* @var string
*/
var $name; var $name;
/**
* table alias
* @var string
*/
var $alias; var $alias;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var array
*/
var $index_hints_list; 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){ function MysqlTableWithHint($name, $alias = NULL, $index_hints_list){
parent::Table($name, $alias); parent::Table($name, $alias);
$this->index_hints_list = $index_hints_list; $this->index_hints_list = $index_hints_list;

View file

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