Example #1
0
 /**
  * Sends event for a logger to write down query text
  * @param mixed $select
  */
 protected function logQuery($select)
 {
     if ($select instanceof Select) {
         $platform = $this->dbAdapter->getPlatform();
         $query = $select->getSqlString($platform);
     } else {
         $query = json_encode($select);
     }
     $this->getEventManager()->trigger(\Application\Utils\Events::LOG_SQL_QUERY, $this, compact('query'));
 }
Example #2
0
 public function __construct(AdapterInterface $adapter)
 {
     $this->adapter = $adapter;
     $platform = $adapter->getPlatform();
     switch (strtolower($platform->getName())) {
         case 'mysql':
             $platform = new Mysql\Mysql();
             $this->decorators = $platform->decorators;
             break;
         case 'sqlserver':
             $platform = new SqlServer\SqlServer();
             $this->decorators = $platform->decorators;
             break;
         case 'oracle':
             $platform = new Oracle\Oracle();
             $this->decorators = $platform->decorators;
             break;
         case 'ibm db2':
         case 'ibm_db2':
         case 'ibmdb2':
             $platform = new IbmDb2\IbmDb2();
             $this->decorators = $platform->decorators;
         default:
     }
 }
Example #3
0
File: Sql.php Project: tillk/vufind
 /**
  * Get sql string using platform or sql object
  *
  * @param SqlInterface      $sqlObject
  * @param PlatformInterface $platform
  *
  * @return string
  */
 public function getSqlStringForSqlObject(SqlInterface $sqlObject, PlatformInterface $platform = null)
 {
     $platform = $platform ?: $this->adapter->getPlatform();
     if ($this->sqlPlatform) {
         $this->sqlPlatform->setSubject($sqlObject);
         return $this->sqlPlatform->getSqlString($platform);
     }
     return $sqlObject->getSqlString($platform);
 }
 /**
  * {@inheritDoc}
  *
  * @return StatementContainerInterface
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $statementContainer->setSql($this->buildSqlString($adapter->getPlatform(), $adapter->getDriver(), $parameterContainer));
     return $statementContainer;
 }
Example #5
0
 public function __construct(AdapterInterface $adapter)
 {
     $this->adapter = $adapter;
     $platform = $adapter->getPlatform();
     switch (strtolower($platform->getName())) {
         case 'mysql':
             $platform = new ZfPlatform\Mysql\Mysql();
             $this->decorators = $platform->decorators;
             $this->setTypeDecorator('Zend\\Db\\Sql\\Select', new Mysql\SelectDecorator());
             break;
         case 'sqlserver':
             $platform = new ZfPlatform\SqlServer\SqlServer();
             $this->decorators = $platform->decorators;
             $this->setTypeDecorator('Zend\\Db\\Sql\\Select', new SqlServer\SelectDecorator());
             break;
         case 'oracle':
             $platform = new ZfPlatform\Oracle\Oracle();
             $this->decorators = $platform->decorators;
             $this->setTypeDecorator('Zend\\Db\\Sql\\Select', new Oracle\SelectDecorator());
             break;
         default:
     }
 }
Example #6
0
 /**
  * Prepare statement
  *
  * @param  AdapterInterface $adapter
  * @param  StatementContainerInterface $statementContainer
  * @return void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     $driver = $adapter->getDriver();
     $platform = $adapter->getPlatform();
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $table = $this->table;
     $schema = null;
     // create quoted table name to use in insert processing
     if ($table instanceof TableIdentifier) {
         list($table, $schema) = $table->getTableAndSchema();
     }
     $table = $platform->quoteIdentifier($table);
     if ($schema) {
         $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
     }
     $columns = array();
     $values = array();
     foreach ($this->columns as $cIndex => $column) {
         $columns[$cIndex] = $platform->quoteIdentifier($column);
         if (isset($this->values[$cIndex]) && $this->values[$cIndex] instanceof Expression) {
             $exprData = $this->processExpression($this->values[$cIndex], $platform, $driver);
             $values[$cIndex] = $exprData->getSql();
             $parameterContainer->merge($exprData->getParameterContainer());
         } else {
             $values[$cIndex] = $driver->formatParameterName($column);
             if (isset($this->values[$cIndex])) {
                 $parameterContainer->offsetSet($column, $this->values[$cIndex]);
             } else {
                 $parameterContainer->offsetSet($column, null);
             }
         }
     }
     $sql = sprintf($this->specifications[self::SPECIFICATION_INSERT], $table, implode(', ', $columns), implode(', ', $values));
     $statementContainer->setSql($sql);
 }
Example #7
0
 /**
  * Prepare statement
  *
  * @param AdapterInterface $adapter
  * @param StatementContainerInterface $statementContainer
  * @return void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     $driver = $adapter->getDriver();
     $platform = $adapter->getPlatform();
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $table = $this->table;
     $schema = null;
     // create quoted table name to use in update processing
     if ($table instanceof TableIdentifier) {
         list($table, $schema) = $table->getTableAndSchema();
     }
     $table = $platform->quoteIdentifier($table);
     if ($schema) {
         $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
     }
     $set = $this->set;
     if (is_array($set)) {
         $setSql = array();
         foreach ($set as $column => $value) {
             if ($value instanceof Expression) {
                 $exprData = $this->processExpression($value, $platform, $driver);
                 $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $exprData->getSql();
                 $parameterContainer->merge($exprData->getParameterContainer());
             } else {
                 $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column);
                 $parameterContainer->offsetSet($column, $value);
             }
         }
         $set = implode(', ', $setSql);
     }
     $sql = sprintf($this->specifications[static::SPECIFICATION_UPDATE], $table, $set);
     // process where
     if ($this->where->count() > 0) {
         $whereParts = $this->processExpression($this->where, $platform, $driver, 'where');
         $parameterContainer->merge($whereParts->getParameterContainer());
         $sql .= ' ' . sprintf($this->specifications[static::SPECIFICATION_WHERE], $whereParts->getSql());
     }
     $statementContainer->setSql($sql);
 }
Example #8
0
 /**
  * @param SqlInterface     $sqlObject
  * @param AdapterInterface $adapter
  *
  * @return string
  *
  * @throws Exception\InvalidArgumentException
  */
 public function buildSqlString(SqlInterface $sqlObject, AdapterInterface $adapter = null)
 {
     return $this->sqlPlatform->setSubject($sqlObject)->getSqlString($adapter ? $adapter->getPlatform() : $this->adapter->getPlatform());
 }
