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

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