source: branches/rel/data/class/SC_DbConn.php @ 12157

Revision 12157, 4.0 KB checked in by uehara, 17 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");
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   
23    // ¥³¥ó¥¹¥È¥é¥¯¥¿
24    function SC_DbConn($dsn = "", $err_disp = true, $new = false){
25        global $objDbConn;
26       
27        // Debug¥â¡¼¥É»ØÄê
28        $options['debug'] = PEAR_DB_DEBUG;
29        // ´û¤ËÀܳ¤µ¤ì¤Æ¤¤¤Ê¤¤¤«¡¢¿·µ¬ÀܳÍ×˾¤Î¾ì¹ç¤ÏÀܳ¤¹¤ë¡£
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   
49    // ¥¯¥¨¥ê¤Î¼Â¹Ô
50    function query($n ,$arr = "", $ignore_err = false){
51        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
52        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
53
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
68    // °ì·ï¤Î¤ß¼èÆÀ
69    function getOne($n, $arr = ""){
70       
71        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
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
89        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
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
104    // SELECTʸ¤Î¼Â¹Ô·ë²Ì¤òÁ´¤Æ¼èÆÀ
105    function getAll($n, $arr = ""){
106
107        // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
108        if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
109       
110        if(PEAR::isError($this->conn)) {
111            if(ADMIN_MODE){
112                sfErrorHeader("DB¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£:" . $this->dsn);
113            }else{
114                sfErrorHeader("DB¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£:");
115            }
116            return 0;
117        }
118
119        if ( $arr ){
120            $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
121        } else {
122            $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
123        }
124       
125        if ($this->conn->isError($result)){
126            $this->send_err_mail ($result, $n);
127        }
128        $this->result = $result;
129       
130        return $this->result;
131    }   
132   
133    function autoExecute($table_name, $fields_values, $sql_where = null){
134   
135        if ( $sql_where ) {
136            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
137        } else {
138            $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
139        }
140       
141        if ($this->conn->isError($result)){
142            $this->send_err_mail ($result, $n);
143        }
144        $this->result = $result;
145        return $this->result;
146    }
147   
148   
149    function prepare($n){
150        global $sql;
151        $sql = $n;     
152        $result = $this->conn->prepare($n);
153        $this->result = $result;
154        return $this->result;
155    }
156   
157    function execute($n, $obj){
158        global $sql;
159        $sql = $n;
160        $result = $this->conn->execute($n, $obj);
161        $this->result = $result;
162        return $this->result;
163    }   
164   
165    function reset(){
166        $this->conn->disconnect();
167    }
168
169    function send_err_mail( $result, $sql ){
170       
171        if ($this->err_disp) {
172            if ($_SERVER['HTTPS'] == "on") {
173                $url = "https://";
174            } else {
175                $url = "http://";
176            }
177            $url.= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
178           
179            $errmsg = $url."\n\n";
180            $errmsg.= $sql . "\n";
181            $errmsg.= $result->message . "\n\n";
182            $errmsg.= $result->userinfo . "\n\n";
183            print_r($errmsg);
184
185            exit();
186        }
187    }
188}
189
190?>
Note: See TracBrowser for help on using the repository browser.