Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / Mysqli / MysqliConnection.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Driver\Mysqli;
21
22 use Doctrine\DBAL\Driver\Connection as Connection;
23
24 /**
25  * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
26  */
27 class MysqliConnection implements Connection
28 {
29     /**
30      * @var \mysqli
31      */
32     private $_conn;
33
34     public function __construct(array $params, $username, $password, array $driverOptions = array())
35     {
36         $port = isset($params['port']) ? $params['port'] : ini_get('mysqli.default_port');
37         $socket = isset($params['unix_socket']) ? $params['unix_socket'] : ini_get('mysqli.default_socket');
38
39         $this->_conn = mysqli_init();
40         if ( ! $this->_conn->real_connect($params['host'], $username, $password, $params['dbname'], $port, $socket)) {
41             throw new MysqliException($this->_conn->connect_error, $this->_conn->connect_errno);
42         }
43
44         if (isset($params['charset'])) {
45             $this->_conn->set_charset($params['charset']);
46         }
47     }
48
49     /**
50      * Retrieve mysqli native resource handle.
51      *
52      * Could be used if part of your application is not using DBAL
53      *
54      * @return \mysqli
55      */
56     public function getWrappedResourceHandle()
57     {
58         return $this->_conn;
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     public function prepare($prepareString)
65     {
66         return new MysqliStatement($this->_conn, $prepareString);
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     public function query()
73     {
74         $args = func_get_args();
75         $sql = $args[0];
76         $stmt = $this->prepare($sql);
77         $stmt->execute();
78         return $stmt;
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     public function quote($input, $type=\PDO::PARAM_STR)
85     {
86         return "'". $this->_conn->escape_string($input) ."'";
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     public function exec($statement)
93     {
94         $this->_conn->query($statement);
95         return $this->_conn->affected_rows;
96     }
97
98     /**
99      * {@inheritdoc}
100      */
101     public function lastInsertId($name = null)
102     {
103         return $this->_conn->insert_id;
104     }
105
106     /**
107      * {@inheritdoc}
108      */
109     public function beginTransaction()
110     {
111         $this->_conn->query('START TRANSACTION');
112         return true;
113     }
114
115     /**
116      * {@inheritdoc}
117      */
118     public function commit()
119     {
120         return $this->_conn->commit();
121     }
122
123     /**
124      * {@inheritdoc}non-PHPdoc)
125      */
126     public function rollBack()
127     {
128         return $this->_conn->rollback();
129     }
130
131     /**
132      * {@inheritdoc}
133      */
134     public function errorCode()
135     {
136         return $this->_conn->errno;
137     }
138
139     /**
140      * {@inheritdoc}
141      */
142     public function errorInfo()
143     {
144         return $this->_conn->error;
145     }
146 }