Skip to content

Commit e2423f3

Browse files
committed
Added some table related methods
- createTable - dropTable - truncateTable Added circleci badge
1 parent aed7233 commit e2423f3

7 files changed

+206
-136
lines changed

README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# DBAL for ReactPHP
22

3-
This is a DBAL on top of ReactPHP SQL libraries and Doctrine QueryBuilder
4-
implementation.
3+
[![CircleCI](https://circleci.com/gh/driftphp/reactphp-dbal.svg?style=svg)](https://circleci.com/gh/driftphp/reactphp-dbal)
4+
5+
This is a DBAL on top of ReactPHP SQL clients and Doctrine model implementation.
6+
You will be able to use
7+
8+
- Doctrine QueryBuilder model
9+
- Doctrine Schema model
10+
- Easy-to-use shortcuts for common operations
11+
- and much more support is being added right now
512

613
> Attention. Only for proof of concept ATM. Do not use this library on
714
> production until the first stable version is tagged.
@@ -183,6 +190,54 @@ $connection
183190
});
184191
```
185192

193+
### Create table
194+
195+
You can easily create a new table with basic information. Needs the table name
196+
and an array of fields and types. Strings are considered with length `255`.
197+
First field position in the array will be considered as primary key. Returns a
198+
Promise with, eventually, the connection.
199+
200+
```php
201+
$connection->createTable('test', [
202+
'id' => 'string',
203+
'name' => 'string',
204+
]);
205+
```
206+
207+
This is a basic table creation method. To create more complex tables, you can
208+
use Doctrine's Schema model. You can execute all Schema SQLs generated by using
209+
this method inside Connection named `executeSchema`. You'll find more
210+
information about this Schema model in
211+
[Doctrine documentation](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/schema-representation.html)
212+
213+
```php
214+
$schema = new Schema();
215+
$table = $schema->createTable('test');
216+
$table->addColumn('id', 'string');
217+
// ...
218+
219+
$connection->executeSchema($schema);
220+
```
221+
222+
### Drop table
223+
224+
You can easily drop an existing table. Needs just the table name, and returns,
225+
eventually, the connection.
226+
227+
```php
228+
$connection->dropTable('test');
229+
```
230+
231+
### Truncate table
232+
233+
You can easily truncate an existing table. Needs just the table name, and returns,
234+
eventually, the connection.
235+
236+
```php
237+
$connection->truncateTable('test');
238+
```
239+
186240
## Tests
187241

188-
You can run tests by running `docker-compose up` and by doing `phpunit`.
242+
You can run tests by running `docker-compose up` and by doing
243+
`php vendor/bin/phpunit`.

src/Connection.php

+118
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
use Doctrine\DBAL\DBALException;
1919
use Doctrine\DBAL\Exception\InvalidArgumentException;
20+
use Doctrine\DBAL\Exception\TableExistsException;
21+
use Doctrine\DBAL\Exception\TableNotFoundException;
2022
use Doctrine\DBAL\Platforms\AbstractPlatform;
2123
use Doctrine\DBAL\Query\QueryBuilder;
24+
use Doctrine\DBAL\Schema\Schema;
2225
use Drift\DBAL\Driver\Driver;
2326
use Drift\DBAL\Mock\MockedDBALConnection;
2427
use Drift\DBAL\Mock\MockedDriver;
2528
use React\Promise\PromiseInterface;
29+
use function React\Promise\map;
2630

2731
/**
2832
* Class Connection.
@@ -154,6 +158,40 @@ public function queryBySQL(string $sql, array $parameters = []): PromiseInterfac
154158
->query($sql, $parameters);
155159
}
156160

161+
/**
162+
* Execute, sequentially, an array of sqls
163+
*
164+
* @param string[] $sqls
165+
*
166+
* @return PromiseInterface<Connection>
167+
*/
168+
public function executeSQLs(array $sqls): PromiseInterface
169+
{
170+
return
171+
map($sqls, function(string $sql) {
172+
return $this->queryBySQL($sql);
173+
})
174+
->then(function() {
175+
return $this;
176+
});
177+
}
178+
179+
/**
180+
* Execute an schema
181+
*
182+
* @param Schema $schema
183+
*
184+
* @return PromiseInterface<Connection>
185+
*/
186+
public function executeSchema(Schema $schema): PromiseInterface
187+
{
188+
return $this
189+
->executeSQLs($schema->toSql($this->platform))
190+
->then(function() {
191+
return $this;
192+
});
193+
}
194+
157195
/**
158196
* Shortcuts.
159197
*/
@@ -309,6 +347,86 @@ public function upsert(
309347
});
310348
}
311349

350+
/**
351+
* Table related shortcuts
352+
*/
353+
354+
/**
355+
* Easy shortcut for creating tables. Fields is just a simple key value,
356+
* being the key the name of the field, and the value the type. By default,
357+
* Varchar types have length 255.
358+
*
359+
* First field is considered as primary key.
360+
*
361+
* @param string $name
362+
* @param array $fields
363+
*
364+
* @return PromiseInterface<Connection>
365+
*
366+
* @throws InvalidArgumentException
367+
* @throws TableExistsException
368+
*/
369+
public function createTable(
370+
string $name,
371+
array $fields
372+
) : PromiseInterface
373+
{
374+
if (empty($fields)) {
375+
throw InvalidArgumentException::fromEmptyFieldsArray();
376+
}
377+
378+
$schema = new Schema();
379+
$table = $schema->createTable($name);
380+
foreach ($fields as $field => $type) {
381+
$extra = [];
382+
if ($type = 'string') {
383+
$extra = ['length' => 255];
384+
}
385+
386+
$table->addColumn($field, $type, $extra);
387+
}
388+
389+
$table->setPrimaryKey([array_key_first($fields)]);
390+
391+
return $this->executeSchema($schema);
392+
}
393+
394+
/**
395+
* @param string $name
396+
*
397+
* @return PromiseInterface<Connection>
398+
*
399+
* @throws TableNotFoundException
400+
*/
401+
public function dropTable(string $name) : PromiseInterface
402+
{
403+
return $this
404+
->queryBySQL("DROP TABLE $name")
405+
->then(function() {
406+
return $this;
407+
});
408+
}
409+
410+
/**
411+
* @param string $name
412+
*
413+
* @return PromiseInterface<Connection>
414+
*
415+
* @throws TableNotFoundException
416+
*/
417+
public function truncateTable(string $name) : PromiseInterface
418+
{
419+
$truncateTableQuery = $this
420+
->platform
421+
->getTruncateTableSQL($name);
422+
423+
return $this
424+
->queryBySQL($truncateTableQuery)
425+
->then(function() {
426+
return $this;
427+
});
428+
}
429+
312430
/**
313431
* Get result by where clause.
314432
*

src/Driver/Driver.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Drift\DBAL\Driver;
1717

1818
use Drift\DBAL\Credentials;
19+
use Drift\DBAL\Result;
1920
use React\Promise\PromiseInterface;
2021

2122
/**
@@ -27,9 +28,6 @@ interface Driver
2728
* Attempts to create a connection with the database.
2829
*
2930
* @param Credentials $credentials
30-
* @param array $options
31-
*
32-
* @return PromiseInterface
3331
*/
3432
public function connect(Credentials $credentials);
3533

@@ -39,7 +37,7 @@ public function connect(Credentials $credentials);
3937
* @param string $sql
4038
* @param array $parameters
4139
*
42-
* @return PromiseInterface
40+
* @return PromiseInterface<Result>
4341
*/
4442
public function query(
4543
string $sql,

tests/ConnectionTest.php

+28-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Drift\DBAL\Tests;
1717

18+
use Doctrine\DBAL\Exception\TableExistsException;
1819
use Doctrine\DBAL\Exception\TableNotFoundException;
1920
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
2021
use Drift\DBAL\Connection;
@@ -46,26 +47,45 @@ abstract class ConnectionTest extends TestCase
4647
abstract protected function getConnection(LoopInterface $loop): Connection;
4748

4849
/**
49-
* Create database and table.
50-
*
5150
* @param Connection $connection
5251
*
5352
* @return PromiseInterface
5453
*/
55-
abstract protected function createInfrastructure(Connection $connection): PromiseInterface;
54+
protected function createInfrastructure(Connection $connection): PromiseInterface
55+
{
56+
return $connection
57+
->createTable('test', [
58+
'id' => 'string',
59+
'field1' => 'string',
60+
'field2' => 'string'
61+
])
62+
->otherwise(function(TableExistsException $_) use ($connection) {
63+
// Silent pass
64+
65+
return $connection;
66+
})
67+
->then(function (Connection $connection) {
68+
return $connection->truncateTable('test');
69+
});
70+
}
5671

5772
/**
58-
* Drop infrastructure.
59-
*
6073
* @param Connection $connection
6174
*
6275
* @return PromiseInterface
6376
*/
64-
abstract protected function dropInfrastructure(Connection $connection): PromiseInterface;
77+
protected function dropInfrastructure(Connection $connection): PromiseInterface
78+
{
79+
return $connection
80+
->dropTable('test')
81+
->otherwise(function (TableNotFoundException $_) use ($connection) {
82+
// Silent pass
83+
84+
return $connection;
85+
});
86+
}
6587

6688
/**
67-
* Create database and table.
68-
*
6989
* @param Connection $connection
7090
*
7191
* @return PromiseInterface

tests/MysqlConnectionTest.php

-40
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,4 @@ protected function getConnection(LoopInterface $loop): Connection
4545
'test'
4646
), $mysqlPlatform);
4747
}
48-
49-
/**
50-
* Create database and table.
51-
*
52-
* @param Connection $connection
53-
*
54-
* @return PromiseInterface
55-
*/
56-
protected function createInfrastructure(Connection $connection): PromiseInterface
57-
{
58-
return $connection
59-
->queryBySQL('CREATE TABLE IF NOT EXISTS test (id VARCHAR(255) PRIMARY KEY, field1 VARCHAR(255), field2 VARCHAR (255))')
60-
->then(function () use ($connection) {
61-
return $connection
62-
->queryBySQL('TRUNCATE TABLE test')
63-
->then(function () use ($connection) {
64-
return $connection;
65-
});
66-
});
67-
}
68-
69-
/**
70-
* Drop infrastructure.
71-
*
72-
* @param Connection $connection
73-
*
74-
* @return PromiseInterface
75-
*/
76-
protected function dropInfrastructure(Connection $connection): PromiseInterface
77-
{
78-
return $connection
79-
->queryBySQL('DROP TABLE test')
80-
->then(function () use ($connection) {
81-
return $connection;
82-
}, function (TableNotFoundException $exception) use ($connection) {
83-
// Silent pass
84-
85-
return $connection;
86-
});
87-
}
8848
}

0 commit comments

Comments
 (0)