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.hayabusa.taglib;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.common.HybsSystemException;
020    import org.opengion.hayabusa.db.DBConstValue;
021    
022    import org.opengion.fukurou.util.StringUtil ;
023    import static org.opengion.fukurou.util.StringUtil.nval ;
024    
025    import java.util.Map;
026    import java.util.LinkedHashMap;
027    
028    /**
029     * TableUpdateTag にパラメーターを渡す為のタグクラスです?
030     *
031     * 汎用???タベ?ス登録処?行えるタグ tableUpdate タグを新規作?します?
032     * これは、?体的なSLQを作?する tableUpdateParam タグと?合わせて使用できます?
033     * tableUpdate タグは、queryType に JDBCTableUpdate を指定します?基本?これ?
034     * です?tableUpdateParam では、sqlType に、INSERT,COPY,UPDATE,MODIFY,DELETE の
035     * どれかを指定する事で、SQL??タイプを?します?COPY,MODIFY は command と
036     * 関連を持たす為に追?て?タイプで、INSERTやUPDATE と同じ処?行います?
037     * tableUpdateParam の table には、作?した?SQL の??ブルを指定します?
038     * where 属?は、検索結果の DBTableModel の更新時に使用する条件を指定します?
039     *
040     * @og.formSample
041     * ●形式?lt;og:tableUpdate command="{@command}" queryType="JDBCTableUpdate" >
042     *            <og:tableUpdateParam
043     *                sqlType       = "{@sqlType}"       // INSERT,COPY,UPDATE,MODIFY,DELETE
044     *                table         = "{@TABLE_NAME}"    // 処?象の??ブル?
045     *                names         = "{@names}"         // 処?象のカラ?
046     *                omitNames     = "{@omitNames}"     // 処?象外?カラ?
047     *                where         = "{@where}"         // 処?象を特定するキー
048     *                whereNames    = "{@whereNames}"    // 処?象を特定するキー条件(where句)をCSV形?
049     *                constKeys     = "{@constKeys}"     // 処?ラ?の中の固定情報カラ?
050     *                constVals     = "{@constVals}"     // 処?ラ?の中の固定情報設定?
051     *                asNames       = "{@asNames}"       // 別名を付けたカラ?(select A as B from TBL の B を指?
052     *                orgNames      = "{@orgNames}"      // tableの実際のカラ?(select A as B from TBL の A を指?
053     *                funcKeys      = "{@funcKeys}"      // 関数等を設定するカラ?
054     *                funcVals      = "{@funcVals}"      // 関数等?設定?
055     *                logicalDelete = "{@logicalDelete}" // sqlTypeがDELETEの場合にもUPDATE?発?
056     *            />
057     *         </og:tableUpdate>
058     *
059     * ●body?な?
060     *
061     * ●Tag定義??
062     *   <og:tableUpdateParam
063     *       sqlType          ○?TAG】BODY部に書かれて? SQLタイプを?しま???)
064     *       table            ○?TAG】??象の??ブル名を?しま???)
065     *       names              【TAG】??象のカラ?をCSV形式で??しま?
066     *       omitNames          【TAG】??象外?カラ?をCSV形式で??しま?
067     *       where              【TAG】??象を特定するキー条件(where句)を指定しま?
068     *       whereNames         【TAG】??象を特定するキー条件(where句)をCSV形式で??しま?
069     *       constKeys          【TAG】設定?を固定?と置き換える対象となるカラ?をCSV形式で??しま?
070     *       constVals          【TAG】設定?を固定?と置き換える対象となる設定?をCSV形式で??しま?
071     *       funcKeys           【TAG】関数等を設定するカラ?をCSV形式で??しま?
072     *       funcVals           【TAG】関数等?設定?をCSV形式で??しま?
073     *       asNames            【TAG】別名を付けたカラ?(select A as B from TBL の B を指?をCSV形式で??しま?
074     *       orgNames           【TAG】tableの実際のカラ?(select A as B from TBL の A を指?をCSV形式で??しま?
075     *       quotCheck          【TAG】リクエスト情報の クォー?ション(') 存在チェ?を実施するかど?[true/false]を設定しま?(初期値:USE_SQL_INJECTION_CHECK[=true])
076     *       constObjKey        【TAG】固定情報カラ??処?ブジェクトを特定するキーを設定しま?初期値:SYSTEM_ID)
077     *       logicalDelete      【TAG】sqlType="DELETE"の場合に論理削除(UPDATE)を行うかど?を指定しま?初期値:false)
078     *       debug              【TAG】デバッグ??を?力するかど?[true/false]を指定しま?初期値:false)
079     *   />
080     *
081     * ●使用?
082     *    ・【entry.jsp?
083     *         <og:tableUpdate command="{@command}" queryType="JDBCTableUpdate" >
084     *            <og:tableUpdateParam
085     *               sqlType  = "{@sqlType}"
086     *               table    = "{@MEM.TABLE_NAME}"
087     *               where    = "ROWID = [ROWID]"
088     *            />
089     *         </og:tableUpdate>
090     *
091     * @og.rev 3.8.8.0 (2007/12/22) 新規作?
092     * @og.rev 4.1.2.0 (2008/03/12) 実??大?修正
093     * @og.group ??登録
094     *
095     * @version  4.0
096     * @author   Kazuhiko Hasegawa
097     * @since    JDK5.0,
098     */
099    public class TableUpdateParamTag extends CommonTagSupport {
100            //* こ?プログラ??VERSION??を設定します?       {@value} */
101            private static final String VERSION = "4.0.0.0 (2005/11/30)" ;
102    
103            private static final long serialVersionUID = 4000 ;     // 4.0.0 (2005/11/30)
104    
105            /** sqlType属?に設定できる値                      {@value} */
106            public static final String SQL_TYPE  = "|INSERT|COPY|UPDATE|MODIFY|DELETE|" ;
107    
108            // 3.8.0.4 (2005/08/08) 印刷時に使用するシス?ID
109            private static final String SYSTEM_ID =HybsSystem.sys( "SYSTEM_ID" );
110    
111            // 4.3.6.0 (2009/05/01) ?ォルトで利用するconstObjのシス?リソース?
112            private static final String DEFAULT_CONST_OBJ = HybsSystem.sys( "DEFAULT_CONST_CLASS" );
113    
114            private String          sqlType         = null;                 // INSERT,COPY,UPDATE,MODIFY,DELETE
115            private String          table           = null;                 // 処?象の??ブル?
116            private String[]        names           = null;                 // 処?象のカラ?
117            private String          omitNames       = ",ROWID,ROWNUM,WRITABLE,";            // 処?象外?カラ?
118            private String          where           = null;                 // 処?象を特定するキー
119            private String          whereNames      = null;                 // 5.5.8.5 (2012/11/27) 処?象を特定するCSV形式?カラ?
120            private String[]        constKeys       = null;                 // 処?ラ?の中の固定情報カラ?
121            private String[]        constVals       = null;                 // 処?ラ?の中の固定情報設定?
122            private String[]        funcKeys        = null;                 // 5.5.1.9 (2012/04/19) 関数等を設定するカラ?
123            private String[]        funcVals        = null;                 // 5.5.1.9 (2012/04/19) 関数等?設定?
124            private String[]        asNames         = null;                 // 5.5.1.9 (2012/04/19) 別名を付けたカラ?(select A as B from TBL の B を指?
125            private String[]        orgNames        = null;                 // 5.5.1.9 (2012/04/19) tableの実際のカラ?(select A as B from TBL の A を指?
126            private String          constObjKey     = SYSTEM_ID;    // 固定情報カラ??処?ブジェクトを特定するキー
127            private boolean         quotCheck       = HybsSystem.sysBool( "USE_SQL_INJECTION_CHECK" );
128            private boolean         logicalDelete = false;          // 4.3.7.0 (2009/06/01) sqlTypeがDELETEの場合にもUPDATE?発?
129    
130            /**
131             * Taglibの開始タグが見つかったときに処??doStartTag() ?オーバ?ライドします?
132             *
133             * @og.rev 5.5.1.9 (2012/04/19) エラーチェ?を?に行います?
134             *
135             * @return      後続????( SKIP_BODY )
136             */
137            @Override
138            public int doStartTag() {
139    
140                    // constKeys,constVals の個数チェ?
141                    if( constKeys != null ) {
142                            if( constVals == null || constKeys.length != constVals.length ) {
143                                    String errMsg = "<b>constKeys と、constVals の個数が異なります?</b><br />"
144                                                                            + " constKeys=[" + StringUtil.array2csv( constKeys ) + "]"
145                                                                            + " constVals=[" + StringUtil.array2csv( constVals ) + "]" ;
146                                    throw new HybsSystemException( errMsg );
147                            }
148                    }
149    
150                    // funcKeys,funcVals の個数チェ?
151                    if( funcKeys != null ) {
152                            if( funcVals == null || funcKeys.length != funcVals.length ) {
153                                    String errMsg = "<b>funcKeys と、funcVals の個数が異なります?</b><br />"
154                                                                            + " funcKeys=[" + StringUtil.array2csv( funcKeys ) + "]"
155                                                                            + " funcVals=[" + StringUtil.array2csv( funcVals ) + "]" ;
156                                    throw new HybsSystemException( errMsg );
157                            }
158                    }
159    
160                    // asNames,orgNames の個数チェ?
161                    if( orgNames != null ) {
162                            if( asNames == null || orgNames.length != asNames.length ) {
163                                    String errMsg = "<b>orgNames と、asNames の個数が異なります?</b><br />"
164                                                                            + " orgNames=[" + StringUtil.array2csv( orgNames ) + "]"
165                                                                            + " asNames=[" + StringUtil.array2csv( asNames ) + "]" ;
166                                    throw new HybsSystemException( errMsg );
167                            }
168                    }
169    
170                    return(SKIP_BODY);                              // Body を評価しな?
171            }
172    
173            /**
174             * Taglibの終?グが見つかったときに処??doEndTag() ?オーバ?ライドします?
175             *
176             * @og.rev 4.3.7.0 (2009/06/01) 論理削除対?
177             *
178             * @return      後続????
179             */
180            @Override
181            public int doEndTag() {
182                    debugPrint();
183    
184                    TableUpdateTag updateTag = (TableUpdateTag)findAncestorWithClass( this,TableUpdateTag.class );
185                    if( updateTag == null ) {
186                            String errMsg = "<b>こ?タグは、TableUpdateTagの??(要?に記述してください?/b>";
187                            throw new HybsSystemException( errMsg );
188                    }
189    
190                    String upSqlType = updateTag.getSqlType() ;
191                    if( upSqlType == null || upSqlType.equals( sqlType ) ) {
192                            // 通常の names カラ??列を設定します?
193                            if( names == null ) { names = updateTag.getNames(); }
194                            NamesData namesData = makeNamesData( names );
195    
196                            String query = null;
197                            if( "INSERT".equalsIgnoreCase( sqlType ) || "COPY".equalsIgnoreCase( sqlType ) ) {
198                                    query = getInsertSQL( namesData );
199                            }
200                            else if( "UPDATE".equalsIgnoreCase( sqlType ) || "MODIFY".equalsIgnoreCase( sqlType )
201                                            || ( "DELETE".equalsIgnoreCase( sqlType ) && logicalDelete ) ) { // 4.3.7.0 (2009/06/01)
202                                    query = getUpdateSQL( namesData );
203                            }
204                            else if( "DELETE".equalsIgnoreCase( sqlType ) ) {
205                                    query = getDeleteSQL();
206                            }
207    
208                            jspPrint( query );
209                    }
210    
211                    return(EVAL_PAGE);
212            }
213    
214            /**
215             * タグリブオブジェクトをリリースします?
216             * キャ?ュされて再利用される?で、フィールド?初期設定を行います?
217             *
218             * @og.rev 4.3.7.0 (2009/06/01) logicalDelete属?追?
219             * @og.rev 5.5.1.9 (2012/04/19) asNames、orgNames、funcKeys、funcVals属?追?
220             * @og.rev 5.5.8.5 (2012/11/27) 処?象を特定するCSV形式?カラ?
221             */
222            @Override
223            protected void release2() {
224                    super.release2();                       // 3.5.6.0 (2004/06/18) 追?抜けて?)
225                    sqlType         = null;                 // INSERT,COPY,UPDATE,MODIFY,DELETE
226                    table           = null;                 // 処?象の??ブル?
227                    names           = null;                 // 処?象のカラ?
228                    omitNames       = ",ROWID,ROWNUM,WRITABLE,";            // 処?象外?カラ?
229                    where           = null;                 // 処?象を特定するキー
230                    whereNames      = null;                 // 5.5.8.5 (2012/11/27) 処?象を特定するCSV形式?カラ?
231                    constKeys       = null;                 // 処?ラ?の中の固定情報カラ?
232                    constVals       = null;                 // 処?ラ?の中の固定情報設定?
233                    quotCheck       = HybsSystem.sysBool( "USE_SQL_INJECTION_CHECK" );
234                    constObjKey     = SYSTEM_ID;    // 固定情報カラ??処?ブジェクトを特定するキー
235                    logicalDelete = false;          // 4.3.7.0 (2009/06/01)
236                    funcKeys        = null;                 // 5.5.1.9 (2012/04/19) 関数等を設定するカラ?
237                    funcVals        = null;                 // 5.5.1.9 (2012/04/19) 関数等?設定?
238                    asNames         = null;                 // 5.5.1.9 (2012/04/19) 別名を付けたカラ?(select A as B from TBL の B を指?
239                    orgNames        = null;                 // 5.5.1.9 (2012/04/19) tableの実際のカラ?(select A as B from TBL の A を指?
240            }
241    
242            /**
243             * 【TAG】BODY部に書かれて? SQLタイプを?します?
244             *
245             * @og.tag
246             * SQLタイプ?、INSERT,COPY,UPDATE,MODIFY,DELETE の中から?す?
247             * ?があります?これら?、?部に書かれるSQLの形式を?する?に使用します?
248             * ?処??、DBTableModelの改?ー?A,C,D)に対して使用され?
249             * SQL を選択する?合???に使用されます?
250             * なお?COPY と MODIFY は、command で?できる簡易機?として用意して?す?
251             * 上位? TableUpdateTag の sqlType 属? と同じsqlType 属?の場合?み、SQL?
252             * 合?・出力します?(上位?sqlTypeがnullの場合?、無条件実行します?)
253             * ??タイプが、異なる?合?、なにも??行いません?
254             *
255             * @param       type BODY部に書かれて? SQL タイ?
256             */
257            public void setSqlType( final String type ) {
258                    sqlType = nval( getRequestParameter( type ),sqlType );
259                    if( sqlType != null && SQL_TYPE.indexOf( "|" + sqlType + "|" ) < 0 ) {
260                            sqlType = null;
261            //              String errMsg = "sqlType属?には? + SQL_TYPE + "以外設定できません?
262            //                                      + " typeIn=[" + type + "]"
263            //                                      + " sqlType=[" + sqlType + "]" ;
264            //              throw new HybsSystemException( errMsg );
265                    }
266            }
267    
268            /**
269             * 【TAG】??象の??ブル名を?します?
270             *
271             * @og.tag
272             * ??ブル名を?することで、sqlTypeに応じ?QUERYを生成することが?来ます?
273             * 生?する場合?カラ?特定する?合?、names 属?で?できます?
274             * また?WHERE条件は、where属?で?します?
275             *
276             * @param       tbl ??ブル?
277             * @see         #setNames( String )
278             * @see         #setWhere( String )
279             * @see         #setSqlType( String )
280             */
281            public void setTable( final String tbl ) {
282                    table = nval( getRequestParameter( tbl ),table );
283            }
284    
285            /**
286             * 【TAG】??象のカラ?をCSV形式で??します?
287             *
288             * @og.tag
289             * 生?するQUERYのカラ?をカンマ区???CSV)で??します?
290             * ?がな??合?、DBTableModel の全カラ?※)を使用して、QUERYを構築します?
291             * ?に、テーブル結合してDBTableModelを構築した?合?、登録すべきカラ?
292             * ?する?があります?
293             * (※)正確には、DBTableModel の全カラ???、ROWID,ROWNUM,WRITABLE カラ??
294             * 無視します?
295             * ?方法???常のパラメータ取得後に、CSV?します?
296             *
297             * @og.rev 3.8.8.5 (2007/03/09) 通常のパラメータ取得後に、CSV?に戻します?
298             *
299             * @param       nms カラ?(CSV形?
300             * @see         #setTable( String )
301             * @see         #setOmitNames( String )
302             */
303            public void setNames( final String nms ) {
304                    names = StringUtil.csv2Array( getRequestParameter( nms ) );
305                    if( names.length == 0 ) { names = null; }
306            }
307    
308            /**
309             * 【TAG】??象外?カラ?をCSV形式で??します?
310             *
311             * @og.tag
312             * 生?するQUERYのカラ?に?しな?ラ?をカンマ区???CSV)で??します?
313             * ?がな??合?、DBTableModel の全カラ?※)を使用して、QUERYを構築します?
314             * ??ブル結合などで、??たくな?ラ?の方が少な??合に、names ですべて?
315             * ?するより少な?述ですみます?
316             * (※)正確には、DBTableModel の全カラ???、ROWID,ROWNUM,WRITABLE カラ??
317             * 無視します?
318             *
319             * @param       nms カラ?(CSV形?
320             * @see         #setTable( String )
321             * @see         #setNames( String )
322             */
323            public void setOmitNames( final String nms ) {
324                    omitNames = omitNames + nval( getRequestParameter( nms ),"" ) + ",";
325            }
326    
327            /**
328             * 【TAG】??象を特定するキー条件(where句)を指定します?
329             *
330             * @og.tag
331             * 生?するQUERYのwhere 句を指定します?通常の WHERE 句の書き方と同じで?
332             * DBTableModelの値を割り当てたい?に[カラ?] を記述します?
333             * ??の場合?設定?をセ?するときに、シングルコー??ション?
334             * 使用しますが、[カラ?]で?する?合?、その前後に?')シングル
335             * コー??ションは、不要です?
336             * {&#064;XXXX}変数を使用する場合?、パース時に固定文字に置き換えられる為?
337             * ???時の(')シングルコー??ションが?になります?
338             * ※ 5.5.8.5 (2012/11/27) whereNames 属?と併用した場合?、where が?and を付けて、文字?結合されます?
339             * 例:FGJ='1' and CLM=[CLM] and SYSTEM_ID in ([SYSID],'**') and KBSAKU='{&#064;KBSAKU}'
340             *
341             * @param       wr 検索条件(where句)
342             */
343            public void setWhere( final String wr ) {
344                    where = nval( getRequestParameter( wr ),where );
345            }
346    
347            /**
348             * 【TAG】??象を特定するキー条件(where句)をCSV形式で??します?
349             *
350             * @og.tag
351             * 生?するQUERYのwhere 句を指定する方法として、?のカラ?をCSV?し、?部で
352             * KEY=[KEY] ??を作?します?
353             * ここでは、カラ?は、データベ?スのカラ?と同じで、かつ、DBTableModel に?
354             * 同じカラ????タが存在して?こと、と?条件付きとします?
355             * また?where 条件との併用を行いますが、こちら?条件が?に使用され、where 条件は?
356             * and を付けて、文字?結合されます?
357             * 例?CLM,SYSTEM_ID,KBSAKU   ?  CLM=[CLM] and SYSTEM_ID=[SYSTEM_ID] and KBSAKU=[KBSAKU]
358             *
359             * @og.rev 5.5.8.5 (2012/11/27) 新規追?
360             *
361             * @param       wrnm 検索条件(where句)作?のためのカラ?(CSV形?
362             */
363            public void setWhereNames( final String wrnm ) {
364                    whereNames = nval( getRequestParameter( wrnm ),whereNames );
365            }
366    
367            /**
368             * 【TAG】設定?を固定?と置き換える対象となるカラ?をCSV形式で??します?
369             *
370             * @og.tag
371             * names 属?のカラ? table 属?より、QUERYを作?して、DBTableModelの値?
372             * 割り当てる?合?DBTableModelの値ではなく?外部から?した固定??
373             * 割り当てたい場合に、そのカラ?をカンマ区???CSV)で??します?
374             * <del>ここで?するカラ?は、names 属?に含まれるか?DBTableModelのカラ?して
375             * 存在する?があります?なお?names 属?に含まれる場合?、DBTableModelのカラ?
376             * 含まれる??ありません?/del>
377             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
378             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
379             *
380             * @param       keys カラ?(CSV形?
381             * @see         #setConstVals( String )
382             */
383            public void setConstKeys( final String keys ) {
384                    constKeys = getCSVParameter( keys );
385            }
386    
387            /**
388             * 【TAG】設定?を固定?と置き換える対象となる設定?をCSV形式で??します?
389             *
390             * @og.tag
391             * names 属?のカラ? table 属?より、QUERYを作?して、DBTableModelの
392             * 値を割り当てる?合?DBTableModelの値ではなく?外部から?した固定??
393             * 割り当てたい場合に、そのカラ?に対応する設定?をカンマ区???CSV)で
394             * ??します?ここで?する設定?は、constKeys 属?と対応させます?
395             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
396             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
397             *
398             * @param       vals 設定?(CSV形?
399             * @see         #setConstKeys( String )
400             */
401            public void setConstVals( final String vals ) {
402                    constVals = getCSVParameter( vals );
403            }
404    
405            /**
406             * 【TAG】関数等を設定するカラ?をCSV形式で??します?
407             *
408             * @og.tag
409             * constVals 属?で設定する?は、?シングルクオートが付与されます?
410             * そ?場合?関数などを設定したい場合でも???として設定しようとします?
411             * ここで?するカラ?(funcKeys)自身は、constKeys と同じ書式です?
412             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
413             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
414             *
415             * @og.rev 5.5.1.9 (2012/04/19) 新規追?
416             *
417             * @param       keys カラ?(CSV形?
418             * @see         #setFuncVals( String )
419             */
420            public void setFuncKeys( final String keys ) {
421                    funcKeys = getCSVParameter( keys );
422            }
423    
424            /**
425             * 【TAG】関数等?設定?をCSV形式で??します?
426             *
427             * @og.tag
428             * funcKeys 属?に対応す?関数などの設定?を割り当てます?
429             * constVals 属?との違いは、funcVals の設定?は、そのままの形で、SQL??
430             * 構築に使われます?
431             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
432             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
433             *
434             * @og.rev 5.5.1.9 (2012/04/19) 新規追?
435             *
436             * @param       vals 設定?(CSV形?
437             * @see         #setFuncKeys( String )
438             */
439            public void setFuncVals( final String vals ) {
440                    funcVals = getCSVParameter( vals );
441            }
442    
443            /**
444             * 【TAG】別名を付けたカラ?(select A as B from TBL の B を指?をCSV形式で??します?
445             *
446             * @og.tag
447             * SELECT ?記述したとき?別名を付けて?り?SELECTした??ブルと別の??ブルに
448             * DBTableModelの値を書き込??合?DBTableModel の持って?カラ?と、実際に
449             * 書き込?ラ?が異なります?そ?ようなケースに、?の別名カラ??します?
450             * orgNames属?の並び?、asNames属?の並び?合わせておく?があります?
451             * こ?カラ?は、DBTableModel には持って?が???ブル側には持って???
452             * なので、?部? omitNames 属?に値を設定します?利用??、omitNames に
453             * 書き込???ありません?
454             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
455             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
456             *
457             * @og.rev 5.5.1.9 (2012/04/19) 新規追?
458             *
459             * @param       keys カラ?(CSV形?
460             * @see         #setOrgNames( String )
461             */
462            public void setAsNames( final String keys ) {
463                    asNames = getCSVParameter( keys );
464            }
465    
466            /**
467             * 【TAG】tableの実際のカラ?(select A as B from TBL の A を指?をCSV形式で??します?
468             *
469             * @og.tag
470             * SELECT ?記述したとき?別名を付けて?り?SELECTした??ブルと別の??ブルに
471             * DBTableModelの値を書き込??合?DBTableModel の持って?カラ?と、実際に
472             * 書き込?ラ?が異なります?そ?ようなケースに、テーブルの実カラ??します?
473             * orgNames属?の並び?、asNames属?の並び?合わせておく?があります?
474             * こ?カラ?は、DBTableModel には持って?せんが???ブル側には持って?値
475             * なので、このカラ?で、SQL?構築します? UPDATE TBL SET A=[B] WHERE … となります?
476             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
477             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
478             *
479             * @og.rev 5.5.1.9 (2012/04/19) 新規追?
480             *
481             * @param       keys カラ?(CSV形?
482             * @see         #setAsNames( String )
483             */
484            public void setOrgNames( final String keys ) {
485                    orgNames = getCSVParameter( keys );
486            }
487    
488            /**
489             * 【TAG】リクエスト情報の クォー?ション(') 存在チェ?を実施するかど?[true/false]を設定しま?
490             *              (初期値:USE_SQL_INJECTION_CHECK[={@og.value org.opengion.hayabusa.common.SystemData#USE_SQL_INJECTION_CHECK}])?
491             *
492             * @og.tag
493             * ???インジェクション対策??として、暫定的ではありますが、SQLのパラメータに
494             * 渡す文字?にクォー?ション(') を許さな?定にすれば、ある程度は防止できます?
495             * 数字タイプ?引数には?or 5=5 などのクォー?ションを使用しな?ードを埋めても?
496             * 数字チェ?で検?可能です??タイプ?場合?、? (')を?ずして?
497             * ' or 'A' like 'A のような形式になる為?')チェ??でも有効です?
498             * (') が含まれて?エラーにする(true)?かノ?チェ??false)を指定します?
499             * (初期値:シス?定数のUSE_SQL_INJECTION_CHECK[={@og.value org.opengion.hayabusa.common.SystemData#USE_SQL_INJECTION_CHECK}])?
500             *
501             * @param   flag クォー?ションチェ? [true:する/それ以?しない]
502             * @see         org.opengion.hayabusa.common.SystemData#USE_SQL_INJECTION_CHECK
503             */
504            public void setQuotCheck( final String flag ) {
505                    quotCheck = nval( getRequestParameter( flag ),quotCheck );
506            }
507    
508            /**
509             * 【TAG】固定情報カラ??処?ブジェクトを特定するキーを設定しま?初期値:SYSTEM_ID)?
510             *
511             * @og.tag
512             * 固定情報カラ?をシス?単位にJavaクラスで管?きます?
513             * そ?クラスオブジェクト?、org.opengion.hayabusa.db.DBConstValue インターフェース?
514             * 継承した、plugin クラスになります?
515             * そ?クラスを特定するキーワードを?します?
516             * 初期値は、SYSTEM_ID でシス?単位にクラスを作?します?
517             * もし、他?シス?と共通?場合?、継承?させることも可能です?
518             * 対応したDBConstValueクラスが?ラグインとして存在しな??合??
519             * シス?リソースのDEFAULT_CONST_CLASSで?されたクラスが利用されます?
520             *
521             * 初期値は、SYSTEM_ID です?
522             *
523             * @param   key 固定情報カラ??処?ブジェクトを特定するキー
524             */
525            public void setConstObjKey( final String key ) {
526                    constObjKey = nval( getRequestParameter( key ),constObjKey );
527            }
528    
529            /**
530             * 【TAG】sqlType="DELETE"の場合に論理削除(UPDATE)を行うかど?を指定しま?初期値:false)?
531             *
532             * @og.tag
533             * sqlType="DELETE"の場合に論理削除(UPDATE)を行うかど?を指定します?
534             * trueが指定された場合?、DELETE?はなく?UPDATE?発行されます?
535             * falseが指定された場合?、DELETE?発行されます?
536             * さらに論理削除を行う場合?org.opengion.hayabusa.db.DBConstValue インターフェースに
537             * 定義されて?、getLogicalDeleteKeys()及?getLogicalDeleteValsを実?ることで?
538             * 論理削除する際?フラグの更新方法を統?に管?ることが可能になります?
539             * 初期値は、false(物?除する)で?
540             *
541             * @param   flg 論理削除(UPDATE)を行うかど?
542             */
543            public void setLogicalDelete( final String flg ) {
544                    logicalDelete = nval( getRequestParameter( flg ),logicalDelete );
545            }
546    
547            /**
548             * ??タをインサートする?合に使用するSQL?作?します?
549             *
550             * @og.rev 4.1.2.1 (2008/03/17) DBConstValue による固定?セ?を採用
551             * @og.rev 4.3.6.4 (2009/05/01) ?ォルト設定をシス?リソースで設定可能にする
552             * @og.rev 5.3.4.0 (2011/04/01) DEFAULT_CONST_OBJの初期値変更(null→ゼロ??)
553             *
554             * @param   namesData NamesDataオブジェク?
555             *
556             * @return  インサー?QL
557             */
558            private String getInsertSQL( final NamesData namesData ) {
559                    String cls = HybsSystem.sys( "DBConstValue_" + constObjKey ) ;
560    
561                    // 4.3.6.4 (2009/05/01) 標準?追?
562                    if( cls == null){
563                            cls = DEFAULT_CONST_OBJ;
564                    }
565    
566    //              if( cls != null ) {             // 5.3.4.0 (2011/04/01)
567                    if( cls != null && !cls.isEmpty() ) {
568                            DBConstValue constVal = (DBConstValue)HybsSystem.newInstance( cls );
569                            // 4.2.1.0 (2008/04/16) 初期化追?
570                            constVal.init( table,getUser().getUserID(),getGUIInfoAttri( "KEY" ) );
571                            String[] keys = constVal.getInsertKeys();
572                            String[] vals = constVal.getInsertVals();
573                            namesData.add( keys,vals );
574                    }
575    
576                    String[] nms = namesData.getNames();
577                    String[] vls = namesData.getVals();
578    
579                    StringBuilder sql = new StringBuilder();
580                    sql.append( "INSERT INTO " ).append( table );
581                    sql.append( " ( " );
582                    sql.append( nms[0] );
583                    for( int i=1; i<nms.length; i++ ) {
584                            sql.append( "," ).append( nms[i] );
585                    }
586                    sql.append( " ) VALUES ( " );
587                    sql.append( vls[0] );
588                    for( int i=1; i<vls.length; i++ ) {
589                            sql.append( "," ).append( vls[i] );
590                    }
591                    sql.append( " )" );
592    
593                    return sql.toString();
594            }
595    
596            /**
597             * ??タをア????トする?合に使用するSQL?作?します?
598             *
599             * where と whereNames が同時に?された場合?、whereNames が?に処??
600             * where 条件は、and 結合されます?
601             *
602             * @og.rev 4.1.2.1 (2008/03/17) DBConstValue による固定?セ?を採用
603             * @og.rev 4.3.6.4 (2009/05/01) ?ォルト設定をシス?リソースで設定可能にする
604             * @og.rev 4.3.7.0 (2009/06/01) 論理削除対?
605             * @og.rev 5.3.7.0 (2011/07/01) DEFAULT_CONST_OBJの初期値変更(null→ゼロ??) 対応忘れ
606             * @og.rev 5.5.8.5 (2012/11/27) whereNames 対?
607             *
608             * @param   namesData NamesDataオブジェク?
609             *
610             * @return  ア?????QL
611             */
612            private String getUpdateSQL( final NamesData namesData ) {
613                    String cls = HybsSystem.sys( "DBConstValue_" + constObjKey ) ;
614    
615                    // 4.3.6.4 (2009/05/01) 標準?追?
616                    if( cls == null){
617                            cls = DEFAULT_CONST_OBJ;
618                    }
619    
620    //              if( cls != null ) {
621                    if( cls != null && !cls.isEmpty() ) {           // 5.3.7.0 (2011/07/01)
622                            DBConstValue constVal = (DBConstValue)HybsSystem.newInstance( cls );
623                            // 4.2.1.0 (2008/04/16) 初期化追?
624                            constVal.init( table,getUser().getUserID(),getGUIInfoAttri( "KEY" ) );
625                            // 4.3.7.0 (2009/06/01) 論理削除対?
626                            String[] keys = null;
627                            String[] vals = null;
628                            if( "DELETE".equalsIgnoreCase( sqlType ) ) {
629                                    keys = constVal.getLogicalDeleteKeys();
630                                    vals = constVal.getLogicalDeleteVals();
631                            }
632                            else {
633                                    keys = constVal.getUpdateKeys();
634                                    vals = constVal.getUpdateVals();
635                            }
636                            namesData.add( keys,vals );
637                    }
638    
639                    String[] nms = namesData.getNames();
640                    String[] vls = namesData.getVals();
641    
642                    StringBuilder sql = new StringBuilder();
643                    sql.append( "UPDATE " ).append( table ).append( " SET " );
644                    sql.append( nms[0] ).append( "=" ).append( vls[0] );
645    
646                    for( int i=1; i<nms.length; i++ ) {
647                            sql.append( "," );
648                            sql.append( nms[i] ).append( "=" ).append( vls[i] );
649                    }
650    
651                    // 5.5.8.5 (2012/11/27) whereNames 対?
652                    String whereAnd = " WHERE " ;
653                    if( whereNames != null && whereNames.length() > 0 ) {
654                            String[] wnms = whereNames.split(",");
655                            sql.append( whereAnd ).append( wnms[0] ).append( "=[" ).append( wnms[0] ).append( "]" );
656    
657                            for( int i=1; i<wnms.length; i++ ) {
658                                    sql.append( " AND " ).append( wnms[i] ).append( "=[" ).append( wnms[i] ).append( "]" );
659                            }
660                            whereAnd = " AND " ;            // whereNames 優先?ここを?らなければ、?期?のまま? WHERE " が使われ?
661                    }
662    
663                    // 5.5.8.5 (2012/11/27) whereNames 対応?whereNames が登録されて?ば、AND で繋げる?
664                    if( where != null && where.length() > 0 ) {
665    //                      sql.append( " WHERE " ).append( where );
666                            sql.append( whereAnd ).append( where );
667                    }
668    
669                    return sql.toString();
670            }
671    
672            /**
673             * ??タをデリートする?合に使用するSQL?作?します?
674             *
675             * where と whereNames が同時に?された場合?、whereNames が?に処??
676             * where 条件は、and 結合されます?
677             *
678             * @og.rev 5.5.8.5 (2012/11/27) whereNames 対?
679             *
680             * @return  ?ー?QL
681             */
682            private String getDeleteSQL() {
683                    StringBuilder sql = new StringBuilder();
684                    sql.append( "DELETE FROM " ).append( table );
685    
686                    // 5.5.8.5 (2012/11/27) whereNames 対?
687                    String whereAnd = " WHERE " ;
688                    if( whereNames != null && whereNames.length() > 0 ) {
689                            String[] wnms = whereNames.split(",");
690                            sql.append( whereAnd ).append( wnms[0] ).append( "=[" ).append( wnms[0] ).append( "]" );
691    
692                            for( int i=1; i<wnms.length; i++ ) {
693                                    sql.append( " AND " ).append( wnms[i] ).append( "=[" ).append( wnms[i] ).append( "]" );
694                            }
695                            whereAnd = " AND " ;            // whereNames 優先?ここを?らなければ、?期?のまま? WHERE " が使われ?
696                    }
697    
698                    // 5.5.8.5 (2012/11/27) whereNames 対応?whereNames が登録されて?ば、AND で繋げる?
699                    if( where != null && where.length() > 0 ) {
700    //                      sql.append( " WHERE " ).append( where );
701                            sql.append( whereAnd ).append( where );
702                    }
703                    return sql.toString();
704            }
705    
706            /**
707             * names,constKeys,omitNames から、?なキー??と、属???を持っ?NamesData を作?します?
708             *
709             * @og.rev 4.1.2.1 (2008/03/17) 固定?の constVals の前後に?'" を?れる?
710             * @og.rev 5.5.1.9 (2012/04/19) asNames、orgNames、funcKeys、funcVals属?追?
711             *
712             * @param   nms カラ?配?
713             *
714             * @return      属???を持ったNamesData
715             */
716            private NamesData makeNamesData( final String[] nms ) {
717    
718                    NamesData namesData = new NamesData();
719    
720                    // 5.5.1.9 (2012/04/19) omitNames に、asNames配?の値を設定しておきます?
721                    if( asNames != null ) {
722                            for( int i=0; i<asNames.length; i++ ) {
723                                    if( asNames[i] != null && asNames[i].length() > 0 ) {
724                                            omitNames = omitNames + asNames[i] + ",";
725                                    }
726                            }
727                    }
728    
729                    // names で?されたカラ?
730                    for( int i=0; i<nms.length; i++ ) {
731                            String nm = nms[i];
732                            if( nm != null && nm.length() > 0 && omitNames.indexOf( "," + nm + "," ) < 0 ) {
733                                    namesData.add( nm,"[" + nm + "]" ) ;
734                            }
735                    }
736    
737                    // 固定?の constKeys カラ??列を設定します?
738                    if( constKeys != null && constKeys.length > 0 ) {
739                            for( int j=0; j<constKeys.length; j++ ) {
740                                    String nm = constKeys[j];
741                                    if( nm != null && nm.length() > 0 ) {
742                                            namesData.add( nm,"'" + constVals[j] + "'" ) ;  // constVals は、シングルクオートで囲?す?
743                                    }
744                            }
745                    }
746    
747                    // 関数値の funcKeys カラ??列を設定します?
748                    if( funcKeys != null && funcKeys.length > 0 ) {
749                            for( int j=0; j<funcKeys.length; j++ ) {
750                                    String nm = funcKeys[j];
751                                    if( nm != null && nm.length() > 0 ) {
752                                            namesData.add( nm, funcVals[j] ) ;              // funcVals は、シングルクオートで囲?せん?
753                                    }
754                            }
755                    }
756    
757                    // 別名? asNames,orgNames カラ??列を設定します?
758                    if( orgNames != null && orgNames.length > 0 ) {
759                            for( int j=0; j<orgNames.length; j++ ) {
760                                    String onm = orgNames[j];
761                                    if( onm != null && onm.length() > 0 ) {
762                                            namesData.add( onm,"[" + asNames[j] + "]" ) ;
763                                    }
764                            }
765                    }
766    
767                    return namesData ;
768            }
769    
770            /**
771             * ???タを受け渡す為の、簡易クラスです?
772             * 更新するカラ?と値のセ?配?を管?て?す?
773             *
774             */
775            private static class NamesData {
776                    final Map<String,String> data = new LinkedHashMap<String,String>() ;
777    
778                    /**
779                     * キーと値のセ?を追?ます?
780                     *
781                     * @param   nm String
782                     * @param   val String
783                     */
784                    public void add( final String nm,final String val ) {
785                            data.put( nm,val );
786                    }
787    
788                    /**
789                     * キー配?と対応する?値配?のセ?を追?ます?
790                     *
791                     * @param   nms String[]
792                     * @param   vals String[]
793                     */
794                    public void add( final String[] nms,final String[] vals ) {
795                            if( nms != null ) {
796                                    for( int i=0; i<nms.length; i++ ) {
797                                            data.put( nms[i],vals[i] );
798                                    }
799                            }
800                    }
801    
802                    /**
803                     * キー配?を返します?
804                     *
805                     * @return   String[]
806                     */
807                    public String[] getNames() {
808                            return data.keySet().toArray( new String[data.size()] );
809                    }
810    
811                    /**
812                     * 値配?を返します?
813                     *
814                     * @return   String[]
815                     */
816                    public String[] getVals()  {
817                            return data.values().toArray( new String[data.size()] );
818                    }
819            }
820    
821            /**
822             * こ?オブジェクト???表現を返します?
823             * 基本???目?使用します?
824             *
825             * @return こ?クラスの??表現
826             */
827            @Override
828            public String toString() {
829                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
830                                    .println( "VERSION"                     ,VERSION                )
831                                    .println( "sqlType"                     ,sqlType                )
832                                    .println( "table"                       ,table                  )
833                                    .println( "names"                       ,names                  )
834                                    .println( "omitNames"           ,omitNames              )
835                                    .println( "where"                       ,where                  )
836                                    .println( "whereNames"          ,whereNames             )
837                                    .println( "constKeys"           ,constKeys              )
838                                    .println( "constVals"           ,constVals              )
839                                    .println( "logicalDelete"       ,logicalDelete  )
840                                    .fixForm().toString() ;
841            }
842    }