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

Revision 17439, 7.7 KB checked in by naka, 16 years ago (diff)

require.phpを統合

  • 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        $this->conn = $objDbConn;
66        $this->error_mail_to = DB_ERROR_MAIL_TO;
67        $this->error_mail_title = DB_ERROR_MAIL_SUBJECT;
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    function getRow($n, $arr = ""){
111
112        // mysqlの場合にはビュー表を変換する
113        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
114
115        if ( $arr ) {
116            $result = $this->conn->getRow($n, $arr);
117        } else {
118            $result = $this->conn->getRow($n);
119        }
120        if ($this->conn->isError($result)){
121            $this->send_err_mail ($result ,$n);
122        }
123        $this->result = $result;
124        return $this->result;
125    }
126
127    function getCol($n, $col, $arr = "") {
128
129        // mysqlの場合にはビュー表を変換する
130        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
131
132        if ($arr) {
133            $result = $this->conn->getCol($n, $col, $arr);
134        } else {
135            $result = $this->conn->getCol($n, $col);
136        }
137        if ($this->conn->isError($result)) {
138            $this->send_err_mail($result, $n);
139        }
140        $this->result = $result;
141        return $this->result;
142    }
143
144    // SELECT文の実行結果を全て取得
145    function getAll($n, $arr = ""){
146
147        // mysqlの場合にはビュー表を変換する
148        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
149
150        if(PEAR::isError($this->conn)) {
151            if(ADMIN_MODE){
152                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn);
153            }else{
154                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:");
155            }
156            return 0;
157        }
158
159        if ( $arr ){
160            $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
161        } else {
162            $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
163        }
164
165        if ($this->conn->isError($result)){
166            $this->send_err_mail ($result, $n);
167        }
168        $this->result = $result;
169
170        return $this->result;
171    }
172
173    function autoExecute($table_name, $fields_values, $sql_where = null){
174
175        if ( $sql_where ) {
176            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
177        } else {
178            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
179        }
180
181        if ($this->conn->isError($result)){
182            $this->send_err_mail ($result, $n);
183        }
184        $this->result = $result;
185        return $this->result;
186    }
187
188
189    function prepare($n){
190        global $sql;
191        $sql = $n;
192        $result = $this->conn->prepare($n);
193        $this->result = $result;
194        return $this->result;
195    }
196
197    function execute($n, $obj){
198        global $sql;
199        $sql = $n;
200        $result = $this->conn->execute($n, $obj);
201        $this->result = $result;
202        return $this->result;
203    }
204
205    function reset(){
206        $this->conn->disconnect();
207    }
208
209    function send_err_mail($result, $sql){
210        $url = '';
211        $errmsg = '';
212
213        if (SC_Utils_Ex::sfIsHTTPS()) {
214            $url = "https://";
215        } else {
216            $url = "http://";
217        }
218        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
219
220        $errmsg = $url."\n\n";
221        $errmsg.= "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . "\n";
222        $errmsg.= "REMOTE_ADDR:" . $_SERVER['REMOTE_ADDR'] . "\n";
223        $errmsg.= "USER_AGENT:" . $_SERVER['HTTP_USER_AGENT'] . "\n\n";
224        $errmsg.= $sql . "\n";
225        $errmsg.= $result->message . "\n\n";
226        $errmsg.= $result->userinfo . "\n\n";
227
228        $arrRbacktrace = array_reverse($result->backtrace);
229
230        foreach($arrRbacktrace as $backtrace) {
231            if($backtrace['class'] != "") {
232                $func = $backtrace['class'] . "->" . $backtrace['function'];
233            } else {
234                $func = $backtrace['function'];
235            }
236
237            $errmsg.= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n";
238        }
239
240        require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_SystemError_Ex.php");
241
242        $objPage = new LC_Page_Error_SystemError_Ex();
243        register_shutdown_function(array($objPage, "destroy"));
244        $objPage->init();
245        $objPage->process();
246
247        if (DEBUG_MODE == true) {
248            print('<pre>');
249            print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE));
250            print('</pre>');
251        }
252
253        GC_Utils_Ex::gfPrintLog($errmsg);
254
255        exit();
256    }
257}
258
259?>
Note: See TracBrowser for help on using the repository browser.