/** * Binds a parameter to the specified variable name. * * @param mixed $parameter Name the parameter, either integer or string. * @param mixed $variable Reference to PHP variable containing the value. * @param mixed $type OPTIONAL Datatype of SQL parameter. * @param mixed $length OPTIONAL Length of SQL parameter. * @param mixed $options OPTIONAL Other options. * @return bool */ public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null) { if (!is_int($parameter) && !is_string($parameter)) { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception('Invalid bind-variable position'); } $position = null; if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) { if ($intval >= 1 || $intval <= count($this->_sqlParam)) { $position = $intval; } } else { if ($this->_adapter->supportsParameters('named')) { if ($parameter[0] != ':') { $parameter = ':' . $parameter; } if (in_array($parameter, $this->_sqlParam) !== false) { $position = $parameter; } } } if ($position === null) { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception("Invalid bind-variable position '{$parameter}'"); } // Finally we are assured that $position is valid $this->_bindParam[$position] =& $variable; return $this->_bindParam($position, $variable, $type, $length, $options); }
/** * Check sanity of bind parameters. Throw exceptions if params are * not valid. * * @param mixed $parameter Name the parameter, either integer or string. * @param mixed $variable Reference to PHP variable containing the value. * @return integer * @throws Zend_Db_Statement_Exception */ protected function _normalizeBindParam($parameter, &$variable) { $position = null; if ((int) $parameter > 0) { if ($this->_adapter->supportsParameters('positional') === false) { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception("Invalid bind-variable position '{$parameter}'"); } if ($parameter > 0 && $parameter <= count($this->_sqlParam)) { // bind by position, 1-based $position = $parameter - 1; $this->_bindParam[$position] =& $variable; } else { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception("Invalid bind-variable position '{$parameter}'"); } } else { if (is_string($parameter)) { if ($this->_adapter->supportsParameters('named') === false) { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception("Invalid bind-variable position '{$parameter}'"); } // bind by name. make sure it has a colon on it. if ($parameter[0] != ':') { $parameter = ":{$parameter}"; } // look up its position in the params. $position = array_search($parameter, $this->_sqlParam); if (is_integer($position)) { $this->_bindParam[$position] =& $variable; } else { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception("Invalid bind-variable position '{$parameter}'"); } } else { /** * @see Zend_Db_Statement_Exception */ require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception('Invalid bind-variable position'); } } return $position; }
public function insertOrUpdate($obj) { if ($obj instanceof My_Model_Domain) { if ($obj->getClientIdUnsetFromData()) { $obj->unsetField($obj->getClientIdKey()); } $data = $obj->getData(); } elseif (is_array($obj)) { $data = $obj; } else { throw new Exception("Unsupported datatype used in insert", -1001); } // extract and quote col names from the array keys $cols = array(); $vals = array(); $i = 0; foreach ($data as $col => $val) { $cols[] = $this->_connection->quoteIdentifier($col, true); if ($val instanceof Zend_Db_Expr) { $vals[] = $val->__toString(); unset($data[$col]); } else { if ($this->_connection->supportsParameters('positional')) { $vals[] = '?'; } else { if ($this->_connection->supportsParameters('named')) { unset($data[$col]); $data[':col' . $i] = $val; $vals[] = ':col' . $i; $i++; } else { /** @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception(get_class($this->_connection) . " doesn't support positional or named binding"); } } } } // build the statement $sql = "INSERT INTO " . $this->_connection->quoteIdentifier($this->_tablename, true) . ' (' . implode(', ', $cols) . ') ' . 'VALUES (' . implode(', ', $vals) . ')'; $duplicate = " ON DUPLICATE KEY UPDATE "; foreach ($cols as $index => $col) { $duplicate .= $col . " = " . $vals[$index] . ","; } $duplicate = rtrim($duplicate, ","); $sql .= $duplicate; // execute the statement and return the number of affected rows if ($this->_connection->supportsParameters('positional')) { $data = array_values($data); } //because we have two $data = array_merge($data, $data); $stmt = $this->_connection->query($sql, $data); $result = $stmt->rowCount(); return $result; }
/** * Check if the adapter supports real SQL parameters. * * @param string $type 'positional' or 'named' * @return bool */ public function supportsParameters($type) { return $this->_adapter->supportsParameters($type); }