開発メモ

開発用のメモです。

SC_Query の拡張

<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2014 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

require_once CLASS_REALDIR . 'SC_Query.php';

class SC_Query_Ex extends SC_Query {

    /**
     * カラム追加クエリの追加
     *
     */
    public function addColumn($table_name, $col_name, $col_type) {
        $arrVal = $this -> extractOnlyColsOf($table_name, array($col_name => $col_type));
        if (empty($arrVal)) {
            $sql = "ALTER TABLE $table_name ADD $col_name $col_type ";
            $this -> query($sql);
        }
    }

    /**
     * カラム削除クエリの追加
     *
     */
    public function dropColumn($table_name, $col_name) {
        $arrVal = $this -> extractOnlyColsOf($table_name, array($col_name => $col_type));
        if (!empty($arrVal)) {
            $sql = "ALTER TABLE $table_name DROP $col_name";
            $this -> query($sql);
        }
    }

    /**
     * カラムの変更
     *
     */
    public function changeColumn($table_name, $col_name, $new_col_name, $new_col_type) {
        switch(DB_TYPE) {
            case "mysql" :
                $sql = "ALTER TABLE $table_name CHANGE COLUMN $col_name $new_col_name $new_col_type";
                $this -> query($sql);
            case 'pgsql' :
                $sql = "ALTER TABLE $table_name RENAME COLUMN $col_name TO   $new_col_name";
                $this -> query($sql);
                $sql = "ALTER TABLE $table_name ALTER  COLUMN $col_name TYPE $new_col_type";
                $this -> query($sql);
        }
    }

    /**
     * INSERT文を実行する.
     *
     * @param  string                   $table      テーブル名
     * @param  array                    $arrVal     array('カラム名' => '値', ...)の連想配列
     * @param  array                    $arrSql     array('カラム名' => 'SQL文', ...)の連想配列
     * @param  array                    $arrSqlVal  SQL文の中で使用するプレースホルダ配列
     * @param  string                   $from       FROM 句・WHERE 句
     * @param  string                   $arrFromVal FROM 句・WHERE 句で使用するプレースホルダ配列
     * @return integer|DB_Error|boolean 挿入件数またはエラー(DB_Error, false)
     */
    public function insert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) {
        // 通常のインサート時に arrForm を直接投入可能に
        $arrVal = $this -> extractOnlyColsOf($table, $arrVal);
        parent::insert($table, $arrVal, $arrSql, $arrSqlVal, $from, $arrFromVal);
    }

    /**
     * UPDATE文を実行する.
     *
     * @param string $table        テーブル名
     * @param array  $arrVal       array('カラム名' => '値', ...)の連想配列
     * @param string $where        WHERE句
     * @param array  $arrWhereVal  WHERE句用のプレースホルダ配列 (従来は追加カラム用も兼ねていた)
     * @param array  $arrRawSql    追加カラム
     * @param array  $arrRawSqlVal 追加カラム用のプレースホルダ配列
     * @return
     */
    public function update($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) {
        // 通常のアップデート時に arrForm を直接投入可能に
        $arrVal = $this -> extractOnlyColsOf($table, $arrVal);
        parent::update($table, $arrVal, $where, $arrWhereVal, $arrRawSql, $arrRawSqlVal);
    }

}
  • SC_Query_Ex::addColumn
    • カラムの追加を行う
  • SC_Query_Ex::dropColumn
    • カラムの削除を行う
  • SC_Query_Ex::changeColumn
    • カラムの変更を行う
  • SC_Query_Ex::insert
    • インサートを行う。 $arrForm(Hash) から 変換で自動で不要項目を除去
  • SC_Query_Ex::update
    • アップデートを行う。 $arrForm(Hash) から 変換で自動で不要項目を除去
Twitter: @asahina_alice