source: branches/beta/data/class/SC_Query.php @ 14919

Revision 14919, 14.3 KB checked in by uehara, 17 years ago (diff)

CSVアップロード改修
MySQLのauto_incrementバグに対応

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     *  ¥ª¥×¥·¥ç¥ó¤Î½é´ü²½
82     *
83     *  @access public
84     *  @return void
85     */
86    function clear(){
87        $arrProperty = array_keys((get_object_vars($this)));
88        foreach ( $arrProperty as $property ) {
89            if ($property != 'conn') {
90                $this->$property = '';
91            }
92        }
93    }
94   
95    /**
96     *  COUNTʸ¤Î¼Â¹Ô
97     *
98     *  @access public
99     *  @param  string  $table  ¥Æ¡¼¥Ö¥ë̾
100     *  @param  string  $where  WHERE¶ç
101     *  @param  array   $arrval ¥×¥ì¡¼¥¹¥Û¥ë¥À¤ÎÇÛÎó
102     *  @return string  ¥ì¥³¡¼¥É·ï¿ô
103     */
104    function count($table, $where = "", $arrval = array()) {
105        if(strlen($where) <= 0) {
106            $sqlse = "SELECT COUNT(*) FROM $table";
107        } else {
108            $sqlse = "SELECT COUNT(*) FROM $table WHERE $where";
109        }
110        // ¥«¥¦¥ó¥Èʸ¤Î¼Â¹Ô
111        $ret = $this->conn->getOne($sqlse, $arrval);
112        return $ret;
113    }
114   
115    /**
116     *  SELECTʸ¤Î¼Â¹Ô
117     *
118     *  @access public
119     *  @param  string  $col    ¥«¥é¥à̾
120     *  @param  string  $table  ¥Æ¡¼¥Ö¥ë̾
121     *  @param  string  $where  WHERE¶ç
122     *  @param  array   $arrval ¥×¥ì¡¼¥¹¥Û¥ë¥À¤ÎÇÛÎó
123     *  @return array   SELECTʸ¤Î¼Â¹Ô·ë²Ì
124     */
125    function select($col, $table, $where = "", $arrval = array()){
126        $sqlse = $this->getsql($col, $table, $where);
127        $ret = $this->conn->getAll($sqlse, $arrval);
128        return $ret;
129    }
130   
131    /**
132     *  ºÇ¸å¤Ë¼Â¹Ô¤·¤¿SQLʸ¤ò¼èÆÀ¤¹¤ë
133     *
134     *  @access public
135     *  @param  boolean $disp SQLʸ¤òprint¤¹¤ë¤«¤É¤¦¤«
136     *  @return string  $disp==false¤Î¾ì¹ç¡§ºÇ¸å¤Ë¼Â¹Ô¤·¤¿SQLʸ¡¡$disp==true¤Î¾ì¹ç¡§¤Ê¤·
137     */
138    function getLastQuery($disp = true) {
139        $sql = $this->conn->conn->last_query;
140        if($disp) {
141            print($sql.";<br />\n");
142        }
143        return $sql;
144    }
145
146    function commit() {
147        $this->conn->query("COMMIT");
148    }
149   
150    function begin() {
151        $this->conn->query("BEGIN");
152    }
153   
154    function rollback() {
155        $this->conn->query("ROLLBACK");
156    }
157   
158    function exec($str, $arrval = array()) {
159        $this->conn->query($str, $arrval);
160    }
161
162    function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) {
163        $strw = "";         
164        $find = false;
165        foreach ($arrwhere as $key => $val) {
166            if(strlen($val) > 0) {
167                if(strlen($strw) <= 0) {
168                    $strw .= $key ." LIKE ?";
169                } else if(strlen($arrcon[$key]) > 0) {
170                    $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?";
171                } else {
172                    $strw .= " AND " . $key ." LIKE ?";
173                }
174               
175                $arrval[] = $val;
176            }
177        }
178       
179        if(strlen($strw) > 0) {
180            $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option;
181        } else {
182            $sqlse = "SELECT $col FROM $table ".$this->option;
183        }
184        $ret = $this->conn->getAll($sqlse, $arrval);
185        return $ret;
186    }
187   
188    function getall($sql, $arrval = array()) {
189        $ret = $this->conn->getAll($sql, $arrval);
190        return $ret;
191    }
192
193    /**
194     *  SELECTʸ¤ò¹½ÃÛ¤¹¤ë
195     *
196     *  @access public
197     *  @param  string  $col    ¥«¥é¥à̾
198     *  @param  string  $table  ¥Æ¡¼¥Ö¥ë̾
199     *  @param  string  $where  WHERE¶ç
200     *  @return string  SQLʸ
201     */
202    function getsql($col, $table, $where="") {
203        if($where != "") {
204            // °ú¿ô¤Î$where¤òÍ¥À褷¤Æ¼Â¹Ô¤¹¤ë¡£
205            $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option;
206        } else {
207            if($this->where != "") {
208                    $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option;
209                } else {
210                    $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option;
211            }
212        }
213        return $sqlse;
214    }
215   
216    /**
217     *  WHERE,GROUPBY,ORDERBY°Ê³°¤Î¥ª¥×¥·¥ç¥Ê¥ë¤Ê¶ç¤ò¥»¥Ã¥È¤¹¤ë
218     *
219     *  @access public
220     *  @param  string  $str ¥ª¥×¥·¥ç¥ó¤Ë»ÈÍѤ¹¤ëʸ»úÎó
221     */
222    function setoption($str) {
223        $this->option = $str;
224    }
225   
226    /**
227     *  LIMIT¶ç¡¢OFFSET¶ç¤ò¥»¥Ã¥È¤¹¤ë
228     *
229     *  @access public
230     *  @param  mixed   $limit  LIMIT¤Î·ï¿ô
231     *  @param  mixed   $offset OFFSET¤Î·ï¿ô
232     *  @param  string  $return À¸À®¤·¤¿LIMIT,OFFSET¶ç¤òreturn¤¹¤ë¤«¤É¤¦¤«
233     *  @return string  À¸À®¤·¤¿LIMIT,OFFSET¶ç
234     */
235    function setlimitoffset($limit, $offset = 0, $return = false) {
236        if (is_numeric($limit) && is_numeric($offset)){
237           
238            $option.= " LIMIT " . $limit;
239            $option.= " OFFSET " . $offset;
240           
241            if($return){
242                return $option;
243            }else{
244                $this->option.= $option;
245            }
246        }
247    }
248
249    /**
250     *  GROUP BY ¶ç¤ò¥»¥Ã¥È¤¹¤ë
251     *
252     *  @access public
253     *  @param  string  $str ¥«¥é¥à̾
254     */
255    function setgroupby($str) {
256        $this->groupby = "GROUP BY " . $str;
257    }
258   
259    /**
260     *  WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë(AND)
261     *
262     *  @access  public
263     *  @param   string  $str WHERE ¶ç
264     *  @example $objQuery->andWhere('product_id = ?');
265     */
266    function andwhere($str) {
267        if($this->where != "") {
268            $this->where .= " AND " . $str;
269        } else {
270            $this->where = $str;
271        }
272    }
273   
274    /**
275     *  WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë(OR)
276     *
277     *  @access  public
278     *  @param   string  $str WHERE ¶ç
279     *  @example $objQuery->orWhere('product_id = ?');
280     */
281    function orwhere($str) {
282        if($this->where != "") {
283            $this->where .= " OR " . $str;
284        } else {
285            $this->where = $str;
286        }
287    }
288   
289    /**
290     *  WHERE ¶ç¤ò¥»¥Ã¥È¤¹¤ë
291     *
292     *  @access  public
293     *  @param   string  $str WHERE ¶ç
294     *  @example $objQuery->setWhere('product_id = ?');
295     */
296    function setwhere($str) {
297        $this->where = $str;
298    }
299   
300    /**
301     *  ORDER BY ¶ç¤ò¥»¥Ã¥È¤¹¤ë
302     *
303     *  @access  public
304     *  @param   string  $str ¥«¥é¥à̾
305     *  @example $objQuery->setorder("rank DESC");
306     */
307    function setorder($str) {
308        $this->order = "ORDER BY " . $str;
309    }
310   
311    /**
312     *  LIMIT ¶ç¤ò¥»¥Ã¥È¤¹¤ë
313     *
314     *  @access  public
315     *  @param   mixed  $limit LIMIT¤Î·ï¿ô
316     *  @example $objQuery->setlimit(50);
317     */
318    function setlimit($limit){
319        if ( is_numeric($limit)){
320            $this->option = " LIMIT " .$limit;
321        }   
322    }
323   
324    /**
325     *  OFFSET ¶ç¤ò¥»¥Ã¥È¤¹¤ë
326     *
327     *  @access  public
328     *  @param   mixed  $offset OFFSET¤Î·ï¿ô
329     *  @example $objQuery->setOffset(30);
330     */
331    function setoffset($offset) {
332        if ( is_numeric($offset)){
333            $this->offset = " OFFSET " .$offset;
334        }   
335    }
336   
337    /**
338     *  INSERTʸ¤ò¼Â¹Ô¤¹¤ë
339     *
340     *  @access  public
341     *  @param   string  $table  ¥Æ¡¼¥Ö¥ë̾
342     *  @param   array   $sqlval (¥«¥é¥à̾ => ÃÍ)¤ÎÏ¢ÁÛÇÛÎó
343     * 
344     *  @return  mixed   $result DB_Error¥ª¥Ö¥¸¥§¥¯¥È(¼ºÇÔ»þ)¤Þ¤¿¤ÏDB_OK(À®¸ù»þ)¤Þ¤¿¤Ïfalse(¥«¥é¥à¤¬¸«¤Ä¤«¤é¤Ê¤¤)
345     */
346    function insert($table, $sqlval) {
347        $strcol = '';
348        $strval = '';
349        $find = false;
350       
351        if(count($sqlval) <= 0 ) return false;
352       
353        foreach ($sqlval as $key => $val) {
354            $strcol .= $key . ',';
355            if(eregi("^Now\(\)$", $val)) {
356                $strval .= 'Now(),';
357            } else {
358                $strval .= '?,';
359                if($val != ""){
360                    $arrval[] = $val;
361                } else {
362                    $arrval[] = NULL;
363                }
364            }
365            $find = true;
366        }
367        if(!$find) {
368            return false;
369        }
370        // ʸËö¤Î","¤òºï½ü
371        $strcol = ereg_replace(",$","",$strcol);
372        // ʸËö¤Î","¤òºï½ü
373        $strval = ereg_replace(",$","",$strval);
374        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
375       
376        // INSERTʸ¤Î¼Â¹Ô
377        $ret = $this->conn->query($sqlin, $arrval);
378       
379        return $ret;       
380    }
381   
382    /**
383     *  INSERTʸ¤ò¼Â¹Ô¤¹¤ë
384     *
385     *  @access  public
386     *  @param   string  $table  ¥Æ¡¼¥Ö¥ë̾
387     *  @param   array   $sqlval (¥«¥é¥à̾ => ÃÍ)¤ÎÏ¢ÁÛÇÛÎó
388     * 
389     *  @return  mixed   $result DB_Error¥ª¥Ö¥¸¥§¥¯¥È(¼ºÇÔ»þ)¤Þ¤¿¤ÏDB_OK(À®¸ù»þ)¤Þ¤¿¤Ïfalse(¥«¥é¥à¤¬¸«¤Ä¤«¤é¤Ê¤¤)
390     */
391    function fast_insert($table, $sqlval) {
392        $strcol = '';
393        $strval = '';
394        $find = false;
395       
396        foreach ($sqlval as $key => $val) {
397                $strcol .= $key . ',';
398                if($val != ""){
399                    $eval = pg_escape_string($val);
400                    $strval .= "'$eval',";
401                } else {
402                    $strval .= "NULL,";
403                }
404                $find = true;
405        }
406        if(!$find) {
407            return false;
408        }
409        // ʸËö¤Î","¤òºï½ü
410        $strcol = ereg_replace(",$","",$strcol);
411        // ʸËö¤Î","¤òºï½ü
412        $strval = ereg_replace(",$","",$strval);
413        $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";
414       
415        // INSERTʸ¤Î¼Â¹Ô
416        $ret = $this->conn->query($sqlin);
417       
418        return $ret;
419    }
420   
421   
422    // UPDATEʸ¤ÎÀ¸À®¡¦¼Â¹Ô
423    // $table   :¥Æ¡¼¥Ö¥ë̾
424    // $sqlval  :Îó̾ => ÃͤγÊǼ¤µ¤ì¤¿¥Ï¥Ã¥·¥åÇÛÎó
425    // $where   :WHEREʸ»úÎó
426    function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") {
427        $strcol = '';
428        $strval = '';
429        $find = false;
430        foreach ($sqlval as $key => $val) {
431            if(eregi("^Now\(\)$", $val)) {
432                $strcol .= $key . '= Now(),';
433            } else {
434                $strcol .= $key . '= ?,';
435                if($val != ""){
436                    $arrval[] = $val;
437                } else {
438                    $arrval[] = NULL;
439                }
440            }
441            $find = true;
442        }
443        if(!$find) {
444            return false;
445        }
446       
447        if($addcol != "") {
448            foreach($addcol as $key => $val) {
449                $strcol .= "$key = $val,";
450            }
451        }
452               
453        // ʸËö¤Î","¤òºï½ü
454        $strcol = ereg_replace(",$","",$strcol);
455        // ʸËö¤Î","¤òºï½ü
456        $strval = ereg_replace(",$","",$strval);
457       
458        if($where != "") {
459            $sqlup = "UPDATE $table SET $strcol WHERE $where";
460        } else {
461            $sqlup = "UPDATE $table SET $strcol";
462        }
463       
464        if(is_array($arradd)) {
465            // ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼ÍѤËÇÛÎó¤òÄɲÃ
466            foreach($arradd as $val) {
467                $arrval[] = $val;
468            }
469        }
470       
471        // INSERTʸ¤Î¼Â¹Ô
472        $ret = $this->conn->query($sqlup, $arrval);
473        return $ret;       
474    }
475
476    // MAXʸ¤Î¼Â¹Ô
477    function max($table, $col, $where = "", $arrval = array()) {
478        if(strlen($where) <= 0) {
479            $sqlse = "SELECT MAX($col) FROM $table";
480        } else {
481            $sqlse = "SELECT MAX($col) FROM $table WHERE $where";
482        }
483        // MAXʸ¤Î¼Â¹Ô
484        $ret = $this->conn->getOne($sqlse, $arrval);
485        return $ret;
486    }
487   
488    // MINʸ¤Î¼Â¹Ô
489    function min($table, $col, $where = "", $arrval = array()) {
490        if(strlen($where) <= 0) {
491            $sqlse = "SELECT MIN($col) FROM $table";
492        } else {
493            $sqlse = "SELECT MIN($col) FROM $table WHERE $where";
494        }
495        // MINʸ¤Î¼Â¹Ô
496        $ret = $this->conn->getOne($sqlse, $arrval);
497        return $ret;
498    }
499   
500    // ÆÃÄê¤Î¥«¥é¥à¤ÎÃͤò¼èÆÀ
501    function get($table, $col, $where = "", $arrval = array()) {
502        if(strlen($where) <= 0) {
503            $sqlse = "SELECT $col FROM $table";
504        } else {
505            $sqlse = "SELECT $col FROM $table WHERE $where";
506        }
507        // SQLʸ¤Î¼Â¹Ô
508        $ret = $this->conn->getOne($sqlse, $arrval);
509        return $ret;
510    }
511   
512    function getone($sql, $arrval = array()) {
513        // SQLʸ¤Î¼Â¹Ô
514        $ret = $this->conn->getOne($sql, $arrval);
515        return $ret;
516       
517    }
518       
519    // °ì¹Ô¤ò¼èÆÀ
520    function getrow($table, $col, $where = "", $arrval = array()) {
521        if(strlen($where) <= 0) {
522            $sqlse = "SELECT $col FROM $table";
523        } else {
524            $sqlse = "SELECT $col FROM $table WHERE $where";
525        }
526        // SQLʸ¤Î¼Â¹Ô
527        $ret = $this->conn->getRow($sqlse, $arrval);
528       
529        return $ret;
530    }
531       
532    // ¥ì¥³¡¼¥É¤Îºï½ü
533    function delete($table, $where = "", $arrval = array()) {
534        if(strlen($where) <= 0) {
535            $sqlde = "DELETE FROM $table";
536        } else {
537            $sqlde = "DELETE FROM $table WHERE $where";
538        }
539        $ret = $this->conn->query($sqlde, $arrval);
540        return $ret;
541    }
542   
543    //»ØÄꤷ¤¿¥«¥é¥à¤Î°ìÈֺǸå¤Ë¥ì¥³¡¼¥É¤òÁÞÆþ
544    function nextval($table, $colname) {
545        $sql = "";
546        // postgresql¤Èmysql¤È¤Ç½èÍý¤òʬ¤±¤ë
547        if (DB_TYPE == "pgsql") {
548            $seqtable = $table . "_" . $colname . "_seq";
549            $sql = "SELECT NEXTVAL('$seqtable')";
550            $ret = $this->conn->getOne($sql);
551        }else if (DB_TYPE == "mysql") {
552            $sql = "SELECT last_insert_id();";
553            $ret = $this->conn->getOne($sql);
554        }
555       
556       
557        return $ret;
558    }
559   
560    function currval($table, $colname) {
561        $sql = "";
562        if (DB_TYPE == "pgsql") {
563            $seqtable = $table . "_" . $colname . "_seq";
564            $sql = "SELECT CURRVAL('$seqtable')";
565        }else if (DB_TYPE == "mysql") {
566            $sql = "SELECT last_insert_id();";
567        }
568        $ret = $this->conn->getOne($sql);
569       
570        return $ret;
571    }   
572   
573    function setval($table, $colname, $data) {
574        $sql = "";
575        if (DB_TYPE == "pgsql") {
576            $seqtable = $table . "_" . $colname . "_seq";
577            $sql = "SELECT SETVAL('$seqtable', $data)";
578            $ret = $this->conn->getOne($sql);
579        }else if (DB_TYPE == "mysql") {
580            $sql = "ALTER TABLE $table AUTO_INCREMENT=$data";
581            $ret = $this->conn->query($sql);
582        }
583       
584        return $ret;
585    }       
586   
587    function query($n ,$arr = "", $ignore_err = false){
588        $result = $this->conn->query($n, $arr, $ignore_err);
589        return $result;
590    }
591   
592    // auto_increment¤ò¼èÆÀ¤¹¤ë
593    function get_auto_increment($table_name){
594        // ¥í¥Ã¥¯¤¹¤ë
595        $this->query("LOCK TABLES $table_name WRITE");
596       
597        // ¼¡¤ÎIncrement¤ò¼èÆÀ
598        $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name));
599        $auto_inc_no = $arrRet[0]["Auto_increment"];
600       
601        // Ãͤò¥«¥¦¥ó¥È¥¢¥Ã¥×¤·¤Æ¤ª¤¯
602        $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1);
603       
604        // ²ò½ü¤¹¤ë
605        $this->query('UNLOCK TABLES');
606       
607        return $auto_inc_no;
608    }
609}
610?>
Note: See TracBrowser for help on using the repository browser.