source: branches/feature-module-update/data/class/SC_Query.php @ 15338

Revision 15338, 10.0 KB checked in by nanasess, 17 years ago (diff)

SC_Query::select() で SC_DB_DBFactory::sfChangeMySQL() を呼ぶように修正

  • Property svn:keywords set to Id
  • Property svn:mime-type set to application/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8class SC_Query {
9    var $option;
10    var $where;
11    var $conn;
12    var $groupby;
13    var $order;
14   
15    // コンストラクタ
16    /*
17        $err_disp:エラー表示を行うか
18        $new:新規に接続を行うか
19     */
20    function SC_Query($dsn = "", $err_disp = true, $new = false) {
21        $this->conn = new SC_DBconn($dsn, $err_disp, $new);
22        $this->where = "";
23        return $this->conn;
24    }
25   
26    // エラー判定
27    function isError() {
28        if(PEAR::isError($this->conn->conn)) {
29            return true;
30        }
31        return false;
32    }
33   
34    // COUNT文の実行
35    function count($table, $where = "", $arrval = array()) {
36        if(strlen($where) <= 0) {
37            $sqlse = "SELECT COUNT(*) FROM $table";
38        } else {
39            $sqlse = "SELECT COUNT(*) FROM $table WHERE $where";
40        }
41        // カウント文の実行
42        $ret = $this->conn->getOne($sqlse, $arrval);
43        return $ret;
44    }
45   
46    function select($col, $table, $where = "", $arrval = array()){
47        $sqlse = $this->getsql($col, $table, $where);
48        // DBに依存した SQL へ変換
49        $dbFactory = SC_DB_DBFactory::getInstance();
50        $sqlse = $dbFactory->sfChangeMySQL($sqlse);
51        $ret = $this->conn->getAll($sqlse, $arrval);
52        return $ret;
53    }
54
55    function getLastQuery($disp = true) {
56        $sql = $this->conn->conn->last_query;
57        if($disp) {
58            print($sql.";<br />\n");
59        }
60        return $sql;
61    }
62
63    function commit() {
64        $this->conn->query("COMMIT");
65    }
66   
67    function begin() {
68        $this->conn->query("BEGIN");
69    }
70   
71    function rollback() {
72        $this->conn->query("ROLLBACK");
73    }
74   
75    function exec($str, $arrval = array()) {
76        $this->conn->query($str, $arrval);
77    }
78
79    function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) {
80        $strw = "";         
81        $find = false;
82        foreach ($arrwhere as $key => $val) {
83            if(strlen($val) > 0) {
84                if(strlen($strw) <= 0) {
85                    $strw .= $key ." LIKE ?";
86                } else if(strlen($arrcon[$key]) > 0) {
87                    $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?";
88                } else {
89                    $strw .= " AND " . $key ." LIKE ?";
90                }
91               
92                $arrval[] = $val;
93            }
94        }
95       
96        if(strlen($strw) > 0) {
97            $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option;
98        } else {
99            $sqlse = "SELECT $col FROM $table ".$this->option;
100        }
101        $ret = $this->conn->getAll($sqlse, $arrval);
102        return $ret;
103    }
104   
105    function getall($sql, $arrval = array()) {
106        $ret = $this->conn->getAll($sql, $arrval);
107        return $ret;
108    }
109
110    function getsql($col, $table, $where) {
111        if($where != "") {
112            // 引数の$whereを優先して実行する。
113            $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
114        } else {
115            if($this->where != "") {
116                    $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
117                } else {
118                    $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
119            }
120        }
121        return $sqlse;
122    }
123           
124    function setoption($str) {
125        $this->option = $str;
126    }
127   
128    function setlimitoffset($limit, $offset = 0, $return = false) {
129        if (is_numeric($limit) && is_numeric($offset)){
130
131            $option = " LIMIT " . $limit;
132            $option.= " OFFSET " . $offset;
133           
134            if($return){
135                return $option;
136            }else{
137                $this->option.= $option;
138            }
139        }
140    }
141   
142    function setgroupby($str) {
143        $this->groupby = "GROUP BY " . $str;
144    }
145   
146    function andwhere($str) {
147        if($this->where != "") {
148            $this->where .= " AND " . $str;
149        } else {
150            $this->where = $str;
151        }
152    }
153   
154    function orwhere($str) {
155        if($this->where != "") {
156            $this->where .= " OR " . $str;
157        } else {
158            $this->where = $str;
159        }
160    }
161       
162    function setwhere($str) {
163        $this->where = $str;
164    }
165   
166    function setorder($str) {
167        $this->order = "ORDER BY " . $str;
168    }
169   
170       
171    function setlimit($limit){
172        if ( is_numeric($limit)){
173            $this->option = " LIMIT " .$limit;
174        }   
175    }
176   
177    function setoffset($offset) {
178        if ( is_numeric($offset)){
179            $this->offset = " OFFSET " .$offset;
180        }   
181    }
182   
183   
184    // INSERT文の生成・実行
185    // $table   :テーブル名
186    // $sqlval  :列名 => 値の格納されたハッシュ配列
187    function insert($table, $sqlval) {
188        $strcol = '';
189        $strval = '';
190        $find = false;
191       
192        if(count($sqlval) <= 0 ) return false;
193       
194        foreach ($sqlval as $key => $val) {
195            $strcol .= $key . ',';
196            if(eregi("^Now\(\)$", $val)) {
197                $strval .= 'Now(),';
198            } else {
199                $strval .= '?,';
200                if($val != ""){
201                    $arrval[] = $val;
202                } else {
203                    $arrval[] = NULL;
204                }
205            }
206            $find = true;
207        }
208        if(!$find) {
209            return false;
210        }
211        // 文末の","を削除
212        $strcol = ereg_replace(",$","",$strcol);
213        // 文末の","を削除
214        $strval = ereg_replace(",$","",$strval);
215        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
216       
217        // INSERT文の実行
218        $ret = $this->conn->query($sqlin, $arrval);
219       
220        return $ret;       
221    }
222   
223        // INSERT文の生成・実行
224    // $table   :テーブル名
225    // $sqlval  :列名 => 値の格納されたハッシュ配列
226    function fast_insert($table, $sqlval) {
227        $strcol = '';
228        $strval = '';
229        $find = false;
230       
231        foreach ($sqlval as $key => $val) {
232                $strcol .= $key . ',';
233                if($val != ""){
234                    $eval = pg_escape_string($val);
235                    $strval .= "'$eval',";
236                } else {
237                    $strval .= "NULL,";
238                }
239                $find = true;
240        }
241        if(!$find) {
242            return false;
243        }
244        // 文末の","を削除
245        $strcol = ereg_replace(",$","",$strcol);
246        // 文末の","を削除
247        $strval = ereg_replace(",$","",$strval);
248        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
249       
250        // INSERT文の実行
251        $ret = $this->conn->query($sqlin);
252       
253        return $ret;       
254    }
255   
256   
257    // UPDATE文の生成・実行
258    // $table   :テーブル名
259    // $sqlval  :列名 => 値の格納されたハッシュ配列
260    // $where   :WHERE文字列
261    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
262        $strcol = '';
263        $strval = '';
264        $find = false;
265        foreach ($sqlval as $key => $val) {
266            if(eregi("^Now\(\)$", $val)) {
267                $strcol .= $key . '= Now(),';
268            } else {
269                $strcol .= $key . '= ?,';
270                if($val != ""){
271                    $arrval[] = $val;
272                } else {
273                    $arrval[] = NULL;
274                }
275            }
276            $find = true;
277        }
278        if(!$find) {
279            return false;
280        }
281       
282        if($addcol != "") {
283            foreach($addcol as $key => $val) {
284                $strcol .= "$key = $val,";
285            }
286        }
287               
288        // 文末の","を削除
289        $strcol = ereg_replace(",$","",$strcol);
290        // 文末の","を削除
291        $strval = ereg_replace(",$","",$strval);
292       
293        if($where != "") {
294            $sqlup = "UPDATE $table SET $strcol WHERE $where";
295        } else {
296            $sqlup = "UPDATE $table SET $strcol";
297        }
298       
299        if(is_array($arradd)) {
300            // プレースホルダー用に配列を追加
301            foreach($arradd as $val) {
302                $arrval[] = $val;
303            }
304        }
305       
306        // INSERT文の実行
307        $ret = $this->conn->query($sqlup, $arrval);
308        return $ret;       
309    }
310
311    // MAX文の実行
312    function max($table, $col, $where = "", $arrval = array()) {
313        if(strlen($where) <= 0) {
314            $sqlse = "SELECT MAX($col) FROM $table";
315        } else {
316            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
317        }
318        // MAX文の実行
319        $ret = $this->conn->getOne($sqlse, $arrval);
320        return $ret;
321    }
322   
323    // MIN文の実行
324    function min($table, $col, $where = "", $arrval = array()) {
325        if(strlen($where) <= 0) {
326            $sqlse = "SELECT MIN($col) FROM $table";
327        } else {
328            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
329        }
330        // MIN文の実行
331        $ret = $this->conn->getOne($sqlse, $arrval);
332        return $ret;
333    }
334   
335    // 特定のカラムの値を取得
336    function get($table, $col, $where = "", $arrval = array()) {
337        if(strlen($where) <= 0) {
338            $sqlse = "SELECT $col FROM $table";
339        } else {
340            $sqlse = "SELECT $col FROM $table WHERE $where";
341        }
342        // SQL文の実行
343        $ret = $this->conn->getOne($sqlse, $arrval);
344        return $ret;
345    }
346   
347    function getone($sql, $arrval = array()) {
348        // SQL文の実行
349        $ret = $this->conn->getOne($sql, $arrval);
350        return $ret;
351       
352    }
353       
354    // 一行を取得
355    function getrow($table, $col, $where = "", $arrval = array()) {
356        if(strlen($where) <= 0) {
357            $sqlse = "SELECT $col FROM $table";
358        } else {
359            $sqlse = "SELECT $col FROM $table WHERE $where";
360        }
361        // SQL文の実行
362        $ret = $this->conn->getRow($sqlse, $arrval);
363       
364        return $ret;
365    }
366       
367    // レコードの削除
368    function delete($table, $where = "", $arrval = array()) {
369        if(strlen($where) <= 0) {
370            $sqlde = "DELETE FROM $table";
371        } else {
372            $sqlde = "DELETE FROM $table WHERE $where";
373        }
374        $ret = $this->conn->query($sqlde, $arrval);
375        return $ret;
376    }
377   
378    function nextval($table, $colname) {
379        $sql = "";
380        // postgresqlとmysqlとで処理を分ける
381        if (DB_TYPE == "pgsql") {
382            $seqtable = $table . "_" . $colname . "_seq";
383            $sql = "SELECT NEXTVAL('$seqtable')";
384        }else if (DB_TYPE == "mysql") {
385            $sql = "SELECT last_insert_id();";
386        }
387        $ret = $this->conn->getOne($sql);
388       
389        return $ret;
390    }
391   
392    function currval($table, $colname) {
393        $sql = "";
394        if (DB_TYPE == "pgsql") {
395            $seqtable = $table . "_" . $colname . "_seq";
396            $sql = "SELECT CURRVAL('$seqtable')";
397        }else if (DB_TYPE == "mysql") {
398            $sql = "SELECT last_insert_id();";
399        }
400        $ret = $this->conn->getOne($sql);
401       
402        return $ret;
403    }   
404   
405    function setval($table, $colname, $data) {
406        $sql = "";
407        if (DB_TYPE == "pgsql") {
408            $seqtable = $table . "_" . $colname . "_seq";
409            $sql = "SELECT SETVAL('$seqtable', $data)";
410            $ret = $this->conn->getOne($sql);
411        }else if (DB_TYPE == "mysql") {
412            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
413            $ret = $this->conn->query($sql);
414        }
415       
416        return $ret;
417    }       
418   
419    function query($n ,$arr = "", $ignore_err = false){
420        $result = $this->conn->query($n, $arr, $ignore_err);
421        return $result;
422    }
423   
424    // auto_incrementを取得する
425    function get_auto_increment($table_name){
426        // ロックする
427        $this->query("LOCK TABLES $table_name WRITE");
428       
429        // 次のIncrementを取得
430        $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
431        $auto_inc_no = $arrRet[0]["Auto_increment"];
432       
433        // 値をカウントアップしておく
434        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
435       
436        // 解除する
437        $this->query('UNLOCK TABLES');
438       
439        return $auto_inc_no;
440    }
441}
442
443?>
Note: See TracBrowser for help on using the repository browser.