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

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

phpdocumentor用のコメントを記述

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