Пример #1
0
 /**
  * Base query method
  */
 function sql_query($query = '', $cache_ttl = 0)
 {
     if ($query != '') {
         global $cache;
         $this->last_query_text = $query;
         $this->query_result = $cache_ttl && method_exists($cache, 'sql_load') ? $cache->sql_load($query) : false;
         $this->sql_add_num_queries($this->query_result);
         if (!$this->query_result) {
             if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false) {
                 $this->sql_error($query);
             }
             if (!$this->transaction) {
                 @ibase_commit_ret();
             }
             if ($cache_ttl && method_exists($cache, 'sql_save')) {
                 $this->open_queries[(int) $this->query_result] = $this->query_result;
                 $cache->sql_save($query, $this->query_result, $cache_ttl);
             } else {
                 if (strpos($query, 'SELECT') === 0 && $this->query_result) {
                     $this->open_queries[(int) $this->query_result] = $this->query_result;
                 }
             }
         }
     } else {
         return false;
     }
     return $this->query_result ? $this->query_result : false;
 }
 /**
  * Executes a prepared statement.
  *
  * @param array $params OPTIONAL Values to bind to parameter placeholders.
  * @return bool
  * @throws ZendX_Db_Statement_Firebird_Exception
  */
 public function _execute(array $params = null)
 {
     if (!$this->_stmtPrepared) {
         return false;
     }
     // if no params were given as an argument to execute(),
     // then default to the _bindParam array
     if ($params === null) {
         $params = $this->_bindParam;
     }
     // send $params as input parameters to the statement
     if ($params) {
         array_unshift($params, $this->_stmtPrepared);
         $retval = @call_user_func_array('ibase_execute', $params);
     } else {
         // execute the statement
         $retval = @ibase_execute($this->_stmtPrepared);
     }
     $this->_stmtResult = $retval;
     if ($retval === false) {
         $last_error = ibase_errmsg();
         $this->_stmtRowCount = 0;
     }
     //Firebird php ibase extension, auto-commit is not after each call, but at
     //end of script. Disabled when transaction is active
     if (!$this->_adapter->getTransaction()) {
         ibase_commit_ret();
     }
     if ($retval === false) {
         /**
          * @see ZendX_Db_Statement_Firebird_Exception
          */
         require_once 'ZendX/Db/Statement/Firebird/Exception.php';
         throw new ZendX_Db_Statement_Firebird_Exception("Firebird statement execute error : " . $last_error);
     }
     // statements that have no result set do not return metadata
     if (is_resource($this->_stmtResult)) {
         // get the column names that will result
         $this->_keys = array();
         $coln = ibase_num_fields($this->_stmtResult);
         $this->_stmtColumnCount = $coln;
         for ($i = 0; $i < $coln; $i++) {
             $col_info = ibase_field_info($this->_stmtResult, $i);
             $this->_keys[] = $this->_adapter->foldCase($col_info['name']);
         }
         // set up a binding space for result variables
         $this->_values = array_fill(0, count($this->_keys), null);
         // set up references to the result binding space.
         // just passing $this->_values in the call_user_func_array()
         // below won't work, you need references.
         $refs = array();
         foreach ($this->_values as $i => &$f) {
             $refs[$i] =& $f;
         }
     }
     if ($trans = $this->_adapter->getTransaction()) {
         $this->_stmtRowCount = ibase_affected_rows($trans);
     } else {
         $this->_stmtRowCount = ibase_affected_rows($this->_adapter->getConnection());
     }
     return true;
 }
Пример #3
0
 function sql_query($query = '', $cache_ttl = 0)
 {
     if ($query != '') {
         global $cache;
         $this->last_query_text = $query;
         $this->query_result = $cache_ttl && method_exists($cache, 'sql_load') ? $cache->sql_load($query) : false;
         if (!$this->query_result) {
             $this->num_queries++;
             if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false) {
                 $this->sql_error($query);
             }
             // TODO: have to debug the commit states in firebird
             if (!$this->transaction) {
                 @ibase_commit_ret();
             }
             if ($cache_ttl && method_exists($cache, 'sql_save')) {
                 $cache->sql_save($query, $this->query_result, $cache_ttl);
             }
         }
     } else {
         return false;
     }
     return $this->query_result ? $this->query_result : false;
 }
