source: temp/trunk/data/class/SC_Query.php @ 6270

Revision 6270, 8.9 KB checked in by naka, 20 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/*
3 * Copyright(c) 2000-2006 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   
13    // ¥³¥ó¥¹¥È¥é¥¯¥¿
14    /*
15        $err_disp:¥¨¥é¡¼É½¼¨¤ò¹Ô¤¦¤«
16        $new¡§¿·µ¬¤ËÀܳ¤ò¹Ô¤¦¤«
17     */
18    function SC_Query($dsn = "", $err_disp = true, $new = false) {
19        $this->conn = new SC_DBconn($dsn, $err_disp, $new);
20        $this->where = "";
21        return $this->conn;
22    }
23   
24    // ¥¨¥é¡¼È½Äê
25    function isError() {
26        if(PEAR::isError($this->conn->conn)) {
27            return true;
28        }
29        return false;
30    }
31   
32    // COUNTʸ¤Î¼Â¹Ô
33    function count($table, $where = "", $arrval = array()) {
34        if(strlen($where) <= 0) {
35            $sqlse = "SELECT COUNT(*) FROM $table";
36        } else {
37            $sqlse = "SELECT COUNT(*) FROM $table WHERE $where";
38        }
39        // ¥«¥¦¥ó¥Èʸ¤Î¼Â¹Ô
40        $ret = $this->conn->getOne($sqlse, $arrval);
41        return $ret;
42    }
43   
44    function select($col, $table, $where = "", $arrval = array()){
45        $sqlse = $this->getsql($col, $table, $where);
46        $ret = $this->conn->getAll($sqlse, $arrval);
47        return $ret;
48    }
49
50    function getLastQuery($disp = true) {
51        $sql = $this->conn->conn->last_query;
52        if($disp) {
53            print($sql.";<br />\n");
54        }
55        return $sql;
56    }
57
58    function commit() {
59        $this->conn->query("COMMIT");
60    }
61   
62    function begin() {
63        $this->conn->query("BEGIN");
64    }
65   
66    function rollback() {
67        $this->conn->query("ROLLBACK");
68    }
69   
70    function exec($str, $arrval = array()) {
71        $this->conn->query($str, $arrval);
72    }
73
74    function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) {
75        $strw = "";         
76        $find = false;
77        foreach ($arrwhere as $key => $val) {
78            if(strlen($val) > 0) {
79                if(strlen($strw) <= 0) {
80                    $strw .= $key ." LIKE ?";
81                } else if(strlen($arrcon[$key]) > 0) {
82                    $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?";
83                } else {
84                    $strw .= " AND " . $key ." LIKE ?";
85                }
86               
87                $arrval[] = $val;
88            }
89        }
90       
91        if(strlen($strw) > 0) {
92            $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option;
93        } else {
94            $sqlse = "SELECT $col FROM $table ".$this->option;
95        }
96        $ret = $this->conn->getAll($sqlse, $arrval);
97        return $ret;
98    }
99   
100    function getall($sql, $arrval = array()) {
101        $ret = $this->conn->getAll($sql, $arrval);
102        return $ret;
103    }
104
105    function getsql($col, $table, $where) {
106        if($where != "") {
107            // °ú¿ô¤Î$where¤òÍ¥À褷¤Æ¼Â¹Ô¤¹¤ë¡£
108            $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
109        } else {
110            if($this->where != "") {
111                    $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
112                } else {
113                    $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
114            }
115        }
116        return $sqlse;
117    }
118           
119    function setoption($str) {
120        $this->option = $str;
121    }
122   
123    function setlimitoffset($limit, $offset = 0) {
124        if (is_numeric($limit) && is_numeric($offset)){
125            $this->option.= " LIMIT " . $limit;
126            $this->option.= " OFFSET " . $offset;
127        }
128    }
129   
130    function setgroupby($str) {
131        $this->groupby = "GROUP BY " . $str;
132    }
133   
134    function andwhere($str) {
135        if($this->where != "") {
136            $this->where .= " AND " . $str;
137        } else {
138            $this->where = $str;
139        }
140    }
141   
142    function orwhere($str) {
143        if($this->where != "") {
144            $this->where .= " OR " . $str;
145        } else {
146            $this->where = $str;
147        }
148    }
149       
150    function setwhere($str) {
151        $this->where = $str;
152    }
153   
154    function setorder($str) {
155        $this->order = "ORDER BY " . $str;
156    }
157   
158       
159    function setlimit($limit){
160        if ( is_numeric($limit)){
161            $this->option = " LIMIT " .$limit;
162        }   
163    }
164   
165    function setoffset($offset) {
166        if ( is_numeric($offset)){
167            $this->offset = " OFFSET " .$offset;
168        }   
169    }
170   
171   
172    // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô
173    // $table   :¥Æ¡¼¥Ö¥ë̾
174    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
175    function insert($table, $sqlval) {
176        $strcol = '';
177        $strval = '';
178        $find = false;
179        foreach ($sqlval as $key => $val) {
180                $strcol .= $key . ',';
181                if(eregi("^Now\(\)$", $val)) {
182                    $strval .= 'Now(),';
183                } else {
184                    $strval .= '?,';
185                    if($val != ""){
186                        $arrval[] = $val;
187                    } else {
188                        $arrval[] = NULL;
189                    }
190                }
191                $find = true;
192        }
193        if(!$find) {
194            return false;
195        }
196        // ʸËö¤Î","¤òºï½ü
197        $strcol = ereg_replace(",$","",$strcol);
198        // ʸËö¤Î","¤òºï½ü
199        $strval = ereg_replace(",$","",$strval);
200        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
201       
202        // INSERTʸ¤Î¼Â¹Ô
203        $ret = $this->conn->query($sqlin, $arrval);
204       
205        return $ret;       
206    }
207   
208        // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô
209    // $table   :¥Æ¡¼¥Ö¥ë̾
210    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
211    function fast_insert($table, $sqlval) {
212        $strcol = '';
213        $strval = '';
214        $find = false;
215       
216        foreach ($sqlval as $key => $val) {
217                $strcol .= $key . ',';
218                if($val != ""){
219                    $eval = pg_escape_string($val);
220                    $strval .= "'$eval',";
221                } else {
222                    $strval .= "NULL,";
223                }
224                $find = true;
225        }
226        if(!$find) {
227            return false;
228        }
229        // ʸËö¤Î","¤òºï½ü
230        $strcol = ereg_replace(",$","",$strcol);
231        // ʸËö¤Î","¤òºï½ü
232        $strval = ereg_replace(",$","",$strval);
233        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
234       
235        // INSERTʸ¤Î¼Â¹Ô
236        $ret = $this->conn->query($sqlin);
237       
238        return $ret;       
239    }
240   
241   
242    // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô
243    // $table   :¥Æ¡¼¥Ö¥ë̾
244    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
245    // $where   :WHEREʸ»úÎó
246    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
247        $strcol = '';
248        $strval = '';
249        $find = false;
250        foreach ($sqlval as $key => $val) {
251            if(eregi("^Now\(\)$", $val)) {
252                $strcol .= $key . '= Now(),';
253            } else {
254                $strcol .= $key . '= ?,';
255                if($val != ""){
256                    $arrval[] = $val;
257                } else {
258                    $arrval[] = NULL;
259                }
260            }
261            $find = true;
262        }
263        if(!$find) {
264            return false;
265        }
266       
267        if($addcol != "") {
268            foreach($addcol as $key => $val) {
269                $strcol .= "$key = $val,";
270            }
271        }
272               
273        // ʸËö¤Î","¤òºï½ü
274        $strcol = ereg_replace(",$","",$strcol);
275        // ʸËö¤Î","¤òºï½ü
276        $strval = ereg_replace(",$","",$strval);
277       
278        if($where != "") {
279            $sqlup = "UPDATE $table SET $strcol WHERE $where";
280        } else {
281            $sqlup = "UPDATE $table SET $strcol";
282        }
283       
284        if(is_array($arradd)) {
285            // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲÃ
286            foreach($arradd as $val) {
287                $arrval[] = $val;
288            }
289        }
290       
291        // INSERTʸ¤Î¼Â¹Ô
292        $ret = $this->conn->query($sqlup, $arrval);
293        return $ret;       
294    }
295
296    // MAXʸ¤Î¼Â¹Ô
297    function max($table, $col, $where = "", $arrval = array()) {
298        if(strlen($where) <= 0) {
299            $sqlse = "SELECT MAX($col) FROM $table";
300        } else {
301            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
302        }
303        // MAXʸ¤Î¼Â¹Ô
304        $ret = $this->conn->getOne($sqlse, $arrval);
305        return $ret;
306    }
307   
308    // MINʸ¤Î¼Â¹Ô
309    function min($table, $col, $where = "", $arrval = array()) {
310        if(strlen($where) <= 0) {
311            $sqlse = "SELECT MIN($col) FROM $table";
312        } else {
313            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
314        }
315        // MINʸ¤Î¼Â¹Ô
316        $ret = $this->conn->getOne($sqlse, $arrval);
317        return $ret;
318    }
319   
320    // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ
321    function get($table, $col, $where = "", $arrval = array()) {
322        if(strlen($where) <= 0) {
323            $sqlse = "SELECT $col FROM $table";
324        } else {
325            $sqlse = "SELECT $col FROM $table WHERE $where";
326        }
327        // SQLʸ¤Î¼Â¹Ô
328        $ret = $this->conn->getOne($sqlse, $arrval);
329        return $ret;
330    }
331   
332    function getone($sql, $arrval = array()) {
333        // SQLʸ¤Î¼Â¹Ô
334        $ret = $this->conn->getOne($sql, $arrval);
335        return $ret;
336       
337    }
338       
339    // °ì¹Ô¤ò¼èÆÀ
340    function getrow($table, $col, $where = "", $arrval = array()) {
341        if(strlen($where) <= 0) {
342            $sqlse = "SELECT $col FROM $table";
343        } else {
344            $sqlse = "SELECT $col FROM $table WHERE $where";
345        }
346        // SQLʸ¤Î¼Â¹Ô
347        $ret = $this->conn->getRow($sqlse, $arrval);
348       
349        return $ret;
350    }
351       
352    // ¥ì¥³¡¼¥É¤Îºï½ü
353    function delete($table, $where = "", $arrval = array()) {
354        if(strlen($where) <= 0) {
355            $sqlde = "DELETE FROM $table";
356        } else {
357            $sqlde = "DELETE FROM $table WHERE $where";
358        }
359        $ret = $this->conn->query($sqlde, $arrval);
360        return $ret;
361    }
362   
363    function nextval($table, $colname) {
364        $sql = "";
365        // postgresql¤Èmysql¤È¤Ç½èÍý¤òʬ¤±¤ë
366        if (DB_TYPE == "pgsql") {
367            $seqtable = $table . "_" . $colname . "_seq";
368            $sql = "SELECT NEXTVAL('$seqtable')";
369        }else if (DB_TYPE == "mysql") {
370            $sql = "SELECT last_insert_id();";
371        }
372        $ret = $this->conn->getOne($sql);
373       
374        return $ret;
375    }
376   
377    function currval($table, $colname) {
378        $sql = "";
379        if (DB_TYPE == "pgsql") {
380            $seqtable = $table . "_" . $colname . "_seq";
381            $sql = "SELECT CURRVAL('$seqtable')";
382        }else if (DB_TYPE == "mysql") {
383            $sql = "SELECT last_insert_id();";
384        }
385        $ret = $this->conn->getOne($sql);
386       
387        return $ret;
388    }   
389   
390    function setval($table, $colname, $data) {
391        $sql = "";
392        if (DB_TYPE == "pgsql") {
393            $seqtable = $table . "_" . $colname . "_seq";
394            $sql = "SELECT SETVAL('$seqtable', $data)";
395            $ret = $this->conn->getOne($sql);
396        }else if (DB_TYPE == "mysql") {
397            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
398            $ret = $this->conn->query($sql);
399        }
400       
401        return $ret;
402    }       
403   
404    function query($n ,$arr = "", $ignore_err = false){
405        $result = $this->conn->query($n, $arr, $ignore_err);
406        return $result;
407    }
408}
409?>
Note: See TracBrowser for help on using the repository browser.