1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\Db; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Utils\Utils; |
7
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
8
|
|
|
|
9
|
|
|
use function is_resource; |
10
|
|
|
use function stream_get_contents; |
11
|
|
|
|
12
|
|
|
abstract class Connection implements ConnectionInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DriverInterface |
16
|
|
|
*/ |
17
|
|
|
protected $driver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Utils |
21
|
|
|
*/ |
22
|
|
|
protected $utils; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The extension name |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $extension; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The client object used to query the database driver |
33
|
|
|
* |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
protected $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var mixed |
40
|
|
|
*/ |
41
|
|
|
public $statement; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The number of rows affected by the last query |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $affectedRows; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The constructor |
52
|
|
|
* |
53
|
|
|
* @param DriverInterface $driver |
54
|
|
|
* @param Utils $utils |
55
|
|
|
* @param string $extension |
56
|
|
|
*/ |
57
|
|
|
public function __construct(DriverInterface $driver, Utils $utils, string $extension) |
58
|
|
|
{ |
59
|
|
|
$this->driver = $driver; |
60
|
|
|
$this->utils = $utils; |
61
|
|
|
$this->extension = $extension; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the number of rows affected by the last query |
66
|
|
|
* |
67
|
|
|
* @param int $affectedRows |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected function setAffectedRows($affectedRows) |
72
|
|
|
{ |
73
|
|
|
$this->affectedRows = $affectedRows; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function affectedRows() |
80
|
|
|
{ |
81
|
|
|
return $this->affectedRows; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function extension() |
88
|
|
|
{ |
89
|
|
|
return $this->extension; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
|
|
public function quote(string $string) |
96
|
|
|
{ |
97
|
|
|
return $string; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
|
|
public function setCharset(string $charset) |
104
|
|
|
{ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the client |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function client() |
113
|
|
|
{ |
114
|
|
|
return $this->client; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritDoc |
119
|
|
|
*/ |
120
|
|
|
public function quoteBinary(string $string) |
121
|
|
|
{ |
122
|
|
|
return $this->quote($string); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritDoc |
127
|
|
|
*/ |
128
|
|
|
public function value($value, TableFieldEntity $field) |
129
|
|
|
{ |
130
|
|
|
return (is_resource($value) ? stream_get_contents($value) : $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritDoc |
135
|
|
|
*/ |
136
|
|
|
public function defaultField() |
137
|
|
|
{ |
138
|
|
|
return 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function warnings() |
145
|
|
|
{ |
146
|
|
|
return ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
|
|
public function close() |
153
|
|
|
{ |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return the regular expression for spaces |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
protected function spaceRegex() |
163
|
|
|
{ |
164
|
|
|
return "(?:\\s|/\\*[\s\S]*?\\*/|(?:#|-- )[^\n]*\n?|--\r?\n)"; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public function execUseQuery(string $query) |
171
|
|
|
{ |
172
|
|
|
$space = $this->spaceRegex(); |
173
|
|
|
if (\preg_match("~^$space*+USE\\b~i", $query)) { |
174
|
|
|
$this->driver->execute($query); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|