From 2ddd5f80337d7f29b8be056f6e850d271cca8545 Mon Sep 17 00:00:00 2001 From: Samuel Kelemen Date: Sun, 4 Oct 2015 11:51:51 +0200 Subject: [PATCH 1/5] Insert to table with multi primary or without autoicrement primary returns inserted record --- src/Database/IStructure.php | 7 ++ src/Database/Structure.php | 41 ++++++- src/Database/Table/Selection.php | 42 ++++--- .../Table/Selection.insert().primaryKeys.phpt | 110 ++++++++++++++++++ tests/Database/Table/bugs/bug10.postgre.phpt | 11 +- .../Database/Table/bugs/bug1342.postgre.phpt | 11 +- tests/Database/files/mysql-nette_test4.sql | 35 ++++++ tests/Database/files/pgsql-nette_test4.sql | 32 +++++ tests/Database/files/sqlite-nette_test4.sql | 27 +++++ tests/Database/files/sqlsrv-nette_test4.sql | 36 ++++++ 10 files changed, 329 insertions(+), 23 deletions(-) create mode 100644 tests/Database/Table/Selection.insert().primaryKeys.phpt create mode 100644 tests/Database/files/mysql-nette_test4.sql create mode 100644 tests/Database/files/pgsql-nette_test4.sql create mode 100644 tests/Database/files/sqlite-nette_test4.sql create mode 100644 tests/Database/files/sqlsrv-nette_test4.sql diff --git a/src/Database/IStructure.php b/src/Database/IStructure.php index 5a31c5186..b9f3e87ea 100644 --- a/src/Database/IStructure.php +++ b/src/Database/IStructure.php @@ -45,6 +45,13 @@ function getColumns($table); */ function getPrimaryKey($table); + /** + * Returns autoincrement primary key name. + * @param string + * @return string|NULL + */ + function getPrimaryAutoincrementKey($table); + /** * Returns table primary key sequence. * @param string diff --git a/src/Database/Structure.php b/src/Database/Structure.php index 9b2f9859b..24f7dd0b3 100644 --- a/src/Database/Structure.php +++ b/src/Database/Structure.php @@ -63,6 +63,33 @@ public function getPrimaryKey($table) return $this->structure['primary'][$table]; } + public function getPrimaryAutoincrementKey($table) + { + $primaryKey = $this->getPrimaryKey($table); + if (!$primaryKey) { + return NULL; + } + + // Search for autoincrement key from multi primary key + if (is_array($primaryKey)) { + foreach ($this->getColumns($table) as $column) { + if (in_array($column['name'], $primaryKey) && $column['autoincrement']) { + return $column['name']; + } + } + + return NULL; + } + + // Search for autoincrement key from simple primary key + foreach ($this->getColumns($table) as $column) { + if ($column['name'] == $primaryKey) { + return $column['autoincrement'] ? $column['name'] : NULL; + } + } + + return NULL; + } public function getPrimaryKeySequence($table) { @@ -74,10 +101,22 @@ public function getPrimaryKeySequence($table) } $primary = $this->getPrimaryKey($table); - if (!$primary || is_array($primary)) { + if (!$primary) { + return NULL; + } + + // Search for sequence from multi primary key + if (is_array($primary)) { + $result = NULL; + foreach ($this->structure['columns'][$table] as $columnMeta) { + if (in_array($columnMeta['name'], $primary) && isset($columnMeta['vendor']['sequence'])) { + return $columnMeta['vendor']['sequence']; + } + } return NULL; } + // Search for sequence from simple primary key foreach ($this->structure['columns'][$table] as $columnMeta) { if ($columnMeta['name'] === $primary) { return isset($columnMeta['vendor']['sequence']) ? $columnMeta['vendor']['sequence'] : NULL; diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index a9f417d67..204ce2cc1 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -746,29 +746,39 @@ public function insert($data) } $primarySequenceName = $this->getPrimarySequence(); - $primaryKey = $this->context->getInsertId( - !empty($primarySequenceName) - ? $this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName) - : $primarySequenceName - ); - if ($primaryKey === FALSE) { - unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]); - return $return->getRowCount(); + $primaryAutoincrementKey = $this->context->getStructure()->getPrimaryAutoincrementKey($this->name); + + $primaryKey = []; + foreach ((array) $this->primary as $key) { + if (isset($data[$key])) { + $primaryKey[$key] = $data[$key]; + } } - if (is_array($this->getPrimary())) { - $primaryKey = []; + // First check sequence + if (!empty($primarySequenceName) && $primaryAutoincrementKey) { + $primaryKey[$primaryAutoincrementKey] = $this->context->getInsertId($primarySequenceName); + + // Autoincrement primary without sequence + } elseif ($primaryAutoincrementKey) { + $primaryKey[$primaryAutoincrementKey] = $this->context->getInsertId($this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName)); - foreach ((array) $this->getPrimary() as $key) { + // Multi column primary without autoincrement + } elseif (is_array($this->primary)) { + foreach ($this->primary as $key) { if (!isset($data[$key])) { return $data; } - - $primaryKey[$key] = $data[$key]; - } - if (count($primaryKey) === 1) { - $primaryKey = reset($primaryKey); } + + // Primary without autoincrement, try get primary from inserting data + } elseif ($this->primary && isset($data[$this->primary])) { + $primaryKey = $data[$this->primary]; + + // If primaryKey cannot be prepared, return inserted rows count + } else { + unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]); + return $return->getRowCount(); } $row = $this->createSelectionInstance() diff --git a/tests/Database/Table/Selection.insert().primaryKeys.phpt b/tests/Database/Table/Selection.insert().primaryKeys.phpt new file mode 100644 index 000000000..c927d8ab1 --- /dev/null +++ b/tests/Database/Table/Selection.insert().primaryKeys.phpt @@ -0,0 +1,110 @@ +table('simple_pk_autoincrement')->insert([ + 'note' => 'Some note here' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $simplePkAutoincrementResult); + Assert::equal(1, $simplePkAutoincrementResult->identifier1); + Assert::equal('Some note here', $simplePkAutoincrementResult->note); + + $simplePkAutoincrementResult2 = $context->table('simple_pk_autoincrement')->insert([ + 'note' => 'Some note here 2' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $simplePkAutoincrementResult2); + Assert::equal(2, $simplePkAutoincrementResult2->identifier1); + Assert::equal('Some note here 2', $simplePkAutoincrementResult2->note); +}); + +// Insert into table with simple primary index (no autoincrement) +test(function() use ($context) { + $simplePkNoAutoincrementResult = $context->table('simple_pk_no_autoincrement')->insert([ + 'identifier1' => 100, + 'note' => 'Some note here' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $simplePkNoAutoincrementResult); + Assert::equal(100, $simplePkNoAutoincrementResult->identifier1); + Assert::equal('Some note here', $simplePkNoAutoincrementResult->note); + + $simplePkNoAutoincrementResult2 = $context->table('simple_pk_no_autoincrement')->insert([ + 'identifier1' => 200, + 'note' => 'Some note here 2' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $simplePkNoAutoincrementResult2); + Assert::equal(200, $simplePkNoAutoincrementResult2->identifier1); + Assert::equal('Some note here 2', $simplePkNoAutoincrementResult2->note); +}); + +// Insert into table with multi column primary index (no autoincrement) +test(function() use ($context) { + $multiPkNoAutoincrementResult = $context->table('multi_pk_no_autoincrement')->insert([ + 'identifier1' => 5, + 'identifier2' => 10, + 'note' => 'Some note here' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $multiPkNoAutoincrementResult); + Assert::equal(5, $multiPkNoAutoincrementResult->identifier1); + Assert::equal(10, $multiPkNoAutoincrementResult->identifier2); + Assert::equal('Some note here', $multiPkNoAutoincrementResult->note); + + $multiPkNoAutoincrementResult2 = $context->table('multi_pk_no_autoincrement')->insert([ + 'identifier1' => 5, + 'identifier2' => 100, + 'note' => 'Some note here 2' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $multiPkNoAutoincrementResult2); + Assert::equal(5, $multiPkNoAutoincrementResult2->identifier1); + Assert::equal(100, $multiPkNoAutoincrementResult2->identifier2); + Assert::equal('Some note here 2', $multiPkNoAutoincrementResult2->note); +}); + +// Insert into table with multi column primary index (autoincrement) +test(function() use ($driverName, $context) { + if (in_array($driverName, ['mysql', 'pgsql'])) { + $multiPkAutoincrementResult = $context->table('multi_pk_autoincrement')->insert([ + 'identifier2' => 999, + 'note' => 'Some note here' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $multiPkAutoincrementResult); + Assert::equal(1, $multiPkAutoincrementResult->identifier1); + Assert::equal(999, $multiPkAutoincrementResult->identifier2); + Assert::equal('Some note here', $multiPkAutoincrementResult->note); + + $multiPkAutoincrementResult2 = $context->table('multi_pk_autoincrement')->insert([ + 'identifier2' => 999, + 'note' => 'Some note here 2' + ]); + + Assert::type(\Nette\Database\Table\ActiveRow::class, $multiPkAutoincrementResult2); + Assert::equal(2, $multiPkAutoincrementResult2->identifier1); + Assert::equal(999, $multiPkAutoincrementResult2->identifier2); + Assert::equal('Some note here 2', $multiPkAutoincrementResult2->note); + } +}); + +// Insert into table without primary key +test(function() use ($context) { + $noPkResult1 = $context->table('no_pk')->insert([ + 'note' => 'Some note here', + ]); + Assert::equal(1, $noPkResult1); +}); diff --git a/tests/Database/Table/bugs/bug10.postgre.phpt b/tests/Database/Table/bugs/bug10.postgre.phpt index 3cd37f69f..6800a351a 100644 --- a/tests/Database/Table/bugs/bug10.postgre.phpt +++ b/tests/Database/Table/bugs/bug10.postgre.phpt @@ -18,8 +18,9 @@ $context->query(' ) '); -$result = $context->table('Bug10')->insert([ - 'D1' => 123, -]); - -Tester\Assert::notEqual(NULL, $result); +// Throw pdo sequence exception: relation "bug10_bug10caseproblem_seq" does not exist +\Tester\Assert::exception(function() use ($context) { + $context->table('Bug10')->insert([ + 'D1' => 123, + ]); +}, '\PDOException', NULL, '42P01'); diff --git a/tests/Database/Table/bugs/bug1342.postgre.phpt b/tests/Database/Table/bugs/bug1342.postgre.phpt index dcf7a5a21..2707f3305 100644 --- a/tests/Database/Table/bugs/bug1342.postgre.phpt +++ b/tests/Database/Table/bugs/bug1342.postgre.phpt @@ -25,4 +25,13 @@ $insertedRows = $context->table('bug1342')->insert([ 'a2' => 2, ]); -Assert::same(1, $insertedRows); +Assert::same($insertedRows->a1, 1); +Assert::same($insertedRows->a2, 2); + +$insertedRows = $context->table('bug1342')->insert([ + 'a1' => 24, + 'a2' => 48, +]); + +Assert::same($insertedRows->a1, 24); +Assert::same($insertedRows->a2, 48); diff --git a/tests/Database/files/mysql-nette_test4.sql b/tests/Database/files/mysql-nette_test4.sql new file mode 100644 index 000000000..efa9b7211 --- /dev/null +++ b/tests/Database/files/mysql-nette_test4.sql @@ -0,0 +1,35 @@ +/*!40102 SET storage_engine = InnoDB */; + +DROP DATABASE IF EXISTS nette_test; +CREATE DATABASE nette_test; +USE nette_test; + +CREATE TABLE simple_pk_autoincrement ( + identifier1 int NOT NULL AUTO_INCREMENT, + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE simple_pk_no_autoincrement ( + identifier1 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE multi_pk_no_autoincrement ( + identifier1 int NOT NULL, + identifier2 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1, identifier2) +); + +CREATE TABLE multi_pk_autoincrement( + identifier1 int NOT NULL AUTO_INCREMENT, + identifier2 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1, identifier2) +); + +CREATE TABLE no_pk ( + note varchar(100) +); diff --git a/tests/Database/files/pgsql-nette_test4.sql b/tests/Database/files/pgsql-nette_test4.sql new file mode 100644 index 000000000..b45fa6850 --- /dev/null +++ b/tests/Database/files/pgsql-nette_test4.sql @@ -0,0 +1,32 @@ +DROP SCHEMA IF EXISTS public CASCADE; +CREATE SCHEMA public; + +CREATE TABLE simple_pk_autoincrement ( + identifier1 serial NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE simple_pk_no_autoincrement ( + identifier1 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE multi_pk_no_autoincrement ( + identifier1 int NOT NULL, + identifier2 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1, identifier2) +); + +CREATE TABLE multi_pk_autoincrement( + identifier1 serial NOT NULL, + identifier2 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1, identifier2) +); + +CREATE TABLE no_pk ( + note varchar(100) +); diff --git a/tests/Database/files/sqlite-nette_test4.sql b/tests/Database/files/sqlite-nette_test4.sql new file mode 100644 index 000000000..e4ffe7889 --- /dev/null +++ b/tests/Database/files/sqlite-nette_test4.sql @@ -0,0 +1,27 @@ +DROP TABLE IF EXISTS simple_pk_autoincrement; +DROP TABLE IF EXISTS simple_pk_no_autoincrement; +DROP TABLE IF EXISTS multi_pk_no_autoincrement; +DROP TABLE IF EXISTS multi_pk_autoincrement; +DROP TABLE IF EXISTS no_pk; + +CREATE TABLE simple_pk_autoincrement ( + identifier1 integer PRIMARY KEY AUTOINCREMENT, + note varchar(100) +); + +CREATE TABLE simple_pk_no_autoincrement ( + identifier1 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE multi_pk_no_autoincrement ( + identifier1 int NOT NULL, + identifier2 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1, identifier2) +); + +CREATE TABLE no_pk ( + note varchar(100) +); diff --git a/tests/Database/files/sqlsrv-nette_test4.sql b/tests/Database/files/sqlsrv-nette_test4.sql new file mode 100644 index 000000000..b9b5a86c7 --- /dev/null +++ b/tests/Database/files/sqlsrv-nette_test4.sql @@ -0,0 +1,36 @@ +IF OBJECT_ID('simple_pk_autoincrement', 'U') IS NOT NULL DROP TABLE simple_pk_autoincrement; +IF OBJECT_ID('simple_pk_no_autoincrement', 'U') IS NOT NULL DROP TABLE simple_pk_no_autoincrement; +IF OBJECT_ID('multi_pk_no_autoincrement', 'U') IS NOT NULL DROP TABLE multi_pk_no_autoincrement; +IF OBJECT_ID('multi_pk_autoincrement', 'U') IS NOT NULL DROP TABLE multi_pk_autoincrement; +IF OBJECT_ID('no_pk', 'U') IS NOT NULL DROP TABLE no_pk; + + +CREATE TABLE simple_pk_autoincrement ( + identifier1 int NOT NULL IDENTITY(1,1), + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE simple_pk_no_autoincrement ( + identifier1 int NOT NULL, + note varchar(100), + PRIMARY KEY (identifier1) +); + +CREATE TABLE multi_pk_no_autoincrement ( + identifier1 int NOT NULL, + identifier2 int NOT NULL, + note varchar(100) +); +ALTER TABLE multi_pk_no_autoincrement ADD CONSTRAINT PK_multi_pk_no_autoincrement PRIMARY KEY CLUSTERED (identifier1, identifier2); + +CREATE TABLE multi_pk_autoincrement( + identifier1 int NOT NULL IDENTITY(1,1), + identifier2 int NOT NULL, + note varchar(100) +); +ALTER TABLE multi_pk_autoincrement ADD CONSTRAINT PK_multi_pk_autoincrement PRIMARY KEY CLUSTERED (identifier1, identifier2); + +CREATE TABLE no_pk ( + note varchar(100) +); From e032bba0191decadfc7a8d9c668af8a4d664b0d6 Mon Sep 17 00:00:00 2001 From: Samuel Kelemen Date: Fri, 9 Oct 2015 20:48:11 +0200 Subject: [PATCH 2/5] Simplify getPrimaryKeySequence function --- src/Database/Structure.php | 17 +++-------------- tests/Database/Structure.phpt | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/Database/Structure.php b/src/Database/Structure.php index 24f7dd0b3..1177dfce5 100644 --- a/src/Database/Structure.php +++ b/src/Database/Structure.php @@ -100,25 +100,14 @@ public function getPrimaryKeySequence($table) return NULL; } - $primary = $this->getPrimaryKey($table); - if (!$primary) { - return NULL; - } - - // Search for sequence from multi primary key - if (is_array($primary)) { - $result = NULL; - foreach ($this->structure['columns'][$table] as $columnMeta) { - if (in_array($columnMeta['name'], $primary) && isset($columnMeta['vendor']['sequence'])) { - return $columnMeta['vendor']['sequence']; - } - } + $autoincrementPrimaryKeyName = $this->getPrimaryAutoincrementKey($table); + if (!$autoincrementPrimaryKeyName) { return NULL; } // Search for sequence from simple primary key foreach ($this->structure['columns'][$table] as $columnMeta) { - if ($columnMeta['name'] === $primary) { + if ($columnMeta['name'] === $autoincrementPrimaryKeyName) { return isset($columnMeta['vendor']['sequence']) ? $columnMeta['vendor']['sequence'] : NULL; } } diff --git a/tests/Database/Structure.phpt b/tests/Database/Structure.phpt index c1298da7b..9cd2849cc 100644 --- a/tests/Database/Structure.phpt +++ b/tests/Database/Structure.phpt @@ -58,24 +58,24 @@ class StructureTestCase extends TestCase ['name' => 'books_view', 'view' => TRUE], ]); $this->driver->shouldReceive('getColumns')->with('authors')->once()->andReturn([ - ['name' => 'id', 'primary' => TRUE, 'vendor' => ['sequence' => '"public"."authors_id_seq"']], - ['name' => 'name', 'primary' => FALSE, 'vendor' => []], + ['name' => 'id', 'primary' => TRUE, 'autoincrement' => TRUE, 'vendor' => ['sequence' => '"public"."authors_id_seq"']], + ['name' => 'name', 'primary' => FALSE, 'autoincrement' => FALSE, 'vendor' => []], ]); $this->driver->shouldReceive('getColumns')->with('Books')->once()->andReturn([ - ['name' => 'id', 'primary' => TRUE, 'vendor' => ['sequence' => '"public"."Books_id_seq"']], - ['name' => 'title', 'primary' => FALSE, 'vendor' => []], + ['name' => 'id', 'primary' => TRUE, 'autoincrement' => TRUE, 'vendor' => ['sequence' => '"public"."Books_id_seq"']], + ['name' => 'title', 'primary' => FALSE, 'autoincrement' => FALSE, 'vendor' => []], ]); $this->driver->shouldReceive('getColumns')->with('tags')->once()->andReturn([ - ['name' => 'id', 'primary' => TRUE, 'vendor' => []], - ['name' => 'name', 'primary' => FALSE, 'vendor' => []], + ['name' => 'id', 'primary' => TRUE, 'autoincrement' => FALSE, 'vendor' => []], + ['name' => 'name', 'primary' => FALSE, 'autoincrement' => FALSE, 'vendor' => []], ]); $this->driver->shouldReceive('getColumns')->with('books_x_tags')->once()->andReturn([ - ['name' => 'book_id', 'primary' => TRUE, 'vendor' => []], - ['name' => 'tag_id', 'primary' => TRUE, 'vendor' => []], + ['name' => 'book_id', 'primary' => TRUE, 'autoincrement' => FALSE, 'vendor' => []], + ['name' => 'tag_id', 'primary' => TRUE, 'autoincrement' => FALSE, 'vendor' => []], ]); $this->driver->shouldReceive('getColumns')->with('books_view')->once()->andReturn([ - ['name' => 'id', 'primary' => FALSE, 'vendor' => []], - ['name' => 'title', 'primary' => FALSE, 'vendor' => []], + ['name' => 'id', 'primary' => FALSE, 'autoincrement' => FALSE, 'vendor' => []], + ['name' => 'title', 'primary' => FALSE, 'autoincrement' => FALSE, 'vendor' => []], ]); $this->connection->shouldReceive('getSupplementalDriver')->times(4)->andReturn($this->driver); $this->driver->shouldReceive('getForeignKeys')->with('authors')->once()->andReturn([]); @@ -108,8 +108,8 @@ class StructureTestCase extends TestCase public function testGetColumns() { $columns = [ - ['name' => 'id', 'primary' => TRUE, 'vendor' => []], - ['name' => 'name', 'primary' => FALSE, 'vendor' => []], + ['name' => 'id', 'primary' => TRUE, 'autoincrement' => FALSE, 'vendor' => []], + ['name' => 'name', 'primary' => FALSE,'autoincrement' => FALSE, 'vendor' => []], ]; Assert::same($columns, $this->structure->getColumns('tags')); From 726f171f85366f8a0512f05a6397f35561bbb641 Mon Sep 17 00:00:00 2001 From: Samuel Kelemen Date: Fri, 9 Oct 2015 20:48:44 +0200 Subject: [PATCH 3/5] Switched sequence delimiting in Selection --- src/Database/Table/Selection.php | 4 ++-- tests/Database/Table/bugs/bug10.postgre.phpt | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index 204ce2cc1..74bac224a 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -757,11 +757,11 @@ public function insert($data) // First check sequence if (!empty($primarySequenceName) && $primaryAutoincrementKey) { - $primaryKey[$primaryAutoincrementKey] = $this->context->getInsertId($primarySequenceName); + $primaryKey[$primaryAutoincrementKey] = $this->context->getInsertId($this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName)); // Autoincrement primary without sequence } elseif ($primaryAutoincrementKey) { - $primaryKey[$primaryAutoincrementKey] = $this->context->getInsertId($this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName)); + $primaryKey[$primaryAutoincrementKey] = $this->context->getInsertId($primarySequenceName); // Multi column primary without autoincrement } elseif (is_array($this->primary)) { diff --git a/tests/Database/Table/bugs/bug10.postgre.phpt b/tests/Database/Table/bugs/bug10.postgre.phpt index 6800a351a..3cd37f69f 100644 --- a/tests/Database/Table/bugs/bug10.postgre.phpt +++ b/tests/Database/Table/bugs/bug10.postgre.phpt @@ -18,9 +18,8 @@ $context->query(' ) '); -// Throw pdo sequence exception: relation "bug10_bug10caseproblem_seq" does not exist -\Tester\Assert::exception(function() use ($context) { - $context->table('Bug10')->insert([ - 'D1' => 123, - ]); -}, '\PDOException', NULL, '42P01'); +$result = $context->table('Bug10')->insert([ + 'D1' => 123, +]); + +Tester\Assert::notEqual(NULL, $result); From d32922e3c8b1c0b0b98167feb542283a6f5b300d Mon Sep 17 00:00:00 2001 From: Samuel Kelemen Date: Thu, 26 Jan 2017 12:37:46 +0100 Subject: [PATCH 4/5] Mysql 5.7 tests fix --- tests/Database/files/mysql-nette_test4.sql | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/Database/files/mysql-nette_test4.sql b/tests/Database/files/mysql-nette_test4.sql index efa9b7211..754ab2f1c 100644 --- a/tests/Database/files/mysql-nette_test4.sql +++ b/tests/Database/files/mysql-nette_test4.sql @@ -1,5 +1,3 @@ -/*!40102 SET storage_engine = InnoDB */; - DROP DATABASE IF EXISTS nette_test; CREATE DATABASE nette_test; USE nette_test; @@ -8,28 +6,28 @@ CREATE TABLE simple_pk_autoincrement ( identifier1 int NOT NULL AUTO_INCREMENT, note varchar(100), PRIMARY KEY (identifier1) -); +) ENGINE=InnoDB; CREATE TABLE simple_pk_no_autoincrement ( identifier1 int NOT NULL, note varchar(100), PRIMARY KEY (identifier1) -); +) ENGINE=InnoDB; CREATE TABLE multi_pk_no_autoincrement ( identifier1 int NOT NULL, identifier2 int NOT NULL, note varchar(100), PRIMARY KEY (identifier1, identifier2) -); +) ENGINE=InnoDB; CREATE TABLE multi_pk_autoincrement( identifier1 int NOT NULL AUTO_INCREMENT, identifier2 int NOT NULL, note varchar(100), PRIMARY KEY (identifier1, identifier2) -); +) ENGINE=InnoDB; CREATE TABLE no_pk ( note varchar(100) -); +) ENGINE=InnoDB; From d8d52f7980c7c04b94a957f23655619b5d372a79 Mon Sep 17 00:00:00 2001 From: Samuel Kelemen Date: Thu, 26 Jan 2017 13:09:34 +0100 Subject: [PATCH 5/5] Remove slow in_array function from Structure --- src/Database/Structure.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Structure.php b/src/Database/Structure.php index 66e7f2ab7..71a2f2df3 100644 --- a/src/Database/Structure.php +++ b/src/Database/Structure.php @@ -74,12 +74,12 @@ public function getPrimaryAutoincrementKey($table) // Search for autoincrement key from multi primary key if (is_array($primaryKey)) { + $keys = array_flip($primaryKey); foreach ($this->getColumns($table) as $column) { - if (in_array($column['name'], $primaryKey) && $column['autoincrement']) { + if (isset($keys[$column['name']]) && $column['autoincrement']) { return $column['name']; } } - return NULL; }