| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2007 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/DB.php"); |
|---|
| 26 | |
|---|
| 27 | $objDbConn = ""; |
|---|
| 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 $objDbConn; |
|---|
| 42 | |
|---|
| 43 | // Debugモード指定 |
|---|
| 44 | $options['debug'] = PEAR_DB_DEBUG; |
|---|
| 45 | // 持続的接続オプション |
|---|
| 46 | $options['persistent'] = PEAR_DB_PERSISTENT; |
|---|
| 47 | |
|---|
| 48 | // 既に接続されていないか、新規接続要望の場合は接続する。 |
|---|
| 49 | if(!isset($objDbConn->connection) || $new) { |
|---|
| 50 | if($dsn != "") { |
|---|
| 51 | $objDbConn = DB::connect($dsn, $options); |
|---|
| 52 | $this->dsn = $dsn; |
|---|
| 53 | } else { |
|---|
| 54 | if(defined('DEFAULT_DSN')) { |
|---|
| 55 | $objDbConn = DB::connect(DEFAULT_DSN, $options); |
|---|
| 56 | $this->dsn = DEFAULT_DSN; |
|---|
| 57 | } else { |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | if (DB_TYPE == 'mysql') { |
|---|
| 64 | $objDbConn->query('SET NAMES utf8'); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | $this->conn = $objDbConn; |
|---|
| 68 | $this->err_disp = $err_disp; |
|---|
| 69 | $this->dbFactory = SC_DB_DBFactory_Ex::getInstance(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | // クエリの実行 |
|---|
| 73 | function query($n ,$arr = "", $ignore_err = false){ |
|---|
| 74 | // mysqlの場合にはビュー表を変換する |
|---|
| 75 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
|---|
| 76 | |
|---|
| 77 | if ( $arr ) { |
|---|
| 78 | $result = $this->conn->query($n, $arr); |
|---|
| 79 | } else { |
|---|
| 80 | $result = $this->conn->query($n); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | if ($this->conn->isError($result) && !$ignore_err){ |
|---|
| 84 | $this->send_err_mail($result, $n); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | $this->result = $result; |
|---|
| 88 | return $this->result; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // 一件のみ取得 |
|---|
| 92 | function getOne($n, $arr = ""){ |
|---|
| 93 | |
|---|
| 94 | // mysqlの場合にはビュー表を変換する |
|---|
| 95 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
|---|
| 96 | |
|---|
| 97 | if ( $arr ) { |
|---|
| 98 | $result = $this->conn->getOne($n, $arr); |
|---|
| 99 | } else { |
|---|
| 100 | $result = $this->conn->getOne($n); |
|---|
| 101 | } |
|---|
| 102 | if ($this->conn->isError($result)){ |
|---|
| 103 | $this->send_err_mail($result ,$n); |
|---|
| 104 | } |
|---|
| 105 | $this->result = $result; |
|---|
| 106 | |
|---|
| 107 | return $this->result; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * クエリを実行し、最初の行を返す |
|---|
| 112 | * |
|---|
| 113 | * @param string $sql SQL クエリ |
|---|
| 114 | * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 |
|---|
| 115 | * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 |
|---|
| 116 | * @return array データを含む1次元配列。失敗した場合に DB_Error オブジェクトを返します。 |
|---|
| 117 | */ |
|---|
| 118 | function getRow($sql, $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) { |
|---|
| 119 | |
|---|
| 120 | // mysqlの場合にはビュー表を変換する |
|---|
| 121 | if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql); |
|---|
| 122 | |
|---|
| 123 | $result = $this->conn->getRow($sql, $arrVal ,$fetchmode); |
|---|
| 124 | |
|---|
| 125 | if ($this->conn->isError($result)){ |
|---|
| 126 | $this->send_err_mail($result ,$sql); |
|---|
| 127 | } |
|---|
| 128 | $this->result = $result; |
|---|
| 129 | return $this->result; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | function getCol($n, $col, $arr = "") { |
|---|
| 133 | |
|---|
| 134 | // mysqlの場合にはビュー表を変換する |
|---|
| 135 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
|---|
| 136 | |
|---|
| 137 | if ($arr) { |
|---|
| 138 | $result = $this->conn->getCol($n, $col, $arr); |
|---|
| 139 | } else { |
|---|
| 140 | $result = $this->conn->getCol($n, $col); |
|---|
| 141 | } |
|---|
| 142 | if ($this->conn->isError($result)) { |
|---|
| 143 | $this->send_err_mail($result, $n); |
|---|
| 144 | } |
|---|
| 145 | $this->result = $result; |
|---|
| 146 | return $this->result; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // SELECT文の実行結果を全て取得 |
|---|
| 150 | function getAll($n, $arr = ""){ |
|---|
| 151 | |
|---|
| 152 | // mysqlの場合にはビュー表を変換する |
|---|
| 153 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
|---|
| 154 | |
|---|
| 155 | if(PEAR::isError($this->conn)) { |
|---|
| 156 | if(ADMIN_MODE){ |
|---|
| 157 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn); |
|---|
| 158 | }else{ |
|---|
| 159 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:"); |
|---|
| 160 | } |
|---|
| 161 | return 0; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if ( $arr ){ |
|---|
| 165 | $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC); |
|---|
| 166 | } else { |
|---|
| 167 | $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if ($this->conn->isError($result)){ |
|---|
| 171 | $this->send_err_mail($result, $n); |
|---|
| 172 | } |
|---|
| 173 | $this->result = $result; |
|---|
| 174 | |
|---|
| 175 | return $this->result; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | function autoExecute($table_name, $fields_values, $sql_where = null){ |
|---|
| 179 | |
|---|
| 180 | if ( $sql_where ) { |
|---|
| 181 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where); |
|---|
| 182 | } else { |
|---|
| 183 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | if ($this->conn->isError($result)){ |
|---|
| 187 | $this->send_err_mail($result, $n); |
|---|
| 188 | } |
|---|
| 189 | $this->result = $result; |
|---|
| 190 | return $this->result; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | function prepare($n){ |
|---|
| 195 | global $sql; |
|---|
| 196 | $sql = $n; |
|---|
| 197 | $result = $this->conn->prepare($n); |
|---|
| 198 | $this->result = $result; |
|---|
| 199 | return $this->result; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | function execute($n, $obj){ |
|---|
| 203 | global $sql; |
|---|
| 204 | $sql = $n; |
|---|
| 205 | $result = $this->conn->execute($n, $obj); |
|---|
| 206 | $this->result = $result; |
|---|
| 207 | return $this->result; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | function reset(){ |
|---|
| 211 | $this->conn->disconnect(); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | function send_err_mail($pearResult, $sql){ |
|---|
| 215 | require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_SystemError_Ex.php"); |
|---|
| 216 | |
|---|
| 217 | $objPage = new LC_Page_Error_SystemError_Ex(); |
|---|
| 218 | register_shutdown_function(array($objPage, "destroy")); |
|---|
| 219 | $objPage->init(); |
|---|
| 220 | $objPage->addDebugMsg($sql); |
|---|
| 221 | $objPage->pearResult = $pearResult; |
|---|
| 222 | GC_Utils_Ex::gfPrintLog($objPage->sfGetErrMsg()); |
|---|
| 223 | $objPage->process(); |
|---|
| 224 | |
|---|
| 225 | exit(); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** |
|---|
| 229 | * 直前に実行されたSQL文を取得する. |
|---|
| 230 | * |
|---|
| 231 | * @param boolean $disp trueの場合、画面出力を行う. |
|---|
| 232 | * @return string SQL文 |
|---|
| 233 | */ |
|---|
| 234 | function getLastQuery($disp = true) { |
|---|
| 235 | $sql = $this->conn->last_query; |
|---|
| 236 | if($disp) { |
|---|
| 237 | print($sql.";<br />\n"); |
|---|
| 238 | } |
|---|
| 239 | return $sql; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | ?> |
|---|