1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | /** |
---|
25 | * SQLの構築・実行を行う |
---|
26 | * |
---|
27 | * @author LOCKON CO.,LTD. |
---|
28 | * @version $Id$ |
---|
29 | */ |
---|
30 | class SC_Query { |
---|
31 | var $option; |
---|
32 | var $where; |
---|
33 | var $conn; |
---|
34 | var $groupby; |
---|
35 | var $order; |
---|
36 | |
---|
37 | /** |
---|
38 | * コンストラクタ. |
---|
39 | * |
---|
40 | * @param $dsn |
---|
41 | * @param boolean $err_disp エラー表示を行うかどうか |
---|
42 | * @param boolean $new 新規に接続を行うかどうか |
---|
43 | * @return SC_Query |
---|
44 | */ |
---|
45 | function SC_Query($dsn = "", $err_disp = true, $new = false) { |
---|
46 | $this->conn = new SC_DBconn($dsn, $err_disp, $new); |
---|
47 | $this->where = ""; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * エラー判定を行う. |
---|
52 | * |
---|
53 | * @return boolean |
---|
54 | */ |
---|
55 | function isError() { |
---|
56 | if(PEAR::isError($this->conn->conn)) { |
---|
57 | return true; |
---|
58 | } |
---|
59 | return false; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * COUNT文を実行する. |
---|
64 | * |
---|
65 | * @param string $table テーブル名 |
---|
66 | * @param string $where where句 |
---|
67 | * @param array $arrval プレースホルダ |
---|
68 | * @return integer 件数 |
---|
69 | */ |
---|
70 | function count($table, $where = "", $arrval = array()) { |
---|
71 | if(strlen($where) <= 0) { |
---|
72 | $sqlse = "SELECT COUNT(*) FROM $table"; |
---|
73 | } else { |
---|
74 | $sqlse = "SELECT COUNT(*) FROM $table WHERE $where"; |
---|
75 | } |
---|
76 | // カウント文の実行 |
---|
77 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
78 | return $ret; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * SELECT文を実行する. |
---|
83 | * |
---|
84 | * @param string $col カラム名. 複数カラムの場合はカンマ区切りで書く |
---|
85 | * @param string $table テーブル名 |
---|
86 | * @param string $where WHERE句 |
---|
87 | * @param array $arrval プレースホルダ |
---|
88 | * @return array|null |
---|
89 | */ |
---|
90 | function select($col, $table, $where = "", $arrval = array()){ |
---|
91 | $sqlse = $this->getsql($col, $table, $where); |
---|
92 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
93 | return $ret; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * 直前に実行されたSQL文を取得する. |
---|
98 | * SC_DBconn::getLastQuery() を利用. |
---|
99 | * |
---|
100 | * @param boolean $disp trueの場合、画面出力を行う. |
---|
101 | * @return string SQL文 |
---|
102 | */ |
---|
103 | function getLastQuery($disp = true) { |
---|
104 | return $this->conn->getLastQuery($disp); |
---|
105 | } |
---|
106 | |
---|
107 | function commit() { |
---|
108 | $this->conn->query("COMMIT"); |
---|
109 | } |
---|
110 | |
---|
111 | function begin() { |
---|
112 | $this->conn->query("BEGIN"); |
---|
113 | } |
---|
114 | |
---|
115 | function rollback() { |
---|
116 | $this->conn->query("ROLLBACK"); |
---|
117 | } |
---|
118 | |
---|
119 | function exec($str, $arrval = array()) { |
---|
120 | $this->conn->query($str, $arrval); |
---|
121 | } |
---|
122 | |
---|
123 | function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { |
---|
124 | $strw = ""; |
---|
125 | $find = false; |
---|
126 | foreach ($arrwhere as $key => $val) { |
---|
127 | if(strlen($val) > 0) { |
---|
128 | if(strlen($strw) <= 0) { |
---|
129 | $strw .= $key ." LIKE ?"; |
---|
130 | } else if(strlen($arrcon[$key]) > 0) { |
---|
131 | $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; |
---|
132 | } else { |
---|
133 | $strw .= " AND " . $key ." LIKE ?"; |
---|
134 | } |
---|
135 | |
---|
136 | $arrval[] = $val; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | if(strlen($strw) > 0) { |
---|
141 | $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; |
---|
142 | } else { |
---|
143 | $sqlse = "SELECT $col FROM $table ".$this->option; |
---|
144 | } |
---|
145 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
146 | return $ret; |
---|
147 | } |
---|
148 | |
---|
149 | function getall($sql, $arrval = array()) { |
---|
150 | $ret = $this->conn->getAll($sql, $arrval); |
---|
151 | return $ret; |
---|
152 | } |
---|
153 | |
---|
154 | function getsql($col, $table, $where) { |
---|
155 | if($where != "") { |
---|
156 | // 引数の$whereを優先して実行する。 |
---|
157 | $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
158 | } else { |
---|
159 | if($this->where != "") { |
---|
160 | $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
161 | } else { |
---|
162 | $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
163 | } |
---|
164 | } |
---|
165 | return $sqlse; |
---|
166 | } |
---|
167 | |
---|
168 | function setoption($str) { |
---|
169 | $this->option = $str; |
---|
170 | } |
---|
171 | |
---|
172 | function setlimitoffset($limit, $offset = 0, $return = false) { |
---|
173 | if (is_numeric($limit) && is_numeric($offset)){ |
---|
174 | |
---|
175 | $option = " LIMIT " . $limit; |
---|
176 | $option.= " OFFSET " . $offset; |
---|
177 | |
---|
178 | if($return){ |
---|
179 | return $option; |
---|
180 | }else{ |
---|
181 | $this->option.= $option; |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | function setgroupby($str) { |
---|
187 | if (strlen($str) == 0) { |
---|
188 | $this->groupby = ''; |
---|
189 | } else { |
---|
190 | $this->groupby = "GROUP BY " . $str; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | function andwhere($str) { |
---|
195 | if($this->where != "") { |
---|
196 | $this->where .= " AND " . $str; |
---|
197 | } else { |
---|
198 | $this->where = $str; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | function orwhere($str) { |
---|
203 | if($this->where != "") { |
---|
204 | $this->where .= " OR " . $str; |
---|
205 | } else { |
---|
206 | $this->where = $str; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | function setwhere($str) { |
---|
211 | $this->where = $str; |
---|
212 | } |
---|
213 | |
---|
214 | function setorder($str) { |
---|
215 | if (strlen($str) == 0) { |
---|
216 | $this->order = ''; |
---|
217 | } else { |
---|
218 | $this->order = "ORDER BY " . $str; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | function setlimit($limit){ |
---|
224 | if ( is_numeric($limit)){ |
---|
225 | $this->option = " LIMIT " .$limit; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | function setoffset($offset) { |
---|
230 | if ( is_numeric($offset)){ |
---|
231 | $this->offset = " OFFSET " .$offset; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * INSERT文を実行する. |
---|
237 | * |
---|
238 | * @param string $table テーブル名 |
---|
239 | * @param array $sqlval array('カラム名' => '値',...)の連想配列 |
---|
240 | * @return |
---|
241 | */ |
---|
242 | function insert($table, $sqlval) { |
---|
243 | $strcol = ''; |
---|
244 | $strval = ''; |
---|
245 | $find = false; |
---|
246 | |
---|
247 | if(count($sqlval) <= 0 ) return false; |
---|
248 | |
---|
249 | foreach ($sqlval as $key => $val) { |
---|
250 | $strcol .= $key . ','; |
---|
251 | if(eregi("^Now\(\)$", $val)) { |
---|
252 | $strval .= 'Now(),'; |
---|
253 | } else { |
---|
254 | $strval .= '?,'; |
---|
255 | $arrval[] = $val; |
---|
256 | } |
---|
257 | $find = true; |
---|
258 | } |
---|
259 | if(!$find) { |
---|
260 | return false; |
---|
261 | } |
---|
262 | // 文末の","を削除 |
---|
263 | $strcol = ereg_replace(",$","",$strcol); |
---|
264 | // 文末の","を削除 |
---|
265 | $strval = ereg_replace(",$","",$strval); |
---|
266 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
---|
267 | // INSERT文の実行 |
---|
268 | $ret = $this->conn->query($sqlin, $arrval); |
---|
269 | |
---|
270 | return $ret; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * UPDATE文を実行する. |
---|
275 | * |
---|
276 | * @param string $table テーブル名 |
---|
277 | * @param array $sqlval array('カラム名' => '値',...)の連想配列 |
---|
278 | * @param string $where WHERE句 |
---|
279 | * @param array $arradd $addcol用のプレースホルダ配列 |
---|
280 | * @param string $addcol 追加カラム |
---|
281 | * @return |
---|
282 | */ |
---|
283 | function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") { |
---|
284 | $strcol = ''; |
---|
285 | $strval = ''; |
---|
286 | $find = false; |
---|
287 | foreach ($sqlval as $key => $val) { |
---|
288 | if(eregi("^Now\(\)$", $val)) { |
---|
289 | $strcol .= $key . '= Now(),'; |
---|
290 | } else { |
---|
291 | $strcol .= $key . '= ?,'; |
---|
292 | $arrval[] = $val; |
---|
293 | } |
---|
294 | $find = true; |
---|
295 | } |
---|
296 | if(!$find) { |
---|
297 | return false; |
---|
298 | } |
---|
299 | |
---|
300 | if($addcol != "") { |
---|
301 | foreach($addcol as $key => $val) { |
---|
302 | $strcol .= "$key = $val,"; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | // 文末の","を削除 |
---|
307 | $strcol = ereg_replace(",$","",$strcol); |
---|
308 | // 文末の","を削除 |
---|
309 | $strval = ereg_replace(",$","",$strval); |
---|
310 | |
---|
311 | if($where != "") { |
---|
312 | $sqlup = "UPDATE $table SET $strcol WHERE $where"; |
---|
313 | } else { |
---|
314 | $sqlup = "UPDATE $table SET $strcol"; |
---|
315 | } |
---|
316 | |
---|
317 | if(is_array($arradd)) { |
---|
318 | // プレースホルダー用に配列を追加 |
---|
319 | foreach($arradd as $val) { |
---|
320 | $arrval[] = $val; |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | // UPDATE文の実行 |
---|
325 | $ret = $this->conn->query($sqlup, $arrval); |
---|
326 | return $ret; |
---|
327 | } |
---|
328 | |
---|
329 | // MAX文の実行 |
---|
330 | function max($table, $col, $where = "", $arrval = array()) { |
---|
331 | if(strlen($where) <= 0) { |
---|
332 | $sqlse = "SELECT MAX($col) FROM $table"; |
---|
333 | } else { |
---|
334 | $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; |
---|
335 | } |
---|
336 | // MAX文の実行 |
---|
337 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
338 | return $ret; |
---|
339 | } |
---|
340 | |
---|
341 | // MIN文の実行 |
---|
342 | function min($table, $col, $where = "", $arrval = array()) { |
---|
343 | if(strlen($where) <= 0) { |
---|
344 | $sqlse = "SELECT MIN($col) FROM $table"; |
---|
345 | } else { |
---|
346 | $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; |
---|
347 | } |
---|
348 | // MIN文の実行 |
---|
349 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
350 | return $ret; |
---|
351 | } |
---|
352 | |
---|
353 | // 特定のカラムの値を取得 |
---|
354 | function get($table, $col, $where = "", $arrval = array()) { |
---|
355 | if(strlen($where) <= 0) { |
---|
356 | $sqlse = "SELECT $col FROM $table"; |
---|
357 | } else { |
---|
358 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
359 | } |
---|
360 | // SQL文の実行 |
---|
361 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
362 | return $ret; |
---|
363 | } |
---|
364 | |
---|
365 | function getone($sql, $arrval = array()) { |
---|
366 | // SQL文の実行 |
---|
367 | $ret = $this->conn->getOne($sql, $arrval); |
---|
368 | return $ret; |
---|
369 | |
---|
370 | } |
---|
371 | |
---|
372 | // 一行を取得 |
---|
373 | function getrow($table, $col, $where = "", $arrval = array()) { |
---|
374 | if(strlen($where) <= 0) { |
---|
375 | $sqlse = "SELECT $col FROM $table"; |
---|
376 | } else { |
---|
377 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
378 | } |
---|
379 | // SQL文の実行 |
---|
380 | $ret = $this->conn->getRow($sqlse, $arrval); |
---|
381 | |
---|
382 | return $ret; |
---|
383 | } |
---|
384 | |
---|
385 | // 1列取得 |
---|
386 | function getCol($table, $col, $where = "", $arrval = array()) { |
---|
387 | if (strlen($where) <= 0) { |
---|
388 | $sqlse = "SELECT $col FROM $table"; |
---|
389 | } else { |
---|
390 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
391 | } |
---|
392 | // SQL文の実行 |
---|
393 | return $this->conn->getCol($sqlse, $col, $arrval); |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * レコードの削除 |
---|
398 | * |
---|
399 | * @param string $table テーブル名 |
---|
400 | * @param string $where WHERE句 |
---|
401 | * @param array $arrval プレースホルダ |
---|
402 | * @return |
---|
403 | */ |
---|
404 | function delete($table, $where = "", $arrval = array()) { |
---|
405 | if(strlen($where) <= 0) { |
---|
406 | $sqlde = "DELETE FROM $table"; |
---|
407 | } else { |
---|
408 | $sqlde = "DELETE FROM $table WHERE $where"; |
---|
409 | } |
---|
410 | $ret = $this->conn->query($sqlde, $arrval); |
---|
411 | return $ret; |
---|
412 | } |
---|
413 | |
---|
414 | function nextval($table, $colname) { |
---|
415 | $sql = ""; |
---|
416 | // postgresqlとmysqlとで処理を分ける |
---|
417 | if (DB_TYPE == "pgsql") { |
---|
418 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
419 | $sql = "SELECT NEXTVAL('$seqtable')"; |
---|
420 | }else if (DB_TYPE == "mysql") { |
---|
421 | $sql = "SELECT last_insert_id();"; |
---|
422 | } |
---|
423 | $ret = $this->conn->getOne($sql); |
---|
424 | |
---|
425 | return $ret; |
---|
426 | } |
---|
427 | |
---|
428 | function currval($table, $colname) { |
---|
429 | $sql = ""; |
---|
430 | if (DB_TYPE == "pgsql") { |
---|
431 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
432 | $sql = "SELECT CURRVAL('$seqtable')"; |
---|
433 | }else if (DB_TYPE == "mysql") { |
---|
434 | $sql = "SELECT last_insert_id();"; |
---|
435 | } |
---|
436 | $ret = $this->conn->getOne($sql); |
---|
437 | |
---|
438 | return $ret; |
---|
439 | } |
---|
440 | |
---|
441 | function setval($table, $colname, $data) { |
---|
442 | $sql = ""; |
---|
443 | if (DB_TYPE == "pgsql") { |
---|
444 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
445 | $sql = "SELECT SETVAL('$seqtable', $data)"; |
---|
446 | $ret = $this->conn->getOne($sql); |
---|
447 | }else if (DB_TYPE == "mysql") { |
---|
448 | $sql = "ALTER TABLE $table AUTO_INCREMENT=$data"; |
---|
449 | $ret = $this->conn->query($sql); |
---|
450 | } |
---|
451 | |
---|
452 | return $ret; |
---|
453 | } |
---|
454 | |
---|
455 | function query($n ,$arr = "", $ignore_err = false){ |
---|
456 | $result = $this->conn->query($n, $arr, $ignore_err); |
---|
457 | return $result; |
---|
458 | } |
---|
459 | |
---|
460 | /** |
---|
461 | * auto_incrementを取得する. |
---|
462 | * |
---|
463 | * @param string $table_name テーブル名 |
---|
464 | * @return integer |
---|
465 | */ |
---|
466 | function get_auto_increment($table_name){ |
---|
467 | // ロックする |
---|
468 | $this->query("LOCK TABLES $table_name WRITE"); |
---|
469 | |
---|
470 | // 次のIncrementを取得 |
---|
471 | $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); |
---|
472 | $auto_inc_no = $arrRet[0]["Auto_increment"]; |
---|
473 | |
---|
474 | // 値をカウントアップしておく |
---|
475 | $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); |
---|
476 | |
---|
477 | // 解除する |
---|
478 | $this->query('UNLOCK TABLES'); |
---|
479 | |
---|
480 | return $auto_inc_no; |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | ?> |
---|