/** * @covers Zend\Db\Sql\Insert::getSqlString * @todo Implement testGetSqlString(). */ public function testGetSqlString() { $this->insert->into('foo')->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'))); $this->assertEquals('INSERT INTO "foo" ("bar", "boo") VALUES (\'baz\', NOW())', $this->insert->getSqlString()); }
/** * @group ZF2-536 */ public function testValuesMerge() { $this->insert->into('foo')->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null)); $this->insert->into('foo')->values(array('qux' => 100), Insert::VALUES_MERGE); $this->assertEquals('INSERT INTO "foo" ("bar", "boo", "bam", "qux") VALUES (\'baz\', NOW(), NULL, \'100\')', $this->insert->getSqlString(new TrustingSql92Platform())); }
/** * @throws Exception\InvalidArgumentException on tracking unit already having an ID * @throws Exception\RuntimeException on insert fail * * @inheritdoc */ public function insert(TrackingUnit $trackingUnit) { if ($trackingUnit->hasId() === true) { $msg = 'Cannot insert tracking unit whth an existing ID'; throw new Exception\InvalidArgumentException($msg); } $dbAdapter = $this->getDbAdapter(); $hydrator = $this->getTrackingUnitHydrator(); $data = $hydrator->extract($trackingUnit); $insert = new Insert(Utils::getTrackingTableName()); $insert->values($data); $sql = $insert->getSqlString($dbAdapter->getPlatform()); $dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE); $lastVal = $dbAdapter->driver->getLastGeneratedValue(); if ((bool) $lastVal === false) { $msg = 'Insert did not succeed'; throw new Exception\RuntimeException($msg); } $trackingUnit->setId($lastVal); return $trackingUnit; }
/** * @param string $expectedSql */ protected function assertTableGatewayLastSqlInsert($expectedSql) { $actualSql = $this->insert->getSqlString($this->mysqlPlatform); $this->assertSqlEquals($expectedSql, $actualSql); }
/** * Get SQL string for this statement * * @param null|PlatformInterface $adapterPlatform Defaults to Sql92 if none provided * @return string */ public function getSqlString(PlatformInterface $adapterPlatform = null) { return parent::getSqlString($adapterPlatform) . " ON DUPLICATE KEY UPDATE " . implode(",", array_map(array($this, "mapValue"), $this->columns)); }