Completed
Push — master ( deea5c...9cc936 )
by Tim
16s
created

PDOConnectionWrapper::commit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Connection\PDOConnectionWrapper
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Connection;
22
23
/**
24
 * A wrapper for a \PDO connection instance.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class PDOConnectionWrapper implements ConnectionInterface
33
{
34
35
    /**
36
     * The \PDO connection that has to be wrapped.
37
     *
38
     * @var \PDO
39
     */
40
    protected $connection;
41
42
    /**
43
     * Initializes the wrapper with the passed PDO connection.
44
     *
45
     * @param \PDO $connection The PDO connection to wrap
46
     */
47
    public function __construct(\PDO $connection)
48
    {
49
        $this->connection = $connection;
50
    }
51
52
    /**
53
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via
54
     * the instance are not committed until you end the transaction by calling commit().
55
     *
56
     * @return boolean Returns TRUE on success or FALSE on failure
57
     * @link http://php.net/manual/en/pdo.begintransaction.php
58
     */
59
    public function beginTransaction()
60
    {
61
        return $this->connection->beginTransaction();
62
    }
63
64
    /**
65
     * Commits a transaction, returning the database connection to autocommit mode until the next call
66
     * to beginTransaction() starts a new transaction.
67
     *
68
     * @return void
69
     * @throws \PDOException Throws a PDOException if there is no active transaction.
70
     * @link http://php.net/manual/en/pdo.commit.php
71
     */
72
    public function commit()
73
    {
74
        $this->connection->commit();
75
    }
76
77
    /**
78
     * Rolls back the current transaction, as initiated by beginTransaction().
79
     *
80
     * @return boolean Returns TRUE on success or FALSE on failure
81
     * @throws \PDOException Throws a PDOException if there is no active transaction
82
     */
83
    public function rollBack()
84
    {
85
        return $this->connection->rollBack();
86
    }
87
88
    /**
89
     * Fetch the SQLSTATE associated with  the last operation on the database handle.
90
     *
91
     * @return mixed The error code of the last operation of the database handle
92
     * @link http://php.net/manual/en/pdo.errorcode.php
93
     */
94
    public function errorCode()
95
    {
96
        return $this->connection->errorCode();
97
    }
98
99
    /**
100
     * Fetch extended error information associated with the last operation on the database handle.
101
     *
102
     * @return array Returns an array of error information about the last operation performed by this database handle
103
     * @link http://php.net/manual/en/pdo.errorinfo.php
104
     */
105
    public function errorInfo()
106
    {
107
        return $this->connection->errorInfo();
108
    }
109
110
    /**
111
     * Executes an SQL statement, returning a result set as a PDOStatement object.
112
     *
113
     * @param string $statement The SQL statement to prepare and execute
114
     *
115
     * @return \PDOStatement|boolean Returns a \PDOStatement object, or FALSE on failure
116
     * @link http://php.net/manual/en/pdo.query.php
117
     */
118
    public function query($statement)
119
    {
120
        return call_user_func_array(array($this->connection, 'query'), func_get_args());
121
    }
122
123
    /**
124
     * Prepares a statement for execution and returns a statement object.
125
     *
126
     * @param string $statement     This must be a valid SQL statement template for the target database server
127
     * @param array  $driverOptions This array holds one or more key=>value pairs to set attribute values for the \PDOStatement object that this method returns
128
     *
129
     * @return \PDOStatement If the database server successfully prepares the statement, this method returns a \PDOStatement object
130
     * @throws \PDOException Throws a PDOException if the database server cannot successfully prepare the statement
131
     * @link http://php.net/manual/en/pdo.prepare.php
132
     */
133
    public function prepare($statement, array $driverOptions = array())
134
    {
135
        return $this->connection->prepare($statement, $driverOptions);
136
    }
137
138
    /**
139
     * Execute an SQL statement and return the number of affected rows.
140
     *
141
     * @param string $statement The SQL statement to prepare and execute
142
     *
143
     * @return integer The number of affected rows
144
     * @link http://php.net/manual/en/pdo.exec.php
145
     */
146
    public function exec($statement)
147
    {
148
        return $this->connection->exec($statement);
149
    }
150
151
    /**
152
     * Returns the ID of the last inserted row or sequence value.
153
     *
154
     * @param string $name Name of the sequence object from which the ID should be returned
155
     *
156
     * @return string A string representing the row ID of the last row that was inserted into the database
157
     * @link http://php.net/manual/en/pdo.lastinsertid.php
158
     */
159
    public function lastInsertId($name = null)
160
    {
161
        return $this->connection->lastInsertId($name);
162
    }
163
164
    /**
165
     * Quotes a string for use in a query.
166
     *
167
     * @param string  $string        The string to be quoted
168
     * @param integer $parameterType Provides a data type hint for drivers that have alternate quoting styles
169
     *
170
     * @return string|boolean Returns a quoted string that is theoretically safe to pass into an SQL statement or FALSE if the driver does not support quoting in this way
171
     * @link http://php.net/manual/en/pdo.quote.php
172
     */
173
    public function quote($string, $parameterType = PDO::PARAM_STR)
174
    {
175
        return $this->connection->quote($string, $parameterType);
176
    }
177
}
178