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

Revision 3679, 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
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 = true) {
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);
86sfprintr("dd");
87        return $ret;
88    }
89
90    function getsql($col, $table, $where) {
91        if($where != "") {
92            // °ú¿ô¤Î$where¤òÍ¥À褷¤Æ¼Â¹Ô¤¹¤ë¡£
93            $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
94        } else {
95            if($this->where != "") {
96                    $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
97                } else {
98                    $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
99            }
100        }
101        return $sqlse;
102    }
103           
104    function setoption($str) {
105        $this->option = $str;
106    }
107   
108    function setlimitoffset($limit, $offset = 0) {
109        if (is_numeric($limit) && is_numeric($offset)){
110            $this->option.= " LIMIT " . $limit;
111            $this->option.= " OFFSET " . $offset;
112        }
113    }
114   
115    function setgroupby($str) {
116        $this->groupby = "GROUP BY " . $str;
117    }
118   
119    function andwhere($str) {
120        if($this->where != "") {
121            $this->where .= " AND " . $str;
122        } else {
123            $this->where = $str;
124        }
125    }
126   
127    function orwhere($str) {
128        if($this->where != "") {
129            $this->where .= " OR " . $str;
130        } else {
131            $this->where = $str;
132        }
133    }
134       
135    function setwhere($str) {
136        $this->where = $str;
137    }
138   
139    function setorder($str) {
140        $this->order = "ORDER BY " . $str;
141    }
142   
143       
144    function setlimit($limit){
145        if ( is_numeric($limit)){
146            $this->option = " LIMIT " .$limit;
147        }   
148    }
149   
150    function setoffset($offset) {
151        if ( is_numeric($offset)){
152            $this->offset = " OFFSET " .$offset;
153        }   
154    }
155   
156   
157    // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô
158    // $table   :¥Æ¡¼¥Ö¥ë̾
159    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
160    function insert($table, $sqlval) {
161        $strcol = '';
162        $strval = '';
163        $find = false;
164        foreach ($sqlval as $key => $val) {
165                $strcol .= $key . ',';
166                if(eregi("^Now\(\)$", $val)) {
167                    $strval .= 'Now(),';
168                } else {
169                    $strval .= '?,';
170                    if($val != ""){
171                        $arrval[] = $val;
172                    } else {
173                        $arrval[] = NULL;
174                    }
175                }
176                $find = true;
177        }
178        if(!$find) {
179            return false;
180        }
181        // ʸËö¤Î","¤òºï½ü
182        $strcol = ereg_replace(",$","",$strcol);
183        // ʸËö¤Î","¤òºï½ü
184        $strval = ereg_replace(",$","",$strval);
185        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
186       
187        // INSERTʸ¤Î¼Â¹Ô
188        $ret = $this->conn->query($sqlin, $arrval);
189       
190        return $ret;       
191    }
192   
193        // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô
194    // $table   :¥Æ¡¼¥Ö¥ë̾
195    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
196    function fast_insert($table, $sqlval) {
197        $strcol = '';
198        $strval = '';
199        $find = false;
200       
201        foreach ($sqlval as $key => $val) {
202                $strcol .= $key . ',';
203                if($val != ""){
204                    $eval = pg_escape_string($val);
205                    $strval .= "'$eval',";
206                } else {
207                    $strval .= "NULL,";
208                }
209                $find = true;
210        }
211        if(!$find) {
212            return false;
213        }
214        // ʸËö¤Î","¤òºï½ü
215        $strcol = ereg_replace(",$","",$strcol);
216        // ʸËö¤Î","¤òºï½ü
217        $strval = ereg_replace(",$","",$strval);
218        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
219       
220        // INSERTʸ¤Î¼Â¹Ô
221        $ret = $this->conn->query($sqlin);
222       
223        return $ret;       
224    }
225   
226   
227    // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô
228    // $table   :¥Æ¡¼¥Ö¥ë̾
229    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
230    // $where   :WHEREʸ»úÎó
231    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
232        $strcol = '';
233        $strval = '';
234        $find = false;
235        foreach ($sqlval as $key => $val) {
236            if(eregi("^Now\(\)$", $val)) {
237                $strcol .= $key . '= Now(),';
238            } else {
239                $strcol .= $key . '= ?,';
240                if($val != ""){
241                    $arrval[] = $val;
242                } else {
243                    $arrval[] = NULL;
244                }
245            }
246            $find = true;
247        }
248        if(!$find) {
249            return false;
250        }
251       
252        if($addcol != "") {
253            foreach($addcol as $key => $val) {
254                $strcol .= "$key = $val,";
255            }
256        }
257               
258        // ʸËö¤Î","¤òºï½ü
259        $strcol = ereg_replace(",$","",$strcol);
260        // ʸËö¤Î","¤òºï½ü
261        $strval = ereg_replace(",$","",$strval);
262       
263        if($where != "") {
264            $sqlup = "UPDATE $table SET $strcol WHERE $where";
265        } else {
266            $sqlup = "UPDATE $table SET $strcol";
267        }
268       
269        if(is_array($arradd)) {
270            // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲÃ
271            foreach($arradd as $val) {
272                $arrval[] = $val;
273            }
274        }
275       
276        // INSERTʸ¤Î¼Â¹Ô
277        $ret = $this->conn->query($sqlup, $arrval);
278        return $ret;       
279    }
280
281    // MAXʸ¤Î¼Â¹Ô
282    function max($table, $col, $where = "", $arrval = array()) {
283        if(strlen($where) <= 0) {
284            $sqlse = "SELECT MAX($col) FROM $table";
285        } else {
286            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
287        }
288        // MAXʸ¤Î¼Â¹Ô
289        $ret = $this->conn->getOne($sqlse, $arrval);
290        return $ret;
291    }
292   
293    // MINʸ¤Î¼Â¹Ô
294    function min($table, $col, $where = "", $arrval = array()) {
295        if(strlen($where) <= 0) {
296            $sqlse = "SELECT MIN($col) FROM $table";
297        } else {
298            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
299        }
300        // MINʸ¤Î¼Â¹Ô
301        $ret = $this->conn->getOne($sqlse, $arrval);
302        return $ret;
303    }
304   
305    // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ
306    function get($table, $col, $where = "", $arrval = array()) {
307        if(strlen($where) <= 0) {
308            $sqlse = "SELECT $col FROM $table";
309        } else {
310            $sqlse = "SELECT $col FROM $table WHERE $where";
311        }
312        // SQLʸ¤Î¼Â¹Ô
313        $ret = $this->conn->getOne($sqlse, $arrval);
314        return $ret;
315    }
316   
317    function getone($sql, $arrval = array()) {
318        // SQLʸ¤Î¼Â¹Ô
319        $ret = $this->conn->getOne($sql, $arrval);
320        return $ret;
321    }
322       
323    // °ì¹Ô¤ò¼èÆÀ
324    function getrow($table, $col, $where = "", $arrval = array()) {
325        if(strlen($where) <= 0) {
326            $sqlse = "SELECT $col FROM $table";
327        } else {
328            $sqlse = "SELECT $col FROM $table WHERE $where";
329        }
330        // SQLʸ¤Î¼Â¹Ô
331        $ret = $this->conn->getRow($sqlse, $arrval);
332        return $ret;
333    }
334       
335    // ¥ì¥³¡¼¥É¤Îºï½ü
336    function delete($table, $where = "", $arrval = array()) {
337        if(strlen($where) <= 0) {
338            $sqlde = "DELETE FROM $table";
339        } else {
340            $sqlde = "DELETE FROM $table WHERE $where";
341        }
342        $ret = $this->conn->query($sqlde, $arrval);
343        return $ret;
344    }
345   
346    function nextval($table, $colname) {
347        $seqtable = $table . "_" . $colname . "_seq";
348        $ret = $this->conn->getOne("SELECT NEXTVAL('$seqtable')");
349        return $ret;
350    }
351   
352    function query($n ,$arr = "", $ignore_err = false){
353        $result = $this->conn->query($n, $arr, $ignore_err);
354        return $result;
355    }
356}
357?>
Note: See TracBrowser for help on using the repository browser.