示例#1
0
 /**
  * query() is a convenience function
  *
  * @param string $sql
  * @param string|array|ParameterContainer $parametersOrQueryMode
  * @throws Exception\InvalidArgumentException
  * @return Driver\StatementInterface|ResultSet\ResultSet
  */
 public function query($sql, $parametersOrQueryMode = self::QUERY_MODE_PREPARE)
 {
     if (is_string($parametersOrQueryMode) && in_array($parametersOrQueryMode, array(self::QUERY_MODE_PREPARE, self::QUERY_MODE_EXECUTE))) {
         $mode = $parametersOrQueryMode;
         $parameters = null;
     } elseif (is_array($parametersOrQueryMode) || $parametersOrQueryMode instanceof ParameterContainer) {
         $mode = self::QUERY_MODE_PREPARE;
         $parameters = $parametersOrQueryMode;
     } else {
         throw new Exception\InvalidArgumentException('Parameter 2 to this method must be a flag, an array, or ParameterContainer');
     }
     if ($mode == self::QUERY_MODE_PREPARE) {
         $this->lastPreparedStatement = null;
         $this->lastPreparedStatement = $this->driver->createStatement($sql);
         $this->lastPreparedStatement->prepare();
         if (is_array($parameters) || $parameters instanceof ParameterContainer) {
             $this->lastPreparedStatement->setParameterContainer(is_array($parameters) ? new ParameterContainer($parameters) : $parameters);
             $result = $this->lastPreparedStatement->execute();
         } else {
             return $this->lastPreparedStatement;
         }
     } else {
         $result = $this->driver->getConnection()->execute($sql);
     }
     if ($result instanceof Driver\ResultInterface && $result->isQueryResult()) {
         $resultSet = clone $this->queryResultSetPrototype;
         $resultSet->initialize($result);
         return $resultSet;
     }
     return $result;
 }
示例#2
0
 /**
  * Prepare the delete statement
  *
  * @param  Adapter $adapter
  * @param  StatementInterface $statement
  * @return void
  */
 public function prepareStatement(Adapter $adapter, StatementInterface $statement)
 {
     $platform = $adapter->getPlatform();
     $parameterContainer = $statement->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statement->setParameterContainer($parameterContainer);
     }
     $table = $platform->quoteIdentifier($this->table);
     $sql = sprintf($this->specifications[self::SPECIFICATION_DELETE], $table);
     // process where
     if ($this->where->count() > 0) {
         $whereParts = $this->processExpression($this->where, $platform, $adapter->getDriver(), 'where');
         if (count($whereParts['parameters']) > 0) {
             $parameterContainer->merge($whereParts['parameters']);
         }
         $sql .= ' ' . sprintf($this->specifications[self::SPECIFICATION_WHERE], $whereParts['sql']);
     }
     $statement->setSql($sql);
 }
示例#3
0
文件: Insert.php 项目: necrogami/zf2
    /**
     * Prepare statement
     *
     * @param  Adapter $adapter
     * @param  StatementInterface $statement
     * @return void
     */
    public function prepareStatement(Adapter $adapter, StatementInterface $statement)
    {
        $driver   = $adapter->getDriver();
        $platform = $adapter->getPlatform();
        $parameterContainer = $statement->getParameterContainer();

        if (!$parameterContainer instanceof ParameterContainer) {
            $parameterContainer = new ParameterContainer();
            $statement->setParameterContainer($parameterContainer);
        }

        $table = $platform->quoteIdentifier($this->table);

        $columns = array();
        $values  = array();

        foreach ($this->columns as $cIndex => $column) {
            $columns[$cIndex] = $platform->quoteIdentifier($column);
            $values[$cIndex] = $driver->formatParameterName($column);
            $parameterContainer->offsetSet($column, $this->values[$cIndex]);
        }

        $sql = sprintf(
            $this->specifications[self::SPECIFICATION_INSERT],
            $table,
            implode(', ', $columns),
            implode(', ', $values)
        );

        $statement->setSql($sql);
    }
示例#4
0
 /**
  * Prepare statement
  *
  * @param \Zend\Db\Adapter\Adapter $adapter
  * @param \Zend\Db\Adapter\Driver\StatementInterface $statement
  * @return void
  */
 public function prepareStatement(Adapter $adapter, StatementInterface $statement)
 {
     // ensure statement has a ParameterContainer
     $parameterContainer = $statement->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statement->setParameterContainer($parameterContainer);
     }
     $sqls = array();
     $parameters = array();
     $platform = $adapter->getPlatform();
     foreach ($this->specifications as $name => $specification) {
         $parameters[$name] = $this->{'process' . $name}($platform, $adapter, $parameterContainer, $sqls, $parameters);
         if ($specification && is_array($parameters[$name])) {
             $sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]);
         }
     }
     $sql = implode(' ', $sqls);
     $statement->setSql($sql);
     return;
 }
示例#5
0
文件: Update.php 项目: brikou/zend_db
 /**
  * Prepare statement
  *
  * @param \Zend\Db\Adapter\Adapter $adapter
  * @param \Zend\Db\Adapter\Driver\StatementInterface $statement
  * @return void
  */
 public function prepareStatement(Adapter $adapter, StatementInterface $statement)
 {
     $driver = $adapter->getDriver();
     $platform = $adapter->getPlatform();
     $parameterContainer = $statement->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statement->setParameterContainer($parameterContainer);
     }
     $prepareType = $driver->getPrepareType();
     $table = $platform->quoteIdentifier($this->table);
     //        if ($this->schema != '') {
     //            $table = $platform->quoteIdentifier($this->schema)
     //                . $platform->getIdentifierSeparator()
     //                . $table;
     //        }
     $set = $this->set;
     if (is_array($set)) {
         $setSql = array();
         $values = array();
         foreach ($set as $column => $value) {
             $parameterContainer->offsetSet($column, $value);
             $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column);
         }
         $set = implode(', ', $setSql);
     }
     $sql = sprintf($this->specifications[self::SPECIFICATION_UPDATE], $table, $set);
     // process where
     if ($this->where->count() > 0) {
         $whereParts = $this->processExpression($this->where, $platform, $adapter->getDriver(), 'where');
         if (count($whereParts['parameters']) > 0) {
             $parameterContainer->merge($whereParts['parameters']);
         }
         $sql .= ' ' . sprintf($this->specifications[self::SPECIFICATION_WHERE], $whereParts['sql']);
     }
     $statement->setSql($sql);
 }