Completed
Push — master ( 6e5d3c...d6ba15 )
by Hong
02:17
created

Driver::realCommit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
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
        $params = array_replace([
111
            'host' => 'localhost', 'username' => 'root', 'password' => null,
112
            'db' => null, 'port' => null, 'socket' => null
113
        ], $parameters);
114
115
        // real connect
116
        $link->real_connect(
117
            $params['host'], $params['username'], $params['password'],
118
            $params['db'], $params['port'], $params['socket']
119
        );
120
121
        // check failure
122
        $this->isConenctFailed($link);
123
124
        // set charset
125
        $this->setCharset($link, $parameters);
126
127
        return $link;
128
    }
129
130
    /**
131
     * Disconnect the \PDO link
132
     *
133
     * {@inheritDoc}
134
     */
135
    protected function realDisconnect()
136
    {
137
        $this->link->close();
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143
    protected function realPing()/*# : bool */
144
    {
145
        return $this->link->ping();
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151 View Code Duplication
    protected function realSetAttribute(/*# string */ $attribute, $value)
152
    {
153
        if (is_string($attribute)) {
154
            $this->checkAttribute($attribute);
155
            $this->link->options(constant($attribute), $value);
156
        } else {
157
            $this->link->options($attribute, $value);
158
        }
159
        return $this;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165
    protected function realGetAttribute(/*# string */ $attribute)
166
    {
167
        if (is_string($attribute)) {
168
            $this->checkAttribute($attribute);
169
        }
170
        return null;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    protected function realBegin()
177
    {
178
        $this->link->autocommit(false);
179
        return $this;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    protected function realCommit()
186
    {
187
        $this->link->commit();
188
        $this->link->autocommit(true);
189
        return $this;
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     */
195
    protected function realRollback()
196
    {
197
        $this->link->rollback();
198
        $this->link->autocommit(true);
199
        return $this;
200
    }
201
202
    /**
203
     *
204
     * @param  \mysqli $link
205
     * @throws LogicException if failed
206
     * @access protected
207
     */
208
    protected function isConenctFailed(\mysqli $link)
209
    {
210
        if ($link->connect_error) {
211
            throw new LogicException(
212
                Message::get(
213
                    Message::DB_CONNECT_FAIL,
214
                    $link->connect_errno,
215
                    $link->connect_error
216
                ),
217
                Message::DB_CONNECT_FAIL
218
            );
219
        }
220
    }
221
222
    /**
223
     * Set charset
224
     *
225
     * @param  \mysqli $link
226
     * @param  array $params
227
     * @access protected
228
     */
229
    protected function setCharset(\mysqli $link, array $params)
230
    {
231
        if (!empty($params['charset'])) {
232
            $link->set_charset($params['charset']);
233
        }
234
    }
235
236
    /**
237
     * Is attribute defined ?
238
     *
239
     * @param  string $attribute
240
     * @throws LogicException
241
     * @access protected
242
     */
243
    protected function checkAttribute(/*# string */ $attribute)
244
    {
245
        if (!defined($attribute)) {
246
            throw new LogicException(
247
                Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute),
248
                Message::DB_ATTRIBUTE_UNKNOWN
249
            );
250
        }
251
    }
252
}
253