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

Revision 17595, 14.7 KB checked in by Seasoft, 18 years ago (diff)

getLastQuery()のロジックを SC_Query から SC_DbConn に移動し、両方で利用可能に。

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