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

Revision 18701, 8.8 KB checked in by nanasess, 14 years ago (diff)

Copyright の更新(#601)

  • 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-2010 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$g_arr_objDbConn = array();
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 $g_arr_objDbConn;
42
43        // Debugモード指定
44        $options['debug'] = PEAR_DB_DEBUG;
45        // 持続的接続オプション
46        $options['persistent'] = PEAR_DB_PERSISTENT;
47
48        if (strlen($dsn) >= 1) {
49            $this->dsn = $dsn;
50        } elseif (defined('DEFAULT_DSN')) {
51            $this->dsn = DEFAULT_DSN;
52        } else {
53            // XXX 以前の仕様を継承しているが、意図が良く分からない。(2010/03/03 Seasoft 塚田)
54            return;
55        }
56
57        // 既に接続されていないか、新規接続要望の場合は接続する。
58        if (!isset($g_arr_objDbConn[$this->dsn]) || !isset($g_arr_objDbConn[$this->dsn]->connection)) {
59            $new = true;
60        }
61
62        if ($new) {
63            $this->conn = DB::connect($this->dsn, $options);
64            $g_arr_objDbConn[$this->dsn] = $this->conn;
65
66            if (DB_TYPE == 'mysql') {
67                $g_arr_objDbConn[$this->dsn]->query('SET NAMES utf8'); // FIXME mysql_set_charset を使える環境では、その方が良さそう (2010/03/03 Seasoft 塚田)
68                $g_arr_objDbConn[$this->dsn]->query("SET SESSION sql_mode = 'ANSI'");
69            }
70        } else {
71            $this->conn = $g_arr_objDbConn[$this->dsn];
72        }
73
74        $this->err_disp = $err_disp;
75        $this->dbFactory = SC_DB_DBFactory_Ex::getInstance();
76    }
77
78    // クエリの実行
79    function query($n ,$arr = "", $ignore_err = false){
80        // mysqlの場合にはビュー表を変換する
81        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
82
83        if ( $arr ) {
84            $result = $this->conn->query($n, $arr);
85        } else {
86            $result = $this->conn->query($n);
87        }
88
89        if ($this->conn->isError($result) && !$ignore_err){
90            $this->send_err_mail($result, $n);
91        }
92
93        $this->result = $result;
94        return $this->result;
95    }
96
97    // 一件のみ取得
98    function getOne($n, $arr = ""){
99
100        // mysqlの場合にはビュー表を変換する
101        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
102
103        if ( $arr ) {
104            $result = $this->conn->getOne($n, $arr);
105        } else {
106            $result = $this->conn->getOne($n);
107        }
108        if ($this->conn->isError($result)){
109            $this->send_err_mail($result ,$n);
110        }
111        $this->result = $result;
112
113        return $this->result;
114    }
115   
116    /**
117     * クエリを実行し、最初の行を返す
118     *
119     * @param string $sql SQL クエリ
120     * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。
121     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。
122     * @return array データを含む1次元配列。失敗した場合に DB_Error オブジェクトを返します。
123     */
124    function getRow($sql, $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) {
125       
126        // mysqlの場合にはビュー表を変換する
127        if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql);
128       
129        $result = $this->conn->getRow($sql, $arrVal ,$fetchmode);
130       
131        if ($this->conn->isError($result)){
132            $this->send_err_mail($result ,$sql);
133        }
134        $this->result = $result;
135        return $this->result;
136    }
137
138    function getCol($n, $col, $arr = "") {
139
140        // mysqlの場合にはビュー表を変換する
141        if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n);
142
143        if ($arr) {
144            $result = $this->conn->getCol($n, $col, $arr);
145        } else {
146            $result = $this->conn->getCol($n, $col);
147        }
148        if ($this->conn->isError($result)) {
149            $this->send_err_mail($result, $n);
150        }
151        $this->result = $result;
152        return $this->result;
153    }
154
155    /**
156     * クエリを実行し、全ての行を返す
157     *
158     * @param string $sql SQL クエリ
159     * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。
160     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。
161     * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。
162     */
163    function getAll($sql, $arrVal = "", $fetchmode = DB_FETCHMODE_ASSOC) {
164
165        // mysqlの場合にはビュー表を変換する
166        if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql);
167
168        // XXX このエラー処理はここで行なうべきなのか疑問。また、戻り値も疑問(なお、変更時はドキュメントも変更を)。
169        if (PEAR::isError($this->conn)) {
170            if (ADMIN_MODE) {
171                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn);
172            } else {
173                SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:");
174            }
175            return 0;
176        }
177
178        if ($arrVal) { // FIXME 判定が曖昧
179            $result = $this->conn->getAll($sql, $arrVal, $fetchmode);
180        } else {
181            $result = $this->conn->getAll($sql, $fetchmode);
182        }
183
184        if ($this->conn->isError($result)) {
185            $this->send_err_mail($result, $sql);
186        }
187        $this->result = $result;
188
189        return $this->result;
190    }
191
192    function autoExecute($table_name, $fields_values, $sql_where = null){
193
194        if ( $sql_where ) {
195            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
196        } else {
197            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
198        }
199
200        if ($this->conn->isError($result)){
201            $this->send_err_mail($result, $n);
202        }
203        $this->result = $result;
204        return $this->result;
205    }
206
207
208    function prepare($n){
209        global $sql;
210        $sql = $n;
211        $result = $this->conn->prepare($n);
212        $this->result = $result;
213        return $this->result;
214    }
215
216    function execute($n, $obj){
217        global $sql;
218        $sql = $n;
219        $result = $this->conn->execute($n, $obj);
220        $this->result = $result;
221        return $this->result;
222    }
223
224    function reset(){
225        $this->conn->disconnect();
226    }
227
228    function send_err_mail($pearResult, $sql){
229
230        $errmsg = $sql . "\n\n";
231
232        // PEAR エラーを伴う場合
233        if (!is_null($pearResult)) {
234            $errmsg .= $pearResult->message . "\n\n";
235            $errmsg .= $pearResult->userinfo . "\n\n";
236            $errmsg .= SC_Utils_Ex::sfBacktraceToString($pearResult->backtrace);
237        }
238        // (上に該当せず)バックトレースを生成できる環境(一般的には PHP 4 >= 4.3.0, PHP 5)の場合
239        else if (function_exists("debug_backtrace")) {
240            $errmsg .= SC_Utils_Ex::sfBacktraceToString(array_slice(debug_backtrace(), 2));
241        }
242
243        GC_Utils_Ex::gfPrintLog($errmsg);
244        trigger_error($errmsg, E_USER_ERROR);
245        exit();
246    }
247
248    /**
249     * 直前に実行されたSQL文を取得する.
250     *
251     * @param boolean $disp trueの場合、画面出力を行う.
252     * @return string SQL文
253     */
254    function getLastQuery($disp = true) {
255        $sql = $this->conn->last_query;
256        if($disp) {
257            print($sql.";<br />\n");
258        }
259        return $sql;
260    }
261}
262?>
Note: See TracBrowser for help on using the repository browser.