開発メモ

開発用のメモです。

EC CUBE 主キー判定

<?php
        $ccu = "";
        $table_catalog = "";
        $table_name = "";
        $constraint_name = "";
        $pkey = "";
        $column_name = "";

        switch (DB_TYPE) {
            case 'mysql' :
                // 主キー判定(pgsql)
                $ccu = "information_schema.columns ccu";
                $table_catalog = "table_schema";
                $table_name = "table_name";
                $constraint_name = "column_key";
                $pkey = "PRI";
                $column_name = "column_name";
                break;

            default :
                // 主キー判定(pgsql)
                $ccu = "information_schema.constraint_column_usage ccu";
                $table_catalog = "table_catalog";
                $table_name = "table_name";
                $constraint_name = "constraint_name";
                $pkey = "%_pkey";
                $column_name = "column_name";
                break;
        }
        $where = "$table_catalog = ? AND $table_name = ? AND $constraint_name LIKE ? AND $column_name = ?";

        if( $this->objQuery->exists( "$ccu", "$where", array( DB_NAME, $table, $constraint_name_value, $column ) ) ) {
            // 主キーなので単独か判別
            if( $this->objQuery->exists( "$ccu", "$table_catalog = ? AND $table_name = ? AND $constraint_name LIKE ? AND $column_name <> ?", array( DB_NAME, $table, $pkey, $column ) ) !== FALSE ) {
                // 主キーが単独なので除外
                return;
            }
        }
Twitter: @asahina_alice