Example #1
0
 /**
  * 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);
 }
Example #2
0
File: Insert.php Project: cwcw/cms
 /**
  * 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);
 }
Example #3
0
 /**
  * Constructs a sequence name based on a table name
  *
  * @param string $tableName table name
  * @return string
  * @throws Streamwide_Db_Adapter_Decorator_Exception
  */
 protected function _getSequenceName($tableName)
 {
     $config = $this->_adapter->getConfig();
     if (!is_array($config)) {
         require_once 'Streamwide/Db/Adapter/Decorator/Exception.php';
         throw new Streamwide_Db_Adapter_Decorator_Exception('Retrieved an invalid adapter configuration format');
     }
     if (!isset($config['options']['sequenceGetter'])) {
         return false;
     }
     $sequenceGetter = Streamwide_Db_SequenceGetter::factory($config['options']['sequenceGetter']);
     if (false === ($sequenceName = $sequenceGetter->getSequenceName($tableName))) {
         return false;
     }
     return $sequenceName;
 }
Example #4
0
 /**
  * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  *
  * As a convention, on RDBMS brands that support sequences
  * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  * from the arguments and returns the last id generated by that sequence.
  * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  * returns the last value generated for such a column, and the table name
  * argument is disregarded.
  *
  * @param string $tableName  (optional) Name of table.
  * @param string $primaryKey (optional) Name of primary key column.
  * @return string
  */
 public function lastInsertId($tableName = null, $primaryKey = null)
 {
     if (null === $tableName) {
         return parent::lastInsertId($tableName, $primaryKey);
     }
     $tableName = trim($tableName);
     if (empty($tableName)) {
         require_once 'Streamwide/Db/Adapter/Decorator/Exception.php';
         throw new Streamwide_Db_Adapter_Decorator_Exception(__METHOD__ . ' requires parameter 1 to be a non empty string');
     }
     try {
         if (false === ($sequence = $this->_getSequenceName($tableName))) {
             return parent::lastInsertId($tableName, $primaryKey);
         }
         return parent::lastSequenceId($sequence);
     } catch (Exception $e) {
         return parent::lastInsertId($tableName, $primaryKey);
     }
 }
Example #5
0
 /**
  * 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);
 }