source: branches/beta/data/class/SC_DbConn.php @ 15088

Revision 15088, 4.5 KB checked in by adati, 17 years ago (diff)

DB接続失敗時にDSN情報を表示しないように修正

Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8$current_dir = realpath(dirname(__FILE__));
9require_once($current_dir . "/../module/DB.php");
10
11$objDbConn = "";
12
13class SC_DbConn{
14
15    var $conn;
16    var $result;
17    var $includePath;
18    var $error_mail_to;
19    var $error_mail_title;
20    var $dsn;
21    var $err_disp = true;
22   
23    // ¥³¥ó¥¹¥È¥é¥¯¥¿
24    function SC_DbConn($dsn = "", $err_disp = true, $new = false){
25        global $objDbConn;
26       
27        // Debug¥â¡¼¥É»ØÄê
28        $options['debug'] = PEAR_DB_DEBUG;
29        // ´û¤ËÀܳ¤µ¤ì¤Æ¤¤¤Ê¤¤¤«¡¢¿·µ¬ÀܳÍ×˾¤Î¾ì¹ç¤ÏÀܳ¤¹¤ë¡£
30        if(!isset($objDbConn->connection) || $new) {
31            if($dsn != "") {
32                $objDbConn = DB::connect($dsn, $options);
33                $this->dsn = $dsn;
34            } else {
35                if(defined('DEFAULT_DSN')) {
36                    $objDbConn = DB::connect(DEFAULT_DSN, $options);
37                    $this->dsn = DEFAULT_DSN;
38                } else {
39                    return;
40                }
41            }
42        }
43        $this->conn = $objDbConn;
44        $this->error_mail_to = DB_ERROR_MAIL_TO;
45        $this->error_mail_title = DB_ERROR_MAIL_SUBJECT;
46        $this->err_disp = $err_disp;
47    }
48   
49    // ¥¯¥¨¥ê¤Î¼Â¹Ô
50    function query($n ,$arr = "", $ignore_err = false){
51        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
52        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
53
54        if ( $arr ) {
55            $result = $this->conn->query($n, $arr);
56        } else {
57            $result = $this->conn->query($n);
58        }
59   
60        if ($this->conn->isError($result) && !$ignore_err){
61            $this->send_err_mail ($result, $n);
62        }
63       
64        $this->result = $result;
65        return $this->result;
66    }
67
68    // °ì·ï¤Î¤ß¼èÆÀ
69    function getOne($n, $arr = ""){
70       
71        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
72        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
73       
74        if ( $arr ) {
75            $result = $this->conn->getOne($n, $arr);
76        } else {
77            $result = $this->conn->getOne($n);
78        }       
79        if ($this->conn->isError($result)){
80            $this->send_err_mail ($result ,$n);
81        }
82        $this->result = $result;
83       
84        return $this->result;
85    }
86   
87    function getRow($n, $arr = ""){
88
89        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
90        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
91
92        if ( $arr ) {
93            $result = $this->conn->getRow($n, $arr);
94        } else {
95            $result = $this->conn->getRow($n);
96        }       
97        if ($this->conn->isError($result)){
98            $this->send_err_mail ($result ,$n);
99        }
100        $this->result = $result;
101        return $this->result;
102    }
103
104    // SELECTʸ¤Î¼Â¹Ô·ë²Ì¤òÁ´¤Æ¼èÆÀ
105    function getAll($n, $arr = ""){
106
107        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
108        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
109       
110        if(PEAR::isError($this->conn)) {
111            sfErrorHeader("DB¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£");
112            gfPrintLog("couldn't connect : " . $this->dsn);
113            return 0;
114        }
115
116        if ( $arr ){
117            $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
118        } else {
119            $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
120        }
121       
122        if ($this->conn->isError($result)){
123            $this->send_err_mail ($result, $n);
124        }
125        $this->result = $result;
126       
127        return $this->result;
128    }   
129   
130    function autoExecute($table_name, $fields_values, $sql_where = null){
131   
132        if ( $sql_where ) {
133            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
134        } else {
135            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
136        }
137       
138        if ($this->conn->isError($result)){
139            $this->send_err_mail ($result, $n);
140        }
141        $this->result = $result;
142        return $this->result;
143    }
144   
145   
146    function prepare($n){
147        global $sql;
148        $sql = $n;     
149        $result = $this->conn->prepare($n);
150        $this->result = $result;
151        return $this->result;
152    }
153   
154    function execute($n, $obj){
155        global $sql;
156        $sql = $n;
157        $result = $this->conn->execute($n, $obj);
158        $this->result = $result;
159        return $this->result;
160    }   
161   
162    function reset(){
163        $this->conn->disconnect();
164    }
165
166    function debug_print($result, $sql){
167        $this->send_err_mail($result, $sql);
168    }
169   
170    function send_err_mail($result, $sql){
171        $url = '';
172        $errmsg = '';
173       
174        if ($_SERVER['HTTPS'] == "on") {
175            $url = "https://";
176        } else {
177            $url = "http://";
178        }
179        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
180       
181        $errmsg  = $url."\n\n";
182        $errmsg .= $sql . "\n";
183        $errmsg .= $result->message . "\n\n";
184        $errmsg .= $result->userinfo . "\n\n";
185       
186        if ($this->err_disp && DEBUG_MODE === true) {
187            print('<pre>');
188            print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE));
189            print('</pre>');
190        }
191       
192        gfDebugLog($errmsg, DB_ERR_LOG_PATH);
193       
194        exit();
195    }
196}
197
198?>
Note: See TracBrowser for help on using the repository browser.