/** * Prepares and executes an SQL statement with bound data. * * @param mixed $sql The SQL statement with placeholders. * May be a string or Zend_Db_Select. * @param mixed $bind An array of data to bind to the placeholders. * @return Zend_Db_Statement_Interface */ public function query($sql, $bind = array()) { // is the $sql a Zend_Db_Select object? if ($sql instanceof Zend_Db_Select) { $sql = $sql->assemble(); } $sql = trim($sql); // some checks to see if we should add the enhancement if (!$this->_isInsert($sql)) { return parent::query($sql, $bind); } // if the query is executed without any parameters there's no point in adding enhancements if (empty($bind)) { return parent::query($sql, $bind); } // the first parameter of the $bind array must be set to null // if it isn't we don't add the enhancement reset($bind); list($key, $first) = each($bind); if (null !== $first) { return parent::query($sql, $bind); } // attempt to build the sequence name from the table name $table = $this->_extractTableName($sql); if (false === ($sequence = $this->_getSequenceName($table))) { return parent::query($sql, $bind); } // add the enhancement (which is setting the first parameter in the $bind array // to the next sequence value) $bind[$key] = parent::nextSequenceId($sequence); reset($bind); return parent::query($sql, $bind); }
/** * Prepares and executes an SQL statement with bound data. * * @param mixed $sql The SQL statement with placeholders. * May be a string or Zend_Db_Select. * @param mixed $bind An array of data to bind to the placeholders. * @return Zend_Db_Statement_Interface */ public function query($sql, $bind = array()) { // is the $sql a Zend_Db_Select object? if ($sql instanceof Zend_Db_Select) { $sql = $sql->assemble(); } $sql = trim($sql); // if the query doesn't have positional parameters, exit early. if (false === strpos($sql, '?') && !empty($bind)) { return parent::query($sql, $bind); } // replace positional parameters with named parameters // E.g. SELECT FROM TABLE WHERE x = ? and y = ? will be // transformed into SELECT FROM TABLE WHERE x = :bind_0001 and y = :bind_0002 $this->_tmp = 0; $sql = preg_replace_callback('~\\?~', array($this, '_replaceQuestionMarks'), $sql, -1, $count); unset($this->_tmp); if ($count < 1) { return parent::query($sql, $bind); } // modifiy the $bind array into an associative array for ($i = 1; $i <= $count; $i++) { $keys[] = sprintf(':bind_%04d', $i); } $bind = array_combine($keys, array_values($bind)); // execute the parent method return parent::query($sql, $bind); }
/** * Prepares and executes an SQL statement with bound data. * * @param mixed $sql The SQL statement with placeholders. * May be a string or Zend_Db_Select. * @param mixed $bind An array of data to bind to the placeholders. * @return Zend_Db_Statement_Interface */ public function query($sql, $bind = array()) { // is the $sql a Zend_Db_Select object? if ($sql instanceof Zend_Db_Select) { $sql = $sql->assemble(); } $sql = trim($sql); if (!$this->_canAddEnhancement($sql, $bind)) { return parent::query($sql, $bind); } $sqlParametersType = $this->_getSqlParametersType($bind); switch ($sqlParametersType) { case 'named': $this->_updateSqlWithNamedParameters($sql, $bind); break; case 'positional': $this->_updateSqlWithPositionalParameters($sql, $bind); break; } return parent::query($sql, $bind); }
/** * Prepares and executes an SQL statement with bound data. * * @param mixed $sql The SQL statement with placeholders. * May be a string or Zend_Db_Select. * @param mixed $bind An array of data to bind to the placeholders. * @return Zend_Db_Statement_Interface */ public function query($sql, $bind = array()) { return $this->_adapter->query($sql, $bind); }