source: branches/feature-module-update/data/class/SC_DbConn.php @ 16582

Revision 16582, 6.6 KB checked in by nanasess, 16 years ago (diff)

ライセンス表記変更

  • 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        if(!isset($objDbConn->connection) || $new) {
49            if($dsn != "") {
50                $objDbConn = DB::connect($dsn, $options);
51                $this->dsn = $dsn;
52            } else {
53                if(defined('DEFAULT_DSN')) {
54                    $objDbConn = DB::connect(DEFAULT_DSN, $options);
55                    $this->dsn = DEFAULT_DSN;
56                } else {
57                    return;
58                }
59            }
60        }
61        $this->conn = $objDbConn;
62        $this->error_mail_to = DB_ERROR_MAIL_TO;
63        $this->error_mail_title = DB_ERROR_MAIL_SUBJECT;
64        $this->err_disp = $err_disp;
65        $this->dbFactory = SC_DB_DBFactory::getInstance();
66    }
67
68    // クエリの実行
69    function query($n ,$arr = "", $ignore_err = false){
70        // mysqlの場合にはビュー表を変換する
71        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
72
73        if ( $arr ) {
74            $result = $this->conn->query($n, $arr);
75        } else {
76            $result = $this->conn->query($n);
77        }
78
79        if ($this->conn->isError($result) && !$ignore_err){
80            $this->send_err_mail ($result, $n);
81        }
82
83        $this->result = $result;
84        return $this->result;
85    }
86
87    // 一件のみ取得
88    function getOne($n, $arr = ""){
89
90        // mysqlの場合にはビュー表を変換する
91        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
92
93        if ( $arr ) {
94            $result = $this->conn->getOne($n, $arr);
95        } else {
96            $result = $this->conn->getOne($n);
97        }
98        if ($this->conn->isError($result)){
99            $this->send_err_mail ($result ,$n);
100        }
101        $this->result = $result;
102
103        return $this->result;
104    }
105
106    function getRow($n, $arr = ""){
107
108        // mysqlの場合にはビュー表を変換する
109        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
110
111        if ( $arr ) {
112            $result = $this->conn->getRow($n, $arr);
113        } else {
114            $result = $this->conn->getRow($n);
115        }
116        if ($this->conn->isError($result)){
117            $this->send_err_mail ($result ,$n);
118        }
119        $this->result = $result;
120        return $this->result;
121    }
122
123    function getCol($n, $col, $arr = "") {
124
125        // mysqlの場合にはビュー表を変換する
126        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
127
128        if ($arr) {
129            $result = $this->conn->getCol($n, $col, $arr);
130        } else {
131            $result = $this->conn->getCol($n, $col);
132        }
133        if ($this->conn->isError($result)) {
134            $this->send_err_mail($result, $n);
135        }
136        $this->result = $result;
137        return $this->result;
138    }
139
140    // SELECT文の実行結果を全て取得
141    function getAll($n, $arr = ""){
142
143        // mysqlの場合にはビュー表を変換する
144        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
145
146        if(PEAR::isError($this->conn)) {
147            if(ADMIN_MODE){
148                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn);
149            }else{
150                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:");
151            }
152            return 0;
153        }
154
155        if ( $arr ){
156            $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
157        } else {
158            $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
159        }
160
161        if ($this->conn->isError($result)){
162            $this->send_err_mail ($result, $n);
163        }
164        $this->result = $result;
165
166        return $this->result;
167    }
168
169    function autoExecute($table_name, $fields_values, $sql_where = null){
170
171        if ( $sql_where ) {
172            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
173        } else {
174            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
175        }
176
177        if ($this->conn->isError($result)){
178            $this->send_err_mail ($result, $n);
179        }
180        $this->result = $result;
181        return $this->result;
182    }
183
184
185    function prepare($n){
186        global $sql;
187        $sql = $n;
188        $result = $this->conn->prepare($n);
189        $this->result = $result;
190        return $this->result;
191    }
192
193    function execute($n, $obj){
194        global $sql;
195        $sql = $n;
196        $result = $this->conn->execute($n, $obj);
197        $this->result = $result;
198        return $this->result;
199    }
200
201    function reset(){
202        $this->conn->disconnect();
203    }
204
205    function send_err_mail($result, $sql){
206        $url = '';
207        $errmsg = '';
208
209        if (SC_Utils_Ex::sfIsHTTPS()) {
210            $url = "https://";
211        } else {
212            $url = "http://";
213        }
214        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
215
216        $errmsg  = $url."\n\n";
217        $errmsg .= $sql . "\n";
218        $errmsg .= $result->message . "\n\n";
219        $errmsg .= $result->userinfo . "\n\n";
220
221        if ($this->err_disp && DEBUG_MODE === true) {
222            print('<pre>');
223            print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE));
224            print('</pre>');
225        }
226
227        GC_Utils_Ex::gfDebugLog($errmsg);
228
229        exit();
230    }
231}
232
233?>
Note: See TracBrowser for help on using the repository browser.