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

Revision 18234, 13.7 KB checked in by Seasoft, 15 years ago (diff)

#528(改行コードが混在している)

  • SVN属性の変更。ただし、以下は除外。
    • /data/module
    • /data/pdf
    • /html/test
  • Property svn:eol-style set to LF
  • 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            } 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 $arrValIn $where,$arrRawSql用のプレースホルダ配列
280     * @param string $arrRawSql 追加カラム
281     * @return
282     */
283    function update($table, $sqlval, $where = "", $arrValIn = array(), $arrRawSql = array()) {
284        $arrCol = array();
285        $arrVal = array();
286        $find = false;
287        foreach ($sqlval as $key => $val) {
288            if (eregi("^Now\(\)$", $val)) {
289                $arrCol[] = $key . '= Now()';
290            } else {
291                $arrCol[] = $key . '= ?';
292                $arrVal[] = $val;
293            }
294            $find = true;
295        }
296
297        if ($arrRawSql != "") {
298            foreach($arrRawSql as $key => $val) {
299                $arrCol[] = "$key = $val";
300            }
301        }
302
303        if (empty($arrCol)) {
304            return false;
305        }
306
307        // 文末の","を削除
308        $strcol = implode(', ', $arrCol);
309
310        if (is_array($arrValIn)) { // 旧版との互換用
311            // プレースホルダー用に配列を追加
312            $arrVal = array_merge($arrVal, $arrValIn);
313        }
314
315        $sqlup = "UPDATE $table SET $strcol";
316        if (strlen($where) >= 1) {
317            $sqlup .= " WHERE $where";
318        }
319
320        // UPDATE文の実行
321        return $this->conn->query($sqlup, $arrVal);
322    }
323
324    // MAX文の実行
325    function max($table, $col, $where = "", $arrval = array()) {
326        if(strlen($where) <= 0) {
327            $sqlse = "SELECT MAX($col) FROM $table";
328        } else {
329            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
330        }
331        // MAX文の実行
332        $ret = $this->conn->getOne($sqlse, $arrval);
333        return $ret;
334    }
335
336    // MIN文の実行
337    function min($table, $col, $where = "", $arrval = array()) {
338        if(strlen($where) <= 0) {
339            $sqlse = "SELECT MIN($col) FROM $table";
340        } else {
341            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
342        }
343        // MIN文の実行
344        $ret = $this->conn->getOne($sqlse, $arrval);
345        return $ret;
346    }
347
348    // 特定のカラムの値を取得
349    function get($table, $col, $where = "", $arrval = array()) {
350        if(strlen($where) <= 0) {
351            $sqlse = "SELECT $col FROM $table";
352        } else {
353            $sqlse = "SELECT $col FROM $table WHERE $where";
354        }
355        // SQL文の実行
356        $ret = $this->conn->getOne($sqlse, $arrval);
357        return $ret;
358    }
359
360    function getone($sql, $arrval = array()) {
361        // SQL文の実行
362        $ret = $this->conn->getOne($sql, $arrval);
363        return $ret;
364
365    }
366
367    /**
368     * 一行をカラム名をキーとした連想配列として取得
369     *
370     * @param string $table テーブル名
371     * @param string $col カラム名
372     * @param string $where WHERE句
373     * @param array $arrVal プレースホルダ配列
374     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。
375     * @return array array('カラム名' => '値', ...)の連想配列
376     */
377    function getRow($table, $col, $where = "", $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) {
378        $sqlse = "SELECT $col FROM $table";
379       
380        if (strlen($where) >= 1) {
381            $sqlse .= " WHERE $where";
382        }
383        // SQL文の実行
384        return $this->conn->getRow($sqlse, $arrVal ,$fetchmode);
385    }
386
387    // 1列取得
388    function getCol($table, $col, $where = "", $arrval = array()) {
389        if (strlen($where) <= 0) {
390            $sqlse = "SELECT $col FROM $table";
391        } else {
392            $sqlse = "SELECT $col FROM $table WHERE $where";
393        }
394        // SQL文の実行
395        return $this->conn->getCol($sqlse, 0, $arrval);
396    }
397
398    /**
399     * レコードの削除
400     *
401     * @param string $table テーブル名
402     * @param string $where WHERE句
403     * @param array $arrval プレースホルダ
404     * @return
405     */
406    function delete($table, $where = "", $arrval = array()) {
407        if(strlen($where) <= 0) {
408            $sqlde = "DELETE FROM $table";
409        } else {
410            $sqlde = "DELETE FROM $table WHERE $where";
411        }
412        $ret = $this->conn->query($sqlde, $arrval);
413        return $ret;
414    }
415
416    function nextval($table, $colname) {
417        $sql = "";
418        // postgresqlとmysqlとで処理を分ける
419        if (DB_TYPE == "pgsql") {
420            $seqtable = $table . "_" . $colname . "_seq";
421            $sql = "SELECT NEXTVAL('$seqtable')";
422        }else if (DB_TYPE == "mysql") {
423            $sql = "SELECT last_insert_id();";
424        }
425        $ret = $this->conn->getOne($sql);
426
427        return $ret;
428    }
429
430    function currval($table, $colname) {
431        $sql = "";
432        if (DB_TYPE == "pgsql") {
433            $seqtable = $table . "_" . $colname . "_seq";
434            $sql = "SELECT CURRVAL('$seqtable')";
435        }else if (DB_TYPE == "mysql") {
436            $sql = "SELECT last_insert_id();";
437        }
438        $ret = $this->conn->getOne($sql);
439
440        return $ret;
441    }
442
443    function setval($table, $colname, $data) {
444        $sql = "";
445        if (DB_TYPE == "pgsql") {
446            $seqtable = $table . "_" . $colname . "_seq";
447            $sql = "SELECT SETVAL('$seqtable', $data)";
448            $ret = $this->conn->getOne($sql);
449        }else if (DB_TYPE == "mysql") {
450            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
451            $ret = $this->conn->query($sql);
452        }
453
454        return $ret;
455    }
456
457    function query($n ,$arr = "", $ignore_err = false){
458        $result = $this->conn->query($n, $arr, $ignore_err);
459        return $result;
460    }
461
462    /**
463     * auto_incrementを取得する.
464     *
465     * @param string $table_name テーブル名
466     * @return integer
467     */
468    function get_auto_increment($table_name){
469        // ロックする
470        $this->query("LOCK TABLES $table_name WRITE");
471
472        // 次のIncrementを取得
473        $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
474        $auto_inc_no = $arrRet[0]["Auto_increment"];
475
476        // 値をカウントアップしておく
477        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
478
479        // 解除する
480        $this->query('UNLOCK TABLES');
481
482        return $auto_inc_no;
483    }
484}
485
486?>
Note: See TracBrowser for help on using the repository browser.