diff --git a/tests/Database/Connection.getInsertId().mysql.phpt b/tests/Database/Connection.getInsertId().mysql.phpt new file mode 100644 index 000000000..48623a3f8 --- /dev/null +++ b/tests/Database/Connection.getInsertId().mysql.phpt @@ -0,0 +1,70 @@ +query('CREATE DATABASE IF NOT EXISTS nette_test'); +$connection->query('USE nette_test'); + +$connection->query(' + CREATE TEMPORARY TABLE noprimarykey ( + col int + ) ENGINE=InnoDB +'); + +$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)'); +Assert::equal('0', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)'); +Assert::equal('0', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE primarykey ( + prim int NOT NULL, + PRIMARY KEY(prim) + ) ENGINE=InnoDB +'); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)'); +Assert::equal('0', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)'); +Assert::equal('0', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE autoprimarykey ( + prim int NOT NULL AUTO_INCREMENT, + col int, + PRIMARY KEY(prim) + ) ENGINE=InnoDB +'); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('2', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE multiautoprimarykey ( + prim1 int NOT NULL AUTO_INCREMENT, + prim2 int NOT NULL, + PRIMARY KEY(prim1, prim2) + ) ENGINE=InnoDB +'); + +$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)'); +Assert::equal('2', $connection->getInsertId()); diff --git a/tests/Database/Connection.getInsertId().postgre.phpt b/tests/Database/Connection.getInsertId().postgre.phpt new file mode 100644 index 000000000..a4de95b24 --- /dev/null +++ b/tests/Database/Connection.getInsertId().postgre.phpt @@ -0,0 +1,55 @@ +query(' + CREATE TEMPORARY TABLE "primarykey" ( + prim int NOT NULL, + PRIMARY KEY(prim) + ) +'); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)'); +Assert::equal(PHP_VERSION_ID < 70011 ? FALSE : '0', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)'); +Assert::equal(PHP_VERSION_ID < 70011 ? FALSE : '0', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE "autoprimarykey" ( + prim serial NOT NULL, + col int, + PRIMARY KEY(prim) + ) +'); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('1', $connection->getInsertId('autoprimarykey_prim_seq')); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('2', $connection->getInsertId('autoprimarykey_prim_seq')); + + +$connection->query(' + CREATE TEMPORARY TABLE "multiautoprimarykey" ( + prim1 serial NOT NULL, + prim2 int NOT NULL, + PRIMARY KEY(prim1, prim2) + ); +'); + +$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)'); +Assert::equal('1', $connection->getInsertId('multiautoprimarykey_prim1_seq')); + +$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)'); +Assert::equal('2', $connection->getInsertId('multiautoprimarykey_prim1_seq')); diff --git a/tests/Database/Connection.getInsertId().sqlite.phpt b/tests/Database/Connection.getInsertId().sqlite.phpt new file mode 100644 index 000000000..3a1b540c0 --- /dev/null +++ b/tests/Database/Connection.getInsertId().sqlite.phpt @@ -0,0 +1,51 @@ +query(' + CREATE TABLE [noprimarykey] ( + [col] INTEGER + ) +'); + +$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (3)'); +Assert::equal('2', $connection->getInsertId()); + + +$connection->query(' + CREATE TABLE [primarykey] ( + [prim] INTEGER PRIMARY KEY NOT NULL + ) +'); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)'); +Assert::equal('5', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)'); +Assert::equal('6', $connection->getInsertId()); + + +$connection->query(' + CREATE TABLE [autoprimarykey] ( + [prim] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + [col] INTEGER + ) +'); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('2', $connection->getInsertId()); diff --git a/tests/Database/Connection.getInsertId().sqlsrv.phpt b/tests/Database/Connection.getInsertId().sqlsrv.phpt new file mode 100644 index 000000000..d1cde96ec --- /dev/null +++ b/tests/Database/Connection.getInsertId().sqlsrv.phpt @@ -0,0 +1,68 @@ +query(' + CREATE TEMPORARY TABLE [noprimarykey] ( + col int + ) +'); + +$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)'); +Assert::equal('2', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE [primarykey] ( + prim int NOT NULL, + PRIMARY KEY(prim) + ) +'); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)'); +Assert::equal('5', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)'); +Assert::equal('6', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE [autoprimarykey] ( + prim int NOT NULL IDENTITY(1,1), + col int, + PRIMARY KEY(prim) + ) +'); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)'); +Assert::equal('2', $connection->getInsertId()); + + +$connection->query(' + CREATE TEMPORARY TABLE [multiautoprimarykey] ( + prim1 int NOT NULL IDENTITY(1,1), + prim2 int NOT NULL, + PRIMARY KEY(prim1, prim2) + ) +'); + +$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)'); +Assert::equal('1', $connection->getInsertId()); + +$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)'); +Assert::equal('2', $connection->getInsertId());