/** * 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); }
/** * Generate a new value from the specified sequence in the database, and return it. * This is supported only on RDBMS brands that support sequences * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null. * * @param string $sequenceName sequence name * @return string */ public function nextSequenceId($sequenceName) { return $this->_adapter->nextSequenceId($sequenceName); }