Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
26.92% |
7 / 26 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SqlServerConnection | |
26.92% |
7 / 26 |
|
20.00% |
1 / 5 |
49.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| connect | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
| 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\SqlServerGrammar; |
| 20 | use phpOMS\DataStorage\Database\Schema\Grammar\SqlServerGrammar as SqlServerSchemaGrammar; |
| 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 SqlServerConnection extends ConnectionAbstract |
| 34 | { |
| 35 | /** |
| 36 | * Object constructor. |
| 37 | * |
| 38 | * Creates the database object and overwrites all default values. |
| 39 | * |
| 40 | * @param array{db:string, host?:string, port?:int, login?:string, password?: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::SQLSRV; |
| 47 | $this->grammar = new SqlServerGrammar(); |
| 48 | $this->schemaGrammar = new SqlServerSchemaGrammar(); |
| 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 | * {@inheritdoc} |
| 60 | */ |
| 61 | public function connect(array $dbdata = null) : void |
| 62 | { |
| 63 | if ($this->status === DatabaseStatus::OK) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $this->dbdata = $dbdata ?? $this->dbdata; |
| 68 | |
| 69 | if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password']) |
| 70 | || !DatabaseType::isValidValue($this->dbdata['db']) |
| 71 | ) { |
| 72 | $this->status = DatabaseStatus::FAILURE; |
| 73 | $this->dbdata['password'] = '****'; |
| 74 | |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $this->close(); |
| 79 | |
| 80 | try { |
| 81 | $this->con = new \PDO('sqlsrv:Server=' . $this->dbdata['host'] . ',' . $this->dbdata['port'] . ';Database=' . $this->dbdata['database'] . ';ConnectionPooling=0', $this->dbdata['login'], $this->dbdata['password']); |
| 82 | //$this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); // Not working! |
| 83 | $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 84 | |
| 85 | $this->status = DatabaseStatus::OK; |
| 86 | } catch (\PDOException $_) { |
| 87 | $this->con = new NullPDO(); |
| 88 | $this->status = DatabaseStatus::MISSING_DATABASE; |
| 89 | } finally { |
| 90 | $this->dbdata['password'] = '****'; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * {@inheritdoc} |
| 96 | */ |
| 97 | public function beginTransaction() : void |
| 98 | { |
| 99 | $this->con->beginTransaction(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * {@inheritdoc} |
| 104 | */ |
| 105 | public function rollBack() : void |
| 106 | { |
| 107 | $this->con->rollBack(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * {@inheritdoc} |
| 112 | */ |
| 113 | public function commit() : void |
| 114 | { |
| 115 | $this->con->commit(); |
| 116 | } |
| 117 | } |