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.db;
017    
018    import java.util.Locale;
019    
020    /**
021     * å?ƒ‡ãƒ¼ã‚¿ãƒ™ã?スã«å¯¾å¿œã™ã‚‹enumåã‚’è¿”ã—ã¾ã™ã?
022     * 主ã«ã€å„ãƒ??タベã?スã«ãŠã‘る関数åã?差異をå¸åŽã™ã‚‹ãŸã‚ã?enumã§ã™ã?
023     * 本æ¥ã¯ã€äº’æ›æ€§ã®ã‚るファンクション以外ã?使用ã—ãªã?‚ˆã?«ã—ã¾ã—ょã??
024     * ã¾ãŸã?ç„¡ã‘れã°äº’æ›æ€§ãƒ‘ックãªã©ã§ã€ãƒ•ァンクションを定義ã—ã¦ã—ã¾ã??ã‚?
025     * ä¸?¤ã®æ–¹æ³•ã§ã™ã?
026     *
027     * <table border="1" frame="box" rules="all" >
028     *  <caption>å?ƒ‡ãƒ¼ã‚¿ãƒ™ã?スã«ãŠã‘る関数</caption>
029     *  <tr><th>ãƒ??タベã?スå?</th><th>連çµ?/th><th>部åˆ?–‡å­—å?</th></tr>
030     *  <tr><td>{&#064;DBF.XXX}</td><td>CON </td><td>SUBSTR    </td></tr>
031     *  <tr><td>ORACLE         </td><td>||  </td><td>SUBSTR    </td></tr>
032     *  <tr><td>HSQL           </td><td>||  </td><td>SUBSTR    </td></tr>
033     *  <tr><td>POSTGRES       </td><td>||  </td><td>SUBSTR    </td></tr>
034     *  <tr><td>MYSQL          </td><td>||  </td><td>SUBSTR    </td></tr>
035     *  <tr><td>SQLSERVER      </td><td>+   </td><td>SUBSTRING </td></tr>
036     *  <tr><td>FIREBIRD       </td><td>||  </td><td>SUBSTR    </td></tr>
037     * </table>
038     *
039     * @og.rev 5.1.4.0 (2010/03/01) æ–°è¦ä½œæ?
040     *
041     * @version  5.0
042     * @author   Kazuhiko Hasegawa
043     * @since    JDK5.0,
044     */
045    public enum DBFunctionName {
046            // 引数付ãenum定義?šã“ã“ã«ã€å¿?¦ãªé–¢æ•°ãŒå¢—ãˆã‚‹ãŸã³ã«ã€è¿½åŠ?—ã¦ã?ã¾ã™ã?
047            //                        CON   SUBSTR
048              ORACLE        ( "||","SUBSTR" )
049            , HSQL          ( "||","SUBSTR" )
050            , POSTGRES      ( "||","SUBSTR" )
051            , MYSQL         ( "||","SUBSTR" )
052            , SQLSERVER     ( "+" ,"SUBSTRING" )
053            , FIREBIRD      ( "||","SUBSTR" ) ;
054    
055            private final String dbfCON ;
056            private final String dbfSUBSTR ;
057    
058            /**
059             * コンストラクター(enum ã®å ´åˆã?ã€private宣è¨?•れる)
060             *
061             * @og.rev 5.1.4.0 (2010/03/01) æ–°è¦ä½œæ?
062             *
063             * @param       con     第ä¸?¼•æ•°ã«ã¦æŒ?®?CON)
064             * @param       substr  第ä¸?¼•æ•°ã«ã¦æŒ?®?SUBSTR)
065             */
066            private DBFunctionName( final String con , final String substr ) {
067                    dbfCON    = con;
068                    dbfSUBSTR = substr;
069            }
070    
071            /**
072             * 共通ファンクションã«å¯¾å¿œã™ã‚‹ãƒ‡ãƒ¼ã‚¿ãƒ™ã?ス個別ã®ãƒ•ァンクションåã‚’è¿”ã—ã¾ã™ã?
073             *
074             * ç¾æ™‚点ã§ã¯ã€NAME,CON,SUBSTR ã®ã¿ä½¿ç”¨ã§ãã¾ã™ã?
075             *
076             *
077             * @og.rev 5.1.4.0 (2010/03/01) æ–°è¦ä½œæ?
078             *
079             * @param   func 共通ファンクション
080             *
081             * @return  ファンクションå?
082             */
083            public String getFunctionName( final String func ) {
084                    if( "NAME".equals(   func ) ) { return toString();      }
085                    if( "CON".equals(    func ) ) { return dbfCON;          }
086                    if( "SUBSTR".equals( func ) ) { return dbfSUBSTR;       }
087    
088                    return func;
089            }
090    
091            /**
092             * シーケンスåよりシーケンスオブジェクトを検索ã—ã?次ã®å€¤ã‚’å–りå?ã—ã¾ã™ã?
093             * DBã«å¯¾ã™ã‚‹ã‚·ãƒ¼ã‚±ãƒ³ã‚¹ã‚ªãƒ–ジェクトã?予ã‚作æ?ã•れã¦ã?‚‹å¿?¦ãŒã‚りã¾ã™ã?
094             *
095             * ã¾ãŸã?MySQLã®å ´åˆã?ã€ã‚·ãƒ¼ã‚±ãƒ³ã‚¹ã‚ªãƒ–ジェクトãŒå®Ÿè£?•れã¦ã?ªã?Ÿã‚ã?
096             * å†?ƒ¨çš?«ã¯ã€å¼•æ•°ã®ã‚·ãƒ¼ã‚±ãƒ³ã‚¹åã¨åŒã˜åå‰ã®ãƒ??ブルã‹ã‚‰ã€Integeråž‹ã?
097             * "SEQID"ã¨ã?†é ?›®åを検索ã™ã‚‹ã“ã¨ã«ã‚ˆã‚Šã€ã‚·ãƒ¼ã‚±ãƒ³ã‚¹ã‚’エミュレートã—ã¦ã?¾ã™ã?
098             *
099             * @og.rev 5.1.9.0 (2010/08/01) æ–°è¦è¿½åŠ?
100             *
101             * @param seqName シーケンスå?
102             * @param tran トランザクション
103             *
104             * @return シーケンス番å·
105             */
106            public int getSequence( final String seqName, final Transaction tran ) {
107                    String sql = null;
108                    String[][] rtn = null;
109                    switch ( this ) {
110                            case ORACLE:
111                                    sql = "select " + seqName + ".nextval from dual";
112                                    break;
113                            case HSQL:
114                                    sql = "select next value for " + seqName + " from dual";
115                                    break;
116                            case POSTGRES:
117                                    sql = "select nextval('" + seqName + "')";
118                                    break;
119                            case MYSQL:
120                                    sql = "update " + seqName + " set SEQID = last_insert_id(SEQID+1)";
121                                    DBUtil.dbExecute( sql, new String[0], tran );
122                                    sql = "select last_insert_id()";
123                                    break;
124                            case SQLSERVER:
125                                    throw new RuntimeException( "ç¾åœ¨ã€SQLSERVERã§ã¯ã‚·ãƒ¼ã‚±ãƒ³ã‚¹æ©Ÿè?ã¯ã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? );
126                            case FIREBIRD:
127                                    sql = "select gen_id(" + seqName + ", 1) from rdb$database";
128                                    break;
129                            default:
130                                    throw new RuntimeException( "ç¾åœ¨ã€ã“ã®ãƒ??タベã?スã§ã¯ã‚·ãƒ¼ã‚±ãƒ³ã‚¹æ©Ÿè?ã¯ã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? );
131                    }
132    
133                    rtn = DBUtil.dbExecute( sql, new String[0], tran );
134                    return Integer.valueOf( rtn[0][0] );
135            }
136    
137            /**
138             * å?ƒ‡ãƒ¼ã‚¿ãƒ™ã?スã«å¯¾å¿œã™ã‚‹enumåã‚’è¿”ã—ã¾ã™ã?
139             *
140             * @og.rev 5.1.4.0 (2010/03/01) æ–°è¦ä½œæ?
141             *
142             * @param   dbName ãƒ??タベã?スå?
143             *
144             * @return  ãƒ??タベã?スã«å¯¾å¿œã™ã‚‹enumå?
145             */
146            public static DBFunctionName getDBName( final String dbName ) {
147                    String dbn = dbName.toUpperCase( Locale.JAPAN );
148    
149                    if(      dbn.indexOf( "ORACLE"          ) >= 0 ) { return DBFunctionName.ORACLE;     }
150                    else if( dbn.indexOf( "HSQL"            ) >= 0 ) { return DBFunctionName.HSQL;               }
151                    else if( dbn.indexOf( "POSTGRES"        ) >= 0 ) { return DBFunctionName.POSTGRES;   }
152                    else if( dbn.indexOf( "MYSQL"           ) >= 0 ) { return DBFunctionName.MYSQL;              }
153                    else if( dbn.indexOf( "SQLSERVER"       ) >= 0 ) { return DBFunctionName.SQLSERVER;  }
154                    else if( dbn.indexOf( "FIREBIRD"        ) >= 0 ) { return DBFunctionName.FIREBIRD;   }
155    
156                    final String errMsg = "åˆæœŸåŒ–時ã«ã€æŒ‡å®šã? dbName キーãŒå­˜åœ¨ã—ã¾ã›ã‚“ã€?
157                                                    + "[" + dbn + "]" ;
158    
159                    throw new RuntimeException( errMsg );
160            }
161    
162            /**
163             * å?ƒ‡ãƒ¼ã‚¿ãƒ™ã?スã«å¯¾å¿œã™ã‚‹ãƒ•ァンクションåã‚’è¿”ã—ã¾ã™ã?
164             *
165             * @og.rev 4.3.8.0 (2009/08/01) SUBSTRを追�
166             * @og.rev 5.1.2.0 (2010/01/01) MySQL対å¿?SUBSTRBå»?­¢(帳票ãƒ??ã‚¿ã®åˆ?‰²ã®å†?ƒ¨å‡¦ç?Œ–ã«ä¼´ã?
167             * @og.rev 5.1.4.0 (2010/03/01) ãƒ??タベã?スå?ã§ã¯ãªãã?dbid ã§åˆ¤æ–­ã™ã‚‹ã‚ˆã†ã«å¤‰æ›´
168             * @og.rev 5.7.7.2 (2014/06/20) DBF.NAME 時ã?処ç??ç°¡ç´?Œ–
169             *
170             * @param   func ファンクションå?定義æ–?­?
171             * @param   dbid 接続å?ID
172             *
173             * @return  実ファンクションå?
174             */
175            public static String getFunctionName( final String func ,final String dbid ) {
176    //              DBFunctionName dbName = DBFunctionName.getDBName( ConnectionFactory.getDBName( dbid ) );
177    
178                    // 5.7.7.2 (2014/06/20) DBF.NAME 時ã?処ç??ç°¡ç´?Œ–
179                    String dbName = ConnectionFactory.getDBName( dbid );
180                    if( "NAME".equals( func ) ) { return dbName; }
181                    else {
182                            return getDBName( dbName ).getFunctionName( func );
183                    }
184    
185    //              return dbName.getFunctionName( func );
186            }
187    }