mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-07 02:31:40 +09:00
issue 2662 DB class coding convetion
git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12212 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
d94a687977
commit
ee9096be03
4 changed files with 1719 additions and 1570 deletions
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -1,162 +1,176 @@
|
|||
<?php
|
||||
require_once('DBMysql.class.php');
|
||||
require_once('DBMysql.class.php');
|
||||
/**
|
||||
* Class to use MySQL innoDB DBMS
|
||||
* mysql innodb handling class
|
||||
*
|
||||
* Does not use prepared statements, since mysql driver does not support them
|
||||
*
|
||||
* @author NHN (developers@xpressengine.com)
|
||||
* @package /classes/db
|
||||
* @version 0.1
|
||||
*/
|
||||
class DBMysql_innodb extends DBMysql
|
||||
{
|
||||
|
||||
/**
|
||||
* Class to use MySQL innoDB DBMS
|
||||
* mysql innodb handling class
|
||||
*
|
||||
* Does not use prepared statements, since mysql driver does not support them
|
||||
*
|
||||
* @author NHN (developers@xpressengine.com)
|
||||
* @package /classes/db
|
||||
* @version 0.1
|
||||
**/
|
||||
class DBMysql_innodb extends DBMysql {
|
||||
* Constructor
|
||||
* @return void
|
||||
*/
|
||||
function DBMysql_innodb()
|
||||
{
|
||||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @return void
|
||||
**/
|
||||
function DBMysql_innodb() {
|
||||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
/**
|
||||
* Create an instance of this class
|
||||
* @return DBMysql_innodb return DBMysql_innodb object instance
|
||||
*/
|
||||
function create()
|
||||
{
|
||||
return new DBMysql_innodb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of this class
|
||||
* @return DBMysql_innodb return DBMysql_innodb object instance
|
||||
*/
|
||||
function create()
|
||||
/**
|
||||
* DB disconnection
|
||||
* this method is private
|
||||
* @param resource $connection
|
||||
* @return void
|
||||
*/
|
||||
function _close($connection)
|
||||
{
|
||||
$this->_query("commit", $connection);
|
||||
@mysql_close($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* DB transaction start
|
||||
* this method is private
|
||||
* @return boolean
|
||||
*/
|
||||
function _begin()
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
$this->_query("begin", $connection);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB transaction rollback
|
||||
* this method is private
|
||||
* @return boolean
|
||||
*/
|
||||
function _rollback()
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
$this->_query("rollback", $connection);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB transaction commit
|
||||
* this method is private
|
||||
* @return boolean
|
||||
*/
|
||||
function _commit()
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
$this->_query("commit", $connection);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query
|
||||
* this method is private
|
||||
* @param string $query
|
||||
* @param resource $connection
|
||||
* @return resource
|
||||
*/
|
||||
function __query($query, $connection)
|
||||
{
|
||||
// Run the query statement
|
||||
$result = @mysql_query($query, $connection);
|
||||
// Error Check
|
||||
if(mysql_error($connection)) $this->setError(mysql_errno($connection), mysql_error($connection));
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$table_name = $this->prefix.$table_name;
|
||||
|
||||
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column;
|
||||
else $columns = $xml_obj->table->column;
|
||||
|
||||
foreach($columns as $column)
|
||||
{
|
||||
return new DBMysql_innodb;
|
||||
$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],
|
||||
$size?'('.$size.')':'',
|
||||
isset($default)?"default '".$default."'":'',
|
||||
$notnull?'not null':'',
|
||||
$auto_increment?'auto_increment':''
|
||||
);
|
||||
|
||||
if($primary_key) $primary_list[] = $name;
|
||||
else if($unique) $unique_list[$unique][] = $name;
|
||||
else if($index) $index_list[$index][] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB disconnection
|
||||
* this method is private
|
||||
* @param resource $connection
|
||||
* @return void
|
||||
*/
|
||||
function _close($connection) {
|
||||
$this->_query("commit", $connection);
|
||||
@mysql_close($connection);
|
||||
}
|
||||
if(count($primary_list))
|
||||
{
|
||||
$column_schema[] = sprintf("primary key (%s)", '`'.implode($primary_list,'`,`').'`');
|
||||
}
|
||||
|
||||
/**
|
||||
* DB transaction start
|
||||
* this method is private
|
||||
* @return boolean
|
||||
*/
|
||||
function _begin() {
|
||||
$connection = $this->_getConnection('master');
|
||||
$this->_query("begin", $connection);
|
||||
return true;
|
||||
}
|
||||
if(count($unique_list))
|
||||
{
|
||||
foreach($unique_list as $key => $val)
|
||||
{
|
||||
$column_schema[] = sprintf("unique %s (%s)", $key, '`'.implode($val,'`,`').'`');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DB transaction rollback
|
||||
* this method is private
|
||||
* @return boolean
|
||||
*/
|
||||
function _rollback() {
|
||||
$connection = $this->_getConnection('master');
|
||||
$this->_query("rollback", $connection);
|
||||
return true;
|
||||
}
|
||||
if(count($index_list))
|
||||
{
|
||||
foreach($index_list as $key => $val)
|
||||
{
|
||||
$column_schema[] = sprintf("index %s (%s)", $key, '`'.implode($val,'`,`').'`');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DB transaction commit
|
||||
* this method is private
|
||||
* @return boolean
|
||||
*/
|
||||
function _commit() {
|
||||
$connection = $this->_getConnection('master');
|
||||
$this->_query("commit", $connection);
|
||||
return true;
|
||||
}
|
||||
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci");
|
||||
|
||||
/**
|
||||
* Execute the query
|
||||
* this method is private
|
||||
* @param string $query
|
||||
* @param resource $connection
|
||||
* @return resource
|
||||
*/
|
||||
function __query($query, $connection) {
|
||||
// Run the query statement
|
||||
$result = @mysql_query($query, $connection);
|
||||
// Error Check
|
||||
if(mysql_error($connection)) $this->setError(mysql_errno($connection), mysql_error($connection));
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$table_name = $this->prefix.$table_name;
|
||||
|
||||
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column;
|
||||
else $columns = $xml_obj->table->column;
|
||||
|
||||
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],
|
||||
$size?'('.$size.')':'',
|
||||
isset($default)?"default '".$default."'":'',
|
||||
$notnull?'not null':'',
|
||||
$auto_increment?'auto_increment':''
|
||||
);
|
||||
|
||||
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,'`,`').'`');
|
||||
}
|
||||
|
||||
if(count($unique_list)) {
|
||||
foreach($unique_list as $key => $val) {
|
||||
$column_schema[] = sprintf("unique %s (%s)", $key, '`'.implode($val,'`,`').'`');
|
||||
}
|
||||
}
|
||||
|
||||
if(count($index_list)) {
|
||||
foreach($index_list as $key => $val) {
|
||||
$column_schema[] = sprintf("index %s (%s)", $key, '`'.implode($val,'`,`').'`');
|
||||
}
|
||||
}
|
||||
|
||||
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci");
|
||||
|
||||
$output = $this->_query($schema);
|
||||
if(!$output) return false;
|
||||
}
|
||||
}
|
||||
?>
|
||||
$output = $this->_query($schema);
|
||||
if(!$output) return false;
|
||||
}
|
||||
}
|
||||
/* End of file DBMysql_innodb.class.php */
|
||||
/* Location: ./classes/db/DBMysql_innodb.class.php */
|
||||
|
|
|
|||
|
|
@ -1,386 +1,409 @@
|
|||
<?php
|
||||
require_once('DBMysql.class.php');
|
||||
require_once('DBMysql.class.php');
|
||||
|
||||
/**
|
||||
* Class to use MySQLi DBMS as mysqli_*
|
||||
* mysql handling class
|
||||
*
|
||||
* Does not use prepared statements, since mysql driver does not support them
|
||||
*
|
||||
* @author NHN (developers@xpressengine.com)
|
||||
* @package /classes/db
|
||||
* @version 0.1
|
||||
*/
|
||||
class DBMysqli extends DBMysql {
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
* Constructor
|
||||
* @return void
|
||||
*/
|
||||
function DBMysqli()
|
||||
{
|
||||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @return void
|
||||
**/
|
||||
function DBMysqli() {
|
||||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
/**
|
||||
* Return if supportable
|
||||
* Check 'mysqli_connect' function exists.
|
||||
* @return boolean
|
||||
*/
|
||||
function isSupported()
|
||||
{
|
||||
if(!function_exists('mysqli_connect')) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if supportable
|
||||
* Check 'mysqli_connect' function exists.
|
||||
* @return boolean
|
||||
*/
|
||||
function isSupported() {
|
||||
if(!function_exists('mysqli_connect')) return false;
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Create an instance of this class
|
||||
* @return DBMysqli return DBMysqli object instance
|
||||
*/
|
||||
function create()
|
||||
{
|
||||
return new DBMysqli;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of this class
|
||||
* @return DBMysqli return DBMysqli object instance
|
||||
*/
|
||||
function create()
|
||||
/**
|
||||
* DB Connect
|
||||
* this method is private
|
||||
* @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password
|
||||
* @return resource
|
||||
*/
|
||||
function __connect($connection)
|
||||
{
|
||||
// Attempt to connect
|
||||
if ($connection["db_port"])
|
||||
{
|
||||
return new DBMysqli;
|
||||
$result = @mysqli_connect($connection["db_hostname"]
|
||||
, $connection["db_userid"]
|
||||
, $connection["db_password"]
|
||||
, $connection["db_database"]
|
||||
, $connection["db_port"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = @mysqli_connect($connection["db_hostname"]
|
||||
, $connection["db_userid"]
|
||||
, $connection["db_password"]
|
||||
, $connection["db_database"]);
|
||||
}
|
||||
$error = mysqli_connect_errno();
|
||||
if($error)
|
||||
{
|
||||
$this->setError($error,mysqli_connect_error());
|
||||
return;
|
||||
}
|
||||
mysqli_set_charset($result,'utf8');
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB Connect
|
||||
* this method is private
|
||||
* @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password
|
||||
* @return resource
|
||||
*/
|
||||
function __connect($connection) {
|
||||
// Attempt to connect
|
||||
if ($connection["db_port"]) {
|
||||
$result = @mysqli_connect($connection["db_hostname"]
|
||||
, $connection["db_userid"]
|
||||
, $connection["db_password"]
|
||||
, $connection["db_database"]
|
||||
, $connection["db_port"]);
|
||||
} else {
|
||||
$result = @mysqli_connect($connection["db_hostname"]
|
||||
, $connection["db_userid"]
|
||||
, $connection["db_password"]
|
||||
, $connection["db_database"]);
|
||||
}
|
||||
$error = mysqli_connect_errno();
|
||||
if($error) {
|
||||
$this->setError($error,mysqli_connect_error());
|
||||
return;
|
||||
}
|
||||
mysqli_set_charset($result,'utf8');
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* DB disconnection
|
||||
* this method is private
|
||||
* @param resource $connection
|
||||
* @return void
|
||||
*/
|
||||
function _close($connection)
|
||||
{
|
||||
mysqli_close($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* DB disconnection
|
||||
* this method is private
|
||||
* @param resource $connection
|
||||
* @return void
|
||||
*/
|
||||
function _close($connection) {
|
||||
mysqli_close($connection);
|
||||
}
|
||||
/**
|
||||
* Handles quatation of the string variables from the query
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
function addQuotes($string)
|
||||
{
|
||||
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
|
||||
if(!is_numeric($string))
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
$string = mysqli_escape_string($connection,$string);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles quatation of the string variables from the query
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
function addQuotes($string) {
|
||||
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
|
||||
if(!is_numeric($string)){
|
||||
$connection = $this->_getConnection('master');
|
||||
$string = mysqli_escape_string($connection,$string);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
/**
|
||||
* Execute the query
|
||||
* this method is private
|
||||
* @param string $query
|
||||
* @param resource $connection
|
||||
* @return resource
|
||||
*/
|
||||
function __query($query, $connection)
|
||||
{
|
||||
if($this->use_prepared_statements == 'Y')
|
||||
{
|
||||
// 1. Prepare query
|
||||
$stmt = mysqli_prepare($connection, $query);
|
||||
if($stmt)
|
||||
{
|
||||
$types = '';
|
||||
$params = array();
|
||||
$this->_prepareQueryParameters($types, $params);
|
||||
|
||||
/**
|
||||
* 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')
|
||||
{
|
||||
// 1. Prepare query
|
||||
$stmt = mysqli_prepare($connection, $query);
|
||||
if($stmt){
|
||||
$types = '';
|
||||
$params = array();
|
||||
$this->_prepareQueryParameters($types, $params);
|
||||
|
||||
if(!empty($params))
|
||||
if(!empty($params))
|
||||
{
|
||||
$args[0] = $stmt;
|
||||
$args[1] = $types;
|
||||
|
||||
$i = 2;
|
||||
foreach($params as $key => $param)
|
||||
{
|
||||
$args[0] = $stmt;
|
||||
$args[1] = $types;
|
||||
|
||||
$i = 2;
|
||||
foreach($params as $key => $param) {
|
||||
$copy[$key] = $param;
|
||||
$args[$i++] = &$copy[$key];
|
||||
}
|
||||
|
||||
// 2. Bind parameters
|
||||
$status = call_user_func_array('mysqli_stmt_bind_param',$args);
|
||||
if(!$status)
|
||||
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
|
||||
$copy[$key] = $param;
|
||||
$args[$i++] = &$copy[$key];
|
||||
}
|
||||
|
||||
// 3. Execute query
|
||||
$status = mysqli_stmt_execute($stmt);
|
||||
|
||||
|
||||
// 2. Bind parameters
|
||||
$status = call_user_func_array('mysqli_stmt_bind_param',$args);
|
||||
if(!$status)
|
||||
$this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
|
||||
|
||||
// Return stmt for other processing - like retrieving resultset (_fetch)
|
||||
return $stmt;
|
||||
// mysqli_stmt_close($stmt);
|
||||
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
|
||||
}
|
||||
|
||||
|
||||
// 3. Execute query
|
||||
$status = mysqli_stmt_execute($stmt);
|
||||
|
||||
if(!$status)
|
||||
$this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
|
||||
|
||||
// Return stmt for other processing - like retrieving resultset (_fetch)
|
||||
return $stmt;
|
||||
// mysqli_stmt_close($stmt);
|
||||
}
|
||||
// Run the query statement
|
||||
$result = mysqli_query($connection,$query);
|
||||
// Error Check
|
||||
$error = mysqli_error($connection);
|
||||
if($error){
|
||||
$this->setError(mysqli_errno($connection), $error);
|
||||
}
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Before execute query, prepare statement
|
||||
* this method is private
|
||||
* @param string $types
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
function _prepareQueryParameters(&$types, &$params){
|
||||
$types = '';
|
||||
$params = array();
|
||||
if(!$this->param) return;
|
||||
|
||||
foreach($this->param as $k => $o){
|
||||
$value = $o->getUnescapedValue();
|
||||
$type = $o->getType();
|
||||
|
||||
// Skip column names -> this should be concatenated to query string
|
||||
if($o->isColumnName()) continue;
|
||||
|
||||
switch($type)
|
||||
|
||||
}
|
||||
// Run the query statement
|
||||
$result = mysqli_query($connection,$query);
|
||||
// Error Check
|
||||
$error = mysqli_error($connection);
|
||||
if($error)
|
||||
{
|
||||
$this->setError(mysqli_errno($connection), $error);
|
||||
}
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Before execute query, prepare statement
|
||||
* this method is private
|
||||
* @param string $types
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
function _prepareQueryParameters(&$types, &$params)
|
||||
{
|
||||
$types = '';
|
||||
$params = array();
|
||||
if(!$this->param) return;
|
||||
|
||||
foreach($this->param as $k => $o)
|
||||
{
|
||||
$value = $o->getUnescapedValue();
|
||||
$type = $o->getType();
|
||||
|
||||
// Skip column names -> this should be concatenated to query string
|
||||
if($o->isColumnName()) continue;
|
||||
|
||||
switch($type)
|
||||
{
|
||||
case 'number' :
|
||||
$type = 'i';
|
||||
break;
|
||||
case 'varchar' :
|
||||
$type = 's';
|
||||
break;
|
||||
default:
|
||||
$type = 's';
|
||||
}
|
||||
|
||||
if(is_array($value))
|
||||
{
|
||||
foreach($value as $v)
|
||||
{
|
||||
case 'number' :
|
||||
$type = 'i';
|
||||
break;
|
||||
case 'varchar' :
|
||||
$type = 's';
|
||||
break;
|
||||
default:
|
||||
$type = 's';
|
||||
}
|
||||
|
||||
if(is_array($value))
|
||||
{
|
||||
foreach($value as $v)
|
||||
{
|
||||
$params[] = $v;
|
||||
$types .= $type;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$params[] = $value;
|
||||
$params[] = $v;
|
||||
$types .= $type;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the result
|
||||
* @param resource $result
|
||||
* @param int|NULL $arrayIndexEndValue
|
||||
* @return array
|
||||
*/
|
||||
function _fetch($result, $arrayIndexEndValue = NULL) {
|
||||
if($this->use_prepared_statements != 'Y'){
|
||||
return parent::_fetch($result, $arrayIndexEndValue);
|
||||
}
|
||||
$output = array();
|
||||
if(!$this->isConnected() || $this->isError() || !$result) return $output;
|
||||
|
||||
// Prepared stements: bind result variable and fetch data
|
||||
$stmt = $result;
|
||||
$meta = mysqli_stmt_result_metadata($stmt);
|
||||
$fields = mysqli_fetch_fields($meta);
|
||||
|
||||
/**
|
||||
* Mysqli has a bug that causes LONGTEXT columns not to get loaded
|
||||
* Unless store_result is called before
|
||||
* MYSQLI_TYPE for longtext is 252
|
||||
*/
|
||||
$longtext_exists = false;
|
||||
foreach($fields as $field)
|
||||
else
|
||||
{
|
||||
if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
|
||||
$field->name = 'repeat_' . $field->name;
|
||||
|
||||
// Array passed needs to contain references, not values
|
||||
$row[$field->name] = "";
|
||||
$resultArray[$field->name] = &$row[$field->name];
|
||||
|
||||
if($field->type == 252) $longtext_exists = true;
|
||||
$params[] = $value;
|
||||
$types .= $type;
|
||||
}
|
||||
$resultArray = array_merge(array($stmt), $resultArray);
|
||||
|
||||
if($longtext_exists)
|
||||
{
|
||||
mysqli_stmt_store_result($stmt);
|
||||
}
|
||||
|
||||
call_user_func_array('mysqli_stmt_bind_result', $resultArray);
|
||||
|
||||
$rows = array();
|
||||
while(mysqli_stmt_fetch($stmt))
|
||||
{
|
||||
$resultObject = new stdClass();
|
||||
|
||||
foreach($resultArray as $key => $value)
|
||||
{
|
||||
if($key === 0) continue; // Skip stmt object
|
||||
if(strpos($key, 'repeat_')) $key = substr($key, 6);
|
||||
$resultObject->$key = $value;
|
||||
}
|
||||
|
||||
$rows[] = $resultObject;
|
||||
}
|
||||
|
||||
mysqli_stmt_close($stmt);
|
||||
}
|
||||
}
|
||||
|
||||
if($arrayIndexEndValue)
|
||||
{
|
||||
foreach($rows as $row)
|
||||
{
|
||||
$output[$arrayIndexEndValue--] = $row;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$output = $rows;
|
||||
}
|
||||
|
||||
if(count($output)==1){
|
||||
if(isset($arrayIndexEndValue)) return $output;
|
||||
else return $output[0];
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles insertAct
|
||||
* @param Object $queryObject
|
||||
* @param boolean $with_values
|
||||
* @return resource
|
||||
*/
|
||||
function _executeInsertAct($queryObject, $with_values = false){
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeInsertAct($queryObject);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeInsertAct($queryObject, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles updateAct
|
||||
* @param Object $queryObject
|
||||
* @param boolean $with_values
|
||||
* @return resource
|
||||
*/
|
||||
function _executeUpdateAct($queryObject, $with_values = false) {
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeUpdateAct($queryObject);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeUpdateAct($queryObject, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles deleteAct
|
||||
* @param Object $queryObject
|
||||
* @param boolean $with_values
|
||||
* @return resource
|
||||
*/
|
||||
function _executeDeleteAct($queryObject, $with_values = false) {
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeDeleteAct($queryObject);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeDeleteAct($queryObject, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle selectAct
|
||||
* In order to get a list of pages easily when selecting \n
|
||||
* it supports a method as navigation
|
||||
* @param Object $queryObject
|
||||
* @param resource $connection
|
||||
* @param boolean $with_values
|
||||
* @return Object
|
||||
*/
|
||||
function _executeSelectAct($queryObject, $connection = null, $with_values = false) {
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeSelectAct($queryObject, $connection);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeSelectAct($queryObject, $connection, $with_values);
|
||||
unset($this->param);
|
||||
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()
|
||||
/**
|
||||
* Fetch the result
|
||||
* @param resource $result
|
||||
* @param int|NULL $arrayIndexEndValue
|
||||
* @return array
|
||||
*/
|
||||
function _fetch($result, $arrayIndexEndValue = NULL)
|
||||
{
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
return mysqli_insert_id($connection);
|
||||
return parent::_fetch($result, $arrayIndexEndValue);
|
||||
}
|
||||
$output = array();
|
||||
if(!$this->isConnected() || $this->isError() || !$result) return $output;
|
||||
|
||||
// Prepared stements: bind result variable and fetch data
|
||||
$stmt = $result;
|
||||
$meta = mysqli_stmt_result_metadata($stmt);
|
||||
$fields = mysqli_fetch_fields($meta);
|
||||
|
||||
/**
|
||||
* Fetch a result row as an object
|
||||
* @param resource $result
|
||||
* @return object
|
||||
* Mysqli has a bug that causes LONGTEXT columns not to get loaded
|
||||
* Unless store_result is called before
|
||||
* MYSQLI_TYPE for longtext is 252
|
||||
*/
|
||||
function db_fetch_object(&$result)
|
||||
$longtext_exists = false;
|
||||
foreach($fields as $field)
|
||||
{
|
||||
return mysqli_fetch_object($result);
|
||||
if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
|
||||
$field->name = 'repeat_' . $field->name;
|
||||
|
||||
// Array passed needs to contain references, not values
|
||||
$row[$field->name] = "";
|
||||
$resultArray[$field->name] = &$row[$field->name];
|
||||
|
||||
if($field->type == 252) $longtext_exists = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free result memory
|
||||
* @param resource $result
|
||||
* @return boolean Returns TRUE on success or FALSE on failure.
|
||||
*/
|
||||
function db_free_result(&$result){
|
||||
return mysqli_free_result($result);
|
||||
}
|
||||
}
|
||||
?>
|
||||
$resultArray = array_merge(array($stmt), $resultArray);
|
||||
|
||||
if($longtext_exists)
|
||||
{
|
||||
mysqli_stmt_store_result($stmt);
|
||||
}
|
||||
|
||||
call_user_func_array('mysqli_stmt_bind_result', $resultArray);
|
||||
|
||||
$rows = array();
|
||||
while(mysqli_stmt_fetch($stmt))
|
||||
{
|
||||
$resultObject = new stdClass();
|
||||
|
||||
foreach($resultArray as $key => $value)
|
||||
{
|
||||
if($key === 0) continue; // Skip stmt object
|
||||
if(strpos($key, 'repeat_')) $key = substr($key, 6);
|
||||
$resultObject->$key = $value;
|
||||
}
|
||||
|
||||
$rows[] = $resultObject;
|
||||
}
|
||||
|
||||
mysqli_stmt_close($stmt);
|
||||
|
||||
if($arrayIndexEndValue)
|
||||
{
|
||||
foreach($rows as $row)
|
||||
{
|
||||
$output[$arrayIndexEndValue--] = $row;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$output = $rows;
|
||||
}
|
||||
|
||||
if(count($output)==1)
|
||||
{
|
||||
if(isset($arrayIndexEndValue)) return $output;
|
||||
else return $output[0];
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles insertAct
|
||||
* @param Object $queryObject
|
||||
* @param boolean $with_values
|
||||
* @return resource
|
||||
*/
|
||||
function _executeInsertAct($queryObject, $with_values = false){
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeInsertAct($queryObject);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeInsertAct($queryObject, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles updateAct
|
||||
* @param Object $queryObject
|
||||
* @param boolean $with_values
|
||||
* @return resource
|
||||
*/
|
||||
function _executeUpdateAct($queryObject, $with_values = false) {
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeUpdateAct($queryObject);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeUpdateAct($queryObject, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles deleteAct
|
||||
* @param Object $queryObject
|
||||
* @param boolean $with_values
|
||||
* @return resource
|
||||
*/
|
||||
function _executeDeleteAct($queryObject, $with_values = false) {
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeDeleteAct($queryObject);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeDeleteAct($queryObject, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle selectAct
|
||||
* In order to get a list of pages easily when selecting \n
|
||||
* it supports a method as navigation
|
||||
* @param Object $queryObject
|
||||
* @param resource $connection
|
||||
* @param boolean $with_values
|
||||
* @return Object
|
||||
*/
|
||||
function _executeSelectAct($queryObject, $connection = null, $with_values = false)
|
||||
{
|
||||
if($this->use_prepared_statements != 'Y')
|
||||
{
|
||||
return parent::_executeSelectAct($queryObject, $connection);
|
||||
}
|
||||
$this->param = $queryObject->getArguments();
|
||||
$result = parent::_executeSelectAct($queryObject, $connection, $with_values);
|
||||
unset($this->param);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID generated in the last query
|
||||
* Return next sequence from sequence table
|
||||
* This method use only mysql
|
||||
* @return int
|
||||
*/
|
||||
function db_insert_id()
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
return mysqli_insert_id($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a result row as an object
|
||||
* @param resource $result
|
||||
* @return object
|
||||
*/
|
||||
function db_fetch_object(&$result)
|
||||
{
|
||||
return mysqli_fetch_object($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free result memory
|
||||
* @param resource $result
|
||||
* @return boolean Returns TRUE on success or FALSE on failure.
|
||||
*/
|
||||
function db_free_result(&$result)
|
||||
{
|
||||
return mysqli_free_result($result);
|
||||
}
|
||||
}
|
||||
/* End of file DBMysqli.class.php */
|
||||
/* Location: ./classes/db/DBMysqli.class.php */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue