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