Changeset 18590


Ignore:
Timestamp:
2010/03/02 16:12:33 (14 years ago)
Author:
Seasoft
Message:
  • #615(SQL文生成時にパラメータが無視されるケースがある)改修
  • 分散しているSQL文生成ロジックを集約
  • SQL文生成ロジックを整理
  • 未参照メソッドの削除
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/comu-ver2/data/class/SC_Query.php

    r18487 r18590  
    122122    } 
    123123 
    124     function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { 
    125         $strw = ""; 
    126         $find = false; 
    127         foreach ($arrwhere as $key => $val) { 
    128             if(strlen($val) > 0) { 
    129                 if(strlen($strw) <= 0) { 
    130                     $strw .= $key ." LIKE ?"; 
    131                 } else if(strlen($arrcon[$key]) > 0) { 
    132                     $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; 
    133                 } else { 
    134                     $strw .= " AND " . $key ." LIKE ?"; 
    135                 } 
    136  
    137                 $arrval[] = $val; 
    138             } 
    139         } 
    140  
    141         if(strlen($strw) > 0) { 
    142             $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; 
    143         } else { 
    144             $sqlse = "SELECT $col FROM $table ".$this->option; 
    145         } 
    146         $ret = $this->conn->getAll($sqlse, $arrval); 
    147         return $ret; 
    148     } 
    149  
    150124    /** 
    151125     * クエリを実行し、全ての行を返す 
     
    161135    } 
    162136 
    163     function getsql($col, $table, $where) { 
    164         if($where != "") { 
    165             // 引数の$whereを優先して実行する。 
    166             $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; 
    167         } else { 
    168             if($this->where != "") { 
    169                     $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; 
    170                 } else { 
    171                     $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; 
    172             } 
    173         } 
     137    function getsql($col, $table, $where = '') { 
     138        $sqlse = "SELECT $col FROM $table"; 
     139 
     140        // 引数の$whereを優先する。 
     141        if (strlen($where) >= 1) { 
     142            $sqlse .= " WHERE $where"; 
     143        } elseif (strlen($this->where) >= 1) { 
     144            $where = $this->where; 
     145        } 
     146 
     147        $sqlse .= ' ' . $this->groupby . ' ' . $this->order . ' ' . $this->option; 
     148 
    174149        return $sqlse; 
    175150    } 
     
    336311    // MAX文の実行 
    337312    function max($table, $col, $where = "", $arrval = array()) { 
    338         if(strlen($where) <= 0) { 
    339             $sqlse = "SELECT MAX($col) FROM $table"; 
    340         } else { 
    341             $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; 
    342         } 
    343         // MAX文の実行 
    344         $ret = $this->conn->getOne($sqlse, $arrval); 
     313        $ret = $this->get($table, "MAX($col)", $where); 
    345314        return $ret; 
    346315    } 
     
    348317    // MIN文の実行 
    349318    function min($table, $col, $where = "", $arrval = array()) { 
    350         if(strlen($where) <= 0) { 
    351             $sqlse = "SELECT MIN($col) FROM $table"; 
    352         } else { 
    353             $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; 
    354         } 
    355         // MIN文の実行 
    356         $ret = $this->conn->getOne($sqlse, $arrval); 
     319        $ret = $this->get($table, "MIN($col)", $where); 
    357320        return $ret; 
    358321    } 
     
    360323    // 特定のカラムの値を取得 
    361324    function get($table, $col, $where = "", $arrval = array()) { 
    362         if(strlen($where) <= 0) { 
    363             $sqlse = "SELECT $col FROM $table"; 
    364         } else { 
    365             $sqlse = "SELECT $col FROM $table WHERE $where"; 
    366         } 
     325        $sqlse = $this->getsql($col, $table, $where); 
    367326        // SQL文の実行 
    368         $ret = $this->conn->getOne($sqlse, $arrval); 
     327        $ret = $this->getone($sqlse, $arrval); 
    369328        return $ret; 
    370329    } 
     
    374333        $ret = $this->conn->getOne($sql, $arrval); 
    375334        return $ret; 
    376  
    377335    } 
    378336 
     
    388346     */ 
    389347    function getRow($table, $col, $where = "", $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 
    390         $sqlse = "SELECT $col FROM $table"; 
    391          
    392         if (strlen($where) >= 1) { 
    393             $sqlse .= " WHERE $where"; 
    394         } 
     348        $sqlse = $this->getsql($col, $table, $where); 
    395349        // SQL文の実行 
    396350        return $this->conn->getRow($sqlse, $arrVal ,$fetchmode); 
     
    399353    // 1列取得 
    400354    function getCol($table, $col, $where = "", $arrval = array()) { 
    401         if (strlen($where) <= 0) { 
    402             $sqlse = "SELECT $col FROM $table"; 
    403         } else { 
    404             $sqlse = "SELECT $col FROM $table WHERE $where"; 
    405         } 
     355        $sqlse = $this->getsql($col, $table, $where); 
    406356        // SQL文の実行 
    407357        return $this->conn->getCol($sqlse, 0, $arrval); 
Note: See TracChangeset for help on using the changeset viewer.