source: branches/version-2/data/class/SC_Query.php @ 17566

Revision 17566, 14.9 KB checked in by nakanishi, 16 years ago (diff)

テストコードが残っていたので削除。

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