Skip to content

Commit

Permalink
Simplify SQL tests and examples
Browse files Browse the repository at this point in the history
See #84
  • Loading branch information
rybakit committed Jun 9, 2023
1 parent 210d3e2 commit 8cc5d0a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 54 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@ the [official documentation](https://www.tarantool.io/en/doc/latest/reference/re
*Code*

```php
$result1 = $client->executeUpdate('
CREATE TABLE users ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(255))
');
$client->execute('CREATE TABLE users ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(255))');

$result1 = $client->executeUpdate('CREATE UNIQUE INDEX email ON users ("email")');

$result2 = $client->executeUpdate('
INSERT INTO users VALUES (null, :email1), (null, :email2)
Expand All @@ -584,9 +584,7 @@ $result2 = $client->executeUpdate('
[':email2' => 'bar@example.com']
);

// Note the SEQSCAN keyword in the query. It is mandatory as of Tarantool 2.11.
// If you are using an older version of Tarantool, omit this keyword.
$result3 = $client->executeQuery('SELECT * FROM SEQSCAN users WHERE "email" = ?', 'foo@example.com');
$result3 = $client->executeQuery('SELECT * FROM users WHERE "email" = ?', 'foo@example.com');
$result4 = $client->executeQuery('SELECT * FROM users WHERE "id" IN (?, ?)', 1, 2);

printf("Result 1: %s\n", json_encode([$result1->count(), $result1->getAutoincrementIds()]));
Expand Down Expand Up @@ -638,7 +636,7 @@ for ($i = 1; $i <= 100; ++$i) {
}
$stmt->close();

// Note the SEQSCAN keyword in the query. It is mandatory as of Tarantool 2.11.
// Note the SEQSCAN keyword in the query. It is available as of Tarantool 2.11.
// If you are using an older version of Tarantool, omit this keyword.
$result = $client->executeQuery('SELECT COUNT("id") AS "cnt" FROM SEQSCAN users');

Expand Down
17 changes: 3 additions & 14 deletions examples/protocol/execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
ensure_server_version_at_least('2', $client);

$client->execute('DROP TABLE IF EXISTS users');
$client->execute('CREATE TABLE users ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(255))');

$result1 = $client->executeUpdate('
CREATE TABLE users ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(255))
');
$result1 = $client->executeUpdate('CREATE UNIQUE INDEX email ON users ("email")');

$result2 = $client->executeUpdate('
INSERT INTO users VALUES (null, :email1), (null, :email2)
Expand All @@ -29,17 +28,7 @@
[':email2' => 'bar@example.com']
);

/**
* SEQSCAN keyword is explicitly allowing to use seqscan:
* https://github.com/tarantool/tarantool/commit/77648827326ad268ec0ffbcd620c2371b65ef2b4
* It was introduced in Tarantool 2.11.0-rc1. If compat.sql_seq_scan_default set to "new"
* (default value since 3.0), query returns error when trying to scan without keyword.
*/
$scanQuery = server_version_at_least('2.11.0-rc1', $client)
? 'SELECT * FROM SEQSCAN users WHERE "email" = ?'
: 'SELECT * FROM users WHERE "email" = ?';
$result3 = $client->executeQuery($scanQuery, 'foo@example.com');

$result3 = $client->executeQuery('SELECT * FROM users WHERE "email" = ?', 'foo@example.com');
$result4 = $client->executeQuery('SELECT * FROM users WHERE "id" IN (?, ?)', 1, 2);

printf("Result 1: %s\n", json_encode([$result1->count(), $result1->getAutoincrementIds()]));
Expand Down
24 changes: 4 additions & 20 deletions tests/Integration/Requests/ExecuteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@
*/
final class ExecuteTest extends TestCase
{
private function provideSeqScan() : string
{
/**
* SEQSCAN keyword is explicitly allowing to use seqscan:
* https://github.com/tarantool/tarantool/commit/77648827326ad268ec0ffbcd620c2371b65ef2b4
* It was introduced in Tarantool 2.11.0-rc1. If compat.sql_seq_scan_default set to "new"
* (default value since 3.0), query returns error when trying to scan without keyword.
*/
return $this->tarantoolVersionSatisfies('>=2.11.0-rc1') ? 'SEQSCAN' : '';
}

/**
* @sql DROP TABLE IF EXISTS exec_update
* @sql CREATE TABLE exec_update (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50))
Expand All @@ -58,8 +47,7 @@ public function testExecuteInsertsRows() : void

public function testExecuteFetchesAllRows() : void
{
$seqScan = $this->provideSeqScan();
$response = $this->client->execute("SELECT * FROM $seqScan exec_query");
$response = $this->client->execute('SELECT * FROM exec_query WHERE id > 0');

self::assertSame([[1, 'A'], [2, 'B']], $response->getBodyField(Keys::DATA));
}
Expand Down Expand Up @@ -106,8 +94,7 @@ public function testExecuteUpdateUpdatesRow() : void

public function testExecuteQueryFetchesAllRows() : void
{
$seqScan = $this->provideSeqScan();
$result = $this->client->executeQuery("SELECT * FROM $seqScan exec_query");
$result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id > 0');

self::assertSame([[1, 'A'], [2, 'B']], $result->getData());
self::assertSame(2, $result->count());
Expand Down Expand Up @@ -168,8 +155,7 @@ public function testSqlQueryResultHoldsMetadata() : void
{
$client = ClientBuilder::createFromEnv()->build();

$seqScan = $this->provideSeqScan();
$response = $client->executeQuery("SELECT * FROM $seqScan exec_query");
$response = $client->executeQuery('SELECT * FROM exec_query WHERE id > 0');

self::assertSame([[
Keys::METADATA_FIELD_NAME => 'ID',
Expand All @@ -192,9 +178,7 @@ public function testSqlQueryResultHoldsExtendedMetadata() : void
$client->execute('SET SESSION "sql_full_metadata" = true');

$tableName = $this->resolvePlaceholders('%target_method%');

$seqScan = $this->provideSeqScan();
$response = $client->executeQuery("SELECT id, name AS full_name FROM $seqScan $tableName");
$response = $client->executeQuery("SELECT id, name AS full_name FROM $tableName WHERE id > 0");

self::assertSame([[
Keys::METADATA_FIELD_NAME => 'ID',
Expand Down
14 changes: 1 addition & 13 deletions tests/Integration/Requests/PrepareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@
*/
final class PrepareTest extends TestCase
{
private function provideSeqScan() : string
{
/**
* SEQSCAN keyword is explicitly allowing to use seqscan:
* https://github.com/tarantool/tarantool/commit/77648827326ad268ec0ffbcd620c2371b65ef2b4
* It was introduced in Tarantool 2.11.0-rc1. If compat.sql_seq_scan_default set to "new"
* (default value since 3.0), query returns error when trying to scan without keyword.
*/
return $this->tarantoolVersionSatisfies('>=2.11.0-rc1') ? 'SEQSCAN' : '';
}

public function testPreparePreparesSqlStatement() : void
{
[$preparedCountBefore] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
Expand Down Expand Up @@ -84,8 +73,7 @@ public function testExecuteUpdateUpdatesRows() : void
$insertResult1 = $stmt->executeUpdate(1, 'foo');
$insertResult2 = $stmt->executeUpdate([':name' => 'bar'], [':id' => 2]);

$seqScan = $this->provideSeqScan();
$selectResult = $this->client->executeQuery("SELECT * FROM $seqScan prepare_execute ORDER BY id");
$selectResult = $this->client->executeQuery('SELECT * FROM prepare_execute WHERE id > 0 ORDER BY id');

try {
self::assertSame(1, $insertResult1->count());
Expand Down

0 comments on commit 8cc5d0a

Please sign in to comment.