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

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