+ ######################################
+ # Prepared statements
+ ######################################
+
+ /**
+ * Intended to be compatible with the PEAR::DB wrapper functions.
+ * http://pear.php.net/manual/en/package.database.db.intro-execute.php
+ *
+ * ? = scalar value, quoted as necessary
+ * ! = raw SQL bit (a function for instance)
+ * & = filename; reads the file and inserts as a blob
+ * (we don't use this though...)
+ * @param $sql String: SQL statement with appropriate markers
+ * @param $func String: Name of the function, for profiling
+ * @return resource a prepared DB2 SQL statement
+ */
+ public function prepare( $sql, $func = 'DB2::prepare' ) {
+ $stmt = db2_prepare( $this->mConn, $sql, $this->mStmtOptions );
+ return $stmt;
+ }
+
+ /**
+ * Frees resources associated with a prepared statement
+ * @return Boolean success or failure
+ */
+ public function freePrepared( $prepared ) {
+ return db2_free_stmt( $prepared );
+ }
+
+ /**
+ * Execute a prepared query with the various arguments
+ * @param $prepared String: the prepared sql
+ * @param $args Mixed: either an array here, or put scalars as varargs
+ * @return Resource: results object
+ */
+ public function execute( $prepared, $args = null ) {
+ if( !is_array( $args ) ) {
+ # Pull the var args
+ $args = func_get_args();
+ array_shift( $args );
+ }
+ $res = db2_execute( $prepared, $args );
+ if ( !$res ) {
+ $this->installPrint( db2_stmt_errormsg() );
+ }
+ return $res;
+ }
+
+ /**
+ * Prepare & execute an SQL statement, quoting and inserting arguments
+ * in the appropriate places.
+ * @param $query String
+ * @param $args ...
+ */
+ public function safeQuery( $query, $args = null ) {
+ // copied verbatim from Database.php
+ $prepared = $this->prepare( $query, 'DB2::safeQuery' );
+ if( !is_array( $args ) ) {
+ # Pull the var args
+ $args = func_get_args();
+ array_shift( $args );
+ }
+ $retval = $this->execute( $prepared, $args );
+ $this->freePrepared( $prepared );
+ return $retval;
+ }
+
+ /**
+ * For faking prepared SQL statements on DBs that don't support
+ * it directly.
+ * @param $preparedQuery String: a 'preparable' SQL statement
+ * @param $args Array of arguments to fill it with
+ * @return String: executable statement
+ */
+ public function fillPrepared( $preparedQuery, $args ) {
+ reset( $args );
+ $this->preparedArgs =& $args;
+
+ foreach ( $args as $i => $arg ) {
+ db2_bind_param( $preparedQuery, $i+1, $args[$i] );
+ }
+
+ return $preparedQuery;
+ }
+
+ /**
+ * Switches module between regular and install modes
+ */
+ public function setMode( $mode ) {
+ $old = $this->mMode;
+ $this->mMode = $mode;
+ return $old;
+ }
+
+ /**
+ * Bitwise negation of a column or value in SQL
+ * Same as (~field) in C
+ * @param $field String
+ * @return String
+ */
+ function bitNot( $field ) {
+ // expecting bit-fields smaller than 4bytes
+ return "BITNOT( $field )";
+ }
+
+ /**
+ * Bitwise AND of two columns or values in SQL
+ * Same as (fieldLeft & fieldRight) in C
+ * @param $fieldLeft String
+ * @param $fieldRight String
+ * @return String
+ */
+ function bitAnd( $fieldLeft, $fieldRight ) {
+ return "BITAND( $fieldLeft, $fieldRight )";
+ }
+
+ /**
+ * Bitwise OR of two columns or values in SQL
+ * Same as (fieldLeft | fieldRight) in C
+ * @param $fieldLeft String
+ * @param $fieldRight String
+ * @return String
+ */
+ function bitOr( $fieldLeft, $fieldRight ) {
+ return "BITOR( $fieldLeft, $fieldRight )";
+ }
+}
+
+class IBM_DB2Helper {
+ public static function makeArray( $maybeArray ) {
+ if ( !is_array( $maybeArray ) ) {
+ return array( $maybeArray );
+ }
+
+ return $maybeArray;
+ }