|
1 |
| -<?php |
2 |
| -/* |
3 |
| - * This file is part of the FODDBALClickHouse package -- Doctrine DBAL library |
4 |
| - * for ClickHouse (a column-oriented DBMS for OLAP <https://clickhouse.yandex/>) |
5 |
| - * |
6 |
| - * (c) FriendsOfDoctrine <https://github.com/FriendsOfDoctrine/>. |
7 |
| - * |
8 |
| - * For the full copyright and license inflormation, please view the LICENSE |
9 |
| - * file that was distributed with this source code. |
10 |
| - */ |
11 |
| - |
12 |
| -namespace FOD\DBALClickHouse\Tests; |
13 |
| - |
14 |
| -use FOD\DBALClickHouse\Connection; |
15 |
| -use PHPUnit\Framework\TestCase; |
16 |
| - |
17 |
| -/** |
18 |
| - * ClickHouse DBAL test class. Testing Insert operations in ClickHouse |
19 |
| - * |
20 |
| - * @author Nikolay Mitrofanov <[email protected]> |
21 |
| - */ |
22 |
| -class InsertTest extends TestCase |
23 |
| -{ |
24 |
| - /** @var Connection */ |
25 |
| - protected $connection; |
26 |
| - |
27 |
| - public function setUp() |
28 |
| - { |
29 |
| - $this->connection = CreateConnectionTest::createConnection(); |
30 |
| - |
31 |
| - $fromSchema = $this->connection->getSchemaManager()->createSchema(); |
32 |
| - $toSchema = clone $fromSchema; |
33 |
| - |
34 |
| - $newTable = $toSchema->createTable('test_insert_table'); |
35 |
| - |
36 |
| - $newTable->addColumn('id', 'integer', ['unsigned' => true]); |
37 |
| - $newTable->addColumn('payload', 'string'); |
38 |
| - $newTable->addOption('engine', 'Memory'); |
39 |
| - $newTable->setPrimaryKey(['id']); |
40 |
| - |
41 |
| - foreach ($fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()) as $sql) { |
42 |
| - $this->connection->exec($sql); |
43 |
| - } |
44 |
| - } |
45 |
| - |
46 |
| - public function tearDown() |
47 |
| - { |
48 |
| - $this->connection->exec('DROP TABLE test_insert_table'); |
49 |
| - } |
50 |
| - |
51 |
| - public function testExecInsert() |
52 |
| - { |
53 |
| - $this->connection->exec("INSERT INTO test_insert_table(id, payload) VALUES (1, 'v1'), (2, 'v2')"); |
54 |
| - $this->assertEquals([['payload' => 'v1'], ['payload' => 'v2']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (1, 2) ORDER BY id")); |
55 |
| - } |
56 |
| - |
57 |
| - public function testFunctionInsert() |
58 |
| - { |
59 |
| - $this->connection->insert('test_insert_table', ['id' => 3, 'payload' => 'v3']); |
60 |
| - $this->connection->insert('test_insert_table', ['id' => 4, 'payload' => 'v4'], ['id' => \PDO::PARAM_INT, 'payload' => \PDO::PARAM_STR]); |
61 |
| - $this->assertEquals([['payload' => 'v3'], ['payload' => 'v4']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (3, 4) ORDER BY id")); |
62 |
| - } |
63 |
| - |
64 |
| - public function testInsertViaQueryBuilder() |
65 |
| - { |
66 |
| - $qb = $this->connection->createQueryBuilder(); |
67 |
| - |
68 |
| - $qb |
69 |
| - ->insert('test_insert_table') |
70 |
| - ->setValue('id', ':id') |
71 |
| - ->setValue('payload', ':payload') |
72 |
| - ->setParameter('id', 5, \PDO::PARAM_INT) |
73 |
| - ->setParameter('payload', 'v5') |
74 |
| - ->execute(); |
75 |
| - |
76 |
| - $qb |
77 |
| - ->setParameter('id', 6) |
78 |
| - ->setParameter('payload', 'v6') |
79 |
| - ->execute(); |
80 |
| - |
81 |
| - $this->assertEquals([['payload' => 'v5'], ['payload' => 'v6']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (5, 6) ORDER BY id")); |
82 |
| - } |
83 |
| - |
84 |
| - public function testStatementInsertWithoutKeyName() |
85 |
| - { |
86 |
| - $statement = $this->connection->prepare('INSERT INTO test_insert_table(id, payload) VALUES (?, ?), (?, ?)'); |
87 |
| - $statement->execute([7, 'v?7', 8, 'v8']); |
88 |
| - $this->assertEquals([['payload' => 'v?7'], ['payload' => 'v8']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (7, 8) ORDER BY id")); |
89 |
| - } |
90 |
| - |
91 |
| - public function testStatementInsertWithKeyName() |
92 |
| - { |
93 |
| - $statement = $this->connection->prepare('INSERT INTO test_insert_table(id, payload) VALUES (:v0, :v1), (:v2, :v3)'); |
94 |
| - $statement->execute(['v0' => 9, 'v1' => 'v?9', 'v2' => 10, 'v3' => 'v10']); |
95 |
| - $this->assertEquals([['payload' => 'v?9'], ['payload' => 'v10']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (9, 10) ORDER BY id")); |
96 |
| - } |
97 |
| -} |
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the FODDBALClickHouse package -- Doctrine DBAL library |
| 4 | + * for ClickHouse (a column-oriented DBMS for OLAP <https://clickhouse.yandex/>) |
| 5 | + * |
| 6 | + * (c) FriendsOfDoctrine <https://github.com/FriendsOfDoctrine/>. |
| 7 | + * |
| 8 | + * For the full copyright and license inflormation, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOD\DBALClickHouse\Tests; |
| 13 | + |
| 14 | +use FOD\DBALClickHouse\Connection; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * ClickHouse DBAL test class. Testing Insert operations in ClickHouse |
| 19 | + * |
| 20 | + * @author Nikolay Mitrofanov <[email protected]> |
| 21 | + */ |
| 22 | +class InsertTest extends TestCase |
| 23 | +{ |
| 24 | + /** @var Connection */ |
| 25 | + protected $connection; |
| 26 | + |
| 27 | + public function setUp() |
| 28 | + { |
| 29 | + $this->connection = CreateConnectionTest::createConnection(); |
| 30 | + |
| 31 | + $fromSchema = $this->connection->getSchemaManager()->createSchema(); |
| 32 | + $toSchema = clone $fromSchema; |
| 33 | + |
| 34 | + $newTable = $toSchema->createTable('test_insert_table'); |
| 35 | + |
| 36 | + $newTable->addColumn('id', 'integer', ['unsigned' => true]); |
| 37 | + $newTable->addColumn('payload', 'string'); |
| 38 | + $newTable->addOption('engine', 'Memory'); |
| 39 | + $newTable->setPrimaryKey(['id']); |
| 40 | + |
| 41 | + foreach ($fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()) as $sql) { |
| 42 | + $this->connection->exec($sql); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function tearDown() |
| 47 | + { |
| 48 | + $this->connection->exec('DROP TABLE test_insert_table'); |
| 49 | + } |
| 50 | + |
| 51 | + public function testExecInsert() |
| 52 | + { |
| 53 | + $this->connection->exec("INSERT INTO test_insert_table(id, payload) VALUES (1, 'v1'), (2, 'v2')"); |
| 54 | + $this->assertEquals([['payload' => 'v1'], ['payload' => 'v2']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (1, 2) ORDER BY id")); |
| 55 | + } |
| 56 | + |
| 57 | + public function testFunctionInsert() |
| 58 | + { |
| 59 | + $this->connection->insert('test_insert_table', ['id' => 3, 'payload' => 'v3']); |
| 60 | + $this->connection->insert('test_insert_table', ['id' => 4, 'payload' => 'v4'], ['id' => \PDO::PARAM_INT, 'payload' => \PDO::PARAM_STR]); |
| 61 | + $this->assertEquals([['payload' => 'v3'], ['payload' => 'v4']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (3, 4) ORDER BY id")); |
| 62 | + } |
| 63 | + |
| 64 | + public function testInsertViaQueryBuilder() |
| 65 | + { |
| 66 | + $qb = $this->connection->createQueryBuilder(); |
| 67 | + |
| 68 | + $qb |
| 69 | + ->insert('test_insert_table') |
| 70 | + ->setValue('id', ':id') |
| 71 | + ->setValue('payload', ':payload') |
| 72 | + ->setParameter('id', 5, \PDO::PARAM_INT) |
| 73 | + ->setParameter('payload', 'v5') |
| 74 | + ->execute(); |
| 75 | + |
| 76 | + $qb |
| 77 | + ->setParameter('id', 6) |
| 78 | + ->setParameter('payload', 'v6') |
| 79 | + ->execute(); |
| 80 | + |
| 81 | + $this->assertEquals([['payload' => 'v5'], ['payload' => 'v6']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (5, 6) ORDER BY id")); |
| 82 | + } |
| 83 | + |
| 84 | + public function testStatementInsertWithoutKeyName() |
| 85 | + { |
| 86 | + $statement = $this->connection->prepare('INSERT INTO test_insert_table(id, payload) VALUES (?, ?), (?, ?)'); |
| 87 | + $statement->execute([7, 'v?7', 8, 'v8']); |
| 88 | + $this->assertEquals([['payload' => 'v?7'], ['payload' => 'v8']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (7, 8) ORDER BY id")); |
| 89 | + } |
| 90 | + |
| 91 | + public function testStatementInsertWithKeyName() |
| 92 | + { |
| 93 | + $statement = $this->connection->prepare('INSERT INTO test_insert_table(id, payload) VALUES (:v0, :v1), (:v2, :v3)'); |
| 94 | + $statement->execute(['v0' => 9, 'v1' => 'v?9', 'v2' => 10, 'v3' => 'v10']); |
| 95 | + $this->assertEquals([['payload' => 'v?9'], ['payload' => 'v10']], $this->connection->fetchAll("SELECT payload from test_insert_table WHERE id IN (9, 10) ORDER BY id")); |
| 96 | + } |
| 97 | +} |
0 commit comments