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

Revision 3686, 7.7 KB checked in by kakinaka, 20 years ago (diff)

blank

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