File size: 9,210 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/*
 *  $Id: MySQLConnection.php,v 1.18 2004/09/01 14:00:28 dlawson_mi Exp $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://creole.phpdb.org>.
 */

require_once 'creole/Connection.php';
require_once 'creole/common/ConnectionCommon.php';
include_once 'creole/drivers/mysql/MySQLResultSet.php';

/**
 * MySQL implementation of Connection.
 *
 *
 * @author    Hans Lellelid <hans@xmpl.org>
 * @author    Stig Bakken <ssb@fast.no>
 * @author    Lukas Smith
 * @version   $Revision: 1.18 $
 * @package   creole.drivers.mysql
 */
class MySQLConnection extends ConnectionCommon implements Connection {

    /** Current database (used in mysql_select_db()). */
    private $database;

    /**
     * Connect to a database and log in as the specified user.
     *
     * @param $dsn the data source name (see DB::parseDSN for syntax)
     * @param $flags Any conneciton flags.
     * @access public
     * @throws SQLException
     * @return void
     */
    function connect($dsninfo, $flags = 0)
    {
        if (!extension_loaded('mysql')) {
            throw new SQLException('mysql extension not loaded');
        }

        $this->dsn = $dsninfo;
        $this->flags = $flags;

        $persistent = ($flags & Creole::PERSISTENT) === Creole::PERSISTENT;

        if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') {
            $dbhost = ':' . $dsninfo['socket'];
        } else {
            $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
            if (!empty($dsninfo['port'])) {
                $dbhost .= ':' . $dsninfo['port'];
            }
        }
        $user = $dsninfo['username'];
        $pw = $dsninfo['password'];

		$encoding = !empty($dsninfo['encoding']) ? $dsninfo['encoding'] : null;

        $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';

        @ini_set('track_errors', true);
        if ($dbhost && $user && $pw) {
            $conn = @$connect_function($dbhost, $user, $pw);
        } elseif ($dbhost && $user) {
            $conn = @$connect_function($dbhost, $user);
        } elseif ($dbhost) {
            $conn = @$connect_function($dbhost);
        } else {
            $conn = false;
        }
        @ini_restore('track_errors');
        if (empty($conn)) {
            if (($err = @mysql_error()) != '') {
                throw new SQLException("connect failed", $err);
            } elseif (empty($php_errormsg)) {
                throw new SQLException("connect failed");
            } else {
                throw new SQLException("connect failed", $php_errormsg);
            }
        }

        if ($dsninfo['database']) {
            if (!@mysql_select_db($dsninfo['database'], $conn)) {
               switch(mysql_errno($conn)) {
                        case 1049:
                            $exc = new SQLException("no such database", mysql_error($conn));
                        break;
                        case 1044:
                            $exc = new SQLException("access violation", mysql_error($conn));
                        break;
                        default:
                           $exc = new SQLException("cannot select database", mysql_error($conn));
                }

                throw $exc;

            }
            // fix to allow calls to different databases in the same script
            $this->database = $dsninfo['database'];
        }

        $this->dblink = $conn;

        if ($encoding) {
			$this->executeUpdate("SET NAMES " . $encoding);
		}
    }

    /**
     * @see Connection::getDatabaseInfo()
     */
    public function getDatabaseInfo()
    {
        return new MySQLDatabaseInfo($this);
    }

    /**
     * @see Connection::getIdGenerator()
     */
    public function getIdGenerator()
    {
        return new MySQLIdGenerator($this);
    }

    /**
     * @see Connection::prepareStatement()
     */
    public function prepareStatement($sql)
    {
        return new MySQLPreparedStatement($this, $sql);
    }

    /**
     * @see Connection::prepareCall()
     */
    public function prepareCall($sql) {
        throw new SQLException('MySQL does not support stored procedures.');
    }

    /**
     * @see Connection::createStatement()
     */
    public function createStatement()
    {
        return new MySQLStatement($this);
    }

    /**
     * @see Connection::disconnect()
     */
    function close()
    {
        $ret = mysql_close($this->dblink);
        $this->dblink = null;
        return $ret;
    }

    /**
     * @see Connection::applyLimit()
     */
    public function applyLimit(&$sql, $offset, $limit)
    {
        if ( $limit > 0 ) {
            $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit;
        } else if ( $offset > 0 ) {
            $sql .= " LIMIT " . $offset . ", 18446744073709551615";
        }
    }

    /**
     * @see Connection::executeQuery()
     */
    function executeQuery($sql, $fetchmode = null)
    {
        $this->lastQuery = $sql;
        if ($this->database) {
            if (!@mysql_select_db($this->database, $this->dblink)) {
                throw new SQLException('No database selected', mysql_error($this->dblink));
            }
        }//echo $sql . '<br /><br />';
        $result = @mysql_query($sql, $this->dblink);

        if (!$result) {
            if (!defined('DEBUG_SQL')) {
                define('DEBUG_SQL', 0);
            }
            if (DEBUG_SQL == 1) {
                throw new SQLException('Could not execute query', mysql_error($this->dblink), $sql);
            } else {
                throw new SQLException('It is not possible to execute the query. Please contact your system administrator');
            }
        }
        return new MySQLResultSet($this, $result, $fetchmode);
    }

    /**
     * @see Connection::executeUpdate()
     */
    function executeUpdate($sql)
    {
        $this->lastQuery = $sql;

        if ($this->database) {
            if (!@mysql_select_db($this->database, $this->dblink)) {
                    throw new SQLException('No database selected', mysql_error($this->dblink));
            }
        }

        $result = @mysql_query($sql, $this->dblink);
        if (!$result) {
            throw new SQLException('Could not execute update', mysql_error($this->dblink), $sql);
        }
        return (int) mysql_affected_rows($this->dblink);
    }

    /**
     * Start a database transaction.
     * @throws SQLException
     * @return void
     */
    protected function beginTrans()
    {
        $result = @mysql_query('SET AUTOCOMMIT=0', $this->dblink);
        $result = @mysql_query('BEGIN', $this->dblink);
        if (!$result) {
            throw new SQLException('Could not begin transaction', mysql_error($this->dblink));
        }
    }

    /**
     * Commit the current transaction.
     * @throws SQLException
     * @return void
     */
    protected function commitTrans()
    {
        if ($this->database) {
            if (!@mysql_select_db($this->database, $this->dblink)) {
                 throw new SQLException('No database selected', mysql_error($this->dblink));
            }
        }
        $result = @mysql_query('COMMIT', $this->dblink);
        $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink);
        if (!$result) {
            throw new SQLException('Can not commit transaction', mysql_error($this->dblink));
        }
    }

    /**
     * Roll back (undo) the current transaction.
     * @throws SQLException
     * @return void
     */
    protected function rollbackTrans()
    {
        if ($this->database) {
            if (!@mysql_select_db($this->database, $this->dblink)) {
                throw new SQLException('No database selected', mysql_error($this->dblink));
            }
        }
        $result = @mysql_query('ROLLBACK', $this->dblink);
        $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink);
        if (!$result) {
            throw new SQLException('Could not rollback transaction', mysql_error($this->dblink));
        }
    }

    /**
     * Gets the number of rows affected by the data manipulation
     * query.
     *
     * @return int Number of rows affected by the last query.
     */
    function getUpdateCount()
    {
        return (int) @mysql_affected_rows($this->dblink);
    }

}