Skip to content
Browse files

Move parameter count management to a class

The MantisDbParam class handles the logic for:
- assignment of parameters, db_param() calls
- push onto stack, db_param_push() calls
- pop stack
  • Loading branch information...
1 parent 592cbb9 commit 8bdc9697f45f4b54be4a7a4e3f2ba2f4364f375a @dregad dregad committed
Showing with 56 additions and 37 deletions.
  1. +56 −37 core/database_api.php
View
93 core/database_api.php
@@ -73,17 +73,58 @@
}
/**
- * Tracks the query parameter count for use with db_param().
- * @global int $g_db_param_count
+ * Mantis Database Parameters Count class
+ * Stores the current parameter count, provides method to generate parameters
+ * and a simple stack mechanism to enable the caller to build multiple queries
+ * concurrently on RDBMS using positional parameters (e.g. PostgreSQL)
+ * @package MantisBT
+ * @subpackage classes
*/
-$g_db_param_count = 0;
+class MantisDbParam {
+ /** Current parameter count */
+ public $count = 0;
+
+ /** Parameter count stack */
+ private $stack = array();
+
+ /**
+ * Generate a string to insert a parameter into a database query string
+ * @return string 'wildcard' matching a parameter in correct ordered format for the current database.
+ */
+ public function assign() {
+ global $g_db;
+ return $g_db->Param( $this->count++ );
+ }
+
+ /**
+ * Pushes current parameter count onto stack and resets its value to 0
+ */
+ public function push() {
+ $this->stack[] = $this->count;
+ $this->count = 0;
+ }
+
+ /**
+ * Pops the previous value of param count from the stack
+ * This function is called by {@see db_query_bound()} and should not need
+ * to be executed directly
+ */
+ public function pop() {
+ global $g_db;
+
+ $this->count = (int)array_pop( $this->stack );
+ if( db_is_pgsql() ) {
+ # Manually reset the ADOdb param number to the value we just popped
+ $g_db->_pnum = $this->count;
+ }
+ }
+}
/**
- * Query parameter count stack
- * Used by {@see db_param_push()} and {@see db_param_pop()}
- * @see $g_db_param_count
+ * Tracks the query parameter count
+ * @global object $g_db_param
*/
-$g_db_param_stack = array();
+$g_db_param = new MantisDbParam();
/**
* Open a connection to the database.
@@ -282,7 +323,7 @@ function db_check_identifier_size( $p_identifier ) {
/**
* execute query, requires connection to be opened
* An error will be triggered if there is a problem executing the query.
- * This will call {@see db_param_pop()} after a successful execution
+ * This will pop the database parameter stack {@see MantisDbParam} after a successful execution
* @global array of previous executed queries for profiling
* @global adodb database connection object
* @global boolean indicating whether queries array is populated
@@ -293,7 +334,7 @@ function db_check_identifier_size( $p_identifier ) {
* @return ADORecordSet|bool adodb result set or false if the query failed.
*/
function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = -1 ) {
- global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param_count;
+ global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param;
$t_db_type = config_get_global( 'db_type' );
@@ -381,7 +422,7 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
trigger_error( ERROR_DB_QUERY_FAILED, ERROR );
return false;
} else {
- db_param_pop();
+ $g_db_param->pop();
return $t_result;
}
}
@@ -391,40 +432,18 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
* @return string 'wildcard' matching a parameter in correct ordered format for the current database.
*/
function db_param() {
- global $g_db;
- global $g_db_param_count;
-
- return $g_db->Param( $g_db_param_count++ );
+ global $g_db_param;
+ return $g_db_param->assign();
}
/**
- * Pushes current value of $g_db_param_count onto stack and resets its value
+ * Pushes current parameter count onto stack and resets its value
* Allows the caller to build multiple queries concurrently on RDBMS using
* positional parameters (e.g. PostgreSQL)
*/
function db_param_push() {
- global $g_db_param_count;
- global $g_db_param_stack;
-
- $g_db_param_stack[] = $g_db_param_count;
- $g_db_param_count = 0;
-}
-
-/**
- * Pops previous value of $g_db_param_count from stack
- * This function is called by {@see db_query_bound()} and should normally not
- * be executed directly
- */
-function db_param_pop() {
- global $g_db_param_count;
- global $g_db_param_stack;
- global $g_db;
-
- $g_db_param_count = (int)array_pop( $g_db_param_stack );
- if( db_is_pgsql() ) {
- # Manually reset the ADOdb param number to the value we just popped
- $g_db->_pnum = $g_db_param_count;
- }
+ global $g_db_param;
+ $g_db_param->push();
}
/**

0 comments on commit 8bdc969

Please sign in to comment.
Something went wrong with that request. Please try again.