From f00ba6f272a5dba551eca317c1527bc7b9eb6ce7 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sat, 30 Jun 2018 15:13:22 +0900 Subject: [PATCH] Remove and disable all other DB classes --- classes/db/DB.class.php | 16 +- classes/db/DBCubrid.class.php | 1326 --------------------------------- classes/db/DBMssql.class.php | 1161 ----------------------------- 3 files changed, 7 insertions(+), 2496 deletions(-) delete mode 100644 classes/db/DBCubrid.class.php delete mode 100644 classes/db/DBMssql.class.php diff --git a/classes/db/DB.class.php b/classes/db/DB.class.php index 95ad0755b..09ca3e2eb 100644 --- a/classes/db/DB.class.php +++ b/classes/db/DB.class.php @@ -23,9 +23,7 @@ class DB * @var array */ protected static $priority_dbms = array( - 'mysqli' => 6, - 'cubrid' => 2, - 'mssql' => 1 + 'mysql' => 1, ); /** @@ -108,7 +106,7 @@ class DB protected $cache_file = 'files/cache/queries/'; /** - * stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set + * stores database type, e.g. mysql * @var string */ public $db_type; @@ -136,15 +134,15 @@ class DB if(!$db_type) { $db_type = config('db.master.type'); - if (config('db.master.engine') === 'innodb') - { - $db_type .= '_innodb'; - } } if(!$db_type && Context::isInstalled()) { return new BaseObject(-1, 'msg_db_not_setted'); } + if(!strncmp($db_type, 'mysql', 5)) + { + $db_type = 'mysql'; + } if(!isset($GLOBALS['__DB__'])) { @@ -271,7 +269,7 @@ class DB // after creating instance of class, check is supported foreach ($supported_list as $db_type) { - if (strncasecmp($db_type, 'mysql', 5) === 0 && strtolower($db_type) !== 'mysqli') + if (strtolower($db_type) !== 'mysql') { continue; } diff --git a/classes/db/DBCubrid.class.php b/classes/db/DBCubrid.class.php deleted file mode 100644 index 0cc8217af..000000000 --- a/classes/db/DBCubrid.class.php +++ /dev/null @@ -1,1326 +0,0 @@ - */ - -/** - * - DB child class - * - Cubrid DBMS to use the class - * - Works with CUBRID up to 8.4.1 - * - * @author NAVER (developers@xpressengine.com) - * @package /classes/db - * @version 0.1 - */ -class DBCubrid extends DB -{ - - /** - * prefix of Rhymix tables(One more Rhymix can be installed on a single DB) - * @var string - */ - var $prefix = 'rx_'; - - /** - * max size of constant in CUBRID(if string is larger than this, '...'+'...' should be used) - * @var int - */ - var $cutlen = 12000; - var $comment_syntax = '/* %s */'; - - /** - * column type used in CUBRID - * - * column_type should be replaced for each DBMS's type - * becasue it uses commonly defined type in the schema/query xml - * @var array - */ - var $column_type = array( - 'bignumber' => 'numeric(20)', - 'number' => 'integer', - 'varchar' => 'character varying', - 'char' => 'character', - 'tinytext' => 'character varying(256)', - 'bigtext' => 'character varying(1073741823)', - 'text' => 'character varying(1073741823)', - 'date' => 'character varying(14)', - 'float' => 'float', - ); - - /** - * constructor - * @return void - */ - function __construct() - { - $this->_setDBInfo(); - $this->_connect(); - } - - /** - * DB Connect - * this method is private - * @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password - * @return resource - */ - function __connect($connection) - { - // attempts to connect - $result = @cubrid_connect($connection['host'], $connection['port'], $connection['database'], $connection['user'], $connection['pass']); - - // check connections - if(!$result) - { - $this->setError(-1, 'database connect fail'); - return; - } - - if(!defined('__CUBRID_VERSION__')) - { - $cubrid_version = cubrid_get_server_info($result); - $cubrid_version_elem = explode('.', $cubrid_version); - $cubrid_version = $cubrid_version_elem[0] . '.' . $cubrid_version_elem[1] . '.' . $cubrid_version_elem[2]; - define('__CUBRID_VERSION__', $cubrid_version); - } - - if(version_compare(__CUBRID_VERSION__, '9.0', '<')) - { - $this->setError(-1, 'Rhymix requires CUBRID 9.0 or later. Current CUBRID version is ' . __CUBRID_VERSION__); - return; - } - - $this->db_version = __CUBRID_VERSION__; - cubrid_set_autocommit($result, CUBRID_AUTOCOMMIT_TRUE); - - return $result; - } - - /** - * DB disconnection - * this method is private - * @param resource $connection - * @return void - */ - function _close($connection) - { - @cubrid_commit($connection); - @cubrid_disconnect($connection); - $this->transaction_started = FALSE; - } - - /** - * Handles quatation of the string variables from the query - * @param string $string - * @return string - */ - function addQuotes($string) - { - if(!is_numeric($string)) - { - /* - if ($this->isConnected()) { - $string = cubrid_real_escape_string($string); - } - else { - $string = str_replace("'","\'",$string); - } - */ - - $string = str_replace("'", "''", $string); - } - - return $string; - } - - /** - * DB transaction start - * this method is private - * @return boolean - */ - function _begin($transactionLevel = 0) - { - if(__CUBRID_VERSION__ >= '8.4.0') - { - $connection = $this->_getConnection('master'); - - if(!$transactionLevel) - { - cubrid_set_autocommit($connection, CUBRID_AUTOCOMMIT_FALSE); - } - else - { - $this->_query("SAVEPOINT SP" . $transactionLevel, $connection); - } - } - return TRUE; - } - - /** - * DB transaction rollback - * this method is private - * @return boolean - */ - function _rollback($transactionLevel = 0) - { - $connection = $this->_getConnection('master'); - - $point = $transactionLevel - 1; - - if($point) - { - $this->_query("ROLLBACK TO SP" . $point, $connection); - } - else - { - @cubrid_rollback($connection); - } - - return TRUE; - } - - /** - * DB transaction commit - * this method is private - * @return boolean - */ - function _commit() - { - $connection = $this->_getConnection('master'); - @cubrid_commit($connection); - return TRUE; - } - - /** - * Execute the query - * this method is private - * @param string $query - * @param resource $connection - * @return resource - */ - function __query($query, $connection) - { - if($this->use_prepared_statements == 'Y') - { - $req = @cubrid_prepare($connection, $query); - if(!$req) - { - $this->_setError(); - return false; - } - - $position = 0; - if($this->param) - { - foreach($this->param as $param) - { - $value = $param->getUnescapedValue(); - $type = $param->getType(); - - if($param->isColumnName()) - { - continue; - } - - switch($type) - { - case 'number' : - $bind_type = 'numeric'; - break; - case 'varchar' : - $bind_type = 'string'; - break; - default: - $bind_type = 'string'; - } - - if(is_array($value)) - { - foreach($value as $v) - { - $bound = @cubrid_bind($req, ++$position, $v, $bind_type); - if(!$bound) - { - $this->_setError(); - return false; - } - } - } - else - { - $bound = @cubrid_bind($req, ++$position, $value, $bind_type); - if(!$bound) - { - $this->_setError(); - return false; - } - } - } - } - - $result = @cubrid_execute($req); - if(!$result) - { - $this->_setError(); - return false; - } - return $req; - } - // Execute the query - $result = @cubrid_execute($connection, $query); - // error check - if(!$result) - { - $this->_setError(); - return false; - } - // Return the result - return $result; - } - - /** - * Retrieve CUBRID error and set to object - * - * @author Corina Udrescu (dev@xpressengine.org) - */ - function _setError() - { - $code = cubrid_error_code(); - $msg = cubrid_error_msg(); - - $this->setError($code, $msg); - } - - /** - * Fetch the result - * @param resource $result - * @param int|NULL $arrayIndexEndValue - * @return array - */ - function _fetch($result, $arrayIndexEndValue = NULL) - { - $output = array(); - if(!$this->isConnected() || $this->isError() || !$result) - { - return array(); - } - - if($this->use_prepared_statements == 'Y') - { - - } - - // TODO Improve this piece of code - // This code trims values from char type columns - $col_types = cubrid_column_types($result); - $col_names = cubrid_column_names($result); - $max = count($col_types); - - for($count = 0; $count < $max; $count++) - { - if(preg_match("/^char/", $col_types[$count]) > 0) - { - $char_type_fields[] = $col_names[$count]; - } - } - - while($tmp = cubrid_fetch($result, CUBRID_OBJECT)) - { - if(is_array($char_type_fields)) - { - foreach($char_type_fields as $val) - { - $tmp->{$val} = rtrim($tmp->{$val}); - } - } - - if($arrayIndexEndValue) - { - $output[$arrayIndexEndValue--] = $tmp; - } - else - { - $output[] = $tmp; - } - } - - unset($char_type_fields); - - if($result) - { - cubrid_close_request($result); - } - - if(count($output) == 1) - { - // If call is made for pagination, always return array - if(isset($arrayIndexEndValue)) - { - return $output; - } - // Else return object instead of array - else - { - return $output[0]; - } - } - return $output; - } - - /** - * Return the sequence value incremented by 1 - * Auto_increment column only used in the CUBRID sequence table - * @return int - */ - function getNextSequence() - { - $this->_makeSequence(); - - $query = sprintf("select \"%ssequence\".\"nextval\" as \"seq\" from db_root", $this->prefix); - $result = $this->_query($query); - $output = $this->_fetch($result); - - return $output->seq; - } - - /** - * if the table already exists, set the status to GLOBALS - * @return void - */ - function _makeSequence() - { - if($_GLOBALS['XE_EXISTS_SEQUENCE']) - return; - - // check cubrid serial - $query = sprintf('select count(*) as "count" from "db_serial" where name=\'%ssequence\'', $this->prefix); - $result = $this->_query($query); - $output = $this->_fetch($result); - - // if do not create serial - if($output->count == 0) - { - $query = sprintf('select max("a"."srl") as "srl" from ' . - '( select max("document_srl") as "srl" from ' . - '"%sdocuments" UNION ' . - 'select max("comment_srl") as "srl" from ' . - '"%scomments" UNION ' . - 'select max("member_srl") as "srl" from ' . - '"%smember"' . - ') as "a"', $this->prefix, $this->prefix, $this->prefix); - - $result = $this->_query($query); - $output = $this->_fetch($result); - $srl = $output->srl; - if($srl < 1) - { - $start = 1; - } - else - { - $start = $srl + 1000000; - } - - // create sequence - $query = sprintf('create serial "%ssequence" start with %s increment by 1 minvalue 1 maxvalue 10000000000000000000000000000000000000 nocycle;', $this->prefix, $start); - $this->_query($query); - } - - $_GLOBALS['XE_EXISTS_SEQUENCE'] = TRUE; - } - - /** - * Check a table exists status - * @param string $target_name - * @return boolean - */ - function isTableExists($target_name) - { - if($target_name == 'sequence') - { - $query = sprintf("select \"name\" from \"db_serial\" where \"name\" = '%s%s'", $this->prefix, $target_name); - } - else - { - $query = sprintf("select \"class_name\" from \"db_class\" where \"class_name\" = '%s%s'", $this->prefix, $target_name); - } - - $result = $this->_query($query); - if(cubrid_num_rows($result) > 0) - { - $output = TRUE; - } - else - { - $output = FALSE; - } - - if($result) - { - cubrid_close_request($result); - } - - return $output; - } - - /** - * 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 = null, $notnull = FALSE) - { - $type = strtoupper($this->column_type[$type]); - if($type == 'INTEGER') - { - $size = ''; - } - - $query = sprintf("alter class \"%s%s\" add \"%s\" ", $this->prefix, $table_name, $column_name); - - if($type == 'char' || $type == 'varchar') - { - if($size) - { - $size = $size * 3; - } - } - - if($size) - { - $query .= sprintf("%s(%s) ", $type, $size); - } - else - { - $query .= sprintf("%s ", $type); - } - - if(isset($default)) - { - if($type == 'INTEGER' || $type == 'BIGINT' || $type == 'INT') - { - $query .= sprintf("default %d ", $default); - } - else - { - $query .= sprintf("default '%s' ", $default); - } - } - - if($notnull) - { - $query .= "not null "; - } - - return $this->_query($query); - } - - /** - * Drop a column from the table - * @param string $table_name table name - * @param string $column_name column name - * @return void - */ - function dropColumn($table_name, $column_name) - { - $query = sprintf("alter class \"%s%s\" drop \"%s\" ", $this->prefix, $table_name, $column_name); - - $this->_query($query); - } - - /** - * Modify a column (only supported in CUBRID 8.4 and above, otherwise returns false) - * @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 bool - */ - function modifyColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = FALSE) - { - if(!version_compare(__CUBRID_VERSION__, '8.4.0', '>=')) - { - return false; - } - - $type = strtoupper($this->column_type[$type]); - if($type == 'INTEGER') - { - $size = ''; - } - - $query = sprintf("alter class \"%s%s\" modify \"%s\" ", $this->prefix, $table_name, $column_name); - - if($type == 'char' || $type == 'varchar') - { - if($size) - { - $size = $size * 3; - } - } - - if($size) - { - $query .= sprintf("%s(%s) ", $type, $size); - } - else - { - $query .= sprintf("%s ", $type); - } - - if($default) - { - if($type == 'INTEGER' || $type == 'BIGINT' || $type == 'INT') - { - $query .= sprintf("default %d ", $default); - } - else - { - $query .= sprintf("default '%s' ", $default); - } - } - - if($notnull) - { - $query .= "not null "; - } - - return $this->_query($query) ? true : false; - } - - /** - * Check column exist status of the table - * @param string $table_name table name - * @param string $column_name column name - * @return boolean - */ - function isColumnExists($table_name, $column_name) - { - $query = sprintf("select \"attr_name\" from \"db_attribute\" where " . "\"attr_name\" ='%s' and \"class_name\" = '%s%s'", $column_name, $this->prefix, $table_name); - $result = $this->_query($query); - - if(cubrid_num_rows($result) > 0) - { - $output = TRUE; - } - else - { - $output = FALSE; - } - - if($result) - { - cubrid_close_request($result); - } - - return $output; - } - - /** - * Get information about a column - * @param string $table_name table name - * @param string $column_name column name - * @return BaseObject - */ - function getColumnInfo($table_name, $column_name) - { - $query = sprintf("select * from \"db_attribute\" where " . "\"attr_name\" ='%s' and \"class_name\" = '%s%s'", $column_name, $this->prefix, $table_name); - $result = $this->_query($query); - if($this->isError()) - { - return; - } - $output = $this->_fetch($result); - if($output) - { - $dbtype = strtolower($output->data_type); - if($dbtype === 'string') $dbtype = 'character varying'; - $size = ($output->prec > 0) ? $output->prec : null; - if($xetype = array_search("$dbtype($size)", $this->column_type)) - { - $dbtype = "$dbtype($size)"; - $size = null; - } - elseif($size !== null) - { - if($xetype = array_search($dbtype, $this->column_type)) - { - if($size % 3 == 0) $size = intval($size / 3); - } - else - { - $xetype = $dbtype; - } - } - else - { - $xetype = $dbtype; - $size = null; - } - return (object)array( - 'name' => $output->attr_name, - 'dbtype' => $dbtype, - 'xetype' => $xetype, - 'size' => $size, - 'default_value' => $output->default_value, - 'notnull' => !$output->is_nullable, - ); - } - else - { - return false; - } - } - - /** - * Add an index to the table - * $target_columns = array(col1, col2) - * $is_unique? unique : none - * @param string $table_name table name - * @param string $index_name index name - * @param string|array $target_columns target column or columns - * @param boolean $is_unique - * @return void - */ - function addIndex($table_name, $index_name, $target_columns, $is_unique = FALSE) - { - if(!is_array($target_columns)) - { - $target_columns = array($target_columns); - } - - $query = sprintf("create %s index \"%s\" on \"%s%s\" (%s);", $is_unique ? 'unique' : '', $index_name, $this->prefix, $table_name, '"' . implode('","', $target_columns) . '"'); - - $this->_query($query); - } - - /** - * Drop an index from the table - * @param string $table_name table name - * @param string $index_name index name - * @param boolean $is_unique - * @return void - */ - function dropIndex($table_name, $index_name, $is_unique = FALSE) - { - $query = sprintf("drop %s index \"%s\" on \"%s%s\"", $is_unique ? 'unique' : '', $index_name, $this->prefix, $table_name); - - $this->_query($query); - } - - /** - * Check index status of the table - * @param string $table_name table name - * @param string $index_name index name - * @return boolean - */ - function isIndexExists($table_name, $index_name) - { - $query = sprintf("select \"index_name\" from \"db_index\" where " . "\"class_name\" = '%s%s' and (\"index_name\" = '%s' or \"index_name\" = '%s') ", $this->prefix, $table_name, $this->prefix . $index_name, $index_name); - $result = $this->_query($query); - - if($this->isError()) - { - return FALSE; - } - - $output = $this->_fetch($result); - - if(!$output) - { - return FALSE; - } - return TRUE; - } - - /** - * Delete duplicated index of the table - * @return boolean - */ - function deleteDuplicateIndexes() - { - $query = sprintf(" - select \"class_name\" - , case - when substr(\"index_name\", 0, %d) = '%s' - then substr(\"index_name\", %d) - else \"index_name\" end as unprefixed_index_name - , \"is_unique\" - from \"db_index\" - where \"class_name\" like %s - group by \"class_name\" - , case - when substr(\"index_name\", 0, %d) = '%s' - then substr(\"index_name\", %d) - else \"index_name\" - end - having count(*) > 1 - ", strlen($this->prefix) - , $this->prefix - , strlen($this->prefix) + 1 - , "'" . $this->prefix . '%' . "'" - , strlen($this->prefix) - , $this->prefix - , strlen($this->prefix) + 1 - ); - $result = $this->_query($query); - - if($this->isError()) - { - return FALSE; - } - - $output = $this->_fetch($result); - if(!$output) - { - return FALSE; - } - - if(!is_array($output)) - { - $indexes_to_be_deleted = array($output); - } - else - { - $indexes_to_be_deleted = $output; - } - - foreach($indexes_to_be_deleted as $index) - { - $this->dropIndex(substr($index->class_name, strlen($this->prefix)) - , $this->prefix . $index->unprefixed_index_name - , $index->is_unique == 'YES' ? TRUE : FALSE); - } - - return TRUE; - } - - /** - * Creates a table by using xml contents - * @param string $xml_doc xml schema contents - * @return void|object - */ - function createTableByXml($xml_doc) - { - return $this->_createTable($xml_doc); - } - - /** - * Creates a table by using xml file path - * @param string $file_name xml schema file path - * @return void|object - */ - function createTableByXmlFile($file_name) - { - if(!file_exists($file_name)) - { - return; - } - // read xml file - $buff = FileHandler::readFile($file_name); - return $this->_createTable($buff); - } - - /** - * Create table by using the schema xml - * - * type : number, varchar, tinytext, text, bigtext, char, date, \n - * opt : notnull, default, size\n - * index : primary key, index, unique\n - * @param string $xml_doc xml schema contents - * @return void|object - */ - function _createTable($xml_doc) - { - // xml parsing - $oXml = new XmlParser(); - $xml_obj = $oXml->parse($xml_doc); - // Create a table schema - $table_name = $xml_obj->table->attrs->name; - - // if the table already exists exit function - if($this->isTableExists($table_name)) - { - return; - } - - // If the table name is sequence, it creates a serial - if($table_name == 'sequence') - { - $query = sprintf('create serial "%s" start with 1 increment by 1' . - ' minvalue 1 ' . - 'maxvalue 10000000000000000000000000000000000000' . ' nocycle;', $this->prefix . $table_name); - - return $this->_query($query); - } - - - $table_name = $this->prefix . $table_name; - - $query = sprintf('create class "%s";', $table_name); - $this->_query($query); - - if(!is_array($xml_obj->table->column)) - { - $columns[] = $xml_obj->table->column; - } - else - { - $columns = $xml_obj->table->column; - } - - $query = sprintf("alter class \"%s\" add attribute ", $table_name); - - $primary_list = array(); - $unique_list = array(); - $index_list = array(); - - foreach($columns as $column) - { - $name = $column->attrs->name; - $type = $column->attrs->type; - $size = $column->attrs->size; - $notnull = $column->attrs->notnull; - $primary_key = $column->attrs->primary_key; - $index = $column->attrs->index; - $unique = $column->attrs->unique; - $default = $column->attrs->default; - - switch($this->column_type[$type]) - { - case 'integer' : - $size = NULL; - break; - case 'text' : - $size = NULL; - break; - } - - if(isset($default) && ($type == 'varchar' || $type == 'char' || - $type == 'text' || $type == 'tinytext' || $type == 'bigtext')) - { - $default = sprintf("'%s'", $default); - } - - if($type == 'varchar' || $type == 'char') - { - if($size) - $size = $size * 3; - } - - - $column_schema[] = sprintf('"%s" %s%s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default " . $default : '', $notnull ? 'not null' : ''); - - if($primary_key) - { - $primary_list[] = $name; - } - else if($unique) - { - $unique_list[$unique][] = $name; - } - else if($index) - { - $index_list[$index][] = $name; - } - } - - $query .= implode(',', $column_schema) . ';'; - $this->_query($query); - - if(count($primary_list)) - { - $query = sprintf("alter class \"%s\" add attribute constraint " . "\"pkey_%s\" PRIMARY KEY(%s);", $table_name, $table_name, '"' . implode('","', $primary_list) . '"'); - $this->_query($query); - } - - if(count($unique_list)) - { - foreach($unique_list as $key => $val) - { - $query = sprintf("create unique index \"%s\" on \"%s\" " . "(%s);", $key, $table_name, '"' . implode('","', $val) . '"'); - $this->_query($query); - } - } - - if(count($index_list)) - { - foreach($index_list as $key => $val) - { - $query = sprintf("create index \"%s\" on \"%s\" (%s);", $key, $table_name, '"' . implode('","', $val) . '"'); - $this->_query($query); - } - } - } - - /** - * Drop table - * - * @param string $name - * @return bool - */ - function dropTable($table_name) - { - // Generate the drop query - $query = sprintf('drop class "%s"', $this->addQuotes($this->prefix . $table_name)); - - // Execute the drop query - $output = $this->_query($query); - if($output) - { - return true; - } - else - { - return false; - } - } - - /** - * Handles insertAct - * @param BaseObject $queryObject - * @param boolean $with_values - * @return resource - */ - function _executeInsertAct($queryObject, $with_values = TRUE) - { - if($this->use_prepared_statements == 'Y') - { - $this->param = $queryObject->getArguments(); - $with_values = FALSE; - } - $query = $this->getInsertSql($queryObject, $with_values); - if($query instanceof BaseObject) - { - unset($this->param); - return; - } - - $result = $this->_query($query); - if($result && !$this->transaction_started) - { - $this->_commit(); - } - unset($this->param); - return $result; - } - - /** - * Handles updateAct - * @param BaseObject $queryObject - * @param boolean $with_values - * @return resource - */ - function _executeUpdateAct($queryObject, $with_values = TRUE) - { - if($this->use_prepared_statements == 'Y') - { - $this->param = $queryObject->getArguments(); - $with_values = FALSE; - } - $query = $this->getUpdateSql($queryObject, $with_values); - if($query instanceof BaseObject) - { - unset($this->param); - return; - } - - $result = $this->_query($query); - - if($result && !$this->transaction_started) - { - $this->_commit(); - } - unset($this->param); - return $result; - } - - /** - * Handles deleteAct - * @param BaseObject $queryObject - * @param boolean $with_values - * @return resource - */ - function _executeDeleteAct($queryObject, $with_values = TRUE) - { - if($this->use_prepared_statements == 'Y') - { - $this->param = $queryObject->getArguments(); - $with_values = FALSE; - } - $query = $this->getDeleteSql($queryObject, $with_values); - if($query instanceof BaseObject) - { - unset($this->param); - return; - } - - $result = $this->_query($query); - - if($result && !$this->transaction_started) - { - $this->_commit(); - } - - unset($this->param); - return $result; - } - - /** - * Handle selectAct - * To get a specific page list easily in select statement, - * a method, navigation, is used - * @param BaseObject $queryObject - * @param resource $connection - * @param boolean $with_values - * @return BaseObject - */ - function _executeSelectAct($queryObject, $connection = NULL, $with_values = TRUE) - { - if($this->use_prepared_statements == 'Y') - { - $this->param = $queryObject->getArguments(); - $with_values = FALSE; - } - $limit = $queryObject->getLimit(); - if($limit && $limit->isPageHandler()) - { - return $this->queryPageLimit($queryObject, $connection, $with_values); - } - else - { - $query = $this->getSelectSql($queryObject, $with_values); - if($query instanceof BaseObject) - { - unset($this->param); - return; - } - - $result = $this->_query($query, $connection); - - if($this->isError()) - { - unset($this->param); - return $this->queryError($queryObject); - } - - $data = $this->_fetch($result); - $buff = new BaseObject; - $buff->data = $data; - - unset($this->param); - return $buff; - } - } - - /** - * Get the number of rows affected by the last query - * @return int - */ - function getAffectedRows() - { - $connection = $this->_getConnection('master'); - return cubrid_affected_rows($connection); - } - - /** - * Get the ID generated in the last query - * @return int - */ - function getInsertID() - { - $connection = $this->_getConnection('master'); - return cubrid_insert_id($connection); - } - - /** - * If have a error, return error object - * @param BaseObject $queryObject - * @return BaseObject - */ - function queryError($queryObject) - { - $limit = $queryObject->getLimit(); - if($limit && $limit->isPageHandler()) - { - $buff = new BaseObject; - $buff->total_count = 0; - $buff->total_page = 0; - $buff->page = 1; - $buff->data = array(); - $buff->page_navigation = new PageHandler(/* $total_count */0, /* $total_page */1, /* $page */1, /* $page_count */10); //default page handler values - return $buff; - }else - return; - } - - /** - * If select query execute, return page info - * @param BaseObject $queryObject - * @param resource $connection - * @param boolean $with_values - * @return BaseObject Object with page info containing - */ - function queryPageLimit($queryObject, $connection, $with_values) - { - $limit = $queryObject->getLimit(); - // Total count - $temp_where = $queryObject->getWhereString($with_values, FALSE); - $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE ' . $temp_where)); - - // Check for distinct query and if found update count query structure - $temp_select = $queryObject->getSelectString($with_values); - $uses_distinct = stripos($temp_select, "distinct") !== FALSE; - $uses_groupby = $queryObject->getGroupByString() != ''; - if($uses_distinct || $uses_groupby) - { - $count_query = sprintf('select %s %s %s %s' - , $temp_select - , 'FROM ' . $queryObject->getFromString($with_values) - , ($temp_where === '' ? '' : ' WHERE ' . $temp_where) - , ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '') - ); - - // If query uses grouping or distinct, count from original select - $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); - } - - $result = $this->_query($count_query, $connection); - $count_output = $this->_fetch($result); - $total_count = (int) (isset($count_output->count) ? $count_output->count : NULL); - - $list_count = $limit->list_count->getValue(); - if(!$list_count) - { - $list_count = 20; - } - $page_count = $limit->page_count->getValue(); - if(!$page_count) - { - $page_count = 10; - } - $page = $limit->page->getValue(); - if(!$page || $page < 1) - { - $page = 1; - } - - // total pages - if($total_count) - { - $total_page = (int) (($total_count - 1) / $list_count) + 1; - } - else - { - $total_page = 1; - } - - // check the page variables - if($page > $total_page) - { - // If requested page is bigger than total number of pages, return empty list - - $buff = new BaseObject; - $buff->total_count = $total_count; - $buff->total_page = $total_page; - $buff->page = $page; - $buff->data = array(); - $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); - unset($this->param); - return $buff; - } - $start_count = ($page - 1) * $list_count; - - $query = $this->getSelectPageSql($queryObject, $with_values, $start_count, $list_count); - $result = $this->_query($query, $connection); - if($this->isError()) - { - unset($this->param); - return $this->queryError($queryObject); - } - - $virtual_no = $total_count - ($page - 1) * $list_count; - $data = $this->_fetch($result, $virtual_no); - - $buff = new BaseObject; - $buff->total_count = $total_count; - $buff->total_page = $total_page; - $buff->page = $page; - $buff->data = $data; - $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); - unset($this->param); - return $buff; - } - - /** - * Return the DBParser - * @param boolean $force - * @return DBParser - */ - function getParser($force = FALSE) - { - return new DBParser('"', '"', $this->prefix); - } - - /** - * If select query execute, return paging sql - * @param object $query - * @param boolean $with_values - * @param int $start_count - * @param int $list_count - * @return string select paging sql - */ - function getSelectPageSql($query, $with_values = TRUE, $start_count = 0, $list_count = 0) - { - - $select = $query->getSelectString($with_values); - if($select == '') - { - return new BaseObject(-1, "Invalid query"); - } - $select = 'SELECT ' . $select; - - $from = $query->getFromString($with_values); - if($from == '') - { - return new BaseObject(-1, "Invalid query"); - } - $from = ' FROM ' . $from; - - $where = $query->getWhereString($with_values); - if($where != '') - { - $where = ' WHERE ' . $where; - } - - $groupBy = $query->getGroupByString(); - if($groupBy != '') - { - $groupBy = ' GROUP BY ' . $groupBy; - } - - $orderBy = $query->getOrderByString(); - if($orderBy != '') - { - $orderBy = ' ORDER BY ' . $orderBy; - } - - $limit = $query->getLimitString(); - if($limit != '') - { - $limit = sprintf(' LIMIT %d, %d', $start_count, $list_count); - } - - return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; - } - -} - -DBCubrid::$isSupported = function_exists('cubrid_connect'); - -/* End of file DBCubrid.class.php */ -/* Location: ./classes/db/DBCubrid.class.php */ diff --git a/classes/db/DBMssql.class.php b/classes/db/DBMssql.class.php deleted file mode 100644 index 2e26d5a66..000000000 --- a/classes/db/DBMssql.class.php +++ /dev/null @@ -1,1161 +0,0 @@ - */ - -/** - * - DBMSSQL - * - Modified to use MSSQL driver by sol (sol@ngleader.com) - * - * @author NAVER (developers@xpressengine.com) - * @package /classes/db - * @version 0.1 - */ -class DBMssql extends DB -{ - - /** - * prefix of Rhymix tables(One more Rhymix can be installed on a single DB) - * @var string - */ - var $prefix = 'rx'; - var $param = array(); - var $comment_syntax = '/* %s */'; - - /** - * column type used in mssql - * - * column_type should be replaced for each DBMS's type - * becasue it uses commonly defined type in the schema/query xml - * @var array - */ - var $column_type = array( - 'bignumber' => 'bigint', - 'number' => 'int', - 'varchar' => 'nvarchar', - 'char' => 'nchar', - 'bigtext' => 'ntext', - 'text' => 'ntext', - 'date' => 'nvarchar(14)', - 'float' => 'float', - ); - - /** - * Last statement executed - */ - var $last_stmt; - - /** - * Constructor - * @return void - */ - function __construct() - { - $this->_setDBInfo(); - $this->_connect(); - } - - /** - * DB Connect - * this method is private - * @param array $connection connection's value is db_hostname, db_database, db_userid, db_password - * @return resource - */ - function __connect($connection) - { - //sqlsrv_configure( 'WarningsReturnAsErrors', 0 ); - //sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL ); - //sqlsrv_configure( 'LogSubsystems', SQLSRV_LOG_SYSTEM_ALL ); - $result = @sqlsrv_connect($connection['host'], array( - 'Database' => $connection['database'], - 'UID' => $connection['user'], - 'PWD' => $connection['pass'], - )); - - if(!$result) - { - $errors = print_r(sqlsrv_errors(), true); - $this->setError(-1, 'database connect fail' . PHP_EOL . $errors); - return; - } - - $server_info = sqlsrv_server_info($result); - $server_version = $server_info['SQLServerVersion']; - $this->db_version = $server_version; - if ($server_version && version_compare($server_version, '10', '<')) - { - $this->setError(-1, 'Rhymix requires Microsoft SQL Server 2008 or later. Current version is ' . $server_version); - return; - } - - return $result; - } - - /** - * DB disconnection - * this method is private - * @param resource $connection - * @return void - */ - function _close($connection) - { - $this->commit(); - sqlsrv_close($connection); - } - - /** - * Handles quatation of the string variables from the query - * @todo See what to do about this - * @param string $string - * @return string - */ - function addQuotes($string) - { - //if(!is_numeric($string)) $string = str_replace("'","''",$string); - - return $string; - } - - /** - * DB transaction start - * this method is private - * @return boolean - */ - function _begin($transactionLevel = 0) - { - $connection = $this->_getConnection('master'); - - if(!$transactionLevel) - { - if(sqlsrv_begin_transaction($connection) === false) - { - return; - } - } - else - { - $this->_query("SAVE TRANS SP" . $transactionLevel, $connection); - } - return true; - } - - /** - * DB transaction rollback - * this method is private - * @return boolean - */ - function _rollback($transactionLevel = 0) - { - $connection = $this->_getConnection('master'); - - $point = $transactionLevel - 1; - - if($point) - { - $this->_query("ROLLBACK TRANS SP" . $point, $connection); - } - else - { - sqlsrv_rollback($connection); - } - return true; - } - - /** - * DB transaction commit - * this method is private - * @return boolean - */ - function _commit() - { - $connection = $this->_getConnection('master'); - sqlsrv_commit($connection); - return true; - } - - /** - * Execute the query - * this method is private - * @param string $query - * @param resource $connection - * @return resource|boolean Returns a statement resource on success and FALSE if an error occurred. - */ - function __query($query, $connection) - { - $_param = array(); - - if(count($this->param)) - { - foreach($this->param as $k => $o) - { - if($o->isColumnName()) - { - continue; - } - if($o->getType() == 'number') - { - $value = $o->getUnescapedValue(); - if(is_array($value)) - { - $_param = array_merge($_param, $value); - } - else - { - $_param[] = $o->getUnescapedValue(); - } - } - else - { - $value = $o->getUnescapedValue(); - if(is_array($value)) - { - foreach($value as $v) - { - $_param[] = array($v, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8')); - } - } - else - { - $_param[] = array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8')); - } - } - } - } - - // Run the query statement - $result = false; - if(count($_param)) - { - $args = $this->_getParametersByReference($_param); - $stmt = sqlsrv_prepare($connection, $query, $args); - } - else - { - $stmt = sqlsrv_prepare($connection, $query); - } - - if(!$stmt) - { - $result = false; - } - else - { - $result = sqlsrv_execute($stmt); - } - - // Error Check - if(!$result) - { - $this->setError(print_r(sqlsrv_errors(), true)); - } - - $this->last_stmt = $stmt; - $this->param = array(); - - return $stmt; - } - - /** - * Parameters to sqlsrv_prepare need to be references, and not literals - * Parameters are sent as an array, where each parameter can be: - * - a PHP variable (by reference) - * - a PHP array (containng param value, type and direction) -> also needs to be sent by reference - * @param array $_param - * @return array - */ - function _getParametersByReference($_param) - { - $copy = array(); - $args = array(); - $i = 0; - foreach($_param as $key => $value) - { - if(is_array($value)) - { - $value_copy = $value; - $value_arg = array(); - $value_arg[] = &$value_copy[0]; - $value_arg[] = $value_copy[1]; - $value_arg[] = $value_copy[2]; - } - else - { - $value_arg = $value; - } - $copy[$key] = $value_arg; - $args[$i++] = &$copy[$key]; - } - return $args; - } - - /** - * Fetch results - * @param resource $result - * @param int|NULL $arrayIndexEndValue - * @return array - */ - function _fetch($result, $arrayIndexEndValue = NULL) - { - $output = array(); - if(!$this->isConnected() || $this->isError() || !$result) - { - return $output; - } - - $c = sqlsrv_num_fields($result); - $m = null; - - while(sqlsrv_fetch($result)) - { - if(!$m) - { - $m = sqlsrv_field_metadata($result); - } - unset($row); - for($i = 0; $i < $c; $i++) - { - $row->{$m[$i]['Name']} = sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING('utf-8')); - } - if($arrayIndexEndValue) - { - $output[$arrayIndexEndValue--] = $row; - } - else - { - $output[] = $row; - } - } - - if(count($output) == 1) - { - if(isset($arrayIndexEndValue)) - { - return $output; - } - else - { - return $output[0]; - } - } - return $output; - } - - /** - * Return the sequence value incremented by 1 - * Auto_increment column only used in the sequence table - * @return int - */ - function getNextSequence() - { - $query = sprintf("insert into %ssequence (seq) values (ident_incr('%ssequence'))", $this->prefix, $this->prefix); - $this->_query($query); - - $query = sprintf("select ident_current('%ssequence')+1 as sequence", $this->prefix); - $result = $this->_query($query); - $tmp = $this->_fetch($result); - - - return $tmp->sequence; - } - - /** - * Check a table exists status - * @param string $target_name - * @return boolean - */ - function isTableExists($target_name) - { - $query = sprintf("select name from sysobjects where name = '%s%s' and xtype='U'", $this->prefix, $this->addQuotes($target_name)); - $result = $this->_query($query); - $tmp = $this->_fetch($result); - - if(!$tmp) - { - return false; - } - return true; - } - - /** - * 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 = null, $notnull = false) - { - if($this->isColumnExists($table_name, $column_name)) - { - return; - } - $type = $this->column_type[$type]; - if(strtoupper($type) == 'INTEGER') - { - $size = ''; - } - - $query = sprintf("alter table %s%s add \"%s\" ", $this->prefix, $table_name, $column_name); - if($size) - { - $query .= sprintf(" %s(%s) ", $type, $size); - } - else - { - $query .= sprintf(" %s ", $type); - } - - if(isset($default)) - { - $query .= sprintf(" default '%s' ", $default); - } - if($notnull) - { - $query .= " not null "; - } - - return $this->_query($query); - } - - /** - * Drop a column from the table - * @param string $table_name table name - * @param string $column_name column name - * @return void - */ - function dropColumn($table_name, $column_name) - { - if(!$this->isColumnExists($table_name, $column_name)) - { - return; - } - $query = sprintf("alter table %s%s drop column \"%s\" ", $this->prefix, $table_name, $column_name); - $this->_query($query); - } - - /** - * Modify a column - * @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 bool - */ - function modifyColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false) - { - $type = $this->column_type[$type]; - if(strtoupper($type) == 'INTEGER') - { - $size = ''; - } - - $query = sprintf("alter table %s%s alter column %s ", $this->prefix, $table_name, $column_name); - if($size) - { - $query .= sprintf(" %s(%s) ", $type, $size); - } - else - { - $query .= sprintf(" %s ", $type); - } - - if($default) - { - $query .= sprintf(" default '%s' ", $default); - } - if($notnull) - { - $query .= " not null "; - } - - return $this->_query($query) ? true : false; - } - - /** - * Check column exist status of the table - * @param string $table_name table name - * @param string $column_name column name - * @return boolean - */ - function isColumnExists($table_name, $column_name) - { - $query = sprintf("select syscolumns.name as name from syscolumns, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = syscolumns.id and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name); - $result = $this->_query($query); - if($this->isError()) - { - return; - } - $tmp = $this->_fetch($result); - if(!$tmp->name) - { - return false; - } - return true; - } - - /** - * Get information about a column - * @param string $table_name table name - * @param string $column_name column name - * @return BaseObject - */ - function getColumnInfo($table_name, $column_name) - { - $query = sprintf("select syscolumns.name as name, systypes.name as type_name, syscolumns.length as length, " . - "syscolumns.isnullable as isnullable, syscomments.text as default_value from syscolumns " . - "inner join sysobjects on sysobjects.id = syscolumns.id " . - "inner join systypes on systypes.xtype = syscolumns.xtype " . - "left join syscomments on syscolumns.cdefault = syscomments.id " . - "where sysobjects.name = '%s%s' and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name); - $result = $this->_query($query); - if($this->isError()) - { - return; - } - $output = $this->_fetch($result); - if($output) - { - $dbtype = $output->type_name; - $size = ($output->length > 0) ? $output->length : null; - if($xetype = array_search("$dbtype($size)", $this->column_type)) - { - $dbtype = "$dbtype($size)"; - $size = null; - } - elseif($size !== null) - { - if($xetype = array_search($dbtype, $this->column_type)) - { - // no-op - } - else - { - $xetype = $dbtype; - } - } - else - { - $xetype = $dbtype; - $size = null; - } - return (object)array( - 'name' => $output->name, - 'dbtype' => $dbtype, - 'xetype' => $xetype, - 'size' => $size, - 'default_value' => $output->default_value, - 'notnull' => !$output->isnullable, - ); - } - else - { - return false; - } - } - - /** - * Add an index to the table - * $target_columns = array(col1, col2) - * $is_unique? unique : none - * @param string $table_name table name - * @param string $index_name index name - * @param string|array $target_columns target column or columns - * @param boolean $is_unique - * @return void - */ - function addIndex($table_name, $index_name, $target_columns, $is_unique = false) - { - if($this->isIndexExists($table_name, $index_name)) - { - return; - } - if(!is_array($target_columns)) - { - $target_columns = array($target_columns); - } - - $query = sprintf("create %s index %s on %s%s (%s)", $is_unique ? 'unique' : '', $index_name, $this->prefix, $table_name, implode(',', $target_columns)); - $this->_query($query); - } - - /** - * Drop an index from the table - * @param string $table_name table name - * @param string $index_name index name - * @param boolean $is_unique - * @return void - */ - function dropIndex($table_name, $index_name, $is_unique = false) - { - if(!$this->isIndexExists($table_name, $index_name)) - { - return; - } - $query = sprintf("drop index %s%s.%s", $this->prefix, $table_name, $index_name); - $this->_query($query); - } - - /** - * Check index status of the table - * @param string $table_name table name - * @param string $index_name index name - * @return boolean - */ - function isIndexExists($table_name, $index_name) - { - $query = sprintf("select sysindexes.name as name from sysindexes, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = sysindexes.id and sysindexes.name = '%s'", $this->prefix, $table_name, $index_name); - - $result = $this->_query($query); - if($this->isError()) - { - return; - } - $tmp = $this->_fetch($result); - - if(!$tmp->name) - { - return false; - } - return true; - } - - /** - * Creates a table by using xml contents - * @param string $xml_doc xml schema contents - * @return void|object - */ - function createTableByXml($xml_doc) - { - return $this->_createTable($xml_doc); - } - - /** - * Creates a table by using xml file path - * @param string $file_name xml schema file path - * @return void|object - */ - function createTableByXmlFile($file_name) - { - if(!file_exists($file_name)) - { - return; - } - // read xml file - $buff = FileHandler::readFile($file_name); - return $this->_createTable($buff); - } - - /** - * Create table by using the schema xml - * - * type : number, varchar, tinytext, text, bigtext, char, date, \n - * opt : notnull, default, size\n - * index : primary key, index, unique\n - * @param string $xml_doc xml schema contents - * @return void|object - */ - function _createTable($xml_doc) - { - // xml parsing - $oXml = new XmlParser(); - $xml_obj = $oXml->parse($xml_doc); - // Create a table schema - $table_name = $xml_obj->table->attrs->name; - if($this->isTableExists($table_name)) - { - return; - } - - if($table_name == 'sequence') - { - $table_name = $this->prefix . $table_name; - $query = sprintf('create table %s ( sequence int identity(1,1), seq int )', $table_name); - return $this->_query($query); - } - else - { - $table_name = $this->prefix . $table_name; - - if(!is_array($xml_obj->table->column)) - { - $columns[] = $xml_obj->table->column; - } - else - { - $columns = $xml_obj->table->column; - } - - $primary_list = array(); - $unique_list = array(); - $index_list = array(); - - $typeList = array('number' => 1, 'text' => 1); - foreach($columns as $column) - { - $name = $column->attrs->name; - $type = $column->attrs->type; - $size = $column->attrs->size; - $notnull = $column->attrs->notnull; - $primary_key = $column->attrs->primary_key; - $index = $column->attrs->index; - $unique = $column->attrs->unique; - $default = $column->attrs->default; - $auto_increment = $column->attrs->auto_increment; - - $column_schema[] = sprintf('[%s] %s%s %s %s %s', $name, $this->column_type[$type], !isset($typeList[$type]) && $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : 'null', $auto_increment ? 'identity(1,1)' : ''); - - if($primary_key) - { - $primary_list[] = $name; - } - else if($unique) - { - $unique_list[$unique][] = $name; - } - else if($index) - { - $index_list[$index][] = $name; - } - } - - if(count($primary_list)) - { - $column_schema[] = sprintf("primary key (%s)", '"' . implode($primary_list, '","') . '"'); - } - - $schema = sprintf('create table [%s] (%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n")); - $output = $this->_query($schema); - if(!$output) - { - return false; - } - - if(count($unique_list)) - { - foreach($unique_list as $key => $val) - { - $query = sprintf("create unique index %s on %s (%s);", $key, $table_name, '[' . implode('],[', $val) . ']'); - $this->_query($query); - } - } - - if(count($index_list)) - { - foreach($index_list as $key => $val) - { - $query = sprintf("create index %s on %s (%s);", $key, $table_name, '[' . implode('],[', $val) . ']'); - $this->_query($query); - } - } - return true; - } - } - - /** - * Drop table - * - * @param string $name - * @return bool - */ - function dropTable($table_name) - { - // Generate the drop query - $query = sprintf('DROP TABLE %s', $this->addQuotes($this->prefix . $table_name)); - - // Execute the drop query - $output = $this->_query($query); - if($output) - { - return true; - } - else - { - return false; - } - } - - /** - * Handles insertAct - * @todo Lookup _filterNumber against sql injection - see if it is still needed and how to integrate - * @param BaseObject $queryObject - * @return resource - */ - function _executeInsertAct($queryObject) - { - $query = $this->getInsertSql($queryObject, false); - $this->param = $queryObject->getArguments(); - return $this->_query($query); - } - - /** - * Handles updateAct - * @param BaseObject $queryObject - * @return resource - */ - function _executeUpdateAct($queryObject) - { - $query = $this->getUpdateSql($queryObject, false); - $this->param = $queryObject->getArguments(); - return $this->_query($query); - } - - /** - * Return update query string - * @param object $query - * @param boolean $with_values - * @param boolean $with_priority - * @return string - */ - function getUpdateSql($query, $with_values = true, $with_priority = false) - { - $columnsList = $query->getUpdateString($with_values); - if($columnsList == '') - { - return new BaseObject(-1, "Invalid query"); - } - - $from = $query->getFromString($with_values); - if($from == '') - { - return new BaseObject(-1, "Invalid query"); - } - - $tables = $query->getTables(); - $alias_list = ''; - foreach($tables as $table) - { - $alias_list .= $table->getAlias(); - } - implode(',', explode(' ', $alias_list)); - - $where = $query->getWhereString($with_values); - if($where != '') - { - $where = ' WHERE ' . $where; - } - - $priority = $with_priority ? $query->getPriority() : ''; - - return "UPDATE $priority $alias_list SET $columnsList FROM " . $from . $where; - } - - /** - * Handles deleteAct - * @param BaseObject $queryObject - * @return resource - */ - function _executeDeleteAct($queryObject) - { - $query = $this->getDeleteSql($queryObject, false); - $this->param = $queryObject->getArguments(); - return $this->_query($query); - } - - /** - * Return select query string - * @param object $query - * @param boolean $with_values - * @return string - */ - function getSelectSql($query, $with_values = TRUE, $connection=NULL) - { - $with_values = false; - - //$limitOffset = $query->getLimit()->getOffset(); - //if($limitOffset) - // TODO Implement Limit with offset with subquery - $limit = ''; - $limitCount = ''; - $limitQueryPart = $query->getLimit(); - if($limitQueryPart) - { - $limitCount = $limitQueryPart->getLimit(); - } - if($limitCount != '') - { - $limit = 'SELECT TOP ' . $limitCount; - } - - $select = $query->getSelectString($with_values); - if($select == '') - { - return new BaseObject(-1, "Invalid query"); - } - if($limit != '') - { - $select = $limit . ' ' . $select; - } - else - { - $select = 'SELECT ' . $select; - } - - $from = $query->getFromString($with_values); - if($from == '') - { - return new BaseObject(-1, "Invalid query"); - } - $from = ' FROM ' . $from; - - $where = $query->getWhereString($with_values); - if($where != '') - { - $where = ' WHERE ' . $where; - } - - $groupBy = $query->getGroupByString(); - if($groupBy != '') - { - $groupBy = ' GROUP BY ' . $groupBy; - } - - $orderBy = $query->getOrderByString(); - if($orderBy != '') - { - $orderBy = ' ORDER BY ' . $orderBy; - } - - if($limitCount != '' && $query->limit->start > 0) - { - $order = $query->getOrder(); - $first_columns = array(); - foreach($order as $val) - { - $tmpColumnName = $val->getPureColumnName(); - $first_columns[] = sprintf('%s(%s) as %s', $val->getPureSortOrder()=='asc'?'max':'min', $tmpColumnName, $tmpColumnName); - $first_sub_columns[] = $tmpColumnName; - } - - $first_query = sprintf("select %s from (select top %d %s %s %s %s %s) xet", implode(',',$first_columns), $query->limit->start, implode(',',$first_sub_columns), $from, $where, $groupBy, $orderBy); - $this->param = $query->getArguments(); - $result = $this->__query($first_query, $connection); - $tmp = $this->_fetch($result); - - $sub_cond = array(); - foreach($order as $k => $v) - { - //for example... use Document - if(get_class($v->sort_order) == 'SortArgument') - { - $sort_order = $v->sort_order->value; - } - //for example... use comment, file - else - { - $sort_order = $v->sort_order; - } - - $sub_cond[] = sprintf("%s %s '%s'", $v->getPureColumnName(), $sort_order=='asc'?'>':'<', $tmp->{$v->getPureColumnName()}); - } - - if(!$where) - { - $sub_condition = ' WHERE ( '.implode(' and ',$sub_cond).' )'; - } - else - { - $sub_condition = ' and ( '.implode(' and ',$sub_cond).' )'; - } - } - return $select . ' ' . $from . ' ' . $where .$sub_condition. ' ' . $groupBy . ' ' . $orderBy; - } - - /** - * Handle selectAct - * In order to get a list of pages easily when selecting \n - * it supports a method as navigation - * @param BaseObject $queryObject - * @param resource $connection - * @return BaseObject - */ - function _executeSelectAct($queryObject, $connection = null) - { - $query = $this->getSelectSql($queryObject, true, $connection); - - if(strpos($query, "substr")) - { - $query = str_replace("substr", "substring", $query); - } - - // TODO Decide if we continue to pass parameters like this - $this->param = $queryObject->getArguments(); - $result = $this->_query($query, $connection); - - if($this->isError()) - { - return $this->queryError($queryObject); - } - else - { - return $this->queryPageLimit($queryObject, $result, $connection); - } - } - - /** - * Get the number of rows affected by the last query - * @return int - */ - function getAffectedRows() - { - $stmt = $this->last_stmt; - return $stmt ? sqlsrv_rows_affected($stmt) : -1; - } - - /** - * Get the ID generated in the last query - * @return int - */ - function getInsertID() - { - $result = $this->_query('SELECT @@IDENTITY as id'); - $output = $this->_fetch($result); - return $output->id ?: 0; - } - - /** - * Return the DBParser - * @param boolean $force - * @return DBParser - */ - function getParser($force = FALSE) - { - return new DBParser("[", "]", $this->prefix); - } - - /** - * If have a error, return error object - * @param BaseObject $queryObject - * @return BaseObject - */ - function queryError($queryObject) - { - $limit = $queryObject->getLimit(); - if($limit && $limit->isPageHandler()) - { - $buff = new BaseObject; - $buff->total_count = 0; - $buff->total_page = 0; - $buff->page = 1; - $buff->data = array(); - $buff->page_navigation = new PageHandler(/* $total_count */0, /* $total_page */1, /* $page */1, /* $page_count */10); //default page handler values - return $buff; - } - else - { - return; - } - } - - /** - * If select query execute, return page info - * @param BaseObject $queryObject - * @param resource $result - * @param resource $connection - * @return BaseObject Object with page info containing - */ - function queryPageLimit($queryObject, $result, $connection) - { - $limit = $queryObject->getLimit(); - if($limit && $limit->isPageHandler()) - { - // Total count - $temp_where = $queryObject->getWhereString(true, false); - $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE ' . $temp_where)); - - // Check for distinct query and if found update count query structure - $temp_select = $queryObject->getSelectString(true); - $uses_distinct = stripos($temp_select, "distinct") !== false; - $uses_groupby = $queryObject->getGroupByString() != ''; - if($uses_distinct || $uses_groupby) - { - $count_query = sprintf('select %s %s %s %s' - , $temp_select - , 'FROM ' . $queryObject->getFromString(true) - , ($temp_where === '' ? '' : ' WHERE ' . $temp_where) - , ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '') - ); - - // If query uses grouping or distinct, count from original select - $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); - } - - $this->param = $queryObject->getArguments(); - $result_count = $this->_query($count_query, $connection); - $count_output = $this->_fetch($result_count); - $total_count = (int) $count_output->count; - - $list_count = $limit->list_count->getValue(); - if(!$list_count) - { - $list_count = 20; - } - $page_count = $limit->page_count->getValue(); - if(!$page_count) - { - $page_count = 10; - } - $page = $limit->page->getValue(); - if(!$page || $page < 1) - { - $page = 1; - } - // Total pages - if($total_count) - { - $total_page = (int) (($total_count - 1) / $list_count) + 1; - } - else - { - $total_page = 1; - } - - // check the page variables - if($page > $total_page) - { - // If requested page is bigger than total number of pages, return empty list - - $buff = new BaseObject; - $buff->total_count = $total_count; - $buff->total_page = $total_page; - $buff->page = $page; - $buff->data = array(); - $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); - return $buff; - - if($queryObject->usesClickCount()) - { - $update_query = $this->getClickCountQuery($queryObject); - $this->_executeUpdateAct($update_query); - } - } - - $start_count = ($page - 1) * $list_count; - $this->param = $queryObject->getArguments(); - $virtual_no = $total_count - $start_count; - $data = $this->_fetch($result, $virtual_no); - - $buff = new BaseObject; - $buff->total_count = $total_count; - $buff->total_page = $total_page; - $buff->page = $page; - $buff->data = $data; - $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count); - } - else - { - $data = $this->_fetch($result); - $buff = new BaseObject; - $buff->data = $data; - } - return $buff; - } - -} - -DBMssql::$isSupported = extension_loaded("sqlsrv"); - -/* End of file DBMssql.class.php */ -/* Location: ./classes/db/DBMssql.class.php */