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 * <tr><th>ãƒ??タベã?スå�?</th><th>連çµ?/th><th>部åˆ?–‡字å?</th></tr>
029 * <tr><td>{@DBF.XXX}</td><td>CON </td><td>SUBSTR </td></tr>
030 * <tr><td>ORACLE </td><td>|| </td><td>SUBSTR </td></tr>
031 * <tr><td>HSQL </td><td>|| </td><td>SUBSTR </td></tr>
032 * <tr><td>POSTGRES </td><td>|| </td><td>SUBSTR </td></tr>
033 * <tr><td>MYSQL </td><td>|| </td><td>SUBSTR </td></tr>
034 * <tr><td>SQLSERVER </td><td>+ </td><td>SUBSTRING </td></tr>
035 * <tr><td>FIREBIRD </td><td>|| </td><td>SUBSTR </td></tr>
036 * </table>
037 *
038 * @og.rev 5.1.4.0 (2010/03/01) 新規作æ?
039 *
040 * @version 5.0
041 * @author Kazuhiko Hasegawa
042 * @since JDK5.0,
043 */
044 public enum DBFunctionName {
045 // 引数付きenum定義?šここに、å¿?¦�な関数が増えるたびに、追åŠ?�—てã�?��ますã?
046 // CON SUBSTR
047 ORACLE ( "||","SUBSTR" )
048 , HSQL ( "||","SUBSTR" )
049 , POSTGRES ( "||","SUBSTR" )
050 , MYSQL ( "||","SUBSTR" )
051 , SQLSERVER ( "+" ,"SUBSTRING" )
052 , FIREBIRD ( "||","SUBSTR" ) ;
053
054 private final String dbfCON ;
055 private final String dbfSUBSTR ;
056
057 /**
058 * コンストラクター(enum の場合ã?、private宣è¨?�•れる)
059 *
060 * @og.rev 5.1.4.0 (2010/03/01) 新規作æ?
061 *
062 * @param con 第ä¸?¼•数にてæŒ?®?CON)
063 * @param substr 第ä¸?¼•数にてæŒ?®?SUBSTR)
064 */
065 private DBFunctionName( final String con , final String substr ) {
066 dbfCON = con;
067 dbfSUBSTR = substr;
068 }
069
070 /**
071 * 共通ファンクションに対応するデータベã?ス個別のファンクション名を返しますã?
072 *
073 * @og.rev 5.1.4.0 (2010/03/01) 新規作æ?
074 *
075 * @param func 共通ファンクション
076 *
077 * @return ファンクションå�?
078 */
079 public String getFunctionName( final String func ) {
080 if( "NAME".equals( func ) ) { return toString(); }
081 if( "CON".equals( func ) ) { return dbfCON; }
082 if( "SUBSTR".equals( func ) ) { return dbfSUBSTR; }
083
084 return func;
085 }
086
087 /**
088 * シーケンス名よりシーケンスオブジェクトを検索しã?次の値を取りå?しますã?
089 * DBに対するシーケンスオブジェクトã?予め作æ?されてã�?‚‹å¿?¦�がありますã?
090 *
091 * またã?MySQLの場合ã?、シーケンスオブジェクトが実è£?�•れてã�?�ªã�?�Ÿめã?
092 * å†?ƒ¨çš?�«は、引数のシーケンス名と同じ名前のãƒ??ブルから、Integer型ã?
093 * "SEQID"とã�?�†é ?›®名を検索することにより、シーケンスをエミュレートしてã�?�¾すã?
094 *
095 * @og.rev 5.1.9.0 (2010/08/01) 新規追åŠ?
096 *
097 * @param seqName シーケンスå�?
098 * @param tran トランザクション
099 *
100 * @return シーケンス番号
101 */
102 public int getSequence( final String seqName, final Transaction tran ) {
103 String sql = null;
104 String[][] rtn = null;
105 switch ( this ) {
106 case ORACLE:
107 sql = "select " + seqName + ".nextval from dual";
108 break;
109 case HSQL:
110 sql = "select next value for " + seqName + " from dual";
111 break;
112 case POSTGRES:
113 sql = "select nextval('" + seqName + "')";
114 break;
115 case MYSQL:
116 sql = "update " + seqName + " set SEQID = last_insert_id(SEQID+1)";
117 DBUtil.dbExecute( sql, new String[0], tran );
118 sql = "select last_insert_id()";
119 break;
120 case SQLSERVER:
121 throw new RuntimeException( "現在、SQLSERVERではシーケンス機è?はサポã?トされてã�?�¾せんã€? );
122 case FIREBIRD:
123 sql = "select gen_id(" + seqName + ", 1) from rdb$database";
124 break;
125 default:
126 throw new RuntimeException( "現在、このãƒ??タベã?スではシーケンス機è?はサポã?トされてã�?�¾せんã€? );
127 }
128
129 rtn = DBUtil.dbExecute( sql, new String[0], tran );
130 return Integer.valueOf( rtn[0][0] );
131 }
132
133 /**
134 * å�?ƒ‡ータベã?スに対応するenum名を返しますã?
135 *
136 * @og.rev 5.1.4.0 (2010/03/01) 新規作æ?
137 *
138 * @param dbName ãƒ??タベã?スå�?
139 *
140 * @return ãƒ??タベã?スに対応するenumå�?
141 */
142 public static DBFunctionName getDBName( final String dbName ) {
143 String dbn = dbName.toUpperCase( Locale.JAPAN );
144
145 if( dbn.indexOf( "ORACLE" ) >= 0 ) { return DBFunctionName.ORACLE; }
146 else if( dbn.indexOf( "HSQL" ) >= 0 ) { return DBFunctionName.HSQL; }
147 else if( dbn.indexOf( "POSTGRES" ) >= 0 ) { return DBFunctionName.POSTGRES; }
148 else if( dbn.indexOf( "MYSQL" ) >= 0 ) { return DBFunctionName.MYSQL; }
149 else if( dbn.indexOf( "SQLSERVER" ) >= 0 ) { return DBFunctionName.SQLSERVER; }
150 else if( dbn.indexOf( "FIREBIRD" ) >= 0 ) { return DBFunctionName.FIREBIRD; }
151
152 final String errMsg = "初期化時に、指定ã? dbName キーが存在しませんã€?
153 + "[" + dbn + "]" ;
154
155 throw new RuntimeException( errMsg );
156 }
157
158 /**
159 * å�?ƒ‡ータベã?スに対応するファンクション名を返しますã?
160 *
161 * @og.rev 4.3.8.0 (2009/08/01) SUBSTRを追åŠ?
162 * @og.rev 5.1.2.0 (2010/01/01) MySQL対å¿?SUBSTRBå»?¢(帳票ãƒ??タのåˆ?‰²のå†?ƒ¨処ç�?Œ–に伴ã�?
163 * @og.rev 5.1.4.0 (2010/03/01) ãƒ??タベã?スå�?ではなくã?dbid で判断するように変更
164 *
165 * @param func ファンクションå�?定義æ–??
166 * @param dbid 接続å?ID
167 *
168 * @return 実ファンクションå�?
169 */
170 public static String getFunctionName( final String func ,final String dbid ) {
171 DBFunctionName dbName = DBFunctionName.getDBName( ConnectionFactory.getDBName( dbid ) );
172
173 return dbName.getFunctionName( func );
174 }
175
176 }