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

Revision 18234, 8.5 KB checked in by Seasoft, 15 years ago (diff)

#528(改行コードが混在している)

  • SVN属性の変更。ただし、以下は除外。
    • /data/module
    • /data/pdf
    • /html/test
  • 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 $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       
65        if (DB_TYPE == 'mysql') {
66            $objDbConn->query('SET NAMES utf8');
67        }
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    /**
115     * クエリを実行し、最初の行を返す
116     *
117     * @param string $sql SQL クエリ
118     * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。
119     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。
120     * @return array データを含む1次元配列。失敗した場合に DB_Error オブジェクトを返します。
121     */
122    function getRow($sql, $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) {
123       
124        // mysqlの場合にはビュー表を変換する
125        if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql);
126       
127        $result = $this->conn->getRow($sql, $arrVal ,$fetchmode);
128       
129        if ($this->conn->isError($result)){
130            $this->send_err_mail($result ,$sql);
131        }
132        $this->result = $result;
133        return $this->result;
134    }
135
136    function getCol($n, $col, $arr = "") {
137
138        // mysqlの場合にはビュー表を変換する
139        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
140
141        if ($arr) {
142            $result = $this->conn->getCol($n, $col, $arr);
143        } else {
144            $result = $this->conn->getCol($n, $col);
145        }
146        if ($this->conn->isError($result)) {
147            $this->send_err_mail($result, $n);
148        }
149        $this->result = $result;
150        return $this->result;
151    }
152
153    // SELECT文の実行結果を全て取得
154    function getAll($n, $arr = ""){
155
156        // mysqlの場合にはビュー表を変換する
157        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
158
159        if(PEAR::isError($this->conn)) {
160            if(ADMIN_MODE){
161                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn);
162            }else{
163                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:");
164            }
165            return 0;
166        }
167
168        if ( $arr ){
169            $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
170        } else {
171            $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
172        }
173
174        if ($this->conn->isError($result)){
175            $this->send_err_mail($result, $n);
176        }
177        $this->result = $result;
178
179        return $this->result;
180    }
181
182    function autoExecute($table_name, $fields_values, $sql_where = null){
183
184        if ( $sql_where ) {
185            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
186        } else {
187            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
188        }
189
190        if ($this->conn->isError($result)){
191            $this->send_err_mail($result, $n);
192        }
193        $this->result = $result;
194        return $this->result;
195    }
196
197
198    function prepare($n){
199        global $sql;
200        $sql = $n;
201        $result = $this->conn->prepare($n);
202        $this->result = $result;
203        return $this->result;
204    }
205
206    function execute($n, $obj){
207        global $sql;
208        $sql = $n;
209        $result = $this->conn->execute($n, $obj);
210        $this->result = $result;
211        return $this->result;
212    }
213
214    function reset(){
215        $this->conn->disconnect();
216    }
217
218    function send_err_mail($result, $sql){
219        $url = '';
220        $errmsg = '';
221
222        if (SC_Utils_Ex::sfIsHTTPS()) {
223            $url = "https://";
224        } else {
225            $url = "http://";
226        }
227        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
228
229        $errmsg = $url."\n\n";
230        $errmsg.= "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . "\n";
231        $errmsg.= "REMOTE_ADDR:" . $_SERVER['REMOTE_ADDR'] . "\n";
232        $errmsg.= "USER_AGENT:" . $_SERVER['HTTP_USER_AGENT'] . "\n\n";
233        $errmsg.= $sql . "\n";
234        $errmsg.= $result->message . "\n\n";
235        $errmsg.= $result->userinfo . "\n\n";
236
237        $arrRbacktrace = array_reverse($result->backtrace);
238
239        foreach($arrRbacktrace as $backtrace) {
240            if($backtrace['class'] != "") {
241                $func = $backtrace['class'] . "->" . $backtrace['function'];
242            } else {
243                $func = $backtrace['function'];
244            }
245
246            $errmsg.= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n";
247        }
248
249        require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_SystemError_Ex.php");
250
251        $objPage = new LC_Page_Error_SystemError_Ex();
252        register_shutdown_function(array($objPage, "destroy"));
253        $objPage->init();
254        $objPage->process();
255
256        if (DEBUG_MODE == true) {
257            print('<pre>');
258            print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE));
259            print('</pre>');
260        }
261
262        GC_Utils_Ex::gfPrintLog($errmsg);
263
264        exit();
265    }
266
267    /**
268     * 直前に実行されたSQL文を取得する.
269     *
270     * @param boolean $disp trueの場合、画面出力を行う.
271     * @return string SQL文
272     */
273    function getLastQuery($disp = true) {
274        $sql = $this->conn->last_query;
275        if($disp) {
276            print($sql.";<br />\n");
277        }
278        return $sql;
279    }
280}
281
282?>
Note: See TracBrowser for help on using the repository browser.