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

Revision 17345, 7.6 KB checked in by adachi, 16 years ago (diff)

Exクラスを使用するように修正

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