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

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