Passed
Push — main ( efab48...3626c9 )
by Thierry
01:26
created

Connection::execUseQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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