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 */
016 package org.opengion.fukurou.process;
017
018 import org.opengion.fukurou.util.Argument;
019 import org.opengion.fukurou.util.SystemParameter;
020 import org.opengion.fukurou.util.LogWriter;
021
022 import org.opengion.fukurou.util.HybsEntry ;
023 import org.opengion.fukurou.util.Closer;
024 import org.opengion.fukurou.db.ConnectionFactory;
025
026 import java.util.Map ;
027 import java.util.LinkedHashMap ;
028 import java.util.Locale ;
029
030 import java.sql.Connection;
031 import java.sql.Statement;
032 import java.sql.ResultSet;
033 import java.sql.ResultSetMetaData;
034 import java.sql.SQLException;
035
036 /**
037 * Process_DBReaderは、データベã?スから読み取ったå?容をã?LineModel に設定後ã?
038 * 下流に渡すã?FirstProcess インターフェースの実è£?‚¯ラスですã?
039 *
040 * ãƒ??タベã?スから読み取ったå?容より、LineModelを作æ?しã?下æµ?プロセス
041 * チェインは、チェインしてã�?‚‹ため、データは上流から下流へと渡されますã?)
042 * に渡しますã?ここでæŒ?®šできるのは、検索系SQL のみですã?
043 *
044 * ãƒ??タベã?ス接続å?等ã?、ParamProcess のサブクラス(Process_DBParam)に
045 * 設定された接ç¶?Connection)を使用しますã?
046 *
047 * 引数æ–?—å?中にスペã?スを含ã‚??合ã?、ダブルコーãƒ??ション("") で括って下さã�??
048 * 引数æ–?—å?の ã€?』ã?前後には、スペã?スは挟めません。å¿?�šã€?key=value の様に
049 * 繋げてくださいã€?
050 *
051 * SQLæ–?�«は、{@DATE.YMDH}等ã?シスãƒ?ƒ 変数が使用できますã?
052 *
053 * @og.formSample
054 * Process_DBReader -dbid=DBGE -sql="select * from GEA08"
055 *
056 * [ -dbid=DB接続ID ] ??-dbid=DBGE (ä¾? Process_DBParam の -configFile でæŒ?®šすã‚?DBConfig.xml ファイルで規å®?
057 * [ -sql=検索SQLæ–? ] ??-sql="select * from GEA08"
058 * [ -sqlFile=検索SQLファイル ] ??-sqlFile=select.sql
059 * -sql= を指定しなã�??合ã?、ファイルでå¿?�šæŒ?®šしてくださいã€?
060 * [ -sql_XXXX=固定å? ] ??-sql_SYSTEM_ID=GE
061 * SQLæ–?¸の{@XXXX}æ–?—å?を指定ã?固定å?で置き換えますã?
062 * WHERE SYSTEM_ID='{@SYSTEM_ID}' �WHERE SYSTEM_ID='GE'
063 * [ -fetchSize=100 ] ?šフェãƒ?ƒ�する行数(初期値:100)
064 * [ -display=false|true ] ?š結果を標準å?力に表示する(true)かしなã�?false)ã�?初期値:false[表示しない])
065 *
066 * @version 4.0
067 * @author Kazuhiko Hasegawa
068 * @since JDK5.0,
069 */
070 public class Process_DBReader extends AbstractProcess implements FirstProcess {
071 private static final String SQL_KEY = "sql_" ;
072
073 private Connection connection = null;
074 private Statement stmt = null ;
075 private ResultSet resultSet = null;
076 private LineModel newData = null;
077 private int count = 0;
078 private int fetchSize = 100;
079
080 private String dbid = null;
081 private boolean display = false; // 表示しなã�?
082
083 private static final Map<String,String> mustProparty ; // ?»プロパティ?½å¿??チェãƒ?‚¯用 Map
084 private static final Map<String,String> usableProparty ; // ?»プロパティ?½整合æ?チェãƒ?‚¯ Map
085
086 static {
087 mustProparty = new LinkedHashMap<String,String>();
088
089 usableProparty = new LinkedHashMap<String,String>();
090 usableProparty.put( "dbid", "Process_DBParam の -configFile でæŒ?®šすã‚?DBConfig.xml ファイルで規å®? );
091 usableProparty.put( "sql", "検索SQLæ–?sql or sqlFile å¿??)ä¾? \"select * from GEA08\"" );
092 usableProparty.put( "sqlFile", "検索SQLファイル(sql or sqlFile å¿??)ä¾? select.sql" );
093 usableProparty.put( "sql_", "SQLæ–?¸の{@XXXX}æ–?—å?を指定ã?固定å?で置き換えますã?" +
094 CR + "WHERE SYSTEM_ID='{@SYSTEM_ID}' �WHERE SYSTEM_ID='GE'" );
095 usableProparty.put( "fetchSize","フェãƒ?ƒ�する行数 (初期値:100)" );
096 usableProparty.put( "display", "結果を標準å?力に表示する(true)かしなã�?false)ã�? +
097 CR + "(初期値:false:表示しなã�?" );
098 }
099
100 /**
101 * ãƒ?ƒ•ォルトコンストラクターã€?
102 * こã?クラスは、動çš?½œæ?されますã?ãƒ?ƒ•ォルトコンストラクターでã€?
103 * super クラスに対して、å¿?¦�な初期化を行っておきますã?
104 *
105 */
106 public Process_DBReader() {
107 super( "org.opengion.fukurou.process.Process_DBReader",mustProparty,usableProparty );
108 }
109
110 /**
111 * プロセスの初期化を行いますã?初めにä¸?º¦ã�?�‘、呼び出されますã?
112 * 初期処ç�?ファイルオープンã€?¼¤?¢オープンç?に使用しますã?
113 *
114 * @param paramProcess ãƒ??タベã?スの接続å?æƒ??などを持ってã�?‚‹オブジェクãƒ?
115 */
116 public void init( final ParamProcess paramProcess ) {
117 Argument arg = getArgument();
118
119 String sql =arg.getFileProparty("sql","sqlFile",true);
120 String fSize =arg.getProparty("fetchSize");
121 display =arg.getProparty("display",display);
122
123 dbid = arg.getProparty("dbid");
124 connection = paramProcess.getConnection( dbid );
125
126 // 3.8.0.1 (2005/06/17) SQLæ–?? {@XXXX} æ–?—å?の固定å?への置き換ã�?
127 HybsEntry[] entry =arg.getEntrys(SQL_KEY); //配å?
128 SystemParameter sysParam = new SystemParameter( sql );
129 sql = sysParam.replace( entry );
130
131 // SQLæ–?? {@XXXX} æ–?—å?の固定å?への置き換ã�?
132 if( fSize != null ) { fetchSize = Integer.parseInt( fSize ); }
133
134 try {
135 stmt = connection.createStatement();
136 if( fetchSize > 0 ) { stmt.setFetchSize( fetchSize ); }
137 resultSet = stmt.executeQuery( sql );
138
139 newData = createLineModel( resultSet );
140
141 if( display ) { println( newData.nameLine() ); }
142 }
143 catch (SQLException ex) {
144 String errMsg = "Query の実行に問題がありますã?[" + sql + "]" ;
145 throw new RuntimeException( errMsg,ex );
146 }
147 }
148
149 /**
150 * プロセスの終äº?‚’行いますã?æœ?¾Œにä¸?º¦ã�?�‘、呼び出されますã?
151 * 終äº??ç�?ファイルクローズã€?¼¤?¢クローズç?に使用しますã?
152 *
153 * @og.rev 4.0.0.0 (2007/11/27) commit,rollback,remove 処ç�?‚’追åŠ?
154 *
155 * @param isOK トã?タルで、OKã�?�£たかどã�?�‹[true:成功/false:失敗]
156 */
157 public void end( final boolean isOK ) {
158 boolean flag1 = Closer.resultClose( resultSet );
159 resultSet = null;
160 boolean flag2 = Closer.stmtClose( stmt );
161 stmt = null;
162
163 ConnectionFactory.remove( connection,dbid );
164
165 if( !flag1 || !flag2 ) {
166 String errMsg = "スãƒ??トメントをクローズ出来ませんã€?;
167 throw new RuntimeException( errMsg );
168 }
169 }
170
171 /**
172 * こã?ãƒ??タの処ç�?�«おいて、次の処ç�?�Œ出来るかどã�?�‹を問ã�?�ˆわせますã?
173 * こã?呼び出し1回毎に、次のãƒ??タを取得する準備を行いますã?
174 *
175 * @return 処ç�?�§きる:true / 処ç�?�§きなã�?false
176 */
177 public boolean next() {
178 try {
179 return resultSet.next() ;
180 }
181 catch (SQLException ex) {
182 String errMsg = "ネクストすることがå?来ませんã€?;
183 throw new RuntimeException( errMsg,ex );
184 }
185 }
186
187 /**
188 * æœ??にã€?行データである LineModel を作æ?しまã�?
189 * FirstProcess は、次ã€?�¨処ç�?‚’チェインしてã�?��æœ??の行データã‚?
190 * 作æ?して、後続ã? ChainProcess クラスに処ç�?ƒ‡ータを渡しますã?
191 *
192 * @param rowNo 処ç�?¸の行番号
193 *
194 * @return 処ç�?¤‰換後ã?LineModel
195 */
196 public LineModel makeLineModel( final int rowNo ) {
197 count++ ;
198 try {
199 for(int clm = 0; clm < newData.size(); clm++) {
200 Object obj = resultSet.getObject(clm+1);
201 if( obj == null ) {
202 // newData.setValue( clm, "" );
203 newData.setValue( clm, null );
204 }
205 else {
206 newData.setValue( clm, obj );
207 }
208 }
209 newData.setRowNo( rowNo );
210 if( display ) { println( newData.dataLine() ); }
211 }
212 catch (SQLException ex) {
213 String errMsg = "ãƒ??タをå?ç�?�§きませんでしたã€?" + rowNo + "]件目 "
214 + newData.toString() ;
215 throw new RuntimeException( errMsg,ex );
216 }
217 return newData;
218 }
219
220 /**
221 * å†?ƒ¨で使用する LineModel を作æ?しますã?
222 * こã?クラスは、ã?ロセスチェインの基点となりますã?で、新è¦?LineModel を返しますã?
223 * Exception 以外では、å¿?�š LineModel オブジェクトを返しますã?
224 *
225 * @param rs ãƒ??タベã?スカーソル(リザルトセãƒ?ƒˆ)
226 *
227 * @return ãƒ??タベã?スから取り出して変換した LineModel
228 * @throws RuntimeException カラãƒ?��を取得できなかったå?合ã?
229 */
230 private LineModel createLineModel( final ResultSet rs ) {
231 LineModel model = new LineModel();
232
233 try {
234 ResultSetMetaData metaData = rs.getMetaData();
235
236 int size = metaData.getColumnCount();
237 model.init( size );
238
239 for(int clm = 0; clm < size; clm++) {
240 String name = (metaData.getColumnLabel(clm+1)).toUpperCase(Locale.JAPAN) ;
241 model.setName( clm,name );
242 }
243 }
244 catch (SQLException ex) {
245 String errMsg = "ResultSetMetaData から、カラãƒ?��を取得できませんでしたã€?;
246 throw new RuntimeException( errMsg,ex );
247 }
248 return model;
249 }
250
251 /**
252 * プロセスの処ç�?µ�果のレポã?ト表現を返しますã?
253 * 処ç�??ログラãƒ?��、å?力件数、å?力件数などのæƒ??ですã?
254 * こã?æ–?—å?をそのまま、標準å?力に出すことで、結果レポã?トと出来るよã�?�ª
255 * 形式で出してくださいã€?
256 *
257 * @return 処ç�?µ�果のレポã?ãƒ?
258 */
259 public String report() {
260 String report = "[" + getClass().getName() + "]" + CR
261 + TAB + "DBID : " + dbid + CR
262 + TAB + "Input Count : " + count ;
263
264 return report ;
265 }
266
267 /**
268 * こã?クラスの使用方法を返しますã?
269 *
270 * @return こã?クラスの使用方æ³?
271 */
272 public String usage() {
273 StringBuilder buf = new StringBuilder();
274
275 buf.append( "Process_DBReaderは、データベã?スから読み取ったå?容をã?LineModel に設定後ã?" ).append( CR );
276 buf.append( "下流に渡すã?FirstProcess インターフェースの実è£?‚¯ラスですã?" ).append( CR );
277 buf.append( CR );
278 buf.append( "ãƒ??タベã?スから読み取ったå?容より、LineModelを作æ?しã?下æµ?プロセス" ).append( CR );
279 buf.append( "チェインは、チェインしてã�?‚‹ため、データは上流から下流へと渡されますã?)" ).append( CR );
280 buf.append( "に渡しますã?ここでæŒ?®šできるのは、検索系SQL のみですã?" ).append( CR );
281 buf.append( CR );
282 buf.append( "ãƒ??タベã?ス接続å?等ã?、ParamProcess のサブクラス(Process_DBParam)に" ).append( CR );
283 buf.append( "設定された接ç¶?Connection)を使用しますã?" ).append( CR );
284 buf.append( CR );
285 buf.append( "引数æ–?—å?中に空白を含ã‚??合ã?、ダブルコーãƒ??ション(\"\") で括って下さã�??" ).append( CR );
286 buf.append( "引数æ–?—å?の ã€?』ã?前後には、空白は挟めません。å¿?�šã€?key=value の様に" ).append( CR );
287 buf.append( "繋げてくださいã€? ).append( CR );
288 buf.append( CR );
289 buf.append( "SQLæ–?�«は、{@DATE.YMDH}等ã?シスãƒ?ƒ 変数が使用できますã?" ).append( CR );
290 buf.append( CR ).append( CR );
291
292 buf.append( getArgument().usage() ).append( CR );
293
294 return buf.toString();
295 }
296
297 /**
298 * こã?クラスは、main メソãƒ?ƒ‰から実行できませんã€?
299 *
300 * @param args コマンド引数配å?
301 */
302 public static void main( final String[] args ) {
303 LogWriter.log( new Process_DBReader().usage() );
304 }
305 }