Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.21% |
25 / 29 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
SQLiteConnection | |
86.21% |
25 / 29 |
|
20.00% |
1 / 5 |
11.32 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
connect | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
6.01 | |||
beginTransaction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rollBack | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
commit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\DataStorage\Database\Connection |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\DataStorage\Database\Connection; |
16 | |
17 | use phpOMS\DataStorage\Database\DatabaseStatus; |
18 | use phpOMS\DataStorage\Database\DatabaseType; |
19 | use phpOMS\DataStorage\Database\Query\Grammar\SQLiteGrammar; |
20 | use phpOMS\DataStorage\Database\Schema\Grammar\SQLiteGrammar as SQLiteSchemaGrammar; |
21 | |
22 | /** |
23 | * Database handler. |
24 | * |
25 | * Handles the database connection. |
26 | * Implementing wrapper functions for multiple databases is planned (far away). |
27 | * |
28 | * @package phpOMS\DataStorage\Database\Connection |
29 | * @license OMS License 2.0 |
30 | * @link https://jingga.app |
31 | * @since 1.0.0 |
32 | */ |
33 | final class SQLiteConnection extends ConnectionAbstract |
34 | { |
35 | /** |
36 | * Object constructor. |
37 | * |
38 | * Creates the database object and overwrites all default values. |
39 | * |
40 | * @param array{db:string, database:string} $dbdata the basic database information for establishing a connection |
41 | * |
42 | * @since 1.0.0 |
43 | */ |
44 | public function __construct(array $dbdata) |
45 | { |
46 | $this->type = DatabaseType::SQLITE; |
47 | $this->grammar = new SQLiteGrammar(); |
48 | $this->schemaGrammar = new SQLiteSchemaGrammar(); |
49 | |
50 | if (isset($dbdata['datetimeformat'])) { |
51 | $this->grammar->setDateTimeFormat($dbdata['datetimeformat']); |
52 | $this->schemaGrammar->setDateTimeFormat($dbdata['datetimeformat']); |
53 | } |
54 | |
55 | $this->dbdata = $dbdata; |
56 | } |
57 | |
58 | /** |
59 | * Connect to database |
60 | * |
61 | * @param null|array{db:string, database:string} $dbdata the basic database information for establishing a connection |
62 | * |
63 | * @return void |
64 | * |
65 | * @throws \PDOException |
66 | * |
67 | * @since 1.0.0 |
68 | */ |
69 | public function connect(array $dbdata = null) : void |
70 | { |
71 | if ($this->status === DatabaseStatus::OK) { |
72 | return; |
73 | } |
74 | |
75 | $this->dbdata = $dbdata ?? $this->dbdata; |
76 | |
77 | if (!isset($this->dbdata['db'], $this->dbdata['database']) |
78 | || !DatabaseType::isValidValue($this->dbdata['db']) |
79 | ) { |
80 | $this->status = DatabaseStatus::FAILURE; |
81 | $this->dbdata['password'] = '****'; |
82 | |
83 | return; |
84 | } |
85 | |
86 | $this->close(); |
87 | |
88 | try { |
89 | if (!\is_file($this->dbdata['database'])) { |
90 | throw new \PDOException(); |
91 | } |
92 | |
93 | $this->con = new \PDO($this->dbdata['db'] . ':' . $this->dbdata['database']); |
94 | $this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
95 | $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
96 | |
97 | $this->status = DatabaseStatus::OK; |
98 | } catch (\PDOException $_) { |
99 | $this->con = new NullPDO(); |
100 | $this->status = DatabaseStatus::MISSING_DATABASE; |
101 | } finally { |
102 | $this->dbdata['password'] = '****'; |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * {@inheritdoc} |
108 | */ |
109 | public function beginTransaction() : void |
110 | { |
111 | $this->con->beginTransaction(); |
112 | } |
113 | |
114 | /** |
115 | * {@inheritdoc} |
116 | */ |
117 | public function rollBack() : void |
118 | { |
119 | $this->con->rollBack(); |
120 | } |
121 | |
122 | /** |
123 | * {@inheritdoc} |
124 | */ |
125 | public function commit() : void |
126 | { |
127 | $this->con->commit(); |
128 | } |
129 | } |