/** * @covers Zend\Db\Sql\Insert::prepareStatement */ public function testPrepareStatement() { $mockDriver = $this->getMock('Zend\\Db\\Adapter\\Driver\\DriverInterface'); $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional')); $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?')); $mockAdapter = $this->getMock('Zend\\Db\\Adapter\\Adapter', null, array($mockDriver)); $mockStatement = $this->getMock('Zend\\Db\\Adapter\\Driver\\StatementInterface'); $pContainer = new \Zend\Db\Adapter\ParameterContainer(array()); $mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer)); $mockStatement->expects($this->at(1))->method('setSql')->with($this->equalTo('INSERT INTO "foo" ("bar", "boo") VALUES (?, NOW())')); $this->insert->into('foo')->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'))); $this->insert->prepareStatement($mockAdapter, $mockStatement); // with TableIdentifier $this->insert = new Insert(); $mockDriver = $this->getMock('Zend\\Db\\Adapter\\Driver\\DriverInterface'); $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional')); $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?')); $mockAdapter = $this->getMock('Zend\\Db\\Adapter\\Adapter', null, array($mockDriver)); $mockStatement = $this->getMock('Zend\\Db\\Adapter\\Driver\\StatementInterface'); $pContainer = new \Zend\Db\Adapter\ParameterContainer(array()); $mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer)); $mockStatement->expects($this->at(1))->method('setSql')->with($this->equalTo('INSERT INTO "sch"."foo" ("bar", "boo") VALUES (?, NOW())')); $this->insert->into(new TableIdentifier('foo', 'sch'))->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'))); $this->insert->prepareStatement($mockAdapter, $mockStatement); }
public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer) { /* init */ $this->hasAutoincrement = false; /* parent prep */ $result = parent::prepareStatement($adapter, $statementContainer); /* oci8 with autoincrement */ if ($statementContainer instanceof \Zend\Db\Adapter\Driver\Oci8\Statement && $this->autoincrement !== null) { /* get sequence */ if ($this->sequence === null) { $this->sequence = 'SEQ_' . $this->table; } /* replace ai field with sequence & move ai field binding to the end with returning */ $count = 0; $sql = preg_replace('/:' . $this->autoincrement . '\\s*/', $this->sequence . '.NEXTVAL', $statementContainer->getSql(), 1, $count) . ' RETURNING "' . $this->autoincrement . '" INTO :' . $this->autoincrement; /* anything replaced? */ if ($count > 0) { /* prep statement to prep resource */ $statementContainer->setSql($sql); $statementContainer->prepare(); /* unset ai field */ $statementContainer->getParameterContainer()->offsetUnset($this->autoincrement); /* get ai field position on values */ $position = array_search($this->autoincrement, $this->columns); $this->values[$position] = 0; $this->hasAutoincrement = true; oci_bind_by_name($statementContainer->getResource(), $this->autoincrement, $this->values[$position], -1, SQLT_INT); } } //oci8 AI return $result; }
/** * Prepare statement * * @param AdapterInterface $adapter * @param StatementContainerInterface $statementContainer * @return void */ public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer) { parent::prepareStatement($adapter, $statementContainer); $sql = $statementContainer->getSql(); $statementContainer->setSql($sql . "ON DUPLICATE KEY UPDATE " . implode(",", array_map(array($this, "mapValue"), $this->columns))); }