-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Insert to table with multi primary or without autoicrement primary #105
Merged
dg
merged 6 commits into
nette:v2.4
from
ricco24:kelemen/insert-not-autoincr-primary-multi-primary
Jan 26, 2017
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ddd5f8
Insert to table with multi primary or without autoicrement primary re…
e032bba
Simplify getPrimaryKeySequence function
726f171
Switched sequence delimiting in Selection
aa87d7d
Merge branch 'v2.4' into kelemen/insert-not-autoincr-primary-multi-pr…
ricco24 d32922e
Mysql 5.7 tests fix
d8d52f7
Remove slow in_array function from Structure
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tests/Database/Table/Selection.insert().primaryKeys.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Database\Table\Selection: Different setup for primary keys | ||
* @dataProvider? ../databases.ini | ||
*/ | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../connect.inc.php'; // create $connection | ||
|
||
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test4.sql"); | ||
|
||
// Insert into table with simple primary index (autoincrement) | ||
test(function() use ($context) { | ||
$simplePkAutoincrementResult = $context->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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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) | ||
) 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in_array() is relative slow operation, is possible this?