Completed
Push — master ( d6ba15...a74b32 )
by Hong
02:41
created

Driver::realDisconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver\Mysqli;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Db\Driver\DriverAbstract;
19
use Phossa2\Db\Exception\LogicException;
20
use Phossa2\Db\Interfaces\StatementInterface;
21
22
/**
23
 * Driver
24
 *
25
 * Mysqli driver
26
 *
27
 * @package Phossa2\Db
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     DriverAbstract
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
class Driver extends DriverAbstract
34
{
35
    /**
36
     * the connection link
37
     *
38
     * @var    \mysqli
39
     * @access protected
40
     */
41
    protected $link;
42
43
    /**
44
     * Default mysqli attributes
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $attributes = [
50
        'MYSQLI_OPT_CONNECT_TIMEOUT' => 300,
51
        'MYSQLI_OPT_LOCAL_INFILE' => true,
52
        'MYSQLI_INIT_COMMAND' => '',
53
    ];
54
55
    /**
56
     * Driver constructor
57
     *
58
     * @param  array $connectInfo
59
     * @param  StatementInterface $statementPrototype
60
     * @throws InvalidArgumentException if link type not right
61
     * @throws LogicException driver specific extension not loaded
62
     * @access public
63
     */
64
    public function __construct(
65
        $connectInfo,
66
        StatementInterface $statementPrototype = null
67
    ) {
68
        parent::__construct($connectInfo);
69
70
        // set prototypes
71
        $this->statement_prototype = $statementPrototype ?: new Statement();
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    protected function extensionLoaded()/*# : bool */
78
    {
79
        return extension_loaded('mysqli');
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    protected function realLastId($name)
86
    {
87
        return $this->link->insert_id;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    protected function realQuote(
94
        $string,
95
        /*# int */ $type
96
    )/*# : string */ {
97
        return '\'' . $this->link->real_escape_string($string) . '\'';
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    protected function realConnect(array $parameters)
104
    {
105
        // init
106
        $link = new \mysqli();
107
        $link->init();
108
109
        // params with defaults
110
        $p = $this->fixParams($parameters);
111
112
        // real connect
113
        $link->real_connect($p['host'], $p['username'], $p['password'],
114
            $p['db'], $p['port'], $p['socket']);
115
116
        $this->isConenctFailed($link);
117
        $this->setCharset($link, $parameters);
118
        return $link;
119
    }
120
121
    /**
122
     * Disconnect the \PDO link
123
     *
124
     * {@inheritDoc}
125
     */
126
    protected function realDisconnect()
127
    {
128
        $this->link->close();
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    protected function realPing()/*# : bool */
135
    {
136
        return $this->link->ping();
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 View Code Duplication
    protected function realSetAttribute(/*# string */ $attribute, $value)
143
    {
144
        if (is_string($attribute)) {
145
            $this->checkAttribute($attribute);
146
            $this->link->options(constant($attribute), $value);
147
        } else {
148
            $this->link->options($attribute, $value);
149
        }
150
        return $this;
151
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156
    protected function realGetAttribute(/*# string */ $attribute)
157
    {
158
        if (is_string($attribute)) {
159
            $this->checkAttribute($attribute);
160
        }
161
        return null;
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167
    protected function realBegin()
168
    {
169
        $this->link->autocommit(false);
170
        return $this;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    protected function realCommit()
177
    {
178
        $this->link->commit();
179
        $this->link->autocommit(true);
180
        return $this;
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186
    protected function realRollback()
187
    {
188
        $this->link->rollback();
189
        $this->link->autocommit(true);
190
        return $this;
191
    }
192
193
    /**
194
     * Fill connect params with defaults
195
     *
196
     * @param  array $params
197
     * @return array
198
     * @access protected
199
     */
200
    protected function fixParams(array $params)/*# : array */
201
    {
202
        return array_replace([
203
            'host' => 'localhost',
204
            'username' => 'root',
205
            'password' => null,
206
            'db' => null,
207
            'port' => null,
208
            'socket' => null
209
        ], $params);
210
    }
211
212
    /**
213
     *
214
     * @param  \mysqli $link
215
     * @throws LogicException if failed
216
     * @access protected
217
     */
218
    protected function isConenctFailed(\mysqli $link)
219
    {
220
        if ($link->connect_error) {
221
            throw new LogicException(
222
                Message::get(
223
                    Message::DB_CONNECT_FAIL,
224
                    $link->connect_errno,
225
                    $link->connect_error
226
                ),
227
                Message::DB_CONNECT_FAIL
228
            );
229
        }
230
    }
231
232
    /**
233
     * Set charset
234
     *
235
     * @param  \mysqli $link
236
     * @param  array $params
237
     * @access protected
238
     */
239
    protected function setCharset(\mysqli $link, array $params)
240
    {
241
        if (!empty($params['charset'])) {
242
            $link->set_charset($params['charset']);
243
        }
244
    }
245
246
    /**
247
     * Is attribute defined ?
248
     *
249
     * @param  string $attribute
250
     * @throws LogicException
251
     * @access protected
252
     */
253
    protected function checkAttribute(/*# string */ $attribute)
254
    {
255
        if (!defined($attribute)) {
256
            throw new LogicException(
257
                Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute),
258
                Message::DB_ATTRIBUTE_UNKNOWN
259
            );
260
        }
261
    }
262
}
263