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

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