source: branches/dev/data/class/SC_Query.php @ 13291

Revision 13291, 12.0 KB checked in by adati, 19 years ago (diff)

phpdocumentor用のコメントを記述

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