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

Revision 7990, 9.4 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
2/*
3 * Copyright(c) 2000-2006 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8class SC_Query {
9    var $option;
10    var $where;
11    var $conn;
12   
13    // ¥³¥ó¥¹¥È¥é¥¯¥¿
14    /*
15        $err_disp:¥¨¥é¡¼É½¼¨¤ò¹Ô¤¦¤«
16        $new¡§¿·µ¬¤ËÀܳ¤ò¹Ô¤¦¤«
17     */
18    function SC_Query($dsn = "", $err_disp = true, $new = false) {
19        $this->conn = new SC_DBconn($dsn, $err_disp, $new);
20        $this->where = "";
21        return $this->conn;
22    }
23   
24    // ¥¨¥é¡¼È½Äê
25    function isError() {
26        if(PEAR::isError($this->conn->conn)) {
27            return true;
28        }
29        return false;
30    }
31   
32    // COUNTʸ¤Î¼Â¹Ô
33    function count($table, $where = "", $arrval = array()) {
34        if(strlen($where) <= 0) {
35            $sqlse = "SELECT COUNT(*) FROM $table";
36        } else {
37            $sqlse = "SELECT COUNT(*) FROM $table WHERE $where";
38        }
39        // ¥«¥¦¥ó¥Èʸ¤Î¼Â¹Ô
40        $ret = $this->conn->getOne($sqlse, $arrval);
41        return $ret;
42    }
43   
44    function select($col, $table, $where = "", $arrval = array()){
45        $sqlse = $this->getsql($col, $table, $where);
46        $ret = $this->conn->getAll($sqlse, $arrval);
47        return $ret;
48    }
49
50    function getLastQuery($disp = true) {
51        $sql = $this->conn->conn->last_query;
52        if($disp) {
53            print($sql.";<br />\n");
54        }
55        return $sql;
56    }
57
58    function commit() {
59        $this->conn->query("COMMIT");
60    }
61   
62    function begin() {
63        $this->conn->query("BEGIN");
64    }
65   
66    function rollback() {
67        $this->conn->query("ROLLBACK");
68    }
69   
70    function exec($str, $arrval = array()) {
71        $this->conn->query($str, $arrval);
72    }
73
74    function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) {
75        $strw = "";         
76        $find = false;
77        foreach ($arrwhere as $key => $val) {
78            if(strlen($val) > 0) {
79                if(strlen($strw) <= 0) {
80                    $strw .= $key ." LIKE ?";
81                } else if(strlen($arrcon[$key]) > 0) {
82                    $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?";
83                } else {
84                    $strw .= " AND " . $key ." LIKE ?";
85                }
86               
87                $arrval[] = $val;
88            }
89        }
90       
91        if(strlen($strw) > 0) {
92            $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option;
93        } else {
94            $sqlse = "SELECT $col FROM $table ".$this->option;
95        }
96        $ret = $this->conn->getAll($sqlse, $arrval);
97        return $ret;
98    }
99   
100    function getall($sql, $arrval = array()) {
101        $ret = $this->conn->getAll($sql, $arrval);
102        return $ret;
103    }
104
105    function getsql($col, $table, $where) {
106        if($where != "") {
107            // °ú¿ô¤Î$where¤òÍ¥À褷¤Æ¼Â¹Ô¤¹¤ë¡£
108            $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
109        } else {
110            if($this->where != "") {
111                    $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
112                } else {
113                    $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
114            }
115        }
116        return $sqlse;
117    }
118           
119    function setoption($str) {
120        $this->option = $str;
121    }
122   
123    function setlimitoffset($limit, $offset = 0) {
124        if (is_numeric($limit) && is_numeric($offset)){
125            $this->option.= " LIMIT " . $limit;
126            $this->option.= " OFFSET " . $offset;
127        }
128    }
129   
130    function setgroupby($str) {
131        $this->groupby = "GROUP BY " . $str;
132    }
133   
134    function andwhere($str) {
135        if($this->where != "") {
136            $this->where .= " AND " . $str;
137        } else {
138            $this->where = $str;
139        }
140    }
141   
142    function orwhere($str) {
143        if($this->where != "") {
144            $this->where .= " OR " . $str;
145        } else {
146            $this->where = $str;
147        }
148    }
149       
150    function setwhere($str) {
151        $this->where = $str;
152    }
153   
154    function setorder($str) {
155        $this->order = "ORDER BY " . $str;
156    }
157   
158       
159    function setlimit($limit){
160        if ( is_numeric($limit)){
161            $this->option = " LIMIT " .$limit;
162        }   
163    }
164   
165    function setoffset($offset) {
166        if ( is_numeric($offset)){
167            $this->offset = " OFFSET " .$offset;
168        }   
169    }
170   
171   
172    // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô
173    // $table   :¥Æ¡¼¥Ö¥ë̾
174    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
175    function insert($table, $sqlval) {
176        $strcol = '';
177        $strval = '';
178        $find = false;
179       
180        if(count($sqlval) <= 0 ) return false;
181       
182        foreach ($sqlval as $key => $val) {
183            $strcol .= $key . ',';
184            if(eregi("^Now\(\)$", $val)) {
185                $strval .= 'Now(),';
186            } else {
187                $strval .= '?,';
188                if($val != ""){
189                    $arrval[] = $val;
190                } else {
191                    $arrval[] = NULL;
192                }
193            }
194            $find = true;
195        }
196        if(!$find) {
197            return false;
198        }
199        // ʸËö¤Î","¤òºï½ü
200        $strcol = ereg_replace(",$","",$strcol);
201        // ʸËö¤Î","¤òºï½ü
202        $strval = ereg_replace(",$","",$strval);
203        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
204       
205        // INSERTʸ¤Î¼Â¹Ô
206        $ret = $this->conn->query($sqlin, $arrval);
207       
208        return $ret;       
209    }
210   
211        // INSERTʸ¤ÎÀ¸À®¡¦¼Â¹Ô
212    // $table   :¥Æ¡¼¥Ö¥ë̾
213    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
214    function fast_insert($table, $sqlval) {
215        $strcol = '';
216        $strval = '';
217        $find = false;
218       
219        foreach ($sqlval as $key => $val) {
220                $strcol .= $key . ',';
221                if($val != ""){
222                    $eval = pg_escape_string($val);
223                    $strval .= "'$eval',";
224                } else {
225                    $strval .= "NULL,";
226                }
227                $find = true;
228        }
229        if(!$find) {
230            return false;
231        }
232        // ʸËö¤Î","¤òºï½ü
233        $strcol = ereg_replace(",$","",$strcol);
234        // ʸËö¤Î","¤òºï½ü
235        $strval = ereg_replace(",$","",$strval);
236        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
237       
238        // INSERTʸ¤Î¼Â¹Ô
239        $ret = $this->conn->query($sqlin);
240       
241        return $ret;       
242    }
243   
244   
245    // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô
246    // $table   :¥Æ¡¼¥Ö¥ë̾
247    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
248    // $where   :WHEREʸ»úÎó
249    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
250        $strcol = '';
251        $strval = '';
252        $find = false;
253        foreach ($sqlval as $key => $val) {
254            if(eregi("^Now\(\)$", $val)) {
255                $strcol .= $key . '= Now(),';
256            } else {
257                $strcol .= $key . '= ?,';
258                if($val != ""){
259                    $arrval[] = $val;
260                } else {
261                    $arrval[] = NULL;
262                }
263            }
264            $find = true;
265        }
266        if(!$find) {
267            return false;
268        }
269       
270        if($addcol != "") {
271            foreach($addcol as $key => $val) {
272                $strcol .= "$key = $val,";
273            }
274        }
275               
276        // ʸËö¤Î","¤òºï½ü
277        $strcol = ereg_replace(",$","",$strcol);
278        // ʸËö¤Î","¤òºï½ü
279        $strval = ereg_replace(",$","",$strval);
280       
281        if($where != "") {
282            $sqlup = "UPDATE $table SET $strcol WHERE $where";
283        } else {
284            $sqlup = "UPDATE $table SET $strcol";
285        }
286       
287        if(is_array($arradd)) {
288            // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲÃ
289            foreach($arradd as $val) {
290                $arrval[] = $val;
291            }
292        }
293       
294        // INSERTʸ¤Î¼Â¹Ô
295        $ret = $this->conn->query($sqlup, $arrval);
296        return $ret;       
297    }
298
299    // MAXʸ¤Î¼Â¹Ô
300    function max($table, $col, $where = "", $arrval = array()) {
301        if(strlen($where) <= 0) {
302            $sqlse = "SELECT MAX($col) FROM $table";
303        } else {
304            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
305        }
306        // MAXʸ¤Î¼Â¹Ô
307        $ret = $this->conn->getOne($sqlse, $arrval);
308        return $ret;
309    }
310   
311    // MINʸ¤Î¼Â¹Ô
312    function min($table, $col, $where = "", $arrval = array()) {
313        if(strlen($where) <= 0) {
314            $sqlse = "SELECT MIN($col) FROM $table";
315        } else {
316            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
317        }
318        // MINʸ¤Î¼Â¹Ô
319        $ret = $this->conn->getOne($sqlse, $arrval);
320        return $ret;
321    }
322   
323    // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ
324    function get($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->getOne($sqlse, $arrval);
332        return $ret;
333    }
334   
335    function getone($sql, $arrval = array()) {
336        // SQLʸ¤Î¼Â¹Ô
337        $ret = $this->conn->getOne($sql, $arrval);
338        return $ret;
339       
340    }
341       
342    // °ì¹Ô¤ò¼èÆÀ
343    function getrow($table, $col, $where = "", $arrval = array()) {
344        if(strlen($where) <= 0) {
345            $sqlse = "SELECT $col FROM $table";
346        } else {
347            $sqlse = "SELECT $col FROM $table WHERE $where";
348        }
349        // SQLʸ¤Î¼Â¹Ô
350        $ret = $this->conn->getRow($sqlse, $arrval);
351       
352        return $ret;
353    }
354       
355    // ¥ì¥³¡¼¥É¤Îºï½ü
356    function delete($table, $where = "", $arrval = array()) {
357        if(strlen($where) <= 0) {
358            $sqlde = "DELETE FROM $table";
359        } else {
360            $sqlde = "DELETE FROM $table WHERE $where";
361        }
362        $ret = $this->conn->query($sqlde, $arrval);
363        return $ret;
364    }
365   
366    function nextval($table, $colname) {
367        $sql = "";
368        // postgresql¤Èmysql¤È¤Ç½èÍý¤òʬ¤±¤ë
369        if (DB_TYPE == "pgsql") {
370            $seqtable = $table . "_" . $colname . "_seq";
371            $sql = "SELECT NEXTVAL('$seqtable')";
372        }else if (DB_TYPE == "mysql") {
373            $sql = "SELECT last_insert_id();";
374        }
375        $ret = $this->conn->getOne($sql);
376       
377        return $ret;
378    }
379   
380    function currval($table, $colname) {
381        $sql = "";
382        if (DB_TYPE == "pgsql") {
383            $seqtable = $table . "_" . $colname . "_seq";
384            $sql = "SELECT CURRVAL('$seqtable')";
385        }else if (DB_TYPE == "mysql") {
386            $sql = "SELECT last_insert_id();";
387        }
388        $ret = $this->conn->getOne($sql);
389       
390        return $ret;
391    }   
392   
393    function setval($table, $colname, $data) {
394        $sql = "";
395        if (DB_TYPE == "pgsql") {
396            $seqtable = $table . "_" . $colname . "_seq";
397            $sql = "SELECT SETVAL('$seqtable', $data)";
398            $ret = $this->conn->getOne($sql);
399        }else if (DB_TYPE == "mysql") {
400            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
401            $ret = $this->conn->query($sql);
402        }
403       
404        return $ret;
405    }       
406   
407    function query($n ,$arr = "", $ignore_err = false){
408        $result = $this->conn->query($n, $arr, $ignore_err);
409        return $result;
410    }
411   
412    // auto_increment¤ò¼èÆÀ¤¹¤ë
413    function get_auto_increment($table_name){
414        // ¥í¥Ã¥¯¤¹¤ë
415    //  $this->BEGIN();
416       
417        // ¼¡¤ÎIncrement¤ò¼èÆÀ
418        //$arrRet = $this->query("SHOW TABLE STATUS LIKE ?", array($table_name));
419        $arrRet = $this->query("SELECT * FROM $table_name");
420        sfprintr($arrRet);
421        $auto_inc_no = $arrRet["Auto_increment"];
422       
423        // Ãͤò¥«¥¦¥ó¥È¥¢¥Ã¥×¤·¤Æ¤ª¤¯
424        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
425       
426        // ²ò½ü¤¹¤ë
427    //  $this->COMMIT();
428       
429        sfprintr($auto_inc_no);
430        exit();
431       
432        return $auto_inc_no;
433    }
434
435}
436?>
Note: See TracBrowser for help on using the repository browser.