Completed
Push — master ( 4bb828...35f8c9 )
by Hong
01:58
created

Driver::realLastId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Pdo;
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
 * PDO 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    \PDO
39
     * @access protected
40
     */
41
    protected $link;
42
43
    /**
44
     * Default PDO attributes
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $attributes = [
50
        'PDO::ATTR_ERRMODE'             => \PDO::ERRMODE_SILENT,
51
        'PDO::ATTR_CASE'                => \PDO::CASE_NATURAL,
52
        'PDO::ATTR_ORACLE_NULLS'        => \PDO::NULL_NATURAL,
53
        'PDO::ATTR_DEFAULT_FETCH_MODE'  => \PDO::FETCH_ASSOC,
54
        'PDO::ATTR_EMULATE_PREPARES'    => false,
55
    ];
56
57
    /**
58
     * Driver constructor
59
     *
60
     * @param  array $connectInfo
61
     * @param  StatementInterface $statementPrototype
62
     * @throws InvalidArgumentException if link type not right
63
     * @throws LogicException driver specific extension not loaded
64
     * @access public
65
     */
66
    public function __construct(
67
        $connectInfo,
68
        StatementInterface $statementPrototype = null
69
    ) {
70
        parent::__construct($connectInfo);
71
72
        // set prototypes
73
        $this->statement_prototype = $statementPrototype ?: new Statement();
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    protected function extensionLoaded()/*# : bool */
80
    {
81
        return extension_loaded('PDO');
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    protected function realLastId($name)
88
    {
89
        return $this->link->lastInsertId($name);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    protected function realQuote(
96
        $string,
97
        /*# int */ $type
98
    )/*# : string */ {
99
        return $this->link->quote($string, $type);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function realConnect(array $parameters)
106
    {
107
        $link = new \PDO(
108
            $parameters['dsn'],
109
            isset($parameters['username']) ? $parameters['username'] : 'root',
110
            isset($parameters['password']) ? $parameters['password'] : null,
111
            isset($parameters['options']) ? $parameters['options'] : null
112
        );
113
114
        // set default attributes
115
        $this->setDefaultAttributes();
116
117
        return $link;
118
    }
119
120
    /**
121
     * Disconnect the \PDO link
122
     *
123
     * {@inheritDoc}
124
     */
125
    protected function realDisconnect()
126
    {
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    protected function realPing()/*# : bool */
133
    {
134
        try {
135
            return (bool) $this->link->query('SELECT 1');
136
        } catch (\Exception $e) {
137
            return false;
138
        }
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144 View Code Duplication
    protected function realSetAttribute(/*# string */ $attribute, $value)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        if (is_string($attribute)) {
147
            $this->checkAttribute($attribute);
148
            $this->link->setAttribute(constant($attribute), $value);
149
        } else {
150
            $this->link->setAttribute($attribute, $value);
151
        }
152
        return $this;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 View Code Duplication
    protected function realGetAttribute(/*# string */ $attribute)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        if (is_string($attribute)) {
161
            $this->checkAttribute($attribute);
162
            return $this->link->getAttribute(constant($attribute));
163
        } else {
164
            return $this->link->getAttribute($attribute);
165
        }
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    protected function realBegin()
172
    {
173
        $this->link->beginTransaction();
174
        return $this;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180
    protected function realCommit()
181
    {
182
        $this->link->commit();
183
        return $this;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189
    protected function realRollback()
190
    {
191
        $this->link->rollBack();
192
        return $this;
193
    }
194
195
    /**
196
     * Set default attributes
197
     *
198
     * @access protected
199
     */
200
    protected function setDefaultAttributes()
201
    {
202
        if (!empty($this->attributes)) {
203
            foreach ($this->attributes as $attr => $val) {
204
                $this->realSetAttribute($attr, $val);
205
            }
206
        }
207
    }
208
209
    /**
210
     * Is attribute defined ?
211
     *
212
     * @param  string $attribute
213
     * @throws LogicException
214
     * @access protected
215
     */
216
    protected function checkAttribute(/*# string */ $attribute)
217
    {
218
        if (!defined($attribute)) {
219
            throw new LogicException(
220
                Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute),
221
                Message::DB_ATTRIBUTE_UNKNOWN
222
            );
223
        }
224
    }
225
}
226