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

Revision 13344, 13.8 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 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  mixed   $limit  LIMIT¤Î·ï¿ô
216     *  @param  mixed   $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    /**
235     *  GROUP BY ¶ç¤ò¥»¥Ã¥È¤¹¤ë
236     *
237     *  @access public
238     *  @param  string  $str ¥«¥é¥à̾
239     */
240    function setgroupby($str) {
241        $this->groupby = "GROUP BY " . $str;
242    }
243   
244    /**
245     *  WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë(AND)
246     *
247     *  @access  public
248     *  @param   string  $str WHERE ¶ç
249     *  @example $objQuery->andWhere('product_id = ?');
250     */
251    function andwhere($str) {
252        if($this->where != "") {
253            $this->where .= " AND " . $str;
254        } else {
255            $this->where = $str;
256        }
257    }
258   
259    /**
260     *  WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë(OR)
261     *
262     *  @access  public
263     *  @param   string  $str WHERE ¶ç
264     *  @example $objQuery->orWhere('product_id = ?');
265     */
266    function orwhere($str) {
267        if($this->where != "") {
268            $this->where .= " OR " . $str;
269        } else {
270            $this->where = $str;
271        }
272    }
273   
274    /**
275     *  WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë
276     *
277     *  @access  public
278     *  @param   string  $str WHERE ¶ç
279     *  @example $objQuery->setWhere('product_id = ?');
280     */
281    function setwhere($str) {
282        $this->where = $str;
283    }
284   
285    /**
286     *  ORDER BY ¶ç¤ò¥»¥Ã¥È¤¹¤ë
287     *
288     *  @access  public
289     *  @param   string  $str ¥«¥é¥à̾
290     *  @example $objQuery->setorder("rank DESC");
291     */
292    function setorder($str) {
293        $this->order = "ORDER BY " . $str;
294    }
295   
296    /**
297     *  LIMIT ¶ç¤ò¥»¥Ã¥È¤¹¤ë
298     *
299     *  @access  public
300     *  @param   mixed  $limit LIMIT¤Î·ï¿ô
301     *  @example $objQuery->setlimit(50);
302     */
303    function setlimit($limit){
304        if ( is_numeric($limit)){
305            $this->option = " LIMIT " .$limit;
306        }   
307    }
308   
309    /**
310     *  OFFSET ¶ç¤ò¥»¥Ã¥È¤¹¤ë
311     *
312     *  @access  public
313     *  @param   mixed  $offset OFFSET¤Î·ï¿ô
314     *  @example $objQuery->setOffset(30);
315     */
316    function setoffset($offset) {
317        if ( is_numeric($offset)){
318            $this->offset = " OFFSET " .$offset;
319        }   
320    }
321   
322    /**
323     *  INSERTʸ¤ò¼Â¹Ô¤¹¤ë
324     *
325     *  @access  public
326     *  @param   string  $table  ¥Æ¡¼¥Ö¥ë̾
327     *  @param   array   $sqlval (¥«¥é¥à̾ => ÃÍ)¤ÎÏ¢ÁÛÇÛÎó
328     * 
329     *  @return  mixed   $result DB_Error¥ª¥Ö¥¸¥§¥¯¥È(¼ºÇÔ»þ)¤Þ¤¿¤ÏDB_OK(À®¸ù»þ)¤Þ¤¿¤Ïfalse(¥«¥é¥à¤¬¸«¤Ä¤«¤é¤Ê¤¤)
330     */
331    function insert($table, $sqlval) {
332        $strcol = '';
333        $strval = '';
334        $find = false;
335       
336        if(count($sqlval) <= 0 ) return false;
337       
338        foreach ($sqlval as $key => $val) {
339            $strcol .= $key . ',';
340            if(eregi("^Now\(\)$", $val)) {
341                $strval .= 'Now(),';
342            } else {
343                $strval .= '?,';
344                if($val != ""){
345                    $arrval[] = $val;
346                } else {
347                    $arrval[] = NULL;
348                }
349            }
350            $find = true;
351        }
352        if(!$find) {
353            return false;
354        }
355        // ʸËö¤Î","¤òºï½ü
356        $strcol = ereg_replace(",$","",$strcol);
357        // ʸËö¤Î","¤òºï½ü
358        $strval = ereg_replace(",$","",$strval);
359        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
360       
361        // INSERTʸ¤Î¼Â¹Ô
362        $ret = $this->conn->query($sqlin, $arrval);
363       
364        return $ret;       
365    }
366   
367    /**
368     *  INSERTʸ¤ò¼Â¹Ô¤¹¤ë
369     *
370     *  @access  public
371     *  @param   string  $table  ¥Æ¡¼¥Ö¥ë̾
372     *  @param   array   $sqlval (¥«¥é¥à̾ => ÃÍ)¤ÎÏ¢ÁÛÇÛÎó
373     * 
374     *  @return  mixed   $result DB_Error¥ª¥Ö¥¸¥§¥¯¥È(¼ºÇÔ»þ)¤Þ¤¿¤ÏDB_OK(À®¸ù»þ)¤Þ¤¿¤Ïfalse(¥«¥é¥à¤¬¸«¤Ä¤«¤é¤Ê¤¤)
375     */
376    function fast_insert($table, $sqlval) {
377        $strcol = '';
378        $strval = '';
379        $find = false;
380       
381        foreach ($sqlval as $key => $val) {
382                $strcol .= $key . ',';
383                if($val != ""){
384                    $eval = pg_escape_string($val);
385                    $strval .= "'$eval',";
386                } else {
387                    $strval .= "NULL,";
388                }
389                $find = true;
390        }
391        if(!$find) {
392            return false;
393        }
394        // ʸËö¤Î","¤òºï½ü
395        $strcol = ereg_replace(",$","",$strcol);
396        // ʸËö¤Î","¤òºï½ü
397        $strval = ereg_replace(",$","",$strval);
398        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
399       
400        // INSERTʸ¤Î¼Â¹Ô
401        $ret = $this->conn->query($sqlin);
402       
403        return $ret;
404    }
405   
406   
407    // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô
408    // $table   :¥Æ¡¼¥Ö¥ë̾
409    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
410    // $where   :WHEREʸ»úÎó
411    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
412        $strcol = '';
413        $strval = '';
414        $find = false;
415        foreach ($sqlval as $key => $val) {
416            if(eregi("^Now\(\)$", $val)) {
417                $strcol .= $key . '= Now(),';
418            } else {
419                $strcol .= $key . '= ?,';
420                if($val != ""){
421                    $arrval[] = $val;
422                } else {
423                    $arrval[] = NULL;
424                }
425            }
426            $find = true;
427        }
428        if(!$find) {
429            return false;
430        }
431       
432        if($addcol != "") {
433            foreach($addcol as $key => $val) {
434                $strcol .= "$key = $val,";
435            }
436        }
437               
438        // ʸËö¤Î","¤òºï½ü
439        $strcol = ereg_replace(",$","",$strcol);
440        // ʸËö¤Î","¤òºï½ü
441        $strval = ereg_replace(",$","",$strval);
442       
443        if($where != "") {
444            $sqlup = "UPDATE $table SET $strcol WHERE $where";
445        } else {
446            $sqlup = "UPDATE $table SET $strcol";
447        }
448       
449        if(is_array($arradd)) {
450            // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲÃ
451            foreach($arradd as $val) {
452                $arrval[] = $val;
453            }
454        }
455       
456        // INSERTʸ¤Î¼Â¹Ô
457        $ret = $this->conn->query($sqlup, $arrval);
458        return $ret;       
459    }
460
461    // MAXʸ¤Î¼Â¹Ô
462    function max($table, $col, $where = "", $arrval = array()) {
463        if(strlen($where) <= 0) {
464            $sqlse = "SELECT MAX($col) FROM $table";
465        } else {
466            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
467        }
468        // MAXʸ¤Î¼Â¹Ô
469        $ret = $this->conn->getOne($sqlse, $arrval);
470        return $ret;
471    }
472   
473    // MINʸ¤Î¼Â¹Ô
474    function min($table, $col, $where = "", $arrval = array()) {
475        if(strlen($where) <= 0) {
476            $sqlse = "SELECT MIN($col) FROM $table";
477        } else {
478            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
479        }
480        // MINʸ¤Î¼Â¹Ô
481        $ret = $this->conn->getOne($sqlse, $arrval);
482        return $ret;
483    }
484   
485    // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ
486    function get($table, $col, $where = "", $arrval = array()) {
487        if(strlen($where) <= 0) {
488            $sqlse = "SELECT $col FROM $table";
489        } else {
490            $sqlse = "SELECT $col FROM $table WHERE $where";
491        }
492        // SQLʸ¤Î¼Â¹Ô
493        $ret = $this->conn->getOne($sqlse, $arrval);
494        return $ret;
495    }
496   
497    function getone($sql, $arrval = array()) {
498        // SQLʸ¤Î¼Â¹Ô
499        $ret = $this->conn->getOne($sql, $arrval);
500        return $ret;
501       
502    }
503       
504    // °ì¹Ô¤ò¼èÆÀ
505    function getrow($table, $col, $where = "", $arrval = array()) {
506        if(strlen($where) <= 0) {
507            $sqlse = "SELECT $col FROM $table";
508        } else {
509            $sqlse = "SELECT $col FROM $table WHERE $where";
510        }
511        // SQLʸ¤Î¼Â¹Ô
512        $ret = $this->conn->getRow($sqlse, $arrval);
513       
514        return $ret;
515    }
516       
517    // ¥ì¥³¡¼¥É¤Îºï½ü
518    function delete($table, $where = "", $arrval = array()) {
519        if(strlen($where) <= 0) {
520            $sqlde = "DELETE FROM $table";
521        } else {
522            $sqlde = "DELETE FROM $table WHERE $where";
523        }
524        $ret = $this->conn->query($sqlde, $arrval);
525        return $ret;
526    }
527   
528    //»ØÄꤷ¤¿¥«¥é¥à¤Î°ìÈֺǸå¤Ë¥ì¥³¡¼¥É¤òÁÞÆþ
529    function nextval($table, $colname) {
530        $sql = "";
531        // postgresql¤Èmysql¤È¤Ç½èÍý¤òʬ¤±¤ë
532        if (DB_TYPE == "pgsql") {
533            $seqtable = $table . "_" . $colname . "_seq";
534            $sql = "SELECT NEXTVAL('$seqtable')";
535            $ret = $this->conn->getOne($sql);
536        }else if (DB_TYPE == "mysql") {
537            $sql = "SELECT last_insert_id();";
538            $ret = $this->conn->getOne($sql);
539        }
540       
541       
542        return $ret;
543    }
544   
545    function currval($table, $colname) {
546        $sql = "";
547        if (DB_TYPE == "pgsql") {
548            $seqtable = $table . "_" . $colname . "_seq";
549            $sql = "SELECT CURRVAL('$seqtable')";
550        }else if (DB_TYPE == "mysql") {
551            $sql = "SELECT last_insert_id();";
552        }
553        $ret = $this->conn->getOne($sql);
554       
555        return $ret;
556    }   
557   
558    function setval($table, $colname, $data) {
559        $sql = "";
560        if (DB_TYPE == "pgsql") {
561            $seqtable = $table . "_" . $colname . "_seq";
562            $sql = "SELECT SETVAL('$seqtable', $data)";
563            $ret = $this->conn->getOne($sql);
564        }else if (DB_TYPE == "mysql") {
565            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
566            $ret = $this->conn->query($sql);
567        }
568       
569        return $ret;
570    }       
571   
572    function query($n ,$arr = "", $ignore_err = false){
573        $result = $this->conn->query($n, $arr, $ignore_err);
574        return $result;
575    }
576   
577    // auto_increment¤ò¼èÆÀ¤¹¤ë
578    function get_auto_increment($table_name){
579        // ¥í¥Ã¥¯¤¹¤ë
580        $this->BEGIN();
581       
582        // ¼¡¤ÎIncrement¤ò¼èÆÀ
583        $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
584        $auto_inc_no = $arrRet[0]["Auto_increment"];
585       
586        // Ãͤò¥«¥¦¥ó¥È¥¢¥Ã¥×¤·¤Æ¤ª¤¯
587        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
588       
589        // ²ò½ü¤¹¤ë
590        $this->COMMIT();
591       
592        return $auto_inc_no;
593    }
594}
595?>
Note: See TracBrowser for help on using the repository browser.