-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSQLiteDriver.php
102 lines (89 loc) · 2.28 KB
/
SQLiteDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/
declare(strict_types=1);
namespace Drift\DBAL\Driver;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory;
use Clue\React\SQLite\Result as SQLiteResult;
use Drift\DBAL\Credentials;
use Drift\DBAL\Exception\DBALException;
use Drift\DBAL\Exception\TableNotFoundException;
use Drift\DBAL\Result;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use RuntimeException;
/**
* Class SQLiteDriver.
*/
class SQLiteDriver implements Driver
{
/**
* @var Factory
*/
private $factory;
/**
* @var DatabaseInterface
*/
private $database;
/**
* SQLiteDriver constructor.
*
* @param LoopInterface $loop
*/
public function __construct(LoopInterface $loop)
{
$this->factory = new Factory($loop);
}
/**
* {@inheritdoc}
*/
public function connect(Credentials $credentials, array $options = [])
{
$this->database = $this
->factory
->openLazy($credentials->getDbName());
}
/**
* {@inheritdoc}
*/
public function query(
string $sql,
array $parameters
): PromiseInterface {
return $this
->database
->query($sql, $parameters)
->then(function (SQLiteResult $sqliteResult) {
return new Result($sqliteResult->rows);
})
->otherwise(function (RuntimeException $exception) {
$this->parseException($exception);
});
}
/**
* Parse exception.
*
* @param RuntimeException $exception
*
* @throws DBALException
*/
private function parseException(RuntimeException $exception)
{
$message = $exception->getMessage();
$match = null;
if (preg_match('~^no such table:\s*(.*?)$~', $message, $match)) {
$tableName = $match[1];
throw TableNotFoundException::createByTableName($tableName);
}
throw DBALException::createGeneric($message);
}
}