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

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