source: branches/comu/data/class/SC_DbConn.php @ 15089

Revision 15089, 4.0 KB checked in by adati, 17 years ago (diff)

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

RevLine 
[5]1<?php
2/*
[15]3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
[5]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   
[11730]23    // ¥³¥ó¥¹¥È¥é¥¯¥¿
[5]24    function SC_DbConn($dsn = "", $err_disp = true, $new = false){
25        global $objDbConn;
26       
[11730]27        // Debug¥â¡¼¥É»ØÄê
[5]28        $options['debug'] = PEAR_DB_DEBUG;
[11730]29        // ´û¤ËÀܳ¤µ¤ì¤Æ¤¤¤Ê¤¤¤«¡¢¿·µ¬ÀܳÍ×˾¤Î¾ì¹ç¤ÏÀܳ¤¹¤ë¡£
[5]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   
[11730]49    // ¥¯¥¨¥ê¤Î¼Â¹Ô
[5]50    function query($n ,$arr = "", $ignore_err = false){
[11984]51        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
52        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
53
[5]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
[11730]68    // °ì·ï¤Î¤ß¼èÆÀ
[5]69    function getOne($n, $arr = ""){
70       
[11730]71        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
[5]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
[11730]89        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
[5]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
[11730]104    // SELECTʸ¤Î¼Â¹Ô·ë²Ì¤òÁ´¤Æ¼èÆÀ
[5]105    function getAll($n, $arr = ""){
106
[11730]107        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
[5]108        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
109       
[15089]110        if(PEAR::isError($this->conn)) {
111            sfErrorHeader("DB¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£");
112            gfPrintLog("couldn't connect : " . $this->dsn);
113            return 0;
114        }
[5]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 send_err_mail( $result, $sql ){
167       
168        if ($this->err_disp) {
169            if ($_SERVER['HTTPS'] == "on") {
170                $url = "https://";
171            } else {
172                $url = "http://";
173            }
174            $url.= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
175           
176            $errmsg = $url."\n\n";
177            $errmsg.= $sql . "\n";
178            $errmsg.= $result->message . "\n\n";
179            $errmsg.= $result->userinfo . "\n\n";
180            print_r($errmsg);
181
182            exit();
183        }
184    }
185}
186
187?>
Note: See TracBrowser for help on using the repository browser.