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

Revision 17556, 14.8 KB checked in by Seasoft, 18 years ago (diff)

SC_DB_DBFactory::sfChangeMySQL() を二重で処理しているようなので、SC_DbConn::getAll() に依存するように変更。

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