/** * @covers Zend\Db\Sql\Update::getSqlString */ public function testGetSqlString() { $this->update->table('foo')->set(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null))->where('x = y'); $this->assertEquals('UPDATE "foo" SET "bar" = \'baz\', "boo" = NOW(), "bam" = NULL WHERE x = y', $this->update->getSqlString()); // with TableIdentifier $this->update = new Update(); $this->update->table(new TableIdentifier('foo', 'sch'))->set(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null))->where('x = y'); $this->assertEquals('UPDATE "sch"."foo" SET "bar" = \'baz\', "boo" = NOW(), "bam" = NULL WHERE x = y', $this->update->getSqlString()); }
/** * @coversNothing */ public function testSpecificationconstantsCouldBeOverridedByExtensionInGetSqlString() { $this->update = new UpdateIgnore(); $this->update->table('foo')->set(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null))->where('x = y'); $this->assertEquals('UPDATE IGNORE "foo" SET "bar" = \'baz\', "boo" = NOW(), "bam" = NULL WHERE x = y', $this->update->getSqlString(new TrustingSql92Platform())); // with TableIdentifier $this->update = new UpdateIgnore(); $this->update->table(new TableIdentifier('foo', 'sch'))->set(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null))->where('x = y'); $this->assertEquals('UPDATE IGNORE "sch"."foo" SET "bar" = \'baz\', "boo" = NOW(), "bam" = NULL WHERE x = y', $this->update->getSqlString(new TrustingSql92Platform())); }
/** * @todo add $columns support * * @param Update $update * @return int * @throws Exception\RuntimeException */ protected function executeUpdate(Update $update) { $updateState = $update->getRawState(); if ($updateState['table'] != $this->table) { throw new Exception\RuntimeException('The table name of the provided Update object must match that of the table'); } // apply preUpdate features $this->featureSet->apply(EventFeatureEventsInterface::EVENT_PRE_UPDATE, [$update]); $unaliasedTable = false; if (is_array($updateState['table'])) { $tableData = array_values($updateState['table']); $unaliasedTable = array_shift($tableData); $update->table($unaliasedTable); } $statement = $this->sql->prepareStatementForSqlObject($update); $result = $statement->execute(); // apply postUpdate features $this->featureSet->apply(EventFeatureEventsInterface::EVENT_POST_UPDATE, [$statement, $result]); // Reset original table information in Update instance, if necessary if ($unaliasedTable) { $update->table($updateState['table']); } return $result->getAffectedRows(); }
/** * @covers Zend\Db\Sql\Update::getSqlString * @todo Implement testGetSqlString(). */ public function testGetSqlString() { $this->update->table('foo')->set(array('bar' => 'baz', 'boo' => new Expression('NOW()')))->where('x = y'); $this->assertEquals('UPDATE "foo" SET "bar" = \'baz\', "boo" = NOW() WHERE x = y', $this->update->getSqlString()); }
/** * Get a sql object to update an existing blog post * * @param int $id * @param PostEntity $post * @return Update */ protected function getPostUpdate($id, PostEntity $post) { $update = new Update(); $update->table(new TableIdentifier('blog_post'))->set(['title' => $post->getTitle(), 'author' => $post->getAuthor(), 'content' => $post->getContent(), 'is_visible' => $post->getIsVisible()])->where(['blog_post_id' => $id]); return $update; }