| 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
| 5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
---|
| 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
| 8 | * |
---|
| 9 | * This program is free software; you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License |
---|
| 11 | * as published by the Free Software Foundation; either version 2 |
---|
| 12 | * of the License, or (at your option) any later version. |
---|
| 13 | * |
---|
| 14 | * This program is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this program; if not, write to the Free Software |
---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | $current_dir = realpath(dirname(__FILE__)); |
---|
| 25 | require_once($current_dir . "/../module/MDB2.php"); |
---|
| 26 | |
---|
| 27 | $g_arr_objDbConn = array(); |
---|
| 28 | |
---|
| 29 | class SC_DbConn { |
---|
| 30 | |
---|
| 31 | var $conn; |
---|
| 32 | var $result; |
---|
| 33 | var $includePath; |
---|
| 34 | var $dsn; |
---|
| 35 | var $err_disp = true; |
---|
| 36 | var $dbFactory; |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | // コンストラクタ |
---|
| 40 | function SC_DbConn($dsn = "", $err_disp = true, $new = false){ |
---|
| 41 | global $g_arr_objDbConn; |
---|
| 42 | |
---|
| 43 | // Debugモード指定 |
---|
| 44 | $options['debug'] = PEAR_DB_DEBUG; |
---|
| 45 | // 持続的接続オプション |
---|
| 46 | $options['persistent'] = PEAR_DB_PERSISTENT; |
---|
| 47 | |
---|
| 48 | if (strlen($dsn) >= 1) { |
---|
| 49 | $this->dsn = $dsn; |
---|
| 50 | } elseif (defined('DEFAULT_DSN')) { |
---|
| 51 | $this->dsn = DEFAULT_DSN; |
---|
| 52 | } else { |
---|
| 53 | // XXX 以前の仕様を継承しているが、意図が良く分からない。(2010/03/03 Seasoft 塚田) |
---|
| 54 | return; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // 既に接続されていないか、新規接続要望の場合は接続する。 |
---|
| 58 | if (!isset($g_arr_objDbConn[$this->dsn]) || !isset($g_arr_objDbConn[$this->dsn]->connection)) { |
---|
| 59 | $new = true; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | if ($new) { |
---|
| 63 | // TODO singleton の方が良いかも |
---|
| 64 | $this->conn = MDB2::connect($this->dsn, $options); |
---|
| 65 | $g_arr_objDbConn[$this->dsn] = $this->conn; |
---|
| 66 | |
---|
| 67 | // TODO MDB2::setCharset() を使った方が良い? |
---|
| 68 | if (DB_TYPE == 'mysql') { |
---|
| 69 | $g_arr_objDbConn[$this->dsn]->query('SET NAMES utf8'); // FIXME mysql_set_charset を使える環境では、その方が良さそう (2010/03/03 Seasoft 塚田) |
---|
| 70 | $g_arr_objDbConn[$this->dsn]->query("SET SESSION sql_mode = 'ANSI'"); |
---|
| 71 | } |
---|
| 72 | } else { |
---|
| 73 | $this->conn = $g_arr_objDbConn[$this->dsn]; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | $this->conn->setFetchMode(MDB2_FETCHMODE_ASSOC); |
---|
| 77 | $this->err_disp = $err_disp; |
---|
| 78 | $this->dbFactory = SC_DB_DBFactory_Ex::getInstance(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // クエリの実行 |
---|
| 82 | function query($n ,$arr = array(), $ignore_err = false){ |
---|
| 83 | // mysqlの場合にはビュー表を変換する |
---|
| 84 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 85 | |
---|
| 86 | $sth = $this->conn->prepare($n); |
---|
| 87 | if ( $arr ) { |
---|
| 88 | $result = $sth->execute($arr); |
---|
| 89 | } else { |
---|
| 90 | $result = $sth->execute(); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | if ($this->conn->isError($result) && !$ignore_err){ |
---|
| 94 | $this->send_err_mail($result, $n); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | $this->result = $result; |
---|
| 98 | return $this->result; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // 一件のみ取得 |
---|
| 102 | function getOne($n, $arr = array()){ |
---|
| 103 | |
---|
| 104 | // mysqlの場合にはビュー表を変換する |
---|
| 105 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 106 | |
---|
| 107 | $sth = $this->conn->prepare($n); |
---|
| 108 | if ( $arr ) { |
---|
| 109 | $affected = $sth->execute($arr); |
---|
| 110 | } else { |
---|
| 111 | $affected = $sth->execute(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if ($this->conn->isError($affected)){ |
---|
| 115 | $this->send_err_mail($affected ,$n); |
---|
| 116 | } |
---|
| 117 | $this->result = $affected->fetchOne(); |
---|
| 118 | return $this->result; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * クエリを実行し、最初の行を返す |
---|
| 123 | * |
---|
| 124 | * @param string $sql SQL クエリ |
---|
| 125 | * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 |
---|
| 126 | * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 |
---|
| 127 | * @return array データを含む1次元配列。失敗した場合に DB_Error オブジェクトを返します。 |
---|
| 128 | */ |
---|
| 129 | function getRow($sql, $arrVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) { |
---|
| 130 | |
---|
| 131 | // mysqlの場合にはビュー表を変換する |
---|
| 132 | if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql); |
---|
| 133 | |
---|
| 134 | $sth = $this->conn->prepare($sql); |
---|
| 135 | if ($arrVal) { |
---|
| 136 | $affected = $sth->execute($arrVal); |
---|
| 137 | } else { |
---|
| 138 | $affected = $sth->execute(); |
---|
| 139 | } |
---|
| 140 | if ($this->conn->isError($affected)) { |
---|
| 141 | $this->send_err_mail($affected, $sql); |
---|
| 142 | } |
---|
| 143 | $this->result = $affected->fetchRow($fetchmode); |
---|
| 144 | |
---|
| 145 | return $this->result; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | function getCol($n, $col, $arr = array()) { |
---|
| 149 | |
---|
| 150 | // mysqlの場合にはビュー表を変換する |
---|
| 151 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 152 | |
---|
| 153 | $sth = $this->conn->prepare($n); |
---|
| 154 | if ($arr) { |
---|
| 155 | $affected = $sth->execute($arr); |
---|
| 156 | } else { |
---|
| 157 | $affected = $sth->execute(); |
---|
| 158 | } |
---|
| 159 | if ($this->conn->isError($affected)) { |
---|
| 160 | $this->send_err_mail($affected, $n); |
---|
| 161 | } |
---|
| 162 | $this->result = $affected->fetchCol($col); |
---|
| 163 | return $this->result; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * クエリを実行し、全ての行を返す |
---|
| 168 | * |
---|
| 169 | * @param string $sql SQL クエリ |
---|
| 170 | * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 |
---|
| 171 | * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 |
---|
| 172 | * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。 |
---|
| 173 | */ |
---|
| 174 | function getAll($sql, $arrVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) { |
---|
| 175 | |
---|
| 176 | // mysqlの場合にはビュー表を変換する |
---|
| 177 | if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql); |
---|
| 178 | |
---|
| 179 | // XXX このエラー処理はここで行なうべきなのか疑問。また、戻り値も疑問(なお、変更時はドキュメントも変更を)。 |
---|
| 180 | if (PEAR::isError($this->conn)) { |
---|
| 181 | if (ADMIN_MODE) { |
---|
| 182 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn); |
---|
| 183 | } else { |
---|
| 184 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:"); |
---|
| 185 | } |
---|
| 186 | return 0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | $sth = $this->conn->prepare($sql); |
---|
| 190 | |
---|
| 191 | if ($arrVal) { // FIXME 判定が曖昧 |
---|
| 192 | $affected = $sth->execute($arrVal); |
---|
| 193 | } else { |
---|
| 194 | $affected = $sth->execute(); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if ($this->conn->isError($affected)) { |
---|
| 198 | $this->send_err_mail($affected, $sql); |
---|
| 199 | } |
---|
| 200 | $this->result = $affected->fetchAll($fetchmode); |
---|
| 201 | |
---|
| 202 | return $this->result; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | function autoExecute($table_name, $fields_values, $sql_where = null){ |
---|
| 206 | |
---|
| 207 | if ( $sql_where ) { |
---|
| 208 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where); |
---|
| 209 | } else { |
---|
| 210 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if ($this->conn->isError($result)){ |
---|
| 214 | $this->send_err_mail($result, $n); |
---|
| 215 | } |
---|
| 216 | $this->result = $result; |
---|
| 217 | return $this->result; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | function prepare($n){ |
---|
| 222 | global $sql; |
---|
| 223 | $sql = $n; |
---|
| 224 | $result = $this->conn->prepare($n); |
---|
| 225 | $this->result = $result; |
---|
| 226 | return $this->result; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | function execute($n, $obj){ |
---|
| 230 | global $sql; |
---|
| 231 | $sql = $n; |
---|
| 232 | $result = $this->conn->execute($n, $obj); |
---|
| 233 | $this->result = $result; |
---|
| 234 | return $this->result; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | function reset(){ |
---|
| 238 | $this->conn->disconnect(); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | function send_err_mail($pearResult, $sql){ |
---|
| 242 | |
---|
| 243 | $errmsg = $sql . "\n\n"; |
---|
| 244 | |
---|
| 245 | // PEAR エラーを伴う場合 |
---|
| 246 | if (!is_null($pearResult)) { |
---|
| 247 | $errmsg .= $pearResult->message . "\n\n"; |
---|
| 248 | $errmsg .= $pearResult->userinfo . "\n\n"; |
---|
| 249 | $errmsg .= SC_Utils_Ex::sfBacktraceToString($pearResult->backtrace); |
---|
| 250 | } |
---|
| 251 | // (上に該当せず)バックトレースを生成できる環境(一般的には PHP 4 >= 4.3.0, PHP 5)の場合 |
---|
| 252 | else if (function_exists("debug_backtrace")) { |
---|
| 253 | $errmsg .= SC_Utils_Ex::sfBacktraceToString(array_slice(debug_backtrace(), 2)); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | GC_Utils_Ex::gfPrintLog($errmsg); |
---|
| 257 | trigger_error($errmsg, E_USER_ERROR); |
---|
| 258 | exit(); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * 直前に実行されたSQL文を取得する. |
---|
| 263 | * |
---|
| 264 | * @param boolean $disp trueの場合、画面出力を行う. |
---|
| 265 | * @return string SQL文 |
---|
| 266 | */ |
---|
| 267 | function getLastQuery($disp = true) { |
---|
| 268 | $sql = $this->conn->last_query; |
---|
| 269 | if($disp) { |
---|
| 270 | print($sql.";<br />\n"); |
---|
| 271 | } |
---|
| 272 | return $sql; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | ?> |
---|