Ignore:
Timestamp:
2010/03/11 10:35:11 (16 years ago)
Author:
kajiwara
Message:

正式版にナイトリービルド版をマージしてみるテスト

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tmp/version-2_5-test/data/class/SC_Query.php

    r18562 r18609  
    8686     * @param string $where WHERE句 
    8787     * @param array $arrval プレースホルダ 
     88     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 
    8889     * @return array|null 
    8990     */ 
    90     function select($col, $table, $where = "", $arrval = array()){ 
     91    function select($col, $table, $where = "", $arrval = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 
    9192        $sqlse = $this->getsql($col, $table, $where); 
    92         // DBに依存した SQL へ変換 
    93         $dbFactory = SC_DB_DBFactory_Ex::getInstance(); 
    94         $sqlse = $dbFactory->sfChangeMySQL($sqlse); 
    95         $ret = $this->conn->getAll($sqlse, $arrval); 
     93        $ret = $this->conn->getAll($sqlse, $arrval, $fetchmode); 
    9694        return $ret; 
    9795    } 
     
    9997    /** 
    10098     * 直前に実行されたSQL文を取得する. 
     99     * SC_DBconn::getLastQuery() を利用. 
    101100     * 
    102101     * @param boolean $disp trueの場合、画面出力を行う. 
     
    104103     */ 
    105104    function getLastQuery($disp = true) { 
    106         $sql = $this->conn->conn->last_query; 
    107         if($disp) { 
    108             print($sql.";<br />\n"); 
    109         } 
    110         return $sql; 
     105        return $this->conn->getLastQuery($disp); 
    111106    } 
    112107 
     
    127122    } 
    128123 
    129     function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { 
    130         $strw = ""; 
    131         $find = false; 
    132         foreach ($arrwhere as $key => $val) { 
    133             if(strlen($val) > 0) { 
    134                 if(strlen($strw) <= 0) { 
    135                     $strw .= $key ." LIKE ?"; 
    136                 } else if(strlen($arrcon[$key]) > 0) { 
    137                     $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; 
    138                 } else { 
    139                     $strw .= " AND " . $key ." LIKE ?"; 
    140                 } 
    141  
    142                 $arrval[] = $val; 
    143             } 
    144         } 
    145  
    146         if(strlen($strw) > 0) { 
    147             $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; 
    148         } else { 
    149             $sqlse = "SELECT $col FROM $table ".$this->option; 
    150         } 
    151         $ret = $this->conn->getAll($sqlse, $arrval); 
    152         return $ret; 
    153     } 
    154  
    155     function getall($sql, $arrval = array()) { 
    156         $ret = $this->conn->getAll($sql, $arrval); 
    157         return $ret; 
    158     } 
    159  
    160     function getsql($col, $table, $where) { 
    161         if($where != "") { 
    162             // 引数の$whereを優先して実行する。 
    163             $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; 
    164         } else { 
    165             if($this->where != "") { 
    166                     $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; 
    167                 } else { 
    168                     $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; 
    169             } 
    170         } 
     124    /** 
     125     * クエリを実行し、全ての行を返す 
     126     * 
     127     * @param string $sql SQL クエリ 
     128     * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。  
     129     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 
     130     * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。 
     131     */ 
     132    function getall($sql, $arrval = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 
     133        $ret = $this->conn->getAll($sql, $arrval, $fetchmode); 
     134        return $ret; 
     135    } 
     136 
     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 
    171149        return $sqlse; 
    172150    } 
     
    191169 
    192170    function setgroupby($str) { 
    193         $this->groupby = "GROUP BY " . $str; 
     171        if (strlen($str) == 0) { 
     172            $this->groupby = ''; 
     173        } else { 
     174            $this->groupby = "GROUP BY " . $str; 
     175        } 
    194176    } 
    195177 
     
    215197 
    216198    function setorder($str) { 
    217         $this->order = "ORDER BY " . $str; 
     199        if (strlen($str) == 0) { 
     200            $this->order = ''; 
     201        } else { 
     202            $this->order = "ORDER BY " . $str; 
     203        } 
    218204    } 
    219205 
     
    249235            if(eregi("^Now\(\)$", $val)) { 
    250236                $strval .= 'Now(),'; 
    251             // 先頭に~があるとプレースホルダーしない。 
    252237            } else { 
    253238                $strval .= '?,'; 
    254                 if($val != ""){ 
    255                     $arrval[] = $val; 
    256                 } else { 
    257                     $arrval[] = NULL; 
    258                 } 
     239                $arrval[] = $val; 
    259240            } 
    260241            $find = true; 
     
    274255    } 
    275256 
    276     // INSERT文の生成・実行 
    277     // $table   :テーブル名 
    278     // $sqlval  :列名 => 値の格納されたハッシュ配列 
    279     function fast_insert($table, $sqlval) { 
    280         $strcol = ''; 
    281         $strval = ''; 
    282         $find = false; 
    283  
    284         foreach ($sqlval as $key => $val) { 
    285                 $strcol .= $key . ','; 
    286                 if($val != ""){ 
    287                     $eval = pg_escape_string($val); 
    288                     $strval .= "'$eval',"; 
    289                 } else { 
    290                     $strval .= "NULL,"; 
    291                 } 
    292                 $find = true; 
    293         } 
    294         if(!$find) { 
    295             return false; 
    296         } 
    297         // 文末の","を削除 
    298         $strcol = ereg_replace(",$","",$strcol); 
    299         // 文末の","を削除 
    300         $strval = ereg_replace(",$","",$strval); 
    301         $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; 
    302  
    303         // INSERT文の実行 
    304         $ret = $this->conn->query($sqlin); 
    305  
    306         return $ret; 
    307     } 
    308  
    309257    /** 
    310258     * UPDATE文を実行する. 
     
    313261     * @param array $sqlval array('カラム名' => '値',...)の連想配列 
    314262     * @param string $where WHERE句 
    315      * @param array $arradd $addcol用のプレースホルダ配列 
    316      * @param string $addcol 追加カラム 
     263     * @param array $arrValIn WHERE句用のプレースホルダ配列 (従来は追加カラム用も兼ねていた) 
     264     * @param array $arrRawSql 追加カラム 
     265     * @param array $arrRawSqlVal 追加カラム用のプレースホルダ配列 
    317266     * @return 
    318267     */ 
    319     function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") { 
    320         $strcol = ''; 
    321         $strval = ''; 
     268    function update($table, $sqlval, $where = "", $arrValIn = array(), $arrRawSql = array(), $arrRawSqlVal = array()) { 
     269        $arrCol = array(); 
     270        $arrVal = array(); 
    322271        $find = false; 
    323272        foreach ($sqlval as $key => $val) { 
    324             if(eregi("^Now\(\)$", $val)) { 
    325                 $strcol .= $key . '= Now(),'; 
    326             // 先頭に~があるとプレースホルダーしない。 
     273            if (eregi("^Now\(\)$", $val)) { 
     274                $arrCol[] = $key . '= Now()'; 
    327275            } else { 
    328                 $strcol .= $key . '= ?,'; 
    329                 if($val != ""){ 
    330                     $arrval[] = $val; 
    331                 } else { 
    332                     $arrval[] = NULL; 
    333                 } 
     276                $arrCol[] = $key . '= ?'; 
     277                $arrVal[] = $val; 
    334278            } 
    335279            $find = true; 
    336280        } 
    337         if(!$find) { 
     281 
     282        if ($arrRawSql != "") { 
     283            foreach($arrRawSql as $key => $val) { 
     284                $arrCol[] = "$key = $val"; 
     285            } 
     286        } 
     287         
     288        $arrVal = array_merge($arrVal, $arrRawSqlVal); 
     289         
     290        if (empty($arrCol)) { 
    338291            return false; 
    339292        } 
    340293 
    341         if($addcol != "") { 
    342             foreach($addcol as $key => $val) { 
    343                 $strcol .= "$key = $val,"; 
    344             } 
    345         } 
    346  
    347294        // 文末の","を削除 
    348         $strcol = ereg_replace(",$","",$strcol); 
    349         // 文末の","を削除 
    350         $strval = ereg_replace(",$","",$strval); 
    351  
    352         if($where != "") { 
    353             $sqlup = "UPDATE $table SET $strcol WHERE $where"; 
    354         } else { 
    355             $sqlup = "UPDATE $table SET $strcol"; 
    356         } 
    357  
    358         if(is_array($arradd)) { 
     295        $strcol = implode(', ', $arrCol); 
     296 
     297        if (is_array($arrValIn)) { // 旧版との互換用 
    359298            // プレースホルダー用に配列を追加 
    360             foreach($arradd as $val) { 
    361                 $arrval[] = $val; 
    362             } 
    363         } 
    364  
    365         // INSERT文の実行 
    366         $ret = $this->conn->query($sqlup, $arrval); 
    367         return $ret; 
     299            $arrVal = array_merge($arrVal, $arrValIn); 
     300        } 
     301 
     302        $sqlup = "UPDATE $table SET $strcol"; 
     303        if (strlen($where) >= 1) { 
     304            $sqlup .= " WHERE $where"; 
     305        } 
     306 
     307        // UPDATE文の実行 
     308        return $this->conn->query($sqlup, $arrVal); 
    368309    } 
    369310 
    370311    // MAX文の実行 
    371312    function max($table, $col, $where = "", $arrval = array()) { 
    372         if(strlen($where) <= 0) { 
    373             $sqlse = "SELECT MAX($col) FROM $table"; 
    374         } else { 
    375             $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; 
    376         } 
    377         // MAX文の実行 
    378         $ret = $this->conn->getOne($sqlse, $arrval); 
     313        $ret = $this->get($table, "MAX($col)", $where); 
    379314        return $ret; 
    380315    } 
     
    382317    // MIN文の実行 
    383318    function min($table, $col, $where = "", $arrval = array()) { 
    384         if(strlen($where) <= 0) { 
    385             $sqlse = "SELECT MIN($col) FROM $table"; 
    386         } else { 
    387             $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; 
    388         } 
    389         // MIN文の実行 
    390         $ret = $this->conn->getOne($sqlse, $arrval); 
     319        $ret = $this->get($table, "MIN($col)", $where); 
    391320        return $ret; 
    392321    } 
     
    394323    // 特定のカラムの値を取得 
    395324    function get($table, $col, $where = "", $arrval = array()) { 
    396         if(strlen($where) <= 0) { 
    397             $sqlse = "SELECT $col FROM $table"; 
    398         } else { 
    399             $sqlse = "SELECT $col FROM $table WHERE $where"; 
    400         } 
     325        $sqlse = $this->getsql($col, $table, $where); 
    401326        // SQL文の実行 
    402         $ret = $this->conn->getOne($sqlse, $arrval); 
     327        $ret = $this->getone($sqlse, $arrval); 
    403328        return $ret; 
    404329    } 
     
    408333        $ret = $this->conn->getOne($sql, $arrval); 
    409334        return $ret; 
    410  
    411     } 
    412  
    413     // 一行を取得 
    414     function getrow($table, $col, $where = "", $arrval = array()) { 
    415         if(strlen($where) <= 0) { 
    416             $sqlse = "SELECT $col FROM $table"; 
    417         } else { 
    418             $sqlse = "SELECT $col FROM $table WHERE $where"; 
    419         } 
     335    } 
     336 
     337    /** 
     338     * 一行をカラム名をキーとした連想配列として取得 
     339     * 
     340     * @param string $table テーブル名 
     341     * @param string $col カラム名 
     342     * @param string $where WHERE句 
     343     * @param array $arrVal プレースホルダ配列 
     344     * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 
     345     * @return array array('カラム名' => '値', ...)の連想配列 
     346     */ 
     347    function getRow($table, $col, $where = "", $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 
     348        $sqlse = $this->getsql($col, $table, $where); 
    420349        // SQL文の実行 
    421         $ret = $this->conn->getRow($sqlse, $arrval); 
    422  
    423         return $ret; 
     350        return $this->conn->getRow($sqlse, $arrVal ,$fetchmode); 
    424351    } 
    425352 
    426353    // 1列取得 
    427354    function getCol($table, $col, $where = "", $arrval = array()) { 
    428         if (strlen($where) <= 0) { 
    429             $sqlse = "SELECT $col FROM $table"; 
    430         } else { 
    431             $sqlse = "SELECT $col FROM $table WHERE $where"; 
    432         } 
     355        $sqlse = $this->getsql($col, $table, $where); 
    433356        // SQL文の実行 
    434357        return $this->conn->getCol($sqlse, 0, $arrval); 
Note: See TracChangeset for help on using the changeset viewer.