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

Revision 15920, 4.8 KB checked in by kakinaka, 19 years ago (diff)

mdb2テスト

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