|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Storage\Database\Raw; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Interfaces\IDriverSources; |
|
7
|
|
|
use kalanis\kw_mapper\Interfaces\IPassConnection; |
|
8
|
|
|
use kalanis\kw_mapper\MapperException; |
|
9
|
|
|
use kalanis\kw_mapper\Storage\Database\ADatabase; |
|
10
|
|
|
use kalanis\kw_mapper\Storage\Database\Dialects; |
|
11
|
|
|
use kalanis\kw_mapper\Storage\Database\TConnection; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Dba |
|
16
|
|
|
* @package kalanis\kw_mapper\Storage\Database\Raw |
|
17
|
|
|
* @link https://www.php.net/manual/en/book.dba.php |
|
18
|
|
|
* @codeCoverageIgnore remote connection |
|
19
|
|
|
*/ |
|
20
|
|
|
class Dba extends ADatabase implements IPassConnection |
|
21
|
|
|
{ |
|
22
|
|
|
use TConnection; |
|
23
|
|
|
|
|
24
|
|
|
protected string $extension = 'dba'; |
|
25
|
|
|
/** @var resource|null */ |
|
26
|
|
|
protected $connection = null; |
|
27
|
|
|
|
|
28
|
|
|
public function languageDialect(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return Dialects\EmptyDialect::class; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function disconnect(): void |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->isConnected()) { |
|
36
|
|
|
dba_close($this->connection); |
|
|
|
|
|
|
37
|
|
|
$this->connection = null; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $key |
|
43
|
|
|
* @throws MapperException |
|
44
|
|
|
* @return array<string|int, string|int|float|null|array<string|int|float|null>> |
|
45
|
|
|
*/ |
|
46
|
|
|
public function query(string $key): array |
|
47
|
|
|
{ |
|
48
|
|
|
if (empty($key)) { |
|
49
|
|
|
return []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->connect(); |
|
53
|
|
|
|
|
54
|
|
|
$results = []; |
|
55
|
|
|
$key = dba_firstkey($this->getConnection()); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
while (false !== $key) { |
|
58
|
|
|
$line = dba_fetch($key, $this->getConnection()); |
|
|
|
|
|
|
59
|
|
|
if (false !== $line) { |
|
60
|
|
|
$results[] = $line; |
|
61
|
|
|
} |
|
62
|
|
|
$key = dba_nextkey($this->getConnection()); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $results; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @param string $action |
|
71
|
|
|
* @param string $content |
|
72
|
|
|
* @throws MapperException |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function exec(string $action, string $key, string $content = ''): bool |
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($key)) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->connect(); |
|
82
|
|
|
|
|
83
|
|
|
if (dba_exists($key, $this->getConnection())) { |
|
|
|
|
|
|
84
|
|
|
if (IDriverSources::ACTION_UPDATE == $action) { |
|
85
|
|
|
return dba_replace($key, $content, $this->getConnection()); |
|
|
|
|
|
|
86
|
|
|
} elseif (IDriverSources::ACTION_DELETE == $action) { |
|
87
|
|
|
return dba_delete($key, $this->getConnection()); |
|
|
|
|
|
|
88
|
|
|
} else { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
if (IDriverSources::ACTION_UPDATE == $action) { |
|
93
|
|
|
return dba_insert($key, $content, $this->getConnection()); |
|
|
|
|
|
|
94
|
|
|
} elseif (IDriverSources::ACTION_INSERT == $action) { |
|
95
|
|
|
return dba_insert($key, $content, $this->getConnection()); |
|
96
|
|
|
} else { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @throws MapperException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function connect(): void |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->isConnected()) { |
|
108
|
|
|
$this->connection = $this->connectToServer(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @throws MapperException |
|
114
|
|
|
* @return resource |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function connectToServer() |
|
117
|
|
|
{ |
|
118
|
|
|
$connection = dba_open( |
|
119
|
|
|
$this->config->getLocation(), |
|
120
|
|
|
$this->config->getDatabase(), |
|
121
|
|
|
$this->config->getType() |
|
122
|
|
|
); |
|
123
|
|
|
if (!$connection) { |
|
|
|
|
|
|
124
|
|
|
throw new MapperException('DBA connection failed.'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $connection; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|