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

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