source: branches/dev/data/class/SC_DbConn.php @ 15938

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