001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.column;
017
018import org.opengion.fukurou.util.StringUtil;
019import org.opengion.hayabusa.db.AbstractDBType;
020
021/**
022 * DATA_DEFAULT カラムで、内容の整合性を整えます。
023 *
024 * DATA_DEFAULT(初期値)カラムは、データベース上に設定されていますが、
025 * DB定義スクリプトや、データベースの種類によって、本来必要な形式で
026 * 取得できないときがあります。
027 * ここでは、初期値の後ろに コメントがある場合は、削除し、
028 * データそのものに、シングルクオートをはずします。
029 * 定義情報出力時には、カラムの属性(数字型、文字型)
030 * また、postgreSQL 対応として、::属性情報が入るので、削除します。
031 * シーケンス使用時にも、nextval が自動的にセットされますが、
032 * 削除します。
033 *
034 * このクラスは、valueAction メソッドで、action="VALSET" でのみ
035 * 動作します。
036 *
037 * @og.group データ属性
038 * @og.rev 5.1.3.0 (2010/02/01) 新規作成
039 *
040 * @version  4.0
041 * @author       Kazuhiko Hasegawa
042 * @since    JDK5.0,
043 */
044public class DBType_DD extends AbstractDBType {
045        /** このプログラムのVERSION文字列を設定します。   {@value} */
046        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
047
048        /**
049         * デフォルトコンストラクター
050         *
051         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
052         */
053        public DBType_DD() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
054
055        /**
056         * action で指定されたコマンドを実行して、値の変換を行います。
057         * oldValue(旧データ)は、元のDBTableModelに設定されていた値です。通常は、
058         * この値を使用してカラム毎に変換を行います。newValue(新データ)は、引数で
059         * 指定された新しい値です。この値には、パラメータを指定して変換方法を
060         * 制御することも可能です。
061         * 指定のアクションがカラムで処理できない場合は、エラーになります。
062         *
063         * @og.rev 5.1.3.0 (2010/02/01) 新規追加
064         * @og.rev 5.7.6.2 (2014/05/16) -- コメント対応。処理の全面見直し
065         * @og.rev 5.7.6.3 (2014/05/23) 処理対象以外は、newValue を返すだけ。
066         *
067         * @param   action アクションコマンド
068         * @param   oldValue 入力データ(旧データ)
069         * @param   newValue 入力データ(新データ)
070         *
071         * @return      実行後のデータ
072         */
073        @Override
074        public String valueAction( final String action,final String oldValue,final String newValue ) {
075                if( "VALSET".equals( action ) && oldValue != null ) {
076                        String val = oldValue.trim() ;
077
078                        if( val.length() > 0 ) {
079                                // 共通:初期値に /* */ コメントが入ることがあるので、削除
080                                int pos = val.indexOf( "/*" );
081                                if( pos > 0 ) { val = val.substring( 0,pos ); }
082
083                                // 共通:初期値に -- コメントが入ることがあるので、削除
084                                pos = val.indexOf( "--" );
085                                if( pos > 0 ) { val = val.substring( 0,pos ); }
086
087                                // postgreSQL 対応:属性情報が入るので、削除
088                                pos = val.indexOf( "::" );
089                                if( pos > 0 ) { val = val.substring( 0,pos ); }
090
091                                // postgreSQL 対応:シーケンス使用時にも、nextval が自動的にセットされるので、削除
092                                pos = val.indexOf( "nextval" );
093                                if( pos > 0 ) { val = val.substring( 0,pos ); }
094
095                                val = val.trim();                               // 一旦、trim します。
096
097                                // 共通:5.7.6.3 (2014/05/23) シングルクオートはずし(先頭か、末尾に付いている場合は、すべてのシングルクオートを外します。)
098                                if( StringUtil.startsChar( val, '\'' ) || val.endsWith("'") ) {                 // 6.4.1.1 (2016/01/16) 1文字 String.startsWith
099                                        val = StringUtil.replace( val,"'","" );
100                                }
101
102                        }
103
104                        return val ;
105                }
106                else {
107                        // 5.7.6.3 (2014/05/23) 処理対象以外は、newValue を返すだけ。
108                        return newValue;
109                }
110        }
111}