Пример #4
0
 /**
  * Base query method
  *
  * @param	string	$query		Contains the SQL query which shall be executed
  * @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  * @return	mixed				When casted to bool the returned value returns true on success and false on failure
  *
  * @access	public
  */
 function sql_query($query = '', $cache_ttl = 0)
 {
     if ($query != '') {
         global $cache;
         // EXPLAIN only in extra debug mode
         if (defined('DEBUG_EXTRA')) {
             $this->sql_report('start', $query);
         }
         $this->last_query_text = $query;
         $this->query_result = $cache_ttl && method_exists($cache, 'sql_load') ? $cache->sql_load($query) : false;
         $this->sql_add_num_queries($this->query_result);
         if ($this->query_result === false) {
             $array = array();
             // We overcome Firebird's 32767 char limit by binding vars
             if (strlen($query) > 32767) {
                 if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs)) {
                     if (strlen($regs[3]) > 32767) {
                         preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
                         $inserts = $vals[0];
                         unset($vals);
                         foreach ($inserts as $key => $value) {
                             if (!empty($value) && $value[0] === "'" && strlen($value) > 32769) {
                                 $inserts[$key] = '?';
                                 $array[] = str_replace("''", "'", substr($value, 1, -1));
                             }
                         }
                         $query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
                     }
                 } else {
                     if (preg_match('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|\\d+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data)) {
                         if (strlen($data[3]) > 32767) {
                             $update = $data[1];
                             $where = $data[4];
                             preg_match_all('/(\\w++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\\d-.]++)/', $data[3], $temp, PREG_SET_ORDER);
                             unset($data);
                             $cols = array();
                             foreach ($temp as $value) {
                                 if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32769) {
                                     $array[] = str_replace("''", "'", substr($value[2], 1, -1));
                                     $cols[] = $value[1] . '=?';
                                 } else {
                                     $cols[] = $value[1] . '=' . $value[2];
                                 }
                             }
                             $query = $update . implode(', ', $cols) . ' ' . $where;
                             unset($cols);
                         }
                     }
                 }
             }
             if (!function_exists('ibase_affected_rows') && (preg_match('/^UPDATE ([\\w_]++)\\s+SET [\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\\d-.]+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\\d-.]+))*+\\s+(WHERE.*)?$/s', $query, $regs) || preg_match('/^DELETE FROM ([\\w_]++)\\s*(WHERE\\s*.*)?$/s', $query, $regs))) {
                 $affected_sql = 'SELECT COUNT(*) as num_rows_affected FROM ' . $regs[1];
                 if (!empty($regs[2])) {
                     $affected_sql .= ' ' . $regs[2];
                 }
                 if (!($temp_q_id = @ibase_query($this->db_connect_id, $affected_sql))) {
                     return false;
                 }
                 $temp_result = @ibase_fetch_assoc($temp_q_id);
                 @ibase_free_result($temp_q_id);
                 $this->affected_rows = $temp_result ? $temp_result['NUM_ROWS_AFFECTED'] : false;
             }
             if (sizeof($array)) {
                 $p_query = @ibase_prepare($this->db_connect_id, $query);
                 array_unshift($array, $p_query);
                 $this->query_result = call_user_func_array('ibase_execute', $array);
                 unset($array);
                 if ($this->query_result === false) {
                     $this->sql_error($query);
                 }
             } else {
                 if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false) {
                     $this->sql_error($query);
                 }
             }
             if (defined('DEBUG_EXTRA')) {
                 $this->sql_report('stop', $query);
             }
             if (!$this->transaction) {
                 if (function_exists('ibase_commit_ret')) {
                     @ibase_commit_ret();
                 } else {
                     // way cooler than ibase_commit_ret :D
                     @ibase_query('COMMIT RETAIN;');
                 }
             }
             if ($cache_ttl && method_exists($cache, 'sql_save')) {
                 $this->open_queries[(int) $this->query_result] = $this->query_result;
                 $cache->sql_save($query, $this->query_result, $cache_ttl);
             } else {
                 if (strpos($query, 'SELECT') === 0 && $this->query_result) {
                     $this->open_queries[(int) $this->query_result] = $this->query_result;
                 }
             }
         } else {
             if (defined('DEBUG_EXTRA')) {
                 $this->sql_report('fromcache', $query);
             }
         }
     } else {
         return false;
     }
     return $this->query_result;
 }
Пример #5
0
 function commit()
 {
     if ($this->debug) {
         echo "<pre style=\"color : green\">Committing {$this->dbpath} </pre>";
     }
     switch ($this->dbtype) {
         /* Firebird Functionality */
         case "firebird":
             return ibase_commit_ret($this->dbh);
             break;
             /* SQLite Functionality */
         /* SQLite Functionality */
         case "sqlite":
             //doesn't support comitting
             return "Not Supported";
             break;
             /* Oracle Functionality */
         /* Oracle Functionality */
         case "oracle":
             return oci_commit($this->dbh);
             break;
     }
 }