Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| ConnectionAbstract | |
100.00% |
12 / 12 |
|
100.00% |
11 / 11 |
12 | |
100.00% |
1 / 1 |
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDatabase | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHost | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getGrammar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSchemaGrammar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| connect | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| __destruct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| close | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isInitialized | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| beginTransaction | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| rollBack | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| commit | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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\Grammar; |
| 20 | use phpOMS\DataStorage\Database\Schema\Grammar\Grammar as SchemaGrammar; |
| 21 | |
| 22 | /** |
| 23 | * Database handler. |
| 24 | * |
| 25 | * Handles the database connection. |
| 26 | * Implementing wrapper functions for multiple databases is planned (far away). |
| 27 | * |
| 28 | * @property \PDO $con |
| 29 | * |
| 30 | * @package phpOMS\DataStorage\Database\Connection |
| 31 | * @license OMS License 2.0 |
| 32 | * @link https://jingga.app |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | abstract class ConnectionAbstract implements ConnectionInterface |
| 36 | { |
| 37 | /** |
| 38 | * Connection object. |
| 39 | * |
| 40 | * This can be used externally to define queries and execute them. |
| 41 | * |
| 42 | * @var \PDO |
| 43 | * @since 1.0.0 |
| 44 | */ |
| 45 | protected \PDO $con; |
| 46 | |
| 47 | /** |
| 48 | * Database data. |
| 49 | * |
| 50 | * @var array{db:string, database:string}|array{db:string, host:string, port:int, login:string, password:string, database:string} |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | protected array $dbdata; |
| 54 | |
| 55 | /** |
| 56 | * Database type. |
| 57 | * |
| 58 | * @var string |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | protected string $type = DatabaseType::UNDEFINED; |
| 62 | |
| 63 | /** |
| 64 | * Database status. |
| 65 | * |
| 66 | * @var int |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | protected int $status = DatabaseStatus::CLOSED; |
| 70 | |
| 71 | /** |
| 72 | * Database grammar. |
| 73 | * |
| 74 | * @var Grammar |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | protected Grammar $grammar; |
| 78 | |
| 79 | /** |
| 80 | * Database grammar. |
| 81 | * |
| 82 | * @var SchemaGrammar |
| 83 | * @since 1.0.0 |
| 84 | */ |
| 85 | protected SchemaGrammar $schemaGrammar; |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | public function getType() : string |
| 91 | { |
| 92 | return $this->type; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * {@inheritdoc} |
| 97 | */ |
| 98 | public function getStatus() : int |
| 99 | { |
| 100 | return $this->status; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get database name. |
| 105 | * |
| 106 | * @return string |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | public function getDatabase() : string |
| 111 | { |
| 112 | return $this->dbdata['database'] ?? ''; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get database host. |
| 117 | * |
| 118 | * @return string |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | */ |
| 122 | public function getHost() : string |
| 123 | { |
| 124 | return $this->dbdata['host'] ?? ''; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get database port. |
| 129 | * |
| 130 | * @return int |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | */ |
| 134 | public function getPort() : int |
| 135 | { |
| 136 | return (int) ($this->dbdata['port'] ?? 0); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * {@inheritdoc} |
| 141 | */ |
| 142 | public function getGrammar() : Grammar |
| 143 | { |
| 144 | return $this->grammar; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * {@inheritdoc} |
| 149 | */ |
| 150 | public function getSchemaGrammar() : SchemaGrammar |
| 151 | { |
| 152 | return $this->schemaGrammar; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Connect to database |
| 157 | * |
| 158 | * @param null|array{db:string, database:string}|array{db:string, host:string, port:int, login:string, password:string, database:string} $dbdata the basic database information for establishing a connection |
| 159 | * |
| 160 | * @return void |
| 161 | * |
| 162 | * @since 1.0.0 |
| 163 | */ |
| 164 | abstract public function connect(array $dbdata = null) : void; |
| 165 | |
| 166 | /** |
| 167 | * Object destructor. |
| 168 | * |
| 169 | * Sets the database connection to null |
| 170 | * |
| 171 | * @since 1.0.0 |
| 172 | */ |
| 173 | public function __destruct() |
| 174 | { |
| 175 | $this->close(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * {@inheritdoc} |
| 180 | */ |
| 181 | public function close() : void |
| 182 | { |
| 183 | $this->con = new NullPDO(); |
| 184 | $this->status = DatabaseStatus::CLOSED; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Checks if the connection is initialized |
| 189 | * |
| 190 | * @return bool |
| 191 | * |
| 192 | * @since 1.0.0 |
| 193 | */ |
| 194 | public function isInitialized() : bool |
| 195 | { |
| 196 | return !($this->con instanceof NullPDO); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get values |
| 201 | * |
| 202 | * @param string $name Variable name |
| 203 | * |
| 204 | * @return mixed Returns the value of the connection |
| 205 | * |
| 206 | * @since 1.0.0 |
| 207 | */ |
| 208 | public function __get(string $name) : mixed |
| 209 | { |
| 210 | return isset($this->{$name}) ? $this->{$name} : null; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Start a transaction |
| 215 | * |
| 216 | * @since 1.0.0 |
| 217 | */ |
| 218 | abstract public function beginTransaction() : void; |
| 219 | |
| 220 | /** |
| 221 | * Roll back a transaction |
| 222 | * |
| 223 | * @since 1.0.0 |
| 224 | */ |
| 225 | abstract public function rollBack() : void; |
| 226 | |
| 227 | /** |
| 228 | * Commit a transaction |
| 229 | * |
| 230 | * @since 1.0.0 |
| 231 | */ |
| 232 | abstract public function commit() : void; |
| 233 | } |