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.table; 017 018import java.io.File; 019import java.io.PrintWriter; 020 021import org.opengion.fukurou.util.ErrorMessage; 022import org.opengion.fukurou.util.FileUtil; 023import org.opengion.fukurou.util.FixLengthData; 024import org.opengion.fukurou.util.StringUtil; 025import org.opengion.hayabusa.common.HybsSystemException; 026import org.opengion.hayabusa.db.AbstractTableFilter; 027import org.opengion.hayabusa.db.DBTableModel; 028 029/** 030 * TableFilter_CMNT_CLM は、TableFilter インターフェースを継承した、DBTableModel 処理用の 031 * 実装クラスです。 032 * 033 * ここでは、テーブルカラム一覧の検索結果より、ORACLE のテーブルカラムにコメントを作成します。 034 * 構文は、「COMMENT ON COLUMN テーブル名.カラム名 IS 'コメント'」です。 035 * このコメントを取り出す場合は、 036 *「SELECT COLUMN_NAME, COMMENTS FROM USER_COL_COMMENTS WHERE TABLE_NAME = 'テーブル名'」 037 * とします。 038 * 039 * SQLのツール類には、このコメントを使用して、テーブルカラムの日本語名を表示させたりします。 040 * 041 * 検索では、(TABLE_NAME,CLM,NAME_JA) の項目を取得する必要があります。 042 * 043 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。 044 * 出力ファイル名は、通常、テーブル で1つ、カラムで1つにまとめて作成されます。 045 * 【パラメータ】 046 * { 047 * DIR : {@BASE_DIR}/sql/install/08_CMNT ; 出力ファイルの基準フォルダ(必須) 048 * FILE : false ; 出力ファイル名(初期値:CMNT_CLM[.sql|.xml]) 049 * XML : false ; XML出力を行うかどうか[true/false]を指定します(初期値:false) 050 * } 051 * 052 * @og.formSample 053 * ●形式: 054 * select SYSTEM_ID,TABLE_NAME,NAME_JA from GF02 055 * 056 * ① <og:tableFilter classId="CMNT_CLM" keys="DIR" vals='"{@BASE_DIR}/sql/install/08_CMNT"' /> 057 * 058 * ② <og:tableFilter classId="CMNT_CLM" > 059 * { 060 * DIR : {@BASE_DIR}/sql/install/08_CMNT ; 061 * FILE : CMNT_CLM ; 062 * XML : false ; 063 * } 064 * </og:tableFilter> 065 * 066 * @og.rev 4.0.0.0 (2005/08/31) 新規作成 067 * 068 * @version 0.9.0 2000/10/17 069 * @author Kazuhiko Hasegawa 070 * @since JDK1.6, 071 */ 072public class TableFilter_CMNT_CLM extends AbstractTableFilter { 073 /** このプログラムのVERSION文字列を設定します。 {@value} */ 074 private static final String VERSION = "6.4.1.1 (2016/01/16)" ; 075 076 private static final String[] DBKEY = {"TABLE_NAME","CLM","NAME_JA"}; 077 078 /** データのアクセス用の配列番号 {@value} */ 079 protected static final int TABLE_NAME = 0; 080 /** データのアクセス用の配列番号 {@value} */ 081 protected static final int CLM = 1; 082 /** データのアクセス用の配列番号 {@value} */ 083 protected static final int NAME_JA = 2; 084 085 // private static final String ENCODE = "Windows-31J" ; 086 private static final String ENCODE = "UTF-8" ; 087 088 private static final int X = FixLengthData.X ; // type 定数 089 private static final int K = FixLengthData.K ; // type 定数 090 091 /** 各種定数 */ 092 // 6.0.2.3 (2014/10/10) AbstractTableFilter へ移動 093 094 /** XML形式かどうか */ 095 096 /** ファイル名(拡張子なし) */ 097 private String fileName = "CMNT_CLM"; 098 099 /** 100 * デフォルトコンストラクター 101 * 102 * @og.rev 6.4.1.1 (2016/01/16) keysMap を、サブクラスから設定させるように変更。 103 */ 104 public TableFilter_CMNT_CLM() { 105 super(); 106 initSet( "DIR" , "出力ファイルの基準フォルダ(必須)" ); 107 initSet( "FILE" , "出力ファイル名(初期値:CMNT_CLM[.sql|.xml])" ); 108 initSet( "XML" , "XML出力を行うかどうか[true/false]を指定(初期値:false)" ); 109 } 110 111 /** 112 * DBTableModel処理を実行します。 113 * 114 * @og.rev 6.0.2.3 (2014/10/10) EXEC_END_TAG を追加。キャッシュします。 115 * @og.rev 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。 116 * 117 * @return 実行結果のテーブルモデル 118 */ 119 public DBTableModel execute() { 120 isXml = StringUtil.nval( getValue( "XML" ), isXml ); 121 execEndTag = isXml ? EXEC_END_TAG : ";" ; // 6.0.2.3 (2014/10/10) CMNTは タグの前に改行を入れない。 122 123 final File dir = new File( getValue( "DIR" ) ); 124 125 if( ! dir.exists() && ! dir.mkdirs() ) { 126 final String errMsg = "所定のフォルダが作成できませんでした。[" + dir + "]" ; 127 // 4.3.4.4 (2009/01/01) 128 throw new HybsSystemException( errMsg ); 129 } 130 131 fileName = StringUtil.nval( getValue( "FILE" ), fileName ); 132 133 // COMMENT ON COLUMN テーブル名.カラム名 IS 'コメント' 134 final int[] addLen = new int[] { 0,0,0,0 }; // 各データ間のスペース 135 final int[] type = new int[] { X,X,X,K }; // 各データの種別 X:半角 S:空白前埋め K:全角混在 136 final FixLengthData fixData = new FixLengthData( addLen,type ); 137 138 String[] data = null; 139 int row = 0; 140 // 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。 141 // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid declaring a variable if it is unreferenced before a possible exit point. 142 final DBTableModel table = getDBTableModel(); 143 try( PrintWriter writer = FileUtil.getPrintWriter( new File( dir,fileName + ( isXml ? ".xml" : ".sql" ) ),ENCODE ) ) { 144 145 if( isXml ) { writer.println( XML_START_TAG ); } 146 147 // 全データを読み込んで、最大長の計算を行う。 148 final int[] clmNo = getTableColumnNo( DBKEY ); 149 final int rowCnt = table.getRowCount(); 150 for( row=0; row<rowCnt; row++ ) { 151 data = table.getValues( row ); 152 final String tblClm = data[clmNo[TABLE_NAME]] + "." + data[clmNo[CLM]]; 153 final String name_ja = "'" + data[clmNo[NAME_JA]] + "'"; 154 155 final String[] outData = new String[] { "COMMENT ON COLUMN " , tblClm , " IS " , name_ja } ; 156 fixData.addListData( outData ); 157 } 158 159 // データの出力 160 for( row=0; row<rowCnt; row++ ) { 161 if( isXml ) { writer.print( EXEC_START_TAG ); } 162 writer.print( fixData.getFixData( row ) ); 163 writer.println( execEndTag ); // 6.0.2.3 (2014/10/10) 164 } 165 166 if( isXml ) { writer.println( XML_END_TAG ); } 167 } 168 catch( final RuntimeException ex ) { // catch は、close() されてから呼ばれます。 169 final ErrorMessage errMessage = makeErrorMessage( "TableFilter_CMNT_CLM Error",ErrorMessage.NG ); 170 data = table.getValues( row ); 171 errMessage.addMessage( row+1,ErrorMessage.NG,"CMNT_CLM",StringUtil.array2csv( data ) ); 172 // BAT から呼び出す場合があるため、標準エラー出力にも情報を出しておきます。 173 System.out.println( errMessage ); 174 System.out.println( ex ); 175 } 176 177 return table; 178 } 179}