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

Revision 18593, 8.4 KB checked in by Seasoft, 14 years ago (diff)

ソース整形

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