source: branches/comu-ver2/data/class/SC_Query.php @ 17597

Revision 17597, 14.9 KB checked in by Seasoft, 16 years ago (diff)

SC_Query::setorder(), setgroupby() に空文字を渡すことで、解除可能に。

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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 */
30class 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            // 先頭に~があるとプレースホルダーしない。
254            } else if(ereg("^~", $val)) {
255                $strval .= ereg_replace("^~", "", $val).",";
256            } else {
257                $strval .= '?,';
258                if($val != ""){
259                    $arrval[] = $val;
260                } else {
261                    $arrval[] = NULL;
262                }
263            }
264            $find = true;
265        }
266        if(!$find) {
267            return false;
268        }
269        // 文末の","を削除
270        $strcol = ereg_replace(",$","",$strcol);
271        // 文末の","を削除
272        $strval = ereg_replace(",$","",$strval);
273        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
274        // INSERT文の実行
275        $ret = $this->conn->query($sqlin, $arrval);
276
277        return $ret;
278    }
279
280    // INSERT文の生成・実行
281    // $table   :テーブル名
282    // $sqlval  :列名 => 値の格納されたハッシュ配列
283    function fast_insert($table, $sqlval) {
284        $strcol = '';
285        $strval = '';
286        $find = false;
287
288        foreach ($sqlval as $key => $val) {
289                $strcol .= $key . ',';
290                if($val != ""){
291                    $eval = pg_escape_string($val);
292                    $strval .= "'$eval',";
293                } else {
294                    $strval .= "NULL,";
295                }
296                $find = true;
297        }
298        if(!$find) {
299            return false;
300        }
301        // 文末の","を削除
302        $strcol = ereg_replace(",$","",$strcol);
303        // 文末の","を削除
304        $strval = ereg_replace(",$","",$strval);
305        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
306
307        // INSERT文の実行
308        $ret = $this->conn->query($sqlin);
309
310        return $ret;
311    }
312
313    /**
314     * UPDATE文を実行する.
315     *
316     * @param string $table テーブル名
317     * @param array $sqlval array('カラム名' => '値',...)の連想配列
318     * @param string $where WHERE句
319     * @param array $arradd $addcol用のプレースホルダ配列
320     * @param string $addcol 追加カラム
321     * @return
322     */
323    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
324        $strcol = '';
325        $strval = '';
326        $find = false;
327        foreach ($sqlval as $key => $val) {
328            if(eregi("^Now\(\)$", $val)) {
329                $strcol .= $key . '= Now(),';
330            // 先頭に~があるとプレースホルダーしない。
331            } else if(ereg("^~", $val)) {
332                $strcol .= $key . "=" . ereg_replace("^~", "", $val) . ",";
333            } else {
334                $strcol .= $key . '= ?,';
335                if($val != ""){
336                    $arrval[] = $val;
337                } else {
338                    $arrval[] = NULL;
339                }
340            }
341            $find = true;
342        }
343        if(!$find) {
344            return false;
345        }
346
347        if($addcol != "") {
348            foreach($addcol as $key => $val) {
349                $strcol .= "$key = $val,";
350            }
351        }
352
353        // 文末の","を削除
354        $strcol = ereg_replace(",$","",$strcol);
355        // 文末の","を削除
356        $strval = ereg_replace(",$","",$strval);
357
358        if($where != "") {
359            $sqlup = "UPDATE $table SET $strcol WHERE $where";
360        } else {
361            $sqlup = "UPDATE $table SET $strcol";
362        }
363
364        if(is_array($arradd)) {
365            // プレースホルダー用に配列を追加
366            foreach($arradd as $val) {
367                $arrval[] = $val;
368            }
369        }
370
371        // INSERT文の実行
372        $ret = $this->conn->query($sqlup, $arrval);
373        return $ret;
374    }
375
376    // MAX文の実行
377    function max($table, $col, $where = "", $arrval = array()) {
378        if(strlen($where) <= 0) {
379            $sqlse = "SELECT MAX($col) FROM $table";
380        } else {
381            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
382        }
383        // MAX文の実行
384        $ret = $this->conn->getOne($sqlse, $arrval);
385        return $ret;
386    }
387
388    // MIN文の実行
389    function min($table, $col, $where = "", $arrval = array()) {
390        if(strlen($where) <= 0) {
391            $sqlse = "SELECT MIN($col) FROM $table";
392        } else {
393            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
394        }
395        // MIN文の実行
396        $ret = $this->conn->getOne($sqlse, $arrval);
397        return $ret;
398    }
399
400    // 特定のカラムの値を取得
401    function get($table, $col, $where = "", $arrval = array()) {
402        if(strlen($where) <= 0) {
403            $sqlse = "SELECT $col FROM $table";
404        } else {
405            $sqlse = "SELECT $col FROM $table WHERE $where";
406        }
407        // SQL文の実行
408        $ret = $this->conn->getOne($sqlse, $arrval);
409        return $ret;
410    }
411
412    function getone($sql, $arrval = array()) {
413        // SQL文の実行
414        $ret = $this->conn->getOne($sql, $arrval);
415        return $ret;
416
417    }
418
419    // 一行を取得
420    function getrow($table, $col, $where = "", $arrval = array()) {
421        if(strlen($where) <= 0) {
422            $sqlse = "SELECT $col FROM $table";
423        } else {
424            $sqlse = "SELECT $col FROM $table WHERE $where";
425        }
426        // SQL文の実行
427        $ret = $this->conn->getRow($sqlse, $arrval);
428
429        return $ret;
430    }
431
432    // 1列取得
433    function getCol($table, $col, $where = "", $arrval = array()) {
434        if (strlen($where) <= 0) {
435            $sqlse = "SELECT $col FROM $table";
436        } else {
437            $sqlse = "SELECT $col FROM $table WHERE $where";
438        }
439        // SQL文の実行
440        return $this->conn->getCol($sqlse, $col, $arrval);
441    }
442
443    /**
444     * レコードの削除
445     *
446     * @param string $table テーブル名
447     * @param string $where WHERE句
448     * @param array $arrval プレースホルダ
449     * @return
450     */
451    function delete($table, $where = "", $arrval = array()) {
452        if(strlen($where) <= 0) {
453            $sqlde = "DELETE FROM $table";
454        } else {
455            $sqlde = "DELETE FROM $table WHERE $where";
456        }
457        $ret = $this->conn->query($sqlde, $arrval);
458        return $ret;
459    }
460
461    function nextval($table, $colname) {
462        $sql = "";
463        // postgresqlとmysqlとで処理を分ける
464        if (DB_TYPE == "pgsql") {
465            $seqtable = $table . "_" . $colname . "_seq";
466            $sql = "SELECT NEXTVAL('$seqtable')";
467        }else if (DB_TYPE == "mysql") {
468            $sql = "SELECT last_insert_id();";
469        }
470        $ret = $this->conn->getOne($sql);
471
472        return $ret;
473    }
474
475    function currval($table, $colname) {
476        $sql = "";
477        if (DB_TYPE == "pgsql") {
478            $seqtable = $table . "_" . $colname . "_seq";
479            $sql = "SELECT CURRVAL('$seqtable')";
480        }else if (DB_TYPE == "mysql") {
481            $sql = "SELECT last_insert_id();";
482        }
483        $ret = $this->conn->getOne($sql);
484
485        return $ret;
486    }
487
488    function setval($table, $colname, $data) {
489        $sql = "";
490        if (DB_TYPE == "pgsql") {
491            $seqtable = $table . "_" . $colname . "_seq";
492            $sql = "SELECT SETVAL('$seqtable', $data)";
493            $ret = $this->conn->getOne($sql);
494        }else if (DB_TYPE == "mysql") {
495            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
496            $ret = $this->conn->query($sql);
497        }
498
499        return $ret;
500    }
501
502    function query($n ,$arr = "", $ignore_err = false){
503        $result = $this->conn->query($n, $arr, $ignore_err);
504        return $result;
505    }
506
507    /**
508     * auto_incrementを取得する.
509     *
510     * @param string $table_name テーブル名
511     * @return integer
512     */
513    function get_auto_increment($table_name){
514        // ロックする
515        $this->query("LOCK TABLES $table_name WRITE");
516
517        // 次のIncrementを取得
518        $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
519        $auto_inc_no = $arrRet[0]["Auto_increment"];
520
521        // 値をカウントアップしておく
522        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
523
524        // 解除する
525        $this->query('UNLOCK TABLES');
526
527        return $auto_inc_no;
528    }
529}
530
531?>
Note: See TracBrowser for help on using the repository browser.