aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-09 11:53:43 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-12-09 11:53:43 +0100
commit5c95939bf388111a21ca94461e37b1b0b0b38159 (patch)
tree0bba0593881c29555fd65a4718bcb4fb3376ed6f
parenteb14c9dc1e2366857cb9c00551daa49c6d3f15b9 (diff)
parentbef6344b277186e37cd02a4af74f270d97435d40 (diff)
downloadnextcloud-server-5c95939bf388111a21ca94461e37b1b0b0b38159.tar.gz
nextcloud-server-5c95939bf388111a21ca94461e37b1b0b0b38159.zip
Merge pull request #21061 from owncloud/fix-oracle-last-insert-id-test
Fix the last insert id test by changing to an autoincremen…
-rw-r--r--lib/private/db/querybuilder/querybuilder.php28
-rw-r--r--tests/lib/db/querybuilder/querybuildertest.php22
2 files changed, 39 insertions, 11 deletions
diff --git a/lib/private/db/querybuilder/querybuilder.php b/lib/private/db/querybuilder/querybuilder.php
index 741da4efc27..a4cbb5abd76 100644
--- a/lib/private/db/querybuilder/querybuilder.php
+++ b/lib/private/db/querybuilder/querybuilder.php
@@ -40,6 +40,9 @@ class QueryBuilder implements IQueryBuilder {
/** @var bool */
private $automaticTablePrefix = true;
+ /** @var string */
+ protected $lastInsertedTable;
+
/**
* Initializes a new QueryBuilder.
*
@@ -445,6 +448,8 @@ class QueryBuilder implements IQueryBuilder {
$this->getTableName($insert)
);
+ $this->lastInsertedTable = $insert;
+
return $this;
}
@@ -1051,10 +1056,10 @@ class QueryBuilder implements IQueryBuilder {
* @throws \BadMethodCallException When being called before an insert query has been run.
*/
public function getLastInsertId() {
- $from = $this->getQueryPart('from');
-
- if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && !empty($from)) {
- return (int) $this->connection->lastInsertId($from['table']);
+ if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) {
+ // lastInsertId() needs the prefix but no quotes
+ $table = $this->prefixTableName($this->lastInsertedTable);
+ return (int) $this->connection->lastInsertId($table);
}
throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.');
@@ -1067,11 +1072,22 @@ class QueryBuilder implements IQueryBuilder {
* @return string
*/
public function getTableName($table) {
+ $table = $this->prefixTableName($table);
+ return $this->helper->quoteColumnName($table);
+ }
+
+ /**
+ * Returns the table name with database prefix as needed by the implementation
+ *
+ * @param string $table
+ * @return string
+ */
+ protected function prefixTableName($table) {
if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) {
- return $this->helper->quoteColumnName($table);
+ return $table;
}
- return $this->helper->quoteColumnName('*PREFIX*' . $table);
+ return '*PREFIX*' . $table;
}
/**
diff --git a/tests/lib/db/querybuilder/querybuildertest.php b/tests/lib/db/querybuilder/querybuildertest.php
index c8e029d9e40..de8f84ac345 100644
--- a/tests/lib/db/querybuilder/querybuildertest.php
+++ b/tests/lib/db/querybuilder/querybuildertest.php
@@ -1124,11 +1124,12 @@ class QueryBuilderTest extends \Test\TestCase {
$this->assertTrue(true);
}
- $qB->insert('appconfig')
+ $qB->insert('properties')
->values([
- 'appid' => $qB->expr()->literal('testFirstResult'),
- 'configkey' => $qB->expr()->literal('testing' . 50),
- 'configvalue' => $qB->expr()->literal(100 - 50),
+ 'userid' => $qB->expr()->literal('testFirstResult'),
+ 'propertypath' => $qB->expr()->literal('testing'),
+ 'propertyname' => $qB->expr()->literal('testing'),
+ 'propertyvalue' => $qB->expr()->literal('testing'),
])
->execute();
@@ -1136,7 +1137,18 @@ class QueryBuilderTest extends \Test\TestCase {
$this->assertNotNull($actual);
$this->assertInternalType('int', $actual);
- $this->assertEquals($this->connection->lastInsertId('*PREFIX*appconfig'), $actual);
+ $this->assertEquals($this->connection->lastInsertId('*PREFIX*properties'), $actual);
+
+ $qB->delete('properties')
+ ->where($qB->expr()->eq('userid', $qB->expr()->literal('testFirstResult')))
+ ->execute();
+
+ try {
+ $qB->getLastInsertId();
+ $this->fail('getLastInsertId() should throw an exception, when being called after delete()');
+ } catch (\BadMethodCallException $e) {
+ $this->assertTrue(true);
+ }
}
public function dataGetTableName() {