source: branches/version-2/data/class/SC_DbConn.php @ 18432

Revision 18432, 7.9 KB checked in by kajiwara, 14 years ago (diff)

EC-CUBE Ver2.4.2 分コミット。詳細はこちら( http://www.ec-cube.net/release/detail.php?release_id=207

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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__));
25require_once($current_dir . "/../module/DB.php");
26require_once($current_dir . "/util/SC_Utils.php");
27require_once($current_dir . "/util/GC_Utils.php");
28$objDbConn = "";
29
30class SC_DbConn{
31
32    var $conn;
33    var $result;
34    var $includePath;
35    var $error_mail_to;
36    var $error_mail_title;
37    var $dsn;
38    var $err_disp = true;
39    var $dbFactory;
40
41
42    // コンストラクタ
43    function SC_DbConn($dsn = "", $err_disp = true, $new = false){
44        global $objDbConn;
45
46        // Debugモード指定
47        $options['debug'] = PEAR_DB_DEBUG;
48        // 持続的接続オプション
49        $options['persistent'] = PEAR_DB_PERSISTENT;
50
51        // 既に接続されていないか、新規接続要望の場合は接続する。
52        if(!isset($objDbConn->connection) || $new) {
53            if($dsn != "") {
54                $objDbConn = DB::connect($dsn, $options);
55                $this->dsn = $dsn;
56            } else {
57                if(defined('DEFAULT_DSN')) {
58                    $objDbConn = DB::connect(DEFAULT_DSN, $options);
59                    $this->dsn = DEFAULT_DSN;
60                } else {
61                    return;
62                }
63            }
64        }
65        //MySQL文字化け対策(MySQLで文字化けする場合は以下のコメントアウトをはずして動作確認してみてください。)
66        //if (DB_TYPE == 'mysql') {
67        //    $objDbConn->query('SET NAMES utf8');
68        //}
69        $this->conn = $objDbConn;
70        $this->error_mail_to = DB_ERROR_MAIL_TO;
71        $this->error_mail_title = DB_ERROR_MAIL_SUBJECT;
72        $this->err_disp = $err_disp;
73        $this->dbFactory = SC_DB_DBFactory_Ex::getInstance();
74    }
75
76    // クエリの実行
77    function query($n ,$arr = "", $ignore_err = false){
78        // mysqlの場合にはビュー表を変換する
79        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
80
81        if ( $arr ) {
82            $result = $this->conn->query($n, $arr);
83        } else {
84            $result = $this->conn->query($n);
85        }
86
87        if ($this->conn->isError($result) && !$ignore_err){
88            $this->send_err_mail ($result, $n);
89        }
90
91        $this->result = $result;
92        return $this->result;
93    }
94
95    // 一件のみ取得
96    function getOne($n, $arr = ""){
97
98        // mysqlの場合にはビュー表を変換する
99        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
100
101        if ( $arr ) {
102            $result = $this->conn->getOne($n, $arr);
103        } else {
104            $result = $this->conn->getOne($n);
105        }
106        if ($this->conn->isError($result)){
107            $this->send_err_mail ($result ,$n);
108        }
109        $this->result = $result;
110
111        return $this->result;
112    }
113
114    function getRow($n, $arr = ""){
115
116        // mysqlの場合にはビュー表を変換する
117        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
118
119        if ( $arr ) {
120            $result = $this->conn->getRow($n, $arr);
121        } else {
122            $result = $this->conn->getRow($n);
123        }
124        if ($this->conn->isError($result)){
125            $this->send_err_mail ($result ,$n);
126        }
127        $this->result = $result;
128        return $this->result;
129    }
130
131    function getCol($n, $col, $arr = "") {
132
133        // mysqlの場合にはビュー表を変換する
134        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
135
136        if ($arr) {
137            $result = $this->conn->getCol($n, $col, $arr);
138        } else {
139            $result = $this->conn->getCol($n, $col);
140        }
141        if ($this->conn->isError($result)) {
142            $this->send_err_mail($result, $n);
143        }
144        $this->result = $result;
145        return $this->result;
146    }
147
148    // SELECT文の実行結果を全て取得
149    function getAll($n, $arr = ""){
150
151        // mysqlの場合にはビュー表を変換する
152        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
153
154        if(PEAR::isError($this->conn)) {
155            if(ADMIN_MODE){
156                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn);
157            }else{
158                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:");
159            }
160            return 0;
161        }
162
163        if ( $arr ){
164            $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
165        } else {
166            $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
167        }
168
169        if ($this->conn->isError($result)){
170            $this->send_err_mail ($result, $n);
171        }
172        $this->result = $result;
173
174        return $this->result;
175    }
176
177    function autoExecute($table_name, $fields_values, $sql_where = null){
178
179        if ( $sql_where ) {
180            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
181        } else {
182            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
183        }
184
185        if ($this->conn->isError($result)){
186            $this->send_err_mail ($result, $n);
187        }
188        $this->result = $result;
189        return $this->result;
190    }
191
192
193    function prepare($n){
194        global $sql;
195        $sql = $n;
196        $result = $this->conn->prepare($n);
197        $this->result = $result;
198        return $this->result;
199    }
200
201    function execute($n, $obj){
202        global $sql;
203        $sql = $n;
204        $result = $this->conn->execute($n, $obj);
205        $this->result = $result;
206        return $this->result;
207    }
208
209    function reset(){
210        $this->conn->disconnect();
211    }
212
213    function send_err_mail($result, $sql){
214        $url = '';
215        $errmsg = '';
216
217        if (SC_Utils_Ex::sfIsHTTPS()) {
218            $url = "https://";
219        } else {
220            $url = "http://";
221        }
222        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
223
224        $errmsg = $url."\n\n";
225        $errmsg.= "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . "\n";
226        $errmsg.= "REMOTE_ADDR:" . $_SERVER['REMOTE_ADDR'] . "\n";
227        $errmsg.= "USER_AGENT:" . $_SERVER['HTTP_USER_AGENT'] . "\n\n";
228        $errmsg.= $sql . "\n";
229        $errmsg.= $result->message . "\n\n";
230        $errmsg.= $result->userinfo . "\n\n";
231
232        $arrRbacktrace = array_reverse($result->backtrace);
233
234        foreach($arrRbacktrace as $backtrace) {
235            if($backtrace['class'] != "") {
236                $func = $backtrace['class'] . "->" . $backtrace['function'];
237            } else {
238                $func = $backtrace['function'];
239            }
240
241            $errmsg.= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n";
242        }
243
244        require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_SystemError_Ex.php");
245
246        $objPage = new LC_Page_Error_SystemError_Ex();
247        register_shutdown_function(array($objPage, "destroy"));
248        $objPage->init();
249        $objPage->process();
250
251        if (DEBUG_MODE == true) {
252            print('<pre>');
253            print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE));
254            print('</pre>');
255        }
256
257        GC_Utils_Ex::gfPrintLog($errmsg);
258
259        exit();
260    }
261}
262
263?>
Note: See TracBrowser for help on using the repository browser.