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 | |
---|
11 | $objDbConn = ""; |
---|
12 | |
---|
13 | class 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 | sfErrorHeader("DBへの接続に失敗しました。"); |
---|
112 | gfPrintLog("couldn't connect : " . $this->dsn); |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | if ( $arr ){ |
---|
117 | $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC); |
---|
118 | } else { |
---|
119 | $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC); |
---|
120 | } |
---|
121 | |
---|
122 | if ($this->conn->isError($result)){ |
---|
123 | $this->send_err_mail ($result, $n); |
---|
124 | } |
---|
125 | $this->result = $result; |
---|
126 | |
---|
127 | return $this->result; |
---|
128 | } |
---|
129 | |
---|
130 | function autoExecute($table_name, $fields_values, $sql_where = null){ |
---|
131 | |
---|
132 | if ( $sql_where ) { |
---|
133 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where); |
---|
134 | } else { |
---|
135 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT); |
---|
136 | } |
---|
137 | |
---|
138 | if ($this->conn->isError($result)){ |
---|
139 | $this->send_err_mail ($result, $n); |
---|
140 | } |
---|
141 | $this->result = $result; |
---|
142 | return $this->result; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | function prepare($n){ |
---|
147 | global $sql; |
---|
148 | $sql = $n; |
---|
149 | $result = $this->conn->prepare($n); |
---|
150 | $this->result = $result; |
---|
151 | return $this->result; |
---|
152 | } |
---|
153 | |
---|
154 | function execute($n, $obj){ |
---|
155 | global $sql; |
---|
156 | $sql = $n; |
---|
157 | $result = $this->conn->execute($n, $obj); |
---|
158 | $this->result = $result; |
---|
159 | return $this->result; |
---|
160 | } |
---|
161 | |
---|
162 | function reset(){ |
---|
163 | $this->conn->disconnect(); |
---|
164 | } |
---|
165 | |
---|
166 | function send_err_mail($result, $sql){ |
---|
167 | $url = ''; |
---|
168 | $errmsg = ''; |
---|
169 | |
---|
170 | if ($_SERVER['HTTPS'] == "on") { |
---|
171 | $url = "https://"; |
---|
172 | } else { |
---|
173 | $url = "http://"; |
---|
174 | } |
---|
175 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
---|
176 | |
---|
177 | $errmsg = $url."\n\n"; |
---|
178 | $errmsg .= $sql . "\n"; |
---|
179 | $errmsg .= $result->message . "\n\n"; |
---|
180 | $errmsg .= $result->userinfo . "\n\n"; |
---|
181 | |
---|
182 | if ($this->err_disp && DEBUG_MODE === true) { |
---|
183 | print('<pre>'); |
---|
184 | print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE)); |
---|
185 | print('</pre>'); |
---|
186 | } |
---|
187 | |
---|
188 | gfDebugLog($errmsg); |
---|
189 | |
---|
190 | exit(); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | ?> |
---|