public function getQueryAsSqlString(SqlInterface $query)
 {
     return $query->getSqlString($this->getPlatform());
 }
 /**
  * Get first row
  *
  * @param Entity $entity Entity
  *
  * @return $this
  */
 public function loadFirstRow(Entity $entity)
 {
     $this->_sqlObject = $this->_sql->select($entity->getTableName());
     $this->_sqlObject->limit(1);
     return $this;
 }
Beispiel #3
0
 /**
  * Return request sql.
  *
  * @param \Zend\Db\Sql\SqlInterface $request
  *
  * @return string
  */
 public function printSql(\Zend\Db\Sql\SqlInterface $request)
 {
     return $request->getSqlString($this->tableGateway->getAdapter()->getPlatform());
 }
 /**
  * @param SqlInterface $query
  * @return void
  */
 public function execute(SqlInterface $query)
 {
     $time = microtime(true);
     $adapterPlatform = $this->adapter->getPlatform();
     if ($query instanceof ZendCreateTable) {
         $this->console->write('    > create table ' . $adapterPlatform->quoteIdentifier($query->getRawState(ZendCreateTable::TABLE)) . '...');
     } elseif ($query instanceof ZendAlterTable) {
         $this->console->write('    > alter table ' . $adapterPlatform->quoteIdentifier($query->getRawState(ZendAlterTable::TABLE)) . '...');
     } elseif ($query instanceof TruncateTable) {
         $this->console->write('    > truncate table ' . $adapterPlatform->quoteIdentifier($query->getRawState(TruncateTable::TABLE)) . '...');
     } elseif ($query instanceof DropTable) {
         $this->console->write('    > drop table ' . $adapterPlatform->quoteIdentifier($query->getRawState(DropTable::TABLE)) . '...');
     } elseif ($query instanceof Insert) {
         $this->console->write('    > insert into ' . $adapterPlatform->quoteIdentifier($query->getRawState('table')) . '...');
     } elseif ($query instanceof Update) {
         $this->console->write('    > update table ' . $adapterPlatform->quoteIdentifier($query->getRawState('table')) . '...');
     } elseif ($query instanceof Delete) {
         $this->console->write('    > delete from ' . $adapterPlatform->quoteIdentifier($query->getRawState('table')) . '...');
     } else {
         throw new InvalidArgumentException(sprintf('Parameter of a method %s must be a executable Sql statement that modifies a table.', __METHOD__));
     }
     $platform = new Platform($this->adapter);
     $platform->setSubject($query);
     if (defined('ANELEGAN_DB_DEBUG') && ANELEGAN_DB_DEBUG) {
         $this->console->write(PHP_EOL . $platform->getSqlString($this->adapter->getPlatform()) . PHP_EOL);
     } else {
         $this->adapter->query($platform->getSqlString(), Adapter::QUERY_MODE_EXECUTE);
     }
     $this->console->writeLine(' done (time: ' . sprintf('%.3f', microtime(true) - $time) . 's)');
 }
Beispiel #5
0
 public function execute(\Zend\Db\Sql\SqlInterface $query)
 {
     /* recycle? */
     if ($query instanceof Sql\Recycle) {
         /* get recycle target data */
         if ($target = $query->getSqlString()) {
             /* init */
             $count = $target->set->count();
             $saved = 0;
             /* table, primary, flag field exists? */
             if ($target->table && $target->primary && $target->flag) {
                 /* any data to save? */
                 if ($count > 0) {
                     /* select existing */
                     $select = $this->select()->from($target->table, array($target->primary, $target->flag))->where($target->where);
                     if ($existing = $this->fetchAll($select)) {
                         /* loop existing and update */
                         foreach ($existing as $row) {
                             /* all new record saved? */
                             if ($saved >= $count) {
                                 /* flag remaining records to unused */
                                 $update = $this->update()->table($target->table)->set(array($target->flag => $query::FLAG_UNUSED))->where(array($target->primary => $row[$target->primary]));
                                 $this->execute($update);
                             } else {
                                 /* overwrite existing record with new data */
                                 $update = $this->update()->table($target->table)->set(array_merge($target->set->item($saved)->pull(array_merge(array($target->primary, $target->flag), $target->exclude)), array($target->flag => $query::FLAG_USED)))->where(array($target->primary => $row[$target->primary]));
                                 if ($this->execute($update)) {
                                     /* assign new id to updated item */
                                     $target->set->item($saved)->push(array($target->primary => $row[$target->primary]));
                                 }
                                 /* increment saved */
                                 $saved++;
                             }
                             //all saved
                         }
                         //each existing
                     }
                     //fetch
                     /* any remaining unsaved records? */
                     if ($saved < $count) {
                         for ($index = $saved; $index < $count; $index++) {
                             /* insert remaining record */
                             $insert = $this->insert()->into($target->table)->values(array_merge($target->set->item($index)->pull(array_merge(array($target->primary, $target->flag), $target->exclude)), array($target->flag => $query::FLAG_USED)));
                             if ($id = $this->execute($insert)) {
                                 /* assign new id to set item */
                                 $target->set->item($index)->push(array($target->primary => $id));
                             }
                         }
                         //each remaining record
                     }
                     //remaining?
                     /* return! */
                     return $saved > 0;
                 } else {
                     /* flag related existing record to unused */
                     $update = $this->update()->table($target->table)->set(array($target->flag => $query::FLAG_UNUSED))->where($target->where);
                     return $this->execute($update);
                 }
                 //any data to save
             }
             //target primary check
         }
         //get data
         /* default return false */
         return false;
         /* store statement */
     } elseif ($query instanceof Sql\Store) {
         /* get store data */
         $target = $query->getSqlString();
         $values = $target->values;
         /* get id if any */
         $id = null;
         if (isset($values[$target->primary])) {
             $id = $values[$target->primary];
         }
         /* have id? update */
         if ($id) {
             /* unset id */
             unset($values[$target->primary]);
             /* generate update statement */
             $update = $this->update()->table($target->into)->set($values)->where(array($target->primary => $id));
             /* execute update! */
             if ($this->execute($update)) {
                 return $id;
             }
             /* no id, insert */
         } else {
             /* unset id if its an autoincrement */
             if ($target->auto == true) {
                 unset($values[$target->primary]);
             }
             /* generate insert */
             $insert = $this->insert()->into($target->into)->values($values);
             /* execute insert */
             return $this->execute($insert);
         }
         /* return false as default */
         return false;
         /* other normal statement */
     } else {
         /* prepare & execute */
         $statement = $this->_sql->prepareStatementForSqlObject($query);
         $result = $statement->execute();
         /* insert, return last generated value (auto increment) */
         if ($result && $query instanceof Sql\Insert) {
             /* oci8? */
             if ($this->_driver == self::DRIVER_OCI8) {
                 /* has ai field? */
                 if (($field = $query->hasAutoincrement()) !== false) {
                     if ($id = $query->{$field}) {
                         return $id;
                     }
                 }
             } else {
                 /* others - mysql */
                 if ($id = $this->_db->getDriver()->getLastGeneratedValue()) {
                     return $id;
                 }
             }
         }
         /* return result */
         return $result;
     }
     //recycle or not
 }