source: branches/comu-ver2/data/class/SC_DbConn.php @ 18538

Revision 18538, 8.4 KB checked in by nanasess, 14 years ago (diff)

merged r18500

  • エラーハンドリングの変更(#573, #567)
    • data/require_base.php で行っていた処理を data/app_initial.php, data/require_classes.php に分散
    • アプリケーション初期化時に, DBアクセスを伴わない data/require_safe.php を追加
    • システムエラー表示用の html/error.php を追加
    • エラーハンドリング処理を行う html/handle_error.php を追加
    • data/class/SC_DbConn.php でのエラーハンドリングを trigger_error に変更
    • エラーハンドリングの変更に伴い, html/require.php, html/mobile/require.php, html/admin/require.php を修正
  • Property svn:eol-style set to LF
  • 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 $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    /**
150     * クエリを実行し、全ての行を返す
151     *
152     * @param string $sql SQL クエリ
153     * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。
154     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。
155     * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。
156     */
157    function getAll($sql, $arrVal = "", $fetchmode = DB_FETCHMODE_ASSOC) {
158
159        // mysqlの場合にはビュー表を変換する
160        if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql);
161
162        // XXX このエラー処理はここで行なうべきなのか疑問。また、戻り値も疑問(なお、変更時はドキュメントも変更を)。
163        if (PEAR::isError($this->conn)) {
164            if (ADMIN_MODE) {
165                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn);
166            } else {
167                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:");
168            }
169            return 0;
170        }
171
172        if ($arrVal) { // FIXME 判定が曖昧
173            $result = $this->conn->getAll($sql, $arrVal, $fetchmode);
174        } else {
175            $result = $this->conn->getAll($sql, $fetchmode);
176        }
177
178        if ($this->conn->isError($result)) {
179            $this->send_err_mail($result, $sql);
180        }
181        $this->result = $result;
182
183        return $this->result;
184    }
185
186    function autoExecute($table_name, $fields_values, $sql_where = null){
187
188        if ( $sql_where ) {
189            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
190        } else {
191            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
192        }
193
194        if ($this->conn->isError($result)){
195            $this->send_err_mail($result, $n);
196        }
197        $this->result = $result;
198        return $this->result;
199    }
200
201
202    function prepare($n){
203        global $sql;
204        $sql = $n;
205        $result = $this->conn->prepare($n);
206        $this->result = $result;
207        return $this->result;
208    }
209
210    function execute($n, $obj){
211        global $sql;
212        $sql = $n;
213        $result = $this->conn->execute($n, $obj);
214        $this->result = $result;
215        return $this->result;
216    }
217
218    function reset(){
219        $this->conn->disconnect();
220    }
221
222    function send_err_mail($pearResult, $sql){
223
224        $errmsg = $sql . "\n\n";
225
226        // PEAR エラーを伴う場合
227        if (!is_null($pearResult)) {
228            $errmsg .= $pearResult->message . "\n\n";
229            $errmsg .= $pearResult->userinfo . "\n\n";
230            $errmsg .= SC_Utils_Ex::sfBacktraceToString($pearResult->backtrace);
231        }
232        // (上に該当せず)バックトレースを生成できる環境(一般的には PHP 4 >= 4.3.0, PHP 5)の場合
233        else if (function_exists("debug_backtrace")) {
234            $errmsg .= SC_Utils_Ex::sfBacktraceToString(array_slice(debug_backtrace(), 2));
235        }
236
237        GC_Utils_Ex::gfPrintLog($errmsg);
238        trigger_error($errmsg, E_USER_ERROR);
239        exit();
240    }
241
242    /**
243     * 直前に実行されたSQL文を取得する.
244     *
245     * @param boolean $disp trueの場合、画面出力を行う.
246     * @return string SQL文
247     */
248    function getLastQuery($disp = true) {
249        $sql = $this->conn->last_query;
250        if($disp) {
251            print($sql.";<br />\n");
252        }
253        return $sql;
254    }
255}
256?>
Note: See TracBrowser for help on using the repository browser.