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

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