| | 1 | <?php |
| | 2 | ############################################################################### |
| | 3 | # Gregarius - A PHP based RSS aggregator. |
| | 4 | # Copyright (C) 2003 - 2005 Marco Bonetti |
| | 5 | # |
| | 6 | ############################################################################### |
| | 7 | # This program is free software and open source software; you can redistribute |
| | 8 | # it and/or modify it under the terms of the GNU General Public License as |
| | 9 | # published by the Free Software Foundation; either version 2 of the License, |
| | 10 | # or (at your option) any later version. |
| | 11 | # |
| | 12 | # This program is distributed in the hope that it will be useful, but WITHOUT |
| | 13 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| | 14 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| | 15 | # more details. |
| | 16 | # |
| | 17 | # You should have received a copy of the GNU General Public License along |
| | 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| | 19 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit |
| | 20 | # http://www.gnu.org/licenses/gpl.html |
| | 21 | # |
| | 22 | ############################################################################### |
| | 23 | # E-mail: mbonetti at gmail dot com |
| | 24 | # Web page: http://gregarius.net/ |
| | 25 | # |
| | 26 | ############################################################################### |
| | 27 | # mysqli adapter 2006 by Niels Ganser, ng at depoll dot de |
| | 28 | # just a quick hack before returning to ruby country :-) |
| | 29 | ############################################################################### |
| | 30 | |
| | 31 | |
| | 32 | require_once(dirname(__FILE__) . '/db.php'); |
| | 33 | |
| | 34 | class MysqliDB extends DB { |
| | 35 | |
| | 36 | var $connection; |
| | 37 | |
| | 38 | function MysqliDB() { |
| | 39 | parent::DB(); |
| | 40 | } |
| | 41 | |
| | 42 | |
| | 43 | //// The following are just wrappers of their mysql counterpart for now |
| | 44 | //// maybe one day I shall support other rdbms and add some differentiation |
| | 45 | //// in here. |
| | 46 | |
| | 47 | function DBConnect($dbserver, $dbuname, $dbpass) { |
| | 48 | if (!($this->connection = new mysqli($dbserver, $dbuname, $dbpass))) { |
| | 49 | die( "<h1>Error connecting to the database!</h1>\n" |
| | 50 | ."<p>Have you edited dbinit.php and correctly defined " |
| | 51 | ."the database username and password?</p>\n" ); |
| | 52 | } |
| | 53 | } |
| | 54 | |
| | 55 | function DBSelectDB($dbname) { |
| | 56 | if (!$this->connection->select_db($dbname)) { |
| | 57 | die( "<h1>Error selecting the database!</h1>\n" |
| | 58 | ."<p>Does your database exist?" |
| | 59 | . "Have you edited dbinit.php and correctly defined " |
| | 60 | ."the database username and password?</p>\n" |
| | 61 | ."<p>Refer to the <a href=\"INSTALL\">INSTALL</a> document " |
| | 62 | ."if in doubt</p>\n" ); |
| | 63 | } |
| | 64 | } |
| | 65 | |
| | 66 | function rss_query ($query, $dieOnError=true, $preventRecursion=false) { |
| | 67 | $ret = $this->connection->query($query); |
| | 68 | |
| | 69 | if ($error = $this -> rss_sql_error()) { |
| | 70 | $errorString = $this -> rss_sql_error_message(); |
| | 71 | } |
| | 72 | |
| | 73 | // if we got a missing table error, look for missing tables in the schema |
| | 74 | // and try to create them |
| | 75 | if ($error == 1146 && !$preventRecursion && $dieOnError) { |
| | 76 | require_once(dirname(__FILE__) . '/../../init.php'); |
| | 77 | rss_require('schema.php'); |
| | 78 | checkSchema(); |
| | 79 | return $this -> rss_query ($query, $dieOnError, true); |
| | 80 | } elseif ($error == 1054 && !$preventRecursion && $dieOnError) { |
| | 81 | if (preg_match("/^[^']+'([^']+)'.*$/",$errorString,$matches)) { |
| | 82 | require_once(dirname(__FILE__) . '/../../init.php'); |
| | 83 | rss_require('schema.php'); |
| | 84 | checkSchemaColumns($matches[1]); |
| | 85 | return $this -> rss_query ($query, $dieOnError, true); |
| | 86 | } |
| | 87 | } |
| | 88 | |
| | 89 | if ($error && $dieOnError) { |
| | 90 | die ("<p>Failed to execute the SQL query <pre>$query</pre> </p>" |
| | 91 | ."<p>Error $error: $errorString</p>"); |
| | 92 | } |
| | 93 | return $ret; |
| | 94 | } |
| | 95 | |
| | 96 | function rss_fetch_row($rs) { |
| | 97 | return mysqli_fetch_row($rs); |
| | 98 | } |
| | 99 | |
| | 100 | function rss_fetch_assoc($rs) { |
| | 101 | return mysqli_fetch_assoc($rs); |
| | 102 | } |
| | 103 | function rss_num_rows($rs) { |
| | 104 | return mysqli_num_rows($rs); |
| | 105 | } |
| | 106 | |
| | 107 | function rss_sql_error() { |
| | 108 | return $this->connection->errno; |
| | 109 | } |
| | 110 | |
| | 111 | function rss_sql_error_message () { |
| | 112 | return $this->connection->error; |
| | 113 | } |
| | 114 | |
| | 115 | function rss_insert_id() { |
| | 116 | return $this->connection->insert_id; |
| | 117 | } |
| | 118 | |
| | 119 | function rss_real_escape_string($string) { |
| | 120 | return $this->connection->real_escape_string($string); |
| | 121 | } |
| | 122 | |
| | 123 | function rss_is_sql_error($kind) { |
| | 124 | switch ($kind) { |
| | 125 | case RSS_SQL_ERROR_NO_ERROR: |
| | 126 | return ($this->connection->errno == 0); |
| | 127 | break; |
| | 128 | case RSS_SQL_ERROR_DUPLICATE_ROW: |
| | 129 | return ($this->connection->errno == 1062); |
| | 130 | break; |
| | 131 | default: |
| | 132 | return false; |
| | 133 | } |
| | 134 | } |
| | 135 | } |
| | 136 | ?> |