Example #9
0
 /**
  * Prepare the delete statement
  *
  * @param  AdapterInterface $adapter
  * @param  StatementContainerInterface $statementContainer
  * @return void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     $driver = $adapter->getDriver();
     $platform = $adapter->getPlatform();
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $table = $this->table;
     $schema = null;
     // create quoted table name to use in delete processing
     if ($table instanceof TableIdentifier) {
         list($table, $schema) = $table->getTableAndSchema();
     }
     $table = $platform->quoteIdentifier($table);
     if ($schema) {
         $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
     }
     $sql = sprintf($this->specifications[static::SPECIFICATION_DELETE], $table);
     // process where
     if ($this->where->count() > 0) {
         $whereParts = $this->processExpression($this->where, $platform, $driver, 'where');
         $parameterContainer->merge($whereParts->getParameterContainer());
         $sql .= ' ' . sprintf($this->specifications[static::SPECIFICATION_WHERE], $whereParts->getSql());
     }
     $statementContainer->setSql($sql);
 }
Example #10
0
 /**
  * Prepare statement
  *
  * @param AdapterInterface $adapter
  * @param StatementContainerInterface $statementContainer
  * @return void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     // ensure statement has a ParameterContainer
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $sqls = array();
     $parameters = array();
     $platform = $adapter->getPlatform();
     $driver = $adapter->getDriver();
     foreach ($this->specifications as $name => $specification) {
         $parameters[$name] = $this->{'process' . $name}($platform, $driver, $parameterContainer, $sqls, $parameters);
         if ($specification && is_array($parameters[$name])) {
             $sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]);
         }
     }
     $sql = implode(' ', $sqls);
     $statementContainer->setSql($sql);
     return;
 }
Example #11
0
 /**
  * @param null|PlatformInterface|AdapterInterface $adapterOrPlatform
  *
  * @return PlatformInterface
  *
  * @throws Exception\InvalidArgumentException
  */
 protected function resolvePlatform($adapterOrPlatform)
 {
     if (!$adapterOrPlatform) {
         return $this->getDefaultPlatform();
     }
     if ($adapterOrPlatform instanceof AdapterInterface) {
         return $adapterOrPlatform->getPlatform();
     }
     if ($adapterOrPlatform instanceof PlatformInterface) {
         return $adapterOrPlatform;
     }
     throw new Exception\InvalidArgumentException(sprintf('$adapterOrPlatform should be null, %s, or %s', 'Zend\\Db\\Adapter\\AdapterInterface', 'Zend\\Db\\Adapter\\Platform\\PlatformInterface'));
 }
Example #12
0
 /**
  * @param   AdapterInterface            $adapter
  * @param   StatementContainerInterface $statementContainer
  * @return  void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     $driver = $adapter->getDriver();
     $platform = $adapter->getPlatform();
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $function = $platform->quoteIdentifierChain($this->function);
     $resultKey = $platform->quoteIdentifier($this->resultKey);
     $arguments = array();
     $i = 0;
     foreach ($this->arguments as $argument) {
         if ($argument instanceof Expression) {
             $exprData = $this->processExpression($argument, $platform, $driver);
             $arguments[] = $exprData->getSql();
             $parameterContainer->merge($exprData->getParameterContainer());
         } else {
             $parameterName = 'func_arg_' . $i++;
             $arguments[] = $driver->formatParameterName($parameterName);
             $parameterContainer->offsetSet($parameterName, $argument);
         }
     }
     $sql = sprintf($this->specifications[$this->mode], $function, implode(', ', $arguments), $resultKey);
     $statementContainer->setSql($sql);
 }
Example #13
0
 public function __invoke($value = null)
 {
     return 'Platform name ' . $this->dbAdapter->getPlatform()->getName();
 }
Example #14
0
 /**
  * Prepare statement
  *
  * @param AdapterInterface $adapter
  * @param StatementContainerInterface $statementContainer
  * @return void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     // ensure statement has a ParameterContainer
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $sqls = array();
     $sqls[self::SHOW] = sprintf($this->specifications[static::SHOW], $this->show);
     $likePart = $this->processLike($adapter->getPlatform(), $adapter->getDriver(), $parameterContainer);
     if (is_array($likePart)) {
         $sqls[self::LIKE] = $this->createSqlFromSpecificationAndParameters($this->specifications[static::LIKE], $likePart);
     }
     $sql = implode(' ', $sqls);
     $statementContainer->setSql($sql);
     return;
 }
Example #15
0
 /**
  * Prepare statement
  *
  * @param  AdapterInterface $adapter
  * @param  StatementContainerInterface $statementContainer
  * @return void
  */
 public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer)
 {
     $driver = $adapter->getDriver();
     $platform = $adapter->getPlatform();
     $parameterContainer = $statementContainer->getParameterContainer();
     if (!$parameterContainer instanceof ParameterContainer) {
         $parameterContainer = new ParameterContainer();
         $statementContainer->setParameterContainer($parameterContainer);
     }
     $table = $this->table;
     $table = $platform->quoteIdentifier($table);
     $set = $this->set;
     $setSql = array();
     foreach ($set as $column => $value) {
         if ($value instanceof Predicate\Expression) {
             $exprData = $this->processExpression($value, $platform, $driver);
             $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $exprData->getSql();
             $parameterContainer->merge($exprData->getParameterContainer());
         } else {
             $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column);
             $parameterContainer->offsetSet($column, $value);
         }
     }
     $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, $driver, 'where');
         $parameterContainer->merge($whereParts->getParameterContainer());
         $sql .= ' ' . sprintf($this->specifications[self::SPECIFICATION_WHERE], $whereParts->getSql());
     }
     // Process option
     $optionParts = $this->processOption($platform, $driver, $parameterContainer);
     if (is_array($optionParts)) {
         $sql .= ' ' . $this->createSqlFromSpecificationAndParameters($this->specifications[self::SPECIFICATION_OPTION], $optionParts);
     }
     $statementContainer->setSql($sql);
 }
Example #16
0
 private function quoteIdentifier($name)
 {
     return self::$dbAdapter->getPlatform()->quoteIdentifier($name);
 }