├── .github └── workflows │ └── gradle.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── miragesql-integration ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── miragesql │ │ └── miragesql │ │ └── integration │ │ ├── guice │ │ ├── MirageModule.java │ │ ├── TransactionInterceptor.java │ │ ├── Transactional.java │ │ └── package-info.java │ │ ├── seasar │ │ ├── SeasarConnectionProvider.java │ │ └── package-info.java │ │ └── spring │ │ ├── SpringConnectionProvider.java │ │ └── package-info.java │ └── test │ ├── java │ └── com │ │ └── miragesql │ │ └── miragesql │ │ ├── AbstractDatabaseTest.java │ │ ├── SqlManagerImplTest.java │ │ └── integration │ │ ├── guice │ │ └── TransactionInterceptorTest.java │ │ └── spring │ │ ├── SpringConnectionProviderTest.java │ │ └── SpringTestDao.java │ └── resources │ └── com │ └── miragesql │ └── miragesql │ ├── SqlManagerImplTest_countBook.sql │ ├── SqlManagerImplTest_removeSemicolon.sql │ ├── SqlManagerImplTest_selectByBookName.sql │ ├── SqlManagerImplTest_selectByBookNames.sql │ ├── SqlManagerImplTest_selectMagazineByBookName.sql │ ├── SqlManagerImplTest_setUp.sql │ ├── SqlManagerImplTest_tearDown.sql │ ├── SqlManagerImplTest_testCall_setUp.sql │ ├── SqlManagerImplTest_testCall_tearDown.sql │ ├── SqlManagerImplTest_testOracleHint1.sql │ ├── SqlManagerImplTest_testOracleHint2.sql │ └── integration │ └── spring │ └── applicationContext.xml ├── miragesql-test ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── miragesql │ │ └── miragesql │ │ └── test │ │ ├── ExecutedSQLInfo.java │ │ ├── MirageTestContext.java │ │ ├── MockCallExecuter.java │ │ ├── MockSqlExecuter.java │ │ ├── MockSqlManager.java │ │ └── package-info.java │ └── test │ ├── java │ └── com │ │ └── miragesql │ │ └── miragesql │ │ ├── AbstractDatabaseTest.java │ │ └── SqlManagerImplTest.java │ └── resources │ └── com │ └── miragesql │ └── miragesql │ ├── SqlManagerImplTest_countBook.sql │ ├── SqlManagerImplTest_removeSemicolon.sql │ ├── SqlManagerImplTest_selectByBookName.sql │ ├── SqlManagerImplTest_selectByBookNames.sql │ ├── SqlManagerImplTest_selectMagazineByBookName.sql │ ├── SqlManagerImplTest_setUp.sql │ ├── SqlManagerImplTest_tearDown.sql │ ├── SqlManagerImplTest_testCall_setUp.sql │ ├── SqlManagerImplTest_testCall_tearDown.sql │ ├── SqlManagerImplTest_testOracleHint1.sql │ └── SqlManagerImplTest_testOracleHint2.sql ├── miragesql-tools ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── miragesql │ │ └── miragesql │ │ └── tools │ │ ├── EntityGen.java │ │ ├── EntityGenTask.java │ │ └── package-info.java │ └── test │ ├── groovy │ └── com │ │ └── miragesql │ │ └── miragesql │ │ └── tools │ │ └── EntityGenSpec.groovy │ ├── java │ └── com │ │ └── miragesql │ │ └── miragesql │ │ ├── AbstractDatabaseTest.java │ │ └── tools │ │ └── EntityGenTest.java │ └── resources │ └── com │ └── miragesql │ └── miragesql │ ├── SqlManagerImplTest_setUp.sql │ ├── SqlManagerImplTest_tearDown.sql │ └── tools │ ├── EntityGenTest_testGetGroovyEntitySource.txt │ ├── EntityGenTest_testGetJavaEntitySource.txt │ └── EntityGenTest_testGetXmlEntitySource.txt ├── settings.gradle └── src ├── main └── java │ └── com │ └── miragesql │ └── miragesql │ ├── CallExecutor.java │ ├── ClasspathSqlResource.java │ ├── DefaultEntityOperator.java │ ├── EntityCreationFailedException.java │ ├── EntityOperator.java │ ├── IterationCallback.java │ ├── SqlExecutor.java │ ├── SqlManager.java │ ├── SqlManagerImpl.java │ ├── SqlResource.java │ ├── StringSqlResource.java │ ├── annotation │ ├── Column.java │ ├── Enumerated.java │ ├── In.java │ ├── InOut.java │ ├── Out.java │ ├── PrimaryKey.java │ ├── ResultSet.java │ ├── Table.java │ ├── Transient.java │ └── package-info.java │ ├── bean │ ├── BeanDesc.java │ ├── BeanDescFactory.java │ ├── BeanDescImpl.java │ ├── DefaultPropertyExtractor.java │ ├── FieldPropertyExtractor.java │ ├── MapBeanDescImpl.java │ ├── MapPropertyDescImpl.java │ ├── PropertyDesc.java │ ├── PropertyDescImpl.java │ ├── PropertyExtractor.java │ ├── PropertyWrapper.java │ ├── PropertyWrapperImpl.java │ ├── ReflectiveOperationFailedException.java │ └── package-info.java │ ├── dialect │ ├── DB2Dialect.java │ ├── DerbyDialect.java │ ├── Dialect.java │ ├── H2Dialect.java │ ├── HyperSQLDialect.java │ ├── MySQLDialect.java │ ├── OracleDialect.java │ ├── PostgreSQLDialect.java │ ├── SQLServerDialect.java │ ├── SQLiteDialect.java │ ├── StandardDialect.java │ └── package-info.java │ ├── exception │ ├── BeanDescException.java │ ├── BreakIterationException.java │ ├── ConfigurationException.java │ ├── IORuntimeException.java │ ├── OgnlRuntimeException.java │ ├── SQLRuntimeException.java │ ├── SessionException.java │ ├── TwoWaySQLException.java │ └── package-info.java │ ├── filter │ ├── OpenSessionInViewFilter.java │ └── package-info.java │ ├── naming │ ├── DefaultNameConverter.java │ ├── Inflection.java │ ├── NameConverter.java │ ├── RailsLikeNameConverter.java │ └── package-info.java │ ├── package-info.java │ ├── parser │ ├── AbstractNode.java │ ├── AddWhereIfNode.java │ ├── BeginNode.java │ ├── BindVariableNode.java │ ├── ContainerNode.java │ ├── ElseNode.java │ ├── EmbeddedValueNode.java │ ├── IfNode.java │ ├── Node.java │ ├── ParenBindVariableNode.java │ ├── PrefixSqlNode.java │ ├── SqlArgWrapper.java │ ├── SqlContext.java │ ├── SqlContextImpl.java │ ├── SqlContextPropertyAccessor.java │ ├── SqlNode.java │ ├── SqlParser.java │ ├── SqlParserImpl.java │ ├── SqlTokenizer.java │ ├── SqlTokenizerImpl.java │ └── package-info.java │ ├── provider │ ├── ConnectionProvider.java │ ├── DataSourceConnectionProvider.java │ ├── DefaultConnectionProvider.java │ ├── JNDIDataSourceConnectionProvider.java │ ├── JNDIXADataSourceConnectionProvider.java │ ├── XADataSourceConnectionProvider.java │ └── package-info.java │ ├── session │ ├── DBCPSessionImpl.java │ ├── DialectAutoSelector.java │ ├── HikariCPSessionImpl.java │ ├── JDBCSessionImpl.java │ ├── JNDISessionImpl.java │ ├── Session.java │ ├── SessionFactory.java │ └── package-info.java │ ├── type │ ├── AbstractResultSetValueType.java │ ├── AbstractValueType.java │ ├── BigDecimalValueType.java │ ├── BooleanPrimitiveValueType.java │ ├── BooleanValueType.java │ ├── ByteArrayValueType.java │ ├── DefaultValueType.java │ ├── DoublePrimitiveValueType.java │ ├── DoubleValueType.java │ ├── FloatPrimitiveValueType.java │ ├── FloatValueType.java │ ├── IntegerPrimitiveValueType.java │ ├── IntegerValueType.java │ ├── LongPrimitiveValueType.java │ ├── LongValueType.java │ ├── ObjectValueType.java │ ├── OracleResultSetValueType.java │ ├── PostgreResultSetValueType.java │ ├── ShortPrimitiveValueType.java │ ├── ShortValueType.java │ ├── SqlDateValueType.java │ ├── StringValueType.java │ ├── TimeValueType.java │ ├── TimestampValueType.java │ ├── UtilDateValueType.java │ ├── ValueType.java │ ├── enumerate │ │ ├── EnumOneBasedOrdinalValueType.java │ │ ├── EnumOrdinalValueType.java │ │ ├── EnumStringValueType.java │ │ └── package-info.java │ └── package-info.java │ ├── updater │ ├── SchemaUpdateListener.java │ ├── SchemaUpdater.java │ └── package-info.java │ └── util │ ├── AnnotationUtils.java │ ├── ExceptionUtil.java │ ├── GenericUtil.java │ ├── IOUtil.java │ ├── JdbcUtil.java │ ├── MirageUtil.java │ ├── ModifierUtil.java │ ├── OgnlUtil.java │ ├── ReflectionUtil.java │ ├── StringUtil.java │ ├── Validate.java │ └── package-info.java └── test ├── groovy └── com │ └── miragesql │ └── miragesql │ ├── DefaultEntityOperatorSpec.groovy │ ├── bean │ ├── BeanDescFactorySpec.groovy │ ├── BeanDescImplSpec.groovy │ └── DefaultPropertyExtractorSpec.groovy │ ├── naming │ └── DefaultNameConverterSpec.groovy │ └── util │ └── MirageUtilSpec.groovy ├── java ├── META-INF │ └── MANIFEST.MF └── com │ └── miragesql │ └── miragesql │ ├── AbstractDatabaseTest.java │ ├── DefaultEntityOperatorTest.java │ ├── SqlManagerImplTest.java │ ├── bean │ ├── BeanDescImplTest.java │ ├── Book.java │ ├── BookType.java │ ├── DefaultPropertyExtractorTest.java │ ├── FieldPropertyExtractorTest.java │ └── Magazine.java │ ├── dialect │ ├── HyperSQLDialectTest.java │ ├── MySQLDialectTest.java │ ├── OracleDialectTest.java │ ├── PostgreSQLDialectTest.java │ └── StandardDialectTest.java │ ├── parser │ └── SqlParserImplTest.java │ └── type │ ├── ByteArrayValueTypeTest.java │ └── DefaultValueTypeTest.java └── resources └── com └── miragesql └── miragesql ├── SqlManagerImplTest_countBook.sql ├── SqlManagerImplTest_removeSemicolon.sql ├── SqlManagerImplTest_selectByBookName.sql ├── SqlManagerImplTest_selectByBookNames.sql ├── SqlManagerImplTest_selectMagazineByBookName.sql ├── SqlManagerImplTest_setUp.sql ├── SqlManagerImplTest_tearDown.sql ├── SqlManagerImplTest_testCall_setUp.sql ├── SqlManagerImplTest_testCall_tearDown.sql ├── SqlManagerImplTest_testOracleHint1.sql └── SqlManagerImplTest_testOracleHint2.sql /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 3 | 4 | name: Build 5 | 6 | on: [push, pull_request] 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up JDK 8 16 | uses: actions/setup-java@v2 17 | with: 18 | java-version: '8' 19 | distribution: 'adopt' 20 | cache: gradle 21 | - name: Grant execute permission for gradlew 22 | run: chmod +x gradlew 23 | - name: Build with Gradle 24 | run: ./gradlew test 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | build/ 3 | temp/ 4 | .gradle 5 | .idea 6 | *.iml 7 | *.ipr 8 | *.iws 9 | out 10 | .classpath 11 | .settings 12 | .project 13 | .gradletasknamecache 14 | gradle-app.setting -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mirage-sql/mirage/f2f2a517e726b85fbd44c9df5651c50216034fe4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /miragesql-integration/build.gradle: -------------------------------------------------------------------------------- 1 | uploadArchives.repositories.mavenDeployer { 2 | pom.project { 3 | name 'Mirage-SQL Integration' 4 | description 'Mirage-SQL Integration is an easy way to integrate Mirage-SQL with Spring, Guice or Seasar2' 5 | } 6 | } 7 | 8 | dependencies { 9 | compile rootProject 10 | compile project(":miragesql-test") 11 | 12 | compile 'org.slf4j:slf4j-api:1.7.25' 13 | 14 | compile 'commons-dbcp:commons-dbcp:1.4', optional 15 | compile 'com.zaxxer:HikariCP:2.7.2', optional 16 | 17 | compile "org.springframework:spring-core:5.3.10", optional 18 | compile "org.springframework:spring-context:5.3.10", optional 19 | compile "org.springframework:spring-jdbc:5.3.10", optional 20 | 21 | compile "com.google.inject:guice:5.0.1", optional 22 | 23 | 24 | compileOnly 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1' 25 | compileOnly 'javax.servlet:servlet-api:2.5' 26 | 27 | compile 'junit:junit:4.12' 28 | testImplementation 'cglib:cglib:3.2.5' 29 | testImplementation 'org.mockito:mockito-core:2.13.0' 30 | testImplementation 'com.h2database:h2:1.4.196' 31 | testImplementation 'org.hsqldb:hsqldb:2.4.0' 32 | 33 | testImplementation 'org.springframework:spring-aop:5.3.10' 34 | } 35 | -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/guice/MirageModule.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.guice; 2 | 3 | import com.miragesql.miragesql.SqlManager; 4 | import com.miragesql.miragesql.session.Session; 5 | import com.miragesql.miragesql.session.SessionFactory; 6 | 7 | import com.google.inject.AbstractModule; 8 | import com.google.inject.Provides; 9 | import com.google.inject.Singleton; 10 | import com.google.inject.matcher.Matchers; 11 | 12 | /** 13 | * The Module implementation to use Mirage-SQL with Google Guice. 14 | * 15 | * @author Naoki Takezoe 16 | */ 17 | public class MirageModule extends AbstractModule { 18 | 19 | // @Override 20 | protected void configure() { 21 | bindInterceptor( 22 | Matchers.any(), 23 | Matchers.annotatedWith(Transactional.class), 24 | new TransactionInterceptor()); 25 | } 26 | 27 | @Provides 28 | @Singleton 29 | public Session getSession(){ 30 | return SessionFactory.getSession(); 31 | } 32 | 33 | @Provides 34 | @Singleton 35 | public SqlManager getSqlManager(Session session){ 36 | return session.getSqlManager(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/guice/TransactionInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.guice; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import com.miragesql.miragesql.session.SessionFactory; 7 | import com.miragesql.miragesql.util.ExceptionUtil; 8 | 9 | import org.aopalliance.intercept.MethodInterceptor; 10 | import org.aopalliance.intercept.MethodInvocation; 11 | 12 | /** 13 | * An interceptor to control transaction. 14 | * 15 | * @author Naoki Takezoe 16 | * @see Transactional 17 | */ 18 | public class TransactionInterceptor implements MethodInterceptor { 19 | 20 | private static final Logger logger = LoggerFactory.getLogger(TransactionInterceptor.class); 21 | 22 | public Object invoke(MethodInvocation invocation) throws Throwable { 23 | 24 | try { 25 | SessionFactory.getSession().begin(); 26 | } catch(Exception ex){ 27 | logger.error("Failed to begin Session."); 28 | logger.error(ExceptionUtil.toString(ex)); 29 | throw ex; 30 | } 31 | 32 | try { 33 | Object result = invocation.proceed(); 34 | try { 35 | SessionFactory.getSession().commit(); 36 | 37 | } catch(Exception ex){ 38 | logger.error("Failed to commit Session."); 39 | logger.error(ExceptionUtil.toString(ex)); 40 | throw ex; 41 | } 42 | 43 | return result; 44 | 45 | } catch(Exception ex){ 46 | try { 47 | SessionFactory.getSession().rollback(); 48 | 49 | } catch(Exception e){ 50 | logger.error("Failed to rollback Session."); 51 | logger.error(ExceptionUtil.toString(e)); 52 | throw e; 53 | } 54 | throw ex; 55 | 56 | } finally { 57 | try { 58 | SessionFactory.getSession().release(); 59 | 60 | } catch (Exception ex) { 61 | logger.error("Failed to release Session."); 62 | logger.error(ExceptionUtil.toString(ex)); 63 | throw ex; 64 | } 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/guice/Transactional.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.guice; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Gives this annotation to transaction boundary methods. 11 | * 12 | * @author Naoki Takezoe 13 | * @see TransactionInterceptor 14 | */ 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Target({ElementType.METHOD}) 17 | @Documented 18 | public @interface Transactional { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/guice/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Google Guice integration functionality. 3 | */ 4 | package com.miragesql.miragesql.integration.guice; -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/seasar/SeasarConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.seasar; 2 | 3 | import java.sql.Connection; 4 | 5 | import javax.transaction.Synchronization; 6 | import javax.transaction.TransactionSynchronizationRegistry; 7 | 8 | import com.miragesql.miragesql.provider.ConnectionProvider; 9 | import com.miragesql.miragesql.provider.XADataSourceConnectionProvider; 10 | 11 | /** 12 | * {@link ConnectionProvider} implementation to use Mirage-SQL with Seasar2. 13 | * 14 | * @author Naoki Takezoe 15 | */ 16 | public class SeasarConnectionProvider extends XADataSourceConnectionProvider { 17 | 18 | private ThreadLocal registered= new ThreadLocal<>(); 19 | private TransactionSynchronizationRegistry syncRegistry; 20 | 21 | public void setTransactionSynchronizationRegistry(TransactionSynchronizationRegistry syncRegistry) { 22 | this.syncRegistry = syncRegistry; 23 | } 24 | 25 | @Override 26 | public Connection getConnection() { 27 | // If TransactionSynchronizationRegistry exists, 28 | // register Synchronization to release connection automatically 29 | // at the first invocation of this method in the current thread. 30 | // TODO: I wonder if I can register for each thread ... 31 | if(syncRegistry != null && registered.get() == null){ 32 | syncRegistry.registerInterposedSynchronization(new Synchronization() { 33 | //@Override 34 | public void beforeCompletion() { 35 | } 36 | 37 | //@Override 38 | public void afterCompletion(int status) { 39 | releaseConnection(); 40 | registered.remove(); 41 | } 42 | }); 43 | registered.set(true); 44 | } 45 | 46 | return super.getConnection(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/seasar/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Seasar2 integration functionality. 3 | */ 4 | package com.miragesql.miragesql.integration.seasar; -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/spring/SpringConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.spring; 2 | 3 | import java.sql.Connection; 4 | 5 | import javax.sql.DataSource; 6 | 7 | import com.miragesql.miragesql.provider.ConnectionProvider; 8 | 9 | import org.springframework.jdbc.datasource.ConnectionHolder; 10 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 11 | import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; 12 | import org.springframework.transaction.support.TransactionSynchronizationManager; 13 | 14 | /** 15 | * {@link ConnectionProvider} implementation to integrate Mirage-SQL with Spring Framework. 16 | * 17 | * @author Naoki Takezoe 18 | */ 19 | public class SpringConnectionProvider implements ConnectionProvider { 20 | 21 | private DataSource dataSource; 22 | 23 | public void setTransactionManager( 24 | DataSourceTransactionManager transactionManager) { 25 | dataSource = transactionManager.getDataSource(); 26 | } 27 | 28 | public void setDataSource(DataSource dataSource) { 29 | if (dataSource instanceof TransactionAwareDataSourceProxy) { 30 | // If we got a TransactionAwareDataSourceProxy, we need to perform transactions 31 | // for its underlying target DataSource, else data access code won't see 32 | // properly exposed transactions (i.e. transactions for the target DataSource). 33 | this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource(); 34 | } 35 | else { 36 | this.dataSource = dataSource; 37 | } 38 | } 39 | 40 | public Connection getConnection() { 41 | ConnectionHolder conHolder = 42 | (ConnectionHolder) TransactionSynchronizationManager.getResource( 43 | dataSource); 44 | 45 | if (conHolder == null) { 46 | throw new IllegalStateException("It seems not to be existing a transaction."); 47 | } 48 | 49 | return conHolder.getConnection(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /miragesql-integration/src/main/java/com/miragesql/miragesql/integration/spring/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the SpringSource Spring integration functionality. 3 | */ 4 | package com.miragesql.miragesql.integration.spring; -------------------------------------------------------------------------------- /miragesql-integration/src/test/java/com/miragesql/miragesql/AbstractDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | import static org.mockito.Mockito.*; 4 | 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | 8 | import com.miragesql.miragesql.dialect.HyperSQLDialect; 9 | import com.miragesql.miragesql.provider.ConnectionProvider; 10 | import com.miragesql.miragesql.util.IOUtil; 11 | import junit.framework.TestCase; 12 | 13 | public abstract class AbstractDatabaseTest extends TestCase { 14 | 15 | protected static final String SQL_PREFIX = "com/miragesql/miragesql/"; 16 | 17 | protected SqlManager sqlManager = new SqlManagerImpl(); 18 | protected Connection conn; 19 | 20 | @Override 21 | protected void setUp() throws Exception { 22 | super.setUp(); 23 | Class.forName("org.hsqldb.jdbc.JDBCDriver"); 24 | conn = DriverManager.getConnection("jdbc:hsqldb:mem:mirage_test", "sa", ""); 25 | 26 | ConnectionProvider connectionProvider = mock(ConnectionProvider.class); 27 | when(connectionProvider.getConnection()).thenReturn(conn); 28 | 29 | sqlManager.setConnectionProvider(connectionProvider); 30 | sqlManager.setDialect(new HyperSQLDialect()); 31 | 32 | executeMultipleStatement(SQL_PREFIX + "SqlManagerImplTest_setUp.sql"); 33 | } 34 | 35 | @Override 36 | protected void tearDown() throws Exception { 37 | super.tearDown(); 38 | executeMultipleStatement(SQL_PREFIX + "SqlManagerImplTest_tearDown.sql"); 39 | conn.close(); 40 | } 41 | 42 | private void executeMultipleStatement(String sqlPath) throws Exception { 43 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); 44 | byte[] bytes = IOUtil.readStream(cl.getResourceAsStream(sqlPath)); 45 | String sql = new String(bytes, "UTF-8"); 46 | for(String statement: sql.split(";")){ 47 | if(statement.trim().length() > 0){ 48 | sqlManager.executeUpdate(new StringSqlResource(statement)); 49 | } 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/java/com/miragesql/miragesql/integration/spring/SpringConnectionProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.spring; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import com.miragesql.miragesql.AbstractDatabaseTest; 6 | import com.miragesql.miragesql.SqlManager; 7 | import com.miragesql.miragesql.SqlManagerImpl; 8 | import com.miragesql.miragesql.SqlManagerImplTest.Book; 9 | import com.miragesql.miragesql.dialect.Dialect; 10 | import com.miragesql.miragesql.dialect.HyperSQLDialect; 11 | 12 | import org.springframework.context.ApplicationContext; 13 | import org.springframework.context.support.ClassPathXmlApplicationContext; 14 | 15 | public class SpringConnectionProviderTest extends AbstractDatabaseTest { 16 | 17 | private static final String APPLICATION_CONTEXT = 18 | "com/miragesql/miragesql/integration/spring/applicationContext.xml"; 19 | 20 | /** 21 | * Tests dialect configuration. 22 | */ 23 | public void testSpringSpringConnectionProvider1() throws Exception { 24 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT); 25 | 26 | SqlManager sqlManager = (SqlManager) applicationContext.getBean("sqlManager"); 27 | 28 | Field field = SqlManagerImpl.class.getDeclaredField("dialect"); 29 | field.setAccessible(true); 30 | Dialect dialect = (Dialect) field.get(sqlManager); 31 | 32 | assertTrue(dialect instanceof HyperSQLDialect); 33 | } 34 | 35 | /** 36 | * Tests SQL execution (commit). 37 | */ 38 | public void testSpringSpringConnectionProvider2() throws Exception { 39 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT); 40 | 41 | SpringTestDao springTestDao = (SpringTestDao) applicationContext.getBean("springTestDao"); 42 | 43 | Book book = new Book(); 44 | book.name = "Mirage in Action"; 45 | book.author = "Naoki Takezoe"; 46 | book.price = 20; 47 | springTestDao.insert(book, false); 48 | 49 | assertEquals(1, springTestDao.getCount()); 50 | } 51 | 52 | /** 53 | * Tests SQL execution (rollback). 54 | */ 55 | public void testSpringSpringConnectionProvider3() throws Exception { 56 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT); 57 | 58 | SpringTestDao springTestDao = (SpringTestDao) applicationContext.getBean("springTestDao"); 59 | 60 | Book book = new Book(); 61 | book.name = "Mirage in Action"; 62 | book.author = "Naoki Takezoe"; 63 | book.price = 20; 64 | try { 65 | springTestDao.insert(book, true); 66 | fail(); 67 | } catch(RuntimeException ignored){ 68 | } 69 | 70 | assertEquals(0, springTestDao.getCount()); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/java/com/miragesql/miragesql/integration/spring/SpringTestDao.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.integration.spring; 2 | 3 | import com.miragesql.miragesql.SqlManager; 4 | import com.miragesql.miragesql.StringSqlResource; 5 | import com.miragesql.miragesql.SqlManagerImplTest.Book; 6 | 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | public class SpringTestDao { 10 | 11 | private SqlManager sqlManager; 12 | 13 | public SqlManager getSqlManager() { 14 | return sqlManager; 15 | } 16 | 17 | public void setSqlManager(SqlManager sqlManager) { 18 | this.sqlManager = sqlManager; 19 | } 20 | 21 | @Transactional 22 | public void insert(Book book, boolean throwException){ 23 | sqlManager.insertEntity(book); 24 | if(throwException){ 25 | throw new RuntimeException(); 26 | } 27 | } 28 | 29 | @Transactional 30 | public int getCount(){ 31 | return sqlManager.getSingleResult( 32 | Integer.class, new StringSqlResource("SELECT COUNT(*) FROM BOOK")); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_countBook.sql: -------------------------------------------------------------------------------- 1 | SELECT COUNT(*) FROM BOOK 2 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_removeSemicolon.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE FROM BOOK; 2 | 3 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_selectByBookName.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE 2 | FROM BOOK 3 | /*IF bookName != null*/ 4 | WHERE BOOK_NAME=/*bookName*/'Mirage in Action' 5 | /*END*/ 6 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_selectByBookNames.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE 2 | FROM BOOK 3 | /*IF bookName != null*/ 4 | WHERE BOOK_NAME IN /*bookNames*/('Mirage in Action') 5 | /*END*/ 6 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_selectMagazineByBookName.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, MAG_TYPE, PRICE 2 | FROM MAGAZINE 3 | /*IF bookName != null*/ 4 | WHERE BOOK_NAME=/*bookName*/'Mirage in Action' 5 | /*END*/ 6 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_setUp.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE BOOK ( 2 | BOOK_ID INT IDENTITY PRIMARY KEY, 3 | BOOK_NAME VARCHAR(200) NOT NULL, 4 | AUTHOR VARCHAR(200) NOT NULL, 5 | PRICE INT 6 | ); 7 | 8 | CREATE TABLE MAGAZINE ( 9 | BOOK_ID INT IDENTITY PRIMARY KEY, 10 | BOOK_NAME VARCHAR(200) NOT NULL, 11 | MAG_TYPE VARCHAR(16) NOT NULL, 12 | PRICE INT 13 | ); 14 | 15 | CREATE TABLE USER_INFO ( 16 | USER_ID INT PRIMARY KEY, 17 | USER_NAME VARCHAR(200) NOT NULL 18 | ); 19 | 20 | CREATE SEQUENCE USER_INFO_USER_ID_SEQ AS INTEGER; 21 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_tearDown.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE BOOK; 2 | DROP TABLE MAGAZINE; 3 | DROP TABLE USER_INFO; 4 | DROP SEQUENCE USER_INFO_USER_ID_SEQ; 5 | -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testCall_setUp.sql: -------------------------------------------------------------------------------- 1 | CREATE PROCEDURE NEW_BOOK 2 | (BOOK_NAME VARCHAR(200), AUTHOR VARCHAR(200), PRICE INT) 3 | MODIFIES SQL DATA 4 | INSERT INTO BOOK 5 | VALUES (DEFAULT, BOOK_NAME, AUTHOR, PRICE) -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testCall_tearDown.sql: -------------------------------------------------------------------------------- 1 | DROP PROCEDURE NEW_BOOK -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testOracleHint1.sql: -------------------------------------------------------------------------------- 1 | select /*+ first_rows(1) */BOOK_ID, BOOK_NAME from BOOK -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testOracleHint2.sql: -------------------------------------------------------------------------------- 1 | select --+ first_rows(1) 2 | BOOK_ID, BOOK_NAME from BOOK -------------------------------------------------------------------------------- /miragesql-integration/src/test/resources/com/miragesql/miragesql/integration/spring/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /miragesql-test/build.gradle: -------------------------------------------------------------------------------- 1 | uploadArchives.repositories.mavenDeployer { 2 | pom.project { 3 | name 'Mirage-SQL Test' 4 | description 'Mirage-SQL Test is a test utility for the Mirage-SQL framework.' 5 | } 6 | } 7 | 8 | dependencies { 9 | compile rootProject 10 | 11 | compile 'org.slf4j:slf4j-api:1.7.25' 12 | 13 | compile 'junit:junit:4.12' 14 | 15 | testImplementation 'cglib:cglib:3.2.5' 16 | testImplementation 'org.mockito:mockito-core:2.13.0' 17 | testImplementation 'com.h2database:h2:1.4.196' 18 | testImplementation 'org.hsqldb:hsqldb:2.4.0' 19 | 20 | // Use the latest Groovy version for Spock testing 21 | testImplementation 'org.codehaus.groovy:groovy-all:2.4.13' 22 | // Use the awesome Spock testing and specification framework even with Java 23 | testImplementation 'org.spockframework:spock-core:1.1-groovy-2.4' 24 | } 25 | -------------------------------------------------------------------------------- /miragesql-test/src/main/java/com/miragesql/miragesql/test/ExecutedSQLInfo.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.test; 2 | 3 | import com.miragesql.miragesql.bean.PropertyDesc; 4 | 5 | /** 6 | * 7 | * @author Naoki Takezoe 8 | */ 9 | public class ExecutedSQLInfo { 10 | 11 | private String sql; 12 | private PropertyDesc[] propDescs; 13 | private Object entity; 14 | private Object[] params; 15 | 16 | /** 17 | * Constructor. 18 | * 19 | * @param sql the executed SQL 20 | * @param propDescs the array of bound parameters 21 | * @param entity the entity 22 | */ 23 | public ExecutedSQLInfo(String sql, PropertyDesc[] propDescs, Object entity){ 24 | this.sql = sql; 25 | this.propDescs = propDescs; 26 | this.entity = entity; 27 | } 28 | 29 | /** 30 | * Constructor. 31 | * 32 | * @param sql the executed SQL 33 | * @param params the array of bound parameters 34 | */ 35 | public ExecutedSQLInfo(String sql, Object[] params){ 36 | this.sql = sql; 37 | this.params = params; 38 | } 39 | 40 | /** 41 | * Returns the executed SQL. 42 | * 43 | * @return the executed SQL 44 | */ 45 | public String getSql(){ 46 | return this.sql; 47 | } 48 | 49 | /** 50 | * Returns the array of bound parameters. 51 | * 52 | * @return the array of bound parameters 53 | */ 54 | public Object[] getParams() { 55 | if(params == null) { 56 | params = new Object[propDescs.length]; 57 | for (int i = 0; i < propDescs.length; i++) { 58 | params[i] = propDescs[i].getValue(entity); 59 | } 60 | } 61 | return params; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /miragesql-test/src/main/java/com/miragesql/miragesql/test/MockSqlManager.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.test; 2 | 3 | import com.miragesql.miragesql.SqlManager; 4 | import com.miragesql.miragesql.SqlManagerImpl; 5 | 6 | /** 7 | * The mock class of {@link SqlManager} for unit testing. 8 | *

9 | * You can run your code which uses SqlManager without database 10 | * using this class instead of {@link SqlManagerImpl}. 11 | * You can also verify executed SQLs via {@link MirageTestContext}. 12 | * 13 | * @author Naoki Takezoe 14 | * @see MockSqlExecuter 15 | * @see MockCallExecuter 16 | * @see MirageTestContext 17 | */ 18 | public class MockSqlManager extends SqlManagerImpl { 19 | 20 | /** 21 | * Constructor. 22 | */ 23 | public MockSqlManager() { 24 | super(); 25 | 26 | this.sqlExecutor = new MockSqlExecuter(); 27 | this.sqlExecutor.setNameConverter(this.nameConverter); 28 | this.sqlExecutor.setDialect(this.dialect); 29 | 30 | this.callExecutor = new MockCallExecuter(); 31 | this.callExecutor.setNameConverter(this.nameConverter); 32 | this.callExecutor.setDialect(this.dialect); 33 | } 34 | 35 | // TODO GenerationType.SEQUENCE processing 36 | 37 | } 38 | -------------------------------------------------------------------------------- /miragesql-test/src/main/java/com/miragesql/miragesql/test/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides Testing utilities for Mirage-SQL based applications. 3 | */ 4 | package com.miragesql.miragesql.test; -------------------------------------------------------------------------------- /miragesql-test/src/test/java/com/miragesql/miragesql/AbstractDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | import static org.mockito.Mockito.*; 4 | 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | 8 | import com.miragesql.miragesql.dialect.HyperSQLDialect; 9 | import com.miragesql.miragesql.provider.ConnectionProvider; 10 | import com.miragesql.miragesql.util.IOUtil; 11 | import junit.framework.TestCase; 12 | 13 | public abstract class AbstractDatabaseTest extends TestCase { 14 | 15 | protected static final String SQL_PREFIX = "com/miragesql/miragesql/"; 16 | 17 | protected SqlManager sqlManager = new SqlManagerImpl(); 18 | protected Connection conn; 19 | 20 | @Override 21 | protected void setUp() throws Exception { 22 | super.setUp(); 23 | Class.forName("org.hsqldb.jdbc.JDBCDriver"); 24 | conn = DriverManager.getConnection("jdbc:hsqldb:mem:mirage_test", "sa", ""); 25 | 26 | ConnectionProvider connectionProvider = mock(ConnectionProvider.class); 27 | when(connectionProvider.getConnection()).thenReturn(conn); 28 | 29 | sqlManager.setConnectionProvider(connectionProvider); 30 | sqlManager.setDialect(new HyperSQLDialect()); 31 | 32 | executeMultipleStatement(SQL_PREFIX + "SqlManagerImplTest_setUp.sql"); 33 | } 34 | 35 | @Override 36 | protected void tearDown() throws Exception { 37 | super.tearDown(); 38 | executeMultipleStatement(SQL_PREFIX + "SqlManagerImplTest_tearDown.sql"); 39 | conn.close(); 40 | } 41 | 42 | private void executeMultipleStatement(String sqlPath) throws Exception { 43 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); 44 | byte[] bytes = IOUtil.readStream(cl.getResourceAsStream(sqlPath)); 45 | String sql = new String(bytes, "UTF-8"); 46 | for(String statement: sql.split(";")){ 47 | if(statement.trim().length() > 0){ 48 | sqlManager.executeUpdate(new StringSqlResource(statement)); 49 | } 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_countBook.sql: -------------------------------------------------------------------------------- 1 | SELECT COUNT(*) FROM BOOK 2 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_removeSemicolon.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE FROM BOOK; 2 | 3 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_selectByBookName.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE 2 | FROM BOOK 3 | /*IF bookName != null*/ 4 | WHERE BOOK_NAME=/*bookName*/'Mirage in Action' 5 | /*END*/ 6 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_selectByBookNames.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE 2 | FROM BOOK 3 | /*IF bookName != null*/ 4 | WHERE BOOK_NAME IN /*bookNames*/('Mirage in Action') 5 | /*END*/ 6 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_selectMagazineByBookName.sql: -------------------------------------------------------------------------------- 1 | SELECT BOOK_ID, BOOK_NAME, MAG_TYPE, PRICE 2 | FROM MAGAZINE 3 | /*IF bookName != null*/ 4 | WHERE BOOK_NAME=/*bookName*/'Mirage in Action' 5 | /*END*/ 6 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_setUp.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE BOOK ( 2 | BOOK_ID INT IDENTITY PRIMARY KEY, 3 | BOOK_NAME VARCHAR(200) NOT NULL, 4 | AUTHOR VARCHAR(200) NOT NULL, 5 | PRICE INT 6 | ); 7 | 8 | CREATE TABLE MAGAZINE ( 9 | BOOK_ID INT IDENTITY PRIMARY KEY, 10 | BOOK_NAME VARCHAR(200) NOT NULL, 11 | MAG_TYPE VARCHAR(16) NOT NULL, 12 | PRICE INT 13 | ); 14 | 15 | CREATE TABLE USER_INFO ( 16 | USER_ID INT PRIMARY KEY, 17 | USER_NAME VARCHAR(200) NOT NULL 18 | ); 19 | 20 | CREATE SEQUENCE USER_INFO_USER_ID_SEQ AS INTEGER; 21 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_tearDown.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE BOOK; 2 | DROP TABLE MAGAZINE; 3 | DROP TABLE USER_INFO; 4 | DROP SEQUENCE USER_INFO_USER_ID_SEQ; 5 | -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testCall_setUp.sql: -------------------------------------------------------------------------------- 1 | CREATE PROCEDURE NEW_BOOK 2 | (BOOK_NAME VARCHAR(200), AUTHOR VARCHAR(200), PRICE INT) 3 | MODIFIES SQL DATA 4 | INSERT INTO BOOK 5 | VALUES (DEFAULT, BOOK_NAME, AUTHOR, PRICE) -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testCall_tearDown.sql: -------------------------------------------------------------------------------- 1 | DROP PROCEDURE NEW_BOOK -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testOracleHint1.sql: -------------------------------------------------------------------------------- 1 | select /*+ first_rows(1) */BOOK_ID, BOOK_NAME from BOOK -------------------------------------------------------------------------------- /miragesql-test/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_testOracleHint2.sql: -------------------------------------------------------------------------------- 1 | select --+ first_rows(1) 2 | BOOK_ID, BOOK_NAME from BOOK -------------------------------------------------------------------------------- /miragesql-tools/build.gradle: -------------------------------------------------------------------------------- 1 | uploadArchives.repositories.mavenDeployer { 2 | pom.project { 3 | name 'Mirage-SQL Tools' 4 | description 'Mirage-SQL Tools is a set of tools to help the development of Mirage-SQL based applications.' 5 | } 6 | } 7 | 8 | dependencies { 9 | compile rootProject 10 | 11 | compile 'org.slf4j:slf4j-api:1.7.25' 12 | 13 | compileOnly 'org.apache.ant:ant:1.10.1' // provided 14 | 15 | testImplementation 'junit:junit:4.12' 16 | testImplementation 'org.mockito:mockito-core:2.13.0' 17 | testImplementation 'com.h2database:h2:1.4.196' 18 | testImplementation 'org.hsqldb:hsqldb:2.4.0' 19 | 20 | // Use the latest Groovy version for Spock testing 21 | testImplementation 'org.codehaus.groovy:groovy-all:2.4.13' 22 | // Use the awesome Spock testing and specification framework even with Java 23 | testImplementation 'org.spockframework:spock-core:1.1-groovy-2.4' 24 | } 25 | -------------------------------------------------------------------------------- /miragesql-tools/src/main/java/com/miragesql/miragesql/tools/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides tools to work with Mirage-SQL. E.g. EntityGenTask will reverse engineer 3 | * a database and create Java entities (with the correct annotations) that maps to that schema. 4 | */ 5 | package com.miragesql.miragesql.tools; -------------------------------------------------------------------------------- /miragesql-tools/src/test/groovy/com/miragesql/miragesql/tools/EntityGenSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.tools 2 | 3 | import spock.lang.Specification 4 | 5 | class EntityGenSpec extends Specification { 6 | def "validateLang() valid data"() { 7 | expect: 8 | EntityGen.validateLang(lang) == result 9 | 10 | where: 11 | lang || result 12 | "java" || true 13 | "groovy" || true 14 | "xml" || true 15 | } 16 | 17 | def "validateLang() invalid data"() { 18 | when: 19 | EntityGen.validateLang(lang) 20 | 21 | then: 22 | def error = thrown(expectedException) 23 | error.message == expectedMessage 24 | 25 | where: 26 | lang || expectedException | expectedMessage 27 | null || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 28 | "" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 29 | "Java" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 30 | "Groovy" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 31 | "scala" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 32 | "edn" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 33 | "puml" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 34 | "json" || IllegalArgumentException | "Argument 'lang' must be 'java', 'groovy' or 'xml' only!" 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /miragesql-tools/src/test/java/com/miragesql/miragesql/AbstractDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | import static org.mockito.Mockito.*; 4 | 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | 8 | import com.miragesql.miragesql.dialect.HyperSQLDialect; 9 | import com.miragesql.miragesql.provider.ConnectionProvider; 10 | import com.miragesql.miragesql.util.IOUtil; 11 | import junit.framework.TestCase; 12 | 13 | public abstract class AbstractDatabaseTest extends TestCase { 14 | 15 | protected static final String SQL_PREFIX = "com/miragesql/miragesql/"; 16 | 17 | protected SqlManager sqlManager = new SqlManagerImpl(); 18 | protected Connection conn; 19 | 20 | @Override 21 | protected void setUp() throws Exception { 22 | super.setUp(); 23 | Class.forName("org.hsqldb.jdbc.JDBCDriver"); 24 | conn = DriverManager.getConnection("jdbc:hsqldb:mem:mirage_test", "sa", ""); 25 | 26 | ConnectionProvider connectionProvider = mock(ConnectionProvider.class); 27 | when(connectionProvider.getConnection()).thenReturn(conn); 28 | 29 | sqlManager.setConnectionProvider(connectionProvider); 30 | sqlManager.setDialect(new HyperSQLDialect()); 31 | 32 | executeMultipleStatement(SQL_PREFIX + "SqlManagerImplTest_setUp.sql"); 33 | } 34 | 35 | @Override 36 | protected void tearDown() throws Exception { 37 | super.tearDown(); 38 | executeMultipleStatement(SQL_PREFIX + "SqlManagerImplTest_tearDown.sql"); 39 | conn.close(); 40 | } 41 | 42 | private void executeMultipleStatement(String sqlPath) throws Exception { 43 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); 44 | byte[] bytes = IOUtil.readStream(cl.getResourceAsStream(sqlPath)); 45 | String sql = new String(bytes, "UTF-8"); 46 | for(String statement: sql.split(";")){ 47 | if(statement.trim().length() > 0){ 48 | sqlManager.executeUpdate(new StringSqlResource(statement)); 49 | } 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /miragesql-tools/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_setUp.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE BOOK ( 2 | BOOK_ID INT IDENTITY PRIMARY KEY, 3 | BOOK_NAME VARCHAR(200) NOT NULL, 4 | AUTHOR VARCHAR(200) NOT NULL, 5 | PRICE INT 6 | ); 7 | 8 | CREATE TABLE MAGAZINE ( 9 | BOOK_ID INT IDENTITY PRIMARY KEY, 10 | BOOK_NAME VARCHAR(200) NOT NULL, 11 | MAG_TYPE VARCHAR(16) NOT NULL, 12 | PRICE INT 13 | ); 14 | 15 | CREATE TABLE USER_INFO ( 16 | USER_ID INT PRIMARY KEY, 17 | USER_NAME VARCHAR(200) NOT NULL 18 | ); 19 | 20 | CREATE SEQUENCE USER_INFO_USER_ID_SEQ AS INTEGER; 21 | -------------------------------------------------------------------------------- /miragesql-tools/src/test/resources/com/miragesql/miragesql/SqlManagerImplTest_tearDown.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE BOOK; 2 | DROP TABLE MAGAZINE; 3 | DROP TABLE USER_INFO; 4 | DROP SEQUENCE USER_INFO_USER_ID_SEQ; 5 | -------------------------------------------------------------------------------- /miragesql-tools/src/test/resources/com/miragesql/miragesql/tools/EntityGenTest_testGetGroovyEntitySource.txt: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.entity 2 | 3 | import com.miragesql.miragesql.annotation.Table 4 | import com.miragesql.miragesql.annotation.Column 5 | import com.miragesql.miragesql.annotation.PrimaryKey 6 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType 7 | 8 | @Table(name="BOOK") 9 | class Book { 10 | 11 | @PrimaryKey(generationType=GenerationType.SEQUENCE, generator="BOOK_BOOK_ID_SEQ") 12 | @Column(name="BOOK_ID") 13 | Integer bookId 14 | 15 | @Column(name="BOOK_NAME") 16 | String bookName 17 | 18 | @Column(name="AUTHOR") 19 | String author 20 | 21 | @Column(name="PRICE") 22 | Integer price 23 | 24 | } 25 | -------------------------------------------------------------------------------- /miragesql-tools/src/test/resources/com/miragesql/miragesql/tools/EntityGenTest_testGetJavaEntitySource.txt: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.entity; 2 | 3 | import com.miragesql.miragesql.annotation.Table; 4 | import com.miragesql.miragesql.annotation.Column; 5 | import com.miragesql.miragesql.annotation.PrimaryKey; 6 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType; 7 | 8 | @Table(name="BOOK") 9 | public class Book { 10 | 11 | @PrimaryKey(generationType=GenerationType.SEQUENCE, generator="BOOK_BOOK_ID_SEQ") 12 | @Column(name="BOOK_ID") 13 | public Integer bookId; 14 | 15 | @Column(name="BOOK_NAME") 16 | public String bookName; 17 | 18 | @Column(name="AUTHOR") 19 | public String author; 20 | 21 | @Column(name="PRICE") 22 | public Integer price; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /miragesql-tools/src/test/resources/com/miragesql/miragesql/tools/EntityGenTest_testGetXmlEntitySource.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'miragesql' 2 | include('miragesql-tools', 'miragesql-test', 'miragesql-integration') 3 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/ClasspathSqlResource.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * {@link SqlResource} represented by an sql path, more precisely the classpath resource of the file containing 8 | * the SQL string, found by the rules of: {@link ClassLoader#getResource(String)}. 9 | */ 10 | public class ClasspathSqlResource implements SqlResource { 11 | 12 | /** The path to the file containing the SQL. */ 13 | private final String sqlPath; 14 | 15 | /** 16 | * Constructs a {@link SqlResource} from a file path. 17 | * 18 | * @param sqlPath the SQL file path. 19 | */ 20 | public ClasspathSqlResource(final String sqlPath) { 21 | this.sqlPath = sqlPath; 22 | } 23 | 24 | /**{@inheritDoc}*/ 25 | @Override 26 | public InputStream getInputStream() throws IOException { 27 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); 28 | return cl.getResourceAsStream(sqlPath); 29 | } 30 | 31 | /**{@inheritDoc}*/ 32 | @Override 33 | public String toString() { 34 | return "ClasspathSqlResource [sqlPath=" + sqlPath + "]"; 35 | } 36 | 37 | /**{@inheritDoc}*/ 38 | @Override 39 | public int hashCode() { 40 | final int prime = 31; 41 | int result = 1; 42 | result = prime * result + ((sqlPath == null) ? 0 : sqlPath.hashCode()); 43 | return result; 44 | } 45 | 46 | /**{@inheritDoc}*/ 47 | @Override 48 | public boolean equals(Object obj) { 49 | if (this == obj) 50 | return true; 51 | if (obj == null) 52 | return false; 53 | if (getClass() != obj.getClass()) 54 | return false; 55 | ClasspathSqlResource other = (ClasspathSqlResource) obj; 56 | if (sqlPath == null) { 57 | if (other.sqlPath != null) 58 | return false; 59 | } else if (!sqlPath.equals(other.sqlPath)) 60 | return false; 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/EntityCreationFailedException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | 4 | /** 5 | * {@link EntityCreationFailedException} is thrown to indicate that 6 | * an {@link EntityOperator} failed to create a result entity. 7 | * 8 | * @see EntityOperator 9 | * @author daisuke 10 | */ 11 | @SuppressWarnings("serial") 12 | public class EntityCreationFailedException extends RuntimeException { 13 | 14 | public EntityCreationFailedException() { 15 | super(); 16 | } 17 | 18 | public EntityCreationFailedException(String message, Throwable cause) { 19 | super(message, cause); 20 | } 21 | 22 | public EntityCreationFailedException(String message) { 23 | super(message); 24 | } 25 | 26 | public EntityCreationFailedException(Throwable cause) { 27 | super(cause); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/IterationCallback.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | /** 4 | * Callback interface for iteration search. 5 | * 6 | * @author Naoki Takezoe 7 | * 8 | * @param the entity type 9 | * @param the return type 10 | * 11 | * @see SqlManager#iterate(Class, IterationCallback, SqlResource) 12 | * @see SqlManager#iterate(Class, IterationCallback, SqlResource, Object) 13 | */ 14 | public interface IterationCallback { 15 | 16 | R iterate(T entity); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/SqlResource.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * SqlResource is a common interface to access SQLs. 8 | */ 9 | public interface SqlResource { 10 | 11 | /** 12 | * Retrieves the Input Stream associated with a given SQL. 13 | * 14 | * @return an {@link InputStream} 15 | * @throws IOException if something goes wrong trying to access the SQL. 16 | */ 17 | InputStream getInputStream() throws IOException; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/StringSqlResource.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.nio.charset.StandardCharsets; 7 | 8 | /** 9 | * {@link SqlResource} represented by an SQL string. 10 | */ 11 | public class StringSqlResource implements SqlResource { 12 | 13 | /** The SQL content. */ 14 | private String sql; 15 | 16 | /** 17 | * Constructs the {@link SqlResource}. 18 | * 19 | * @param sql string representing an SQL 20 | */ 21 | public StringSqlResource(final String sql) { 22 | this.sql = sql; 23 | } 24 | 25 | /**{@inheritDoc}*/ 26 | @Override 27 | public InputStream getInputStream() throws IOException { 28 | return new ByteArrayInputStream(sql.getBytes(StandardCharsets.UTF_8)); 29 | } 30 | 31 | /**{@inheritDoc}*/ 32 | @Override 33 | public String toString() { 34 | return "StringSqlResource [sql=" + sql + "]"; 35 | } 36 | 37 | /**{@inheritDoc}*/ 38 | @Override 39 | public int hashCode() { 40 | final int prime = 31; 41 | int result = 1; 42 | result = prime * result + ((sql == null) ? 0 : sql.hashCode()); 43 | return result; 44 | } 45 | 46 | /**{@inheritDoc}*/ 47 | @Override 48 | public boolean equals(Object obj) { 49 | if (this == obj) 50 | return true; 51 | if (obj == null) 52 | return false; 53 | if (getClass() != obj.getClass()) 54 | return false; 55 | StringSqlResource other = (StringSqlResource) obj; 56 | if (sql == null) { 57 | if (other.sql != null) 58 | return false; 59 | } else if (!sql.equals(other.sql)) 60 | return false; 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/Column.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import com.miragesql.miragesql.naming.NameConverter; 10 | 11 | /** 12 | * Annotation that specifies the DB column name which is mapped to the annotated property. 13 | *

14 | * By default, Mirage-SQL converts the property name to the column name using {@link NameConverter}. 15 | * However if the entity property has this annotation, Mirage-SQL uses the specified column name instead of 16 | * NameConverter conversion. 17 | * 18 | * @author Naoki Takezoe 19 | * @author SHUJI Watanabe 20 | */ 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Target({ElementType.FIELD, ElementType.METHOD}) 23 | @Documented 24 | public @interface Column { 25 | 26 | /** 27 | * The column name which is mapped to the annotated property. 28 | * 29 | * @return the column name. 30 | */ 31 | String name(); 32 | 33 | /** 34 | * The place holder when generate insert / update SQL. 35 | * Defaults to value: ? 36 | * 37 | * @return the place holder 38 | */ 39 | String placeHolder() default "?"; 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/Enumerated.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Daisuke Miyamoto. 3 | * Created on 2011/10/24 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 14 | * either express or implied. See the License for the specific language 15 | * governing permissions and limitations under the License. 16 | */ 17 | package com.miragesql.miragesql.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Annotation that specifies that a persistent property or field should be persisted as an enumerated type.

26 | * It may be used in conjunction with the Basic annotation. 27 | * 28 | * @since 1.0 29 | * @author daisuke 30 | */ 31 | @Target({ 32 | ElementType.METHOD, 33 | ElementType.FIELD, 34 | ElementType.TYPE 35 | }) 36 | @Retention(RetentionPolicy.RUNTIME) 37 | public @interface Enumerated { 38 | 39 | /** 40 | * (Optional) The type used in mapping an enum type. 41 | * 42 | * @return the value 43 | */ 44 | EnumType value() default EnumType.ORDINAL; 45 | 46 | 47 | /** 48 | * Defines a mapping for the enumerated types. The constants of this enumerated type specify how persistent the property 49 | * or field should be persisted as a enumerated type. 50 | */ 51 | enum EnumType { 52 | /** Persists enumerated type property or field as an integer. */ 53 | ORDINAL, 54 | 55 | /** Persists enumerated type property or field as an integer. */ 56 | ONE_BASED_ORDINAL, 57 | 58 | /** Persists enumerated type property or field as a string. */ 59 | STRING, 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/In.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Annotation that indicates that the property is an IN parameter of the parameter class 11 | * which is used for DB store procedure / function invocation. 12 | */ 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target({ElementType.FIELD, ElementType.METHOD}) 15 | @Documented 16 | public @interface In { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/InOut.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Annotation that indicates that the property is IN-OUT parameter of the parameter class 11 | * which is used for the DB store procedure / function invocation. 12 | */ 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target({ElementType.FIELD, ElementType.METHOD}) 15 | @Documented 16 | public @interface InOut { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/Out.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Annotation that indicates that the property is an OUT parameter of the parameter class 11 | * which is used for DB store procedure / function invocation. 12 | */ 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target({ElementType.FIELD, ElementType.METHOD}) 15 | @Documented 16 | public @interface Out { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/PrimaryKey.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Annotation indicating that a property corresponds to the DB primary key column. 11 | * 12 | * @author Naoki Takezoe 13 | */ 14 | @Target({ElementType.FIELD, ElementType.METHOD}) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | public @interface PrimaryKey { 18 | 19 | GenerationType generationType(); 20 | 21 | String generator() default ""; 22 | 23 | /** 24 | * The type of the generated primary key. Supported types are: APPLICATION, IDENTITY and 25 | * SEQUENCE. 26 | */ 27 | enum GenerationType { 28 | /** The primary key is generated by the application itself. */ 29 | APPLICATION, 30 | 31 | /** The primary key is generated by the database using autoid/identity type of functionality. */ 32 | IDENTITY, 33 | 34 | /** The primary key is generated by the database using a sequence. */ 35 | SEQUENCE 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/ResultSet.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Annotation indicating that the annotated property maps a {@link java.sql.ResultSet}. 11 | */ 12 | @Retention(RetentionPolicy.RUNTIME) 13 | @Target({ElementType.FIELD, ElementType.METHOD}) 14 | @Documented 15 | public @interface ResultSet { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/Table.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import com.miragesql.miragesql.naming.NameConverter; 10 | 11 | /** 12 | * Annotation that specifies the DB table name which is mapped to the entity class. 13 | *

14 | * By default, Mirage-SQL converts the entity class name to the table name using {@link NameConverter}. 15 | * However if the entity class has this annotation, Mirage-SQL uses the specified table name instead of 16 | * NameConverter conversion. 17 | * 18 | * @author Naoki Takezoe 19 | */ 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Target(ElementType.TYPE) 22 | @Documented 23 | public @interface Table { 24 | 25 | /** 26 | * The table name which is mapped to the annotated entity class. 27 | * 28 | * @return the table name. 29 | */ 30 | String name(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/Transient.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Annotation that indicates that the property is transient, so it won't be persisted. 11 | * 12 | * @author Naoki Takezoe 13 | */ 14 | @Target({ElementType.FIELD, ElementType.METHOD}) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | public @interface Transient { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/annotation/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Mirage-SQL Annotations, useful to annotate beans used in conjunction with the 2WaySQL feature. 3 | */ 4 | package com.miragesql.miragesql.annotation; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/BeanDesc.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.annotation.Annotation; 4 | 5 | /** 6 | * Bean descriptor interface of an entity. 7 | */ 8 | public interface BeanDesc { 9 | 10 | /** 11 | * Returns the class of entity. 12 | * 13 | * @return class of entity 14 | */ 15 | Class getType(); 16 | 17 | /** 18 | * Returns the {@link PropertyDesc} of this bean.

19 | * The {@code name} parameter is a {@link String} specifying the simple name of the desired property. 20 | * 21 | * @param name property name 22 | * 23 | * @return the {@link PropertyDesc} or {@code null} if no property with this name is found 24 | */ 25 | PropertyDesc getPropertyDesc(String name); 26 | 27 | /** 28 | * Returns the size of {@link PropertyDesc} which this entity has. 29 | * 30 | * @return size 31 | */ 32 | int getPropertyDescSize(); 33 | 34 | /** 35 | * Returns {@link PropertyDesc} of this bean. 36 | * 37 | * @param i index number 38 | * 39 | * @return the {@link PropertyDesc} 40 | */ 41 | PropertyDesc getPropertyDesc(int i); 42 | 43 | /** 44 | * Returns the {@link Annotation} declared at this entity. 45 | * 46 | * @param the annotation type 47 | * @param type type of {@link Annotation} 48 | * 49 | * @return {@link Annotation} or {@code null} if no {@link Annotation} with this {@code type} is found 50 | */ 51 | T getAnnotation(Class type); 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/BeanDescFactory.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.util.HashMap; 4 | import java.util.LinkedHashMap; 5 | import java.util.Map; 6 | import java.util.concurrent.ConcurrentHashMap; 7 | 8 | /** 9 | * Factory class to build BeanDesc instances depending on the bean types. 10 | */ 11 | public class BeanDescFactory { 12 | 13 | private Map, BeanDesc> cacheMap = new ConcurrentHashMap<>(); 14 | private boolean cacheEnabled = false; 15 | private PropertyExtractor propertyExtractor = new DefaultPropertyExtractor(); 16 | 17 | public void setCacheEnabled(boolean cacheEnabled){ 18 | this.cacheEnabled = cacheEnabled; 19 | } 20 | 21 | public boolean isCacheEnabled(){ 22 | return cacheEnabled; 23 | } 24 | 25 | public void setPropertyExtractor(PropertyExtractor propertyExtractor){ 26 | this.propertyExtractor = propertyExtractor; 27 | } 28 | 29 | /** 30 | * Constructs a bean descriptor out of an Object instance. 31 | * 32 | * @param obj the object to create the descriptor from (the object can also be a Map) 33 | * @return a descriptor 34 | */ 35 | @SuppressWarnings("unchecked") 36 | public BeanDesc getBeanDesc(Object obj){ 37 | if(obj instanceof Map){ 38 | return new MapBeanDescImpl((Map) obj); 39 | } else { 40 | return getBeanDesc(obj.getClass()); 41 | } 42 | } 43 | 44 | /** 45 | * Constructs a bean descriptor out of a class. 46 | * 47 | * @param clazz the class to create the descriptor from. For Map.class, HashMap.class and 48 | * LinkedHashMap.class no properties can be extracted, so this operations needs to be done later. 49 | * @return a descriptor 50 | */ 51 | public BeanDesc getBeanDesc(Class clazz){ 52 | if(clazz == Map.class || clazz == HashMap.class || clazz == LinkedHashMap.class){ 53 | return new MapBeanDescImpl(); 54 | } 55 | 56 | if(cacheEnabled && cacheMap.containsKey(clazz)){ 57 | return cacheMap.get(clazz); 58 | } 59 | 60 | BeanDesc beanDesc = new BeanDescImpl(clazz, propertyExtractor.extractProperties(clazz)); 61 | 62 | if(cacheEnabled){ 63 | cacheMap.put(clazz, beanDesc); 64 | } 65 | 66 | return beanDesc; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/FieldPropertyExtractor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Daisuke Miyamoto. 3 | * Created on 2011/10/21 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 14 | * either express or implied. See the License for the specific language 15 | * governing permissions and limitations under the License. 16 | */ 17 | package com.miragesql.miragesql.bean; 18 | 19 | import java.lang.reflect.Field; 20 | import java.lang.reflect.Method; 21 | import java.lang.reflect.Modifier; 22 | import java.util.LinkedHashMap; 23 | import java.util.Map; 24 | 25 | /** 26 | * An implementation of {@link PropertyExtractor} which always retrieves property types and names 27 | * from the entity field if it has getters and setters. 28 | * 29 | * @since 1.1.4 30 | * @author daisuke 31 | */ 32 | public class FieldPropertyExtractor implements PropertyExtractor { 33 | 34 | /**{@inheritDoc}*/ 35 | public Map extractProperties(Class clazz) { 36 | Map map = new LinkedHashMap<>(); 37 | extractProperties0(clazz, map); 38 | return map; 39 | } 40 | 41 | private void extractProperties0(Class clazz, Map map) { 42 | if (clazz == null) { 43 | return; 44 | } 45 | Field[] fields = clazz.getDeclaredFields(); 46 | for (Field field : fields) { 47 | field.setAccessible(true); 48 | int modifiers = field.getModifiers(); 49 | if (map.containsKey(field.getName()) == false 50 | && Modifier.isStatic(modifiers) == false 51 | && Modifier.isFinal(modifiers) == false) { 52 | map.put(field.getName(), new ReadableWritablePropertyWrapperImpl(field.getName(), null, null, field)); 53 | } 54 | } 55 | extractProperties0(clazz.getSuperclass(), map); 56 | } 57 | 58 | private static class ReadableWritablePropertyWrapperImpl extends PropertyWrapperImpl { 59 | 60 | private ReadableWritablePropertyWrapperImpl(String name, Method getter, Method setter, Field field) { 61 | super(name, getter, setter, field); 62 | } 63 | 64 | @Override 65 | public boolean isReadable() { 66 | return true; 67 | } 68 | 69 | @Override 70 | public boolean isWritable() { 71 | return true; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/MapBeanDescImpl.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.util.*; 5 | import java.util.Map.Entry; 6 | 7 | public class MapBeanDescImpl implements BeanDesc { 8 | 9 | private Map map; 10 | private PropertyDesc[] propertyArray; 11 | 12 | // keep the order of original properties 13 | private final Map propertyMap = Collections.synchronizedMap(new LinkedHashMap<>()); 14 | 15 | public MapBeanDescImpl(){ 16 | } 17 | 18 | public MapBeanDescImpl(Map map){ 19 | this.map = map; 20 | 21 | List properties = new ArrayList<>(); 22 | 23 | for(Entry entry: map.entrySet()){ 24 | String propertyName = entry.getKey(); 25 | Object value = entry.getValue(); 26 | PropertyDesc pd = new MapPropertyDescImpl(propertyName, value); 27 | properties.add(pd); 28 | addToMap(propertyName, pd); 29 | } 30 | 31 | this.propertyArray = properties.toArray(new PropertyDesc[properties.size()]); 32 | } 33 | 34 | // @Override 35 | /**{@inheritDoc}*/ 36 | public PropertyDesc getPropertyDesc(String name) { 37 | if(this.map == null){ 38 | return new MapPropertyDescImpl(name, new Object()); 39 | } 40 | return this.propertyMap.get(name); 41 | } 42 | 43 | // @Override 44 | /**{@inheritDoc}*/ 45 | public PropertyDesc getPropertyDesc(int i) { 46 | if(this.map == null){ 47 | return null; 48 | } 49 | return this.propertyArray[i]; 50 | } 51 | 52 | // @Override 53 | /**{@inheritDoc}*/ 54 | public int getPropertyDescSize() { 55 | if(this.map == null){ 56 | return 0; 57 | } 58 | return this.propertyArray.length; 59 | } 60 | 61 | // @Override 62 | /**{@inheritDoc}*/ 63 | public Class getType() { 64 | if(this.map == null){ 65 | return Map.class; 66 | } 67 | return this.map.getClass(); 68 | } 69 | 70 | // @Override 71 | /**{@inheritDoc}*/ 72 | public T getAnnotation(Class type) { 73 | return null; 74 | } 75 | 76 | private void addToMap(String propertyName, PropertyDesc pd) { 77 | synchronized (propertyMap) { 78 | propertyMap.put(propertyName,pd); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/MapPropertyDescImpl.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.lang.reflect.Field; 5 | import java.util.Map; 6 | 7 | public class MapPropertyDescImpl implements PropertyDesc { 8 | 9 | private Object value; 10 | private String propertyName; 11 | 12 | public MapPropertyDescImpl(String propertyName, Object value){ 13 | this.propertyName = propertyName; 14 | this.value = value; 15 | } 16 | 17 | // @Override 18 | /**{@inheritDoc}*/ 19 | public T getAnnotation(Class type) { 20 | return null; 21 | } 22 | 23 | // @Override 24 | /**{@inheritDoc}*/ 25 | public Field getField() { 26 | return null; 27 | } 28 | 29 | // @Override 30 | /**{@inheritDoc}*/ 31 | public String getPropertyName() { 32 | return propertyName; 33 | } 34 | 35 | // @Override 36 | /**{@inheritDoc}*/ 37 | public Class getPropertyType() { 38 | return (null == value) ? null : value.getClass(); 39 | } 40 | 41 | // @Override 42 | // @SuppressWarnings("unchecked") 43 | /**{@inheritDoc}*/ 44 | public Object getValue(Object entity) { 45 | return Map.class.cast(entity).get(propertyName); 46 | } 47 | 48 | // @Override 49 | /**{@inheritDoc}*/ 50 | public boolean isReadable() { 51 | return true; 52 | } 53 | 54 | // @Override 55 | /**{@inheritDoc}*/ 56 | public boolean isWritable() { 57 | return true; 58 | } 59 | 60 | // @Override 61 | /**{@inheritDoc}*/ 62 | @SuppressWarnings("unchecked") 63 | public void setValue(Object entity, Object value) { 64 | ((Map) entity).put(propertyName, value); 65 | } 66 | 67 | // @Override 68 | /**{@inheritDoc}*/ 69 | public boolean isTransient() { 70 | return false; 71 | } 72 | 73 | /**{@inheritDoc}*/ 74 | @Override 75 | public String toString() { 76 | return "MapPropertyDescImpl [value=" + value + ", propertyName=" + propertyName + "]"; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/PropertyDesc.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.lang.reflect.Field; 5 | 6 | import com.miragesql.miragesql.annotation.Transient; 7 | import com.miragesql.miragesql.exception.BeanDescException; 8 | 9 | /** 10 | * Descriptor of a property of an entity. 11 | */ 12 | public interface PropertyDesc { 13 | 14 | /** 15 | * Returns the value of this property which the {@code entity} has. 16 | * 17 | * @param entity entity object 18 | * 19 | * @return the property value 20 | * 21 | * @throws BeanDescException TODO TBD 22 | */ 23 | Object getValue(Object entity); 24 | 25 | /** 26 | * Sets the value to this property to the {@code entity}. 27 | * 28 | * @param entity entity object 29 | * @param value the property value 30 | * 31 | * @throws BeanDescException TODO TBD 32 | */ 33 | void setValue(Object entity, Object value); 34 | 35 | /** 36 | * Tests whether this property is readable. 37 | * 38 | * @return true if this property is readable 39 | */ 40 | boolean isReadable(); 41 | 42 | /** 43 | * Tests whether this property is writable. 44 | * 45 | * @return {@code true} if this property is writable 46 | */ 47 | boolean isWritable(); 48 | 49 | /** 50 | * Returns the type of property. 51 | * 52 | * @return the property type 53 | */ 54 | Class getPropertyType(); 55 | 56 | /** 57 | * Returns the name of property. 58 | * 59 | * @return the property name 60 | */ 61 | String getPropertyName(); 62 | 63 | /** 64 | * Returns the field object of this property. 65 | * 66 | * @return {@link Field} or {@code null} if the property's implementation is not a field. 67 | */ 68 | Field getField(); 69 | 70 | /** 71 | * Returns whether this property is transient (transient field or {@link Transient} annotated property). 72 | * 73 | * @return {@code true} if this property is transient 74 | */ 75 | boolean isTransient(); 76 | 77 | /** 78 | * Returns {@link Annotation} which declared at this property. 79 | * 80 | * @param the annotation type 81 | * @param type type of {@link Annotation} 82 | * 83 | * @return {@link Annotation} or {@code null} if no {@link Annotation} with this {@code type} is found 84 | */ 85 | T getAnnotation(Class type); 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/PropertyDescImpl.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.lang.reflect.Field; 5 | import java.lang.reflect.InvocationTargetException; 6 | 7 | import com.miragesql.miragesql.exception.BeanDescException; 8 | 9 | public class PropertyDescImpl implements PropertyDesc { 10 | 11 | private PropertyWrapper propertyWrapper; 12 | 13 | public PropertyDescImpl(PropertyWrapper propertyWrapper){ 14 | this.propertyWrapper = propertyWrapper; 15 | } 16 | 17 | // @Override 18 | /**{@inheritDoc}*/ 19 | public Object getValue(Object entity){ 20 | try { 21 | return propertyWrapper.get(entity); 22 | } catch (IllegalAccessException ex) { 23 | throw new BeanDescException(ex); 24 | 25 | } catch (InvocationTargetException ex) { 26 | throw new BeanDescException(ex); 27 | 28 | } 29 | } 30 | 31 | // @Override 32 | /**{@inheritDoc}*/ 33 | public void setValue(Object entity, Object value){ 34 | try { 35 | propertyWrapper.set(entity, value); 36 | } catch (IllegalAccessException ex) { 37 | throw new BeanDescException(ex); 38 | 39 | } catch (InvocationTargetException ex) { 40 | throw new BeanDescException(ex); 41 | 42 | } 43 | } 44 | 45 | // @Override 46 | /**{@inheritDoc}*/ 47 | public boolean isReadable(){ 48 | return propertyWrapper.isReadable(); 49 | } 50 | 51 | // @Override 52 | /**{@inheritDoc}*/ 53 | public boolean isWritable(){ 54 | return propertyWrapper.isWritable(); 55 | } 56 | 57 | // @Override 58 | /**{@inheritDoc}*/ 59 | public Class getPropertyType(){ 60 | return propertyWrapper.getType(); 61 | } 62 | 63 | // @Override 64 | /**{@inheritDoc}*/ 65 | public String getPropertyName(){ 66 | return propertyWrapper.getName(); 67 | } 68 | 69 | // @Override 70 | /**{@inheritDoc}*/ 71 | public Field getField(){ 72 | return propertyWrapper.getField(); 73 | } 74 | 75 | // @Override 76 | /**{@inheritDoc}*/ 77 | public T getAnnotation(Class type){ 78 | return propertyWrapper.getAnnotation(type); 79 | } 80 | 81 | // @Override 82 | /**{@inheritDoc}*/ 83 | public boolean isTransient() { 84 | return propertyWrapper.isTransient(); 85 | } 86 | 87 | /**{@inheritDoc}*/ 88 | @Override 89 | public String toString() { 90 | return super.toString() + 91 | "[" + getPropertyName() + ":" + (propertyWrapper.getType() == null ? null : 92 | propertyWrapper.getType().getSimpleName()) + 93 | "]"; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/PropertyExtractor.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.reflect.Field; 4 | import java.lang.reflect.Method; 5 | import java.util.Map; 6 | 7 | /** 8 | * An interface for extracting property information from a class object. 9 | *

10 | * You can implement your own PropertyExtractor and enable it by {@link BeanDescFactory#setPropertyExtractor(PropertyExtractor)}. 11 | * 12 | * @author Naoki Takezoe 13 | */ 14 | public interface PropertyExtractor { 15 | 16 | /** 17 | * Extracts the properties from a class 18 | * @param clazz the class to extract properties from. 19 | * 20 | * @return a Map of properties 21 | */ 22 | Map extractProperties(Class clazz); 23 | 24 | class PropertyInfo { 25 | public String name; 26 | public Class type; 27 | public Method getterMethod; 28 | public Method setterMethod; 29 | public Field field; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/ReflectiveOperationFailedException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.bean; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | 5 | /** 6 | * {@link ReflectiveOperationFailedException} is thrown to indicate reflective operation has failed. 7 | * 8 | * @see ClassNotFoundException 9 | * @see InvocationTargetException 10 | * @see IllegalAccessException 11 | * @see InstantiationException 12 | * @see NoSuchMethodException 13 | * @see NoSuchFieldException 14 | * @author daisuke 15 | */ 16 | @SuppressWarnings("serial") 17 | public class ReflectiveOperationFailedException extends RuntimeException { 18 | 19 | public ReflectiveOperationFailedException(ClassNotFoundException e) { 20 | super(e); 21 | } 22 | 23 | public ReflectiveOperationFailedException(InvocationTargetException e) { 24 | super(e); 25 | } 26 | 27 | public ReflectiveOperationFailedException(IllegalAccessException e) { 28 | super(e); 29 | } 30 | 31 | public ReflectiveOperationFailedException(InstantiationException e) { 32 | super(e); 33 | } 34 | 35 | public ReflectiveOperationFailedException(NoSuchFieldException e) { 36 | super(e); 37 | } 38 | 39 | public ReflectiveOperationFailedException(NoSuchMethodException e) { 40 | super(e); 41 | } 42 | 43 | 44 | public ReflectiveOperationFailedException(String message, ClassNotFoundException e) { 45 | super(message, e); 46 | } 47 | 48 | public ReflectiveOperationFailedException(String message, InvocationTargetException e) { 49 | super(message, e); 50 | } 51 | 52 | public ReflectiveOperationFailedException(String message, IllegalAccessException e) { 53 | super(message, e); 54 | } 55 | 56 | public ReflectiveOperationFailedException(String message, InstantiationException e) { 57 | super(message, e); 58 | } 59 | 60 | public ReflectiveOperationFailedException(String message, NoSuchFieldException e) { 61 | super(message, e); 62 | } 63 | 64 | public ReflectiveOperationFailedException(String message, NoSuchMethodException e) { 65 | super(message, e); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/bean/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Beans functionality, required when working with Bean Entities. 3 | */ 4 | package com.miragesql.miragesql.bean; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/DB2Dialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | public class DB2Dialect extends StandardDialect { 4 | 5 | /**{@inheritDoc}*/ 6 | @Override 7 | public String getName() { 8 | return "db2"; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/DerbyDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | public class DerbyDialect extends StandardDialect { 4 | 5 | /**{@inheritDoc}*/ 6 | @Override 7 | public String getName() { 8 | return "derby"; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/Dialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType; 4 | import com.miragesql.miragesql.type.ValueType; 5 | 6 | public interface Dialect { 7 | 8 | /** 9 | * Returns the dialect name. 10 | * 11 | * @return the dialect name 12 | */ 13 | String getName(); 14 | 15 | /** 16 | * Returns true if the ResultSet of the procedure invocation requires parameter or not. 17 | * 18 | * @return true then required, false then not required 19 | */ 20 | boolean needsParameterForResultSet(); 21 | 22 | /** 23 | * Returns true if a specific primary key generation type is supported. 24 | * @param generationType the pk generation type 25 | * 26 | * @return true if generation type is supported, false otherwise 27 | */ 28 | boolean supportsGenerationType(GenerationType generationType); 29 | 30 | /** 31 | * Returns an SQL select (String) to execute the sequence. 32 | * 33 | * @param sequenceName the name of the DB Sequence 34 | * 35 | * @return an SQL select to execute the sequence 36 | */ 37 | String getSequenceSql(String sequenceName); 38 | 39 | /** 40 | * Wraps a select statement with another in order to count the rows. 41 | * @param sql the select to wrap with count 42 | * 43 | * @return a select to count the rows 44 | */ 45 | String getCountSql(String sql); 46 | 47 | /** 48 | * Returns the {@link ValueType} for the database product. 49 | * 50 | * @return the value type or null 51 | */ 52 | ValueType getValueType(); 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/H2Dialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | public class H2Dialect extends StandardDialect { 4 | 5 | /**{@inheritDoc}*/ 6 | @Override 7 | public String getName() { 8 | return "h2"; 9 | } 10 | 11 | /**{@inheritDoc}*/ 12 | @Override 13 | public String getSequenceSql(String sequenceName) { 14 | return String.format("SELECT NEXTVAL('%s')", sequenceName); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/HyperSQLDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | public class HyperSQLDialect extends StandardDialect { 4 | 5 | /**{@inheritDoc}*/ 6 | @Override 7 | public String getName() { 8 | return "hsqldb"; 9 | } 10 | 11 | /**{@inheritDoc}*/ 12 | @Override 13 | public String getSequenceSql(String sequenceName) { 14 | return String.format("SELECT NEXT VALUE FOR %s " + 15 | "FROM INFORMATION_SCHEMA.SYSTEM_TABLES " + 16 | "WHERE table_name = 'SYSTEM_TABLES'", sequenceName); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/MySQLDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType; 4 | 5 | // TODO: stored procedure / function and sequence support 6 | public class MySQLDialect extends StandardDialect { 7 | 8 | /**{@inheritDoc}*/ 9 | @Override 10 | public String getName() { 11 | return "mysql"; 12 | } 13 | 14 | /**{@inheritDoc}*/ 15 | @Override 16 | public String getCountSql(String sql) { 17 | return "SELECT COUNT(*) FROM (" + sql + ") A"; 18 | } 19 | 20 | /**{@inheritDoc}*/ 21 | @Override 22 | public boolean supportsGenerationType(GenerationType generationType) { 23 | if(generationType == GenerationType.SEQUENCE){ 24 | return false; 25 | } 26 | return true; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/OracleDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType; 4 | import com.miragesql.miragesql.type.OracleResultSetValueType; 5 | import com.miragesql.miragesql.type.ValueType; 6 | 7 | public class OracleDialect extends StandardDialect { 8 | 9 | private OracleResultSetValueType valueType = new OracleResultSetValueType(); 10 | 11 | /**{@inheritDoc}*/ 12 | @Override 13 | public String getName() { 14 | return "oracle"; 15 | } 16 | 17 | /**{@inheritDoc}*/ 18 | @Override 19 | public boolean needsParameterForResultSet() { 20 | return true; 21 | } 22 | 23 | /**{@inheritDoc}*/ 24 | @Override 25 | public ValueType getValueType() { 26 | return valueType; 27 | } 28 | 29 | /**{@inheritDoc}*/ 30 | @Override 31 | public String getSequenceSql(String sequenceName) { 32 | return String.format("SELECT %s.NEXTVAL FROM DUAL", sequenceName); 33 | } 34 | 35 | /**{@inheritDoc}*/ 36 | @Override 37 | public boolean supportsGenerationType(GenerationType generationType) { 38 | if(generationType == GenerationType.IDENTITY){ 39 | return false; 40 | } 41 | return true; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/PostgreSQLDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType; 4 | import com.miragesql.miragesql.type.PostgreResultSetValueType; 5 | import com.miragesql.miragesql.type.ValueType; 6 | 7 | // TODO LargeObject support 8 | public class PostgreSQLDialect extends StandardDialect { 9 | 10 | private PostgreResultSetValueType valueType = new PostgreResultSetValueType(); 11 | 12 | /**{@inheritDoc}*/ 13 | @Override 14 | public String getName() { 15 | return "postgresql"; 16 | } 17 | 18 | /**{@inheritDoc}*/ 19 | @Override 20 | public ValueType getValueType(){ 21 | return valueType; 22 | } 23 | 24 | /**{@inheritDoc}*/ 25 | @Override 26 | public String getSequenceSql(String sequenceName) { 27 | return String.format("SELECT NEXTVAL('%s')", sequenceName); 28 | } 29 | 30 | /**{@inheritDoc}*/ 31 | @Override 32 | public boolean supportsGenerationType(GenerationType generationType) { 33 | if(generationType == GenerationType.IDENTITY){ 34 | return false; 35 | } 36 | return true; 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/SQLServerDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | public class SQLServerDialect extends StandardDialect { 4 | 5 | /**{@inheritDoc}*/ 6 | @Override 7 | public String getName() { 8 | return "sqlserver"; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/SQLiteDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | public class SQLiteDialect extends StandardDialect { 4 | 5 | /**{@inheritDoc}*/ 6 | @Override 7 | public String getName() { 8 | return "sqlite"; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/StandardDialect.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.dialect; 2 | 3 | import com.miragesql.miragesql.annotation.PrimaryKey.GenerationType; 4 | import com.miragesql.miragesql.type.ValueType; 5 | 6 | public class StandardDialect implements Dialect { 7 | 8 | public String getName() { 9 | return null; 10 | } 11 | 12 | public boolean needsParameterForResultSet() { 13 | return false; 14 | } 15 | 16 | public ValueType getValueType() { 17 | return null; 18 | } 19 | 20 | public String getSequenceSql(String sequenceName) { 21 | return null; 22 | } 23 | 24 | public boolean supportsGenerationType(GenerationType generationType) { 25 | return true; 26 | } 27 | 28 | public String getCountSql(String sql) { 29 | return "SELECT COUNT(*) FROM (" + sql + ")"; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/dialect/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the supported Database dialect features in order to gracefully handle the differences of various database features. 3 | */ 4 | package com.miragesql.miragesql.dialect; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/BeanDescException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | 5 | import com.miragesql.miragesql.bean.ReflectiveOperationFailedException; 6 | 7 | public class BeanDescException extends ReflectiveOperationFailedException { 8 | 9 | private static final long serialVersionUID = 1L; 10 | 11 | public BeanDescException(InvocationTargetException e) { 12 | super(e); 13 | } 14 | 15 | public BeanDescException(IllegalAccessException e) { 16 | super(e); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/BreakIterationException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | /** 4 | * Exception thrown in the {@link com.miragesql.miragesql.IterationCallback} to discontinue iteration search. 5 | *

6 | * 7 | * Note: to don't use this exception for other purposes. 8 | * 9 | * @author Naoki Takezoe 10 | */ 11 | public class BreakIterationException extends RuntimeException { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/ConfigurationException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | /** 4 | * Exception thrown when the Mirage-SQL {@link com.miragesql.miragesql.session.Session} is not properly configured. 5 | */ 6 | public class ConfigurationException extends RuntimeException { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | public ConfigurationException(String message, Throwable cause) { 11 | super(message, cause); 12 | } 13 | 14 | public ConfigurationException(String message) { 15 | super(message); 16 | } 17 | 18 | public ConfigurationException(Throwable cause) { 19 | super(cause); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/IORuntimeException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | import java.io.IOException; 4 | 5 | 6 | @SuppressWarnings("serial") 7 | public class IORuntimeException extends RuntimeException { 8 | 9 | public IORuntimeException(String message, IOException cause) { 10 | super(message, cause); 11 | } 12 | 13 | public IORuntimeException(IOException cause) { 14 | super(cause); 15 | } 16 | 17 | @Override 18 | public IOException getCause() { 19 | return (IOException) super.getCause(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/OgnlRuntimeException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | public class OgnlRuntimeException extends RuntimeException { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public OgnlRuntimeException(Throwable cause, String path, int lineNumber) { 8 | super("OGNL error in path: " + path + ", line:" + lineNumber, cause); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/SQLRuntimeException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | import java.sql.SQLException; 4 | 5 | public class SQLRuntimeException extends RuntimeException { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | public SQLRuntimeException(String message, SQLException cause) { 10 | super(message, cause); 11 | } 12 | 13 | public SQLRuntimeException(SQLException cause) { 14 | super(cause); 15 | } 16 | 17 | @Override 18 | public SQLException getCause() { 19 | return (SQLException) super.getCause(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/SessionException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | public class SessionException extends RuntimeException { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public SessionException(String message, Throwable cause) { 8 | super(message, cause); 9 | } 10 | 11 | public SessionException(String message) { 12 | super(message); 13 | } 14 | 15 | public SessionException(Throwable cause) { 16 | super(cause); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/TwoWaySQLException.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.exception; 2 | 3 | public class TwoWaySQLException extends RuntimeException { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public TwoWaySQLException(String message, Throwable cause) { 8 | super(message, cause); 9 | } 10 | 11 | public TwoWaySQLException(String message) { 12 | super(message); 13 | } 14 | 15 | public TwoWaySQLException(Throwable cause) { 16 | super(cause); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/exception/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides a package of Runtime Exceptions. 3 | */ 4 | package com.miragesql.miragesql.exception; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/filter/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides Servlet Filter functionality, useful in web applications. 3 | */ 4 | package com.miragesql.miragesql.filter; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/naming/DefaultNameConverter.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.naming; 2 | 3 | /** 4 | * The default implementation of {@link NameConverter}. 5 | *

6 | * This implementation uses camel case for entity / property names, and underscore for table / column names. 7 | * 8 | * @author Naoki Takezoe 9 | */ 10 | public class DefaultNameConverter implements NameConverter { 11 | 12 | // @Override 13 | /**{@inheritDoc}*/ 14 | public String columnToProperty(String columnName) { 15 | StringBuilder sb = new StringBuilder(); 16 | boolean uppercase = false; 17 | 18 | for(int i=0;i= 0){ 40 | entityName = entityName.substring(index + 1); 41 | } 42 | index = entityName.lastIndexOf('$'); 43 | if(index >= 0){ 44 | entityName = entityName.substring(index + 1); 45 | } 46 | 47 | StringBuilder sb = new StringBuilder(); 48 | 49 | for(int i=0;i= 'A' && c <= 'Z' && sb.length() > 0){ 52 | sb.append('_'); 53 | } 54 | sb.append(String.valueOf(c).toUpperCase()); 55 | } 56 | 57 | return sb.toString(); 58 | } 59 | 60 | // @Override 61 | /**{@inheritDoc}*/ 62 | public String propertyToColumn(String propertyName) { 63 | StringBuilder sb = new StringBuilder(); 64 | 65 | for(int i=0;i= 'A' && c <= 'Z'){ 68 | sb.append('_'); 69 | } 70 | sb.append(String.valueOf(c).toUpperCase()); 71 | } 72 | 73 | return sb.toString(); 74 | 75 | } 76 | 77 | // @Override 78 | // public String tableToEntity(String tableName) { 79 | // return null; 80 | // } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/naming/NameConverter.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.naming; 2 | 3 | import com.miragesql.miragesql.SqlManager; 4 | 5 | /** 6 | * The interface of the converter which converts table / column names and entity / property names. 7 | * 8 | * @author Naoki Takezoe 9 | * @see SqlManager#setNameConverter(NameConverter) 10 | */ 11 | public interface NameConverter { 12 | 13 | /** 14 | * Converts the property name to the column name. 15 | * 16 | * @param propertyName the property name 17 | * @return the column name 18 | */ 19 | String propertyToColumn(String propertyName); 20 | 21 | /** 22 | * Converts the column name to the property name. 23 | * 24 | * @param columnName the column name 25 | * @return the property name 26 | */ 27 | String columnToProperty(String columnName); 28 | 29 | /** 30 | * Converts the entity name to the table name. 31 | * 32 | * @param entityName the entity name 33 | * @return the table name 34 | */ 35 | String entityToTable(String entityName); 36 | 37 | // public String tableToEntity(String tableName); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/naming/RailsLikeNameConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Daisuke Miyamoto. 3 | * Created on 2011/10/21 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 14 | * either express or implied. See the License for the specific language 15 | * governing permissions and limitations under the License. 16 | */ 17 | package com.miragesql.miragesql.naming; 18 | 19 | /** 20 | * An implementation of {@link NameConverter} which provides naming convention like Ruby on Rails, i.e. 21 | * it also pluralizes table names using the {@link Inflection} utility for English names only. 22 | * 23 | * @since 1.1.4 24 | * @author daisuke 25 | */ 26 | public class RailsLikeNameConverter extends DefaultNameConverter { 27 | 28 | /**{@inheritDoc}*/ 29 | @Override 30 | public String entityToTable(String entityName) { 31 | String pluralized = Inflection.pluralize(entityName); 32 | return super.entityToTable(pluralized); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/naming/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides naming converter functionality, i.e. conversion between table and entities, columns and properties. 3 | */ 4 | package com.miragesql.miragesql.naming; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the main Mirage-SQL package. 3 | */ 4 | package com.miragesql.miragesql; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/AbstractNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * Abstract {@link Node} implementation. 23 | * 24 | * @author higa 25 | */ 26 | public abstract class AbstractNode implements Node { 27 | 28 | /** List with child nodes.*/ 29 | List children = new ArrayList<>(); 30 | 31 | public AbstractNode() { 32 | } 33 | 34 | // @Override 35 | public int getChildSize() { 36 | return children.size(); 37 | } 38 | 39 | // @Override 40 | public Node getChild(int index) { 41 | return children.get(index); 42 | } 43 | 44 | // @Override 45 | public void addChild(Node node) { 46 | children.add(node); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/AddWhereIfNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | import java.util.regex.Matcher; 19 | import java.util.regex.Pattern; 20 | 21 | /** 22 | * {@link Node} representing the WHERE clause of an SQL. 23 | * 24 | * @author higa 25 | */ 26 | public class AddWhereIfNode extends ContainerNode { 27 | 28 | Pattern pat = Pattern.compile("\\s*(order\\sby)|$)"); 29 | 30 | public AddWhereIfNode() { 31 | } 32 | 33 | @Override 34 | public void accept(SqlContext ctx) { 35 | SqlContext childCtx = new SqlContextImpl(ctx); 36 | super.accept(childCtx); 37 | if (childCtx.isEnabled()) { 38 | String sql = childCtx.getSql(); 39 | Matcher m = pat.matcher(sql); 40 | if (!m.lookingAt()) { 41 | sql = " WHERE " + sql; 42 | } 43 | ctx.addSql(sql, childCtx.getBindVariables(), childCtx 44 | .getBindVariableTypes()); 45 | } 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return "AddWhereIfNode [children=" + children + "]"; 51 | } 52 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/BeginNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * {@link Node} corresponding to the BEGIN comment. 20 | * 21 | * @author higa 22 | */ 23 | public class BeginNode extends ContainerNode { 24 | 25 | public BeginNode() { 26 | } 27 | 28 | @Override 29 | public void accept(SqlContext ctx) { 30 | SqlContext childCtx = new SqlContextImpl(ctx); 31 | super.accept(childCtx); 32 | if (childCtx.isEnabled()) { 33 | ctx.addSql(childCtx.getSql(), childCtx.getBindVariables(), childCtx 34 | .getBindVariableTypes()); 35 | } 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "BeginNode [children=" + children + "]"; 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/ContainerNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * Base {@link Node} that can contain other nodes. 20 | * 21 | * @author higa 22 | */ 23 | public class ContainerNode extends AbstractNode { 24 | 25 | public ContainerNode() { 26 | } 27 | 28 | // @Override 29 | public void accept(SqlContext ctx) { 30 | for (int i = 0; i < getChildSize(); ++i) { 31 | getChild(i).accept(ctx); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/ElseNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * {@link Node} representing an ELSE comment. 20 | * 21 | * @author higa 22 | */ 23 | public class ElseNode extends ContainerNode { 24 | 25 | public ElseNode() { 26 | } 27 | 28 | @Override 29 | public void accept(SqlContext ctx) { 30 | super.accept(ctx); 31 | ctx.setEnabled(true); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "ElseNode [children=" + children + "]"; 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * Node Interface representing the individual elements that make up an SQL 20 | * 21 | * @author higa 22 | */ 23 | public interface Node { 24 | 25 | /** 26 | * @return the number of children elements. 27 | */ 28 | int getChildSize(); 29 | 30 | /** 31 | * Returns the child Node at the specified index. 32 | * 33 | * @param index the index 34 | * 35 | * @return the child node at the specified index 36 | */ 37 | Node getChild(int index); 38 | 39 | /** 40 | * Adds a child Node. 41 | * 42 | * @param node the node to add as child 43 | */ 44 | void addChild(Node node); 45 | 46 | /** 47 | * CommandContextをこのNodeに、 適用します。 48 | * 49 | * @param ctx the SQL context 50 | */ 51 | void accept(SqlContext ctx); 52 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/PrefixSqlNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * Node for AND, OR and ',' prefix. 20 | * 21 | * @author higa 22 | */ 23 | public class PrefixSqlNode extends AbstractNode { 24 | 25 | private String prefix; 26 | 27 | private String sql; 28 | 29 | /** 30 | * Creates a PrefixSqlNode 31 | * 32 | * @param prefix the prefix 33 | * @param sql the SQL 34 | */ 35 | public PrefixSqlNode(String prefix, String sql) { 36 | this.prefix = prefix; 37 | this.sql = sql; 38 | } 39 | 40 | /** 41 | * @return the prefix 42 | */ 43 | public String getPrefix() { 44 | return prefix; 45 | } 46 | 47 | /** 48 | * @return the SQL 49 | */ 50 | public String getSql() { 51 | return sql; 52 | } 53 | 54 | // @Override 55 | public void accept(SqlContext ctx) { 56 | if (ctx.isEnabled()) { 57 | ctx.addSql(prefix); 58 | } 59 | ctx.addSql(sql); 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "PrefixSqlNode [prefix=" + prefix + ", sql=" + sql + ", children=" + children + "]"; 65 | } 66 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/SqlArgWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * Wrapper interface for SQL arguments. 20 | * 21 | * @author taedium 22 | */ 23 | public interface SqlArgWrapper { 24 | 25 | /** 26 | * @return the wrapped value. 27 | */ 28 | Object getValue(); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/SqlContextPropertyAccessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | import java.util.Map; 19 | 20 | import ognl.ObjectPropertyAccessor; 21 | import ognl.OgnlException; 22 | 23 | /** 24 | * Helper based on OGNL to access properties from the {@link SqlContext} 25 | * 26 | * @author higa 27 | */ 28 | public class SqlContextPropertyAccessor extends ObjectPropertyAccessor { 29 | 30 | private static final String HAS_PREFIX = "has_"; 31 | 32 | @Override 33 | @SuppressWarnings("rawtypes") 34 | public Object getProperty(Map cx, Object target, Object name) 35 | throws OgnlException { 36 | 37 | SqlContext ctx = (SqlContext) target; 38 | String argName = name.toString(); 39 | if (argName.startsWith(HAS_PREFIX)) { 40 | return Boolean.valueOf(ctx.hasArg(argName.substring(HAS_PREFIX 41 | .length()))); 42 | } 43 | Object arg = ctx.getArg(argName); 44 | if (arg instanceof SqlArgWrapper) { 45 | SqlArgWrapper wrapper = (SqlArgWrapper) arg; 46 | return wrapper.getValue(); 47 | } 48 | return arg; 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/SqlNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | 19 | /** 20 | * {@link Node} holding the SQL string itself. 21 | * 22 | * @author higa 23 | */ 24 | public class SqlNode extends AbstractNode { 25 | 26 | private String sql; 27 | 28 | public SqlNode(String sql) { 29 | this.sql = sql; 30 | } 31 | 32 | /** 33 | * @return the sql string 34 | */ 35 | public String getSql() { 36 | return sql; 37 | } 38 | 39 | public void accept(SqlContext ctx) { 40 | ctx.addSql(sql); 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "SqlNode [sql=" + sql + ", children=" + children + "]"; 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/SqlParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * {@link SqlParser} parses SQL and construct tree of {@link Node}. 20 | * 21 | * @author higa 22 | */ 23 | public interface SqlParser { 24 | 25 | /** 26 | * Parse SQL and return {@link Node} tree. 27 | * 28 | * @return root {@link Node} 29 | */ 30 | Node parse(); 31 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/SqlTokenizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2010 the Seasar Foundation and the Others. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | * either express or implied. See the License for the specific language 14 | * governing permissions and limitations under the License. 15 | */ 16 | package com.miragesql.miragesql.parser; 17 | 18 | /** 19 | * The SQL Tokenizer Interface to decompose a SQL into tokens. 20 | * 21 | * @author higa 22 | */ 23 | public interface SqlTokenizer { 24 | 25 | /** 26 | * @return a token 27 | */ 28 | String getToken(); 29 | 30 | /** 31 | * @return the SQL String 32 | */ 33 | String getSql(); 34 | 35 | /** 36 | * @return the SQL String until the actual position of the Tokenizer. 37 | */ 38 | String getBefore(); 39 | 40 | /** 41 | * @return the SQL String after the current position of the Tokenizer. 42 | */ 43 | String getAfter(); 44 | 45 | /** 46 | * @return the position that is currently being analyzed ty the Tokenizer. 47 | */ 48 | int getPosition(); 49 | 50 | /** 51 | * @return the current token type. 52 | */ 53 | TokenType getTokenType(); 54 | 55 | /** 56 | * @return the next token type. 57 | */ 58 | TokenType getNextTokenType(); 59 | 60 | /** 61 | * @return advanced the Tokenizer to the next token. 62 | */ 63 | TokenType next(); 64 | 65 | /** 66 | * Skips a token. 67 | * 68 | * @return the token being skipped 69 | */ 70 | String skipToken(); 71 | 72 | /** 73 | * Skips the whitespace. 74 | * 75 | * @return the whitespace being skipped 76 | */ 77 | String skipWhitespace(); 78 | 79 | enum TokenType { 80 | SQL, 81 | COMMENT, 82 | ELSE, 83 | BIND_VARIABLE, 84 | EOF 85 | } 86 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/parser/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Mirage-SQL Parser. This parser was manually written, so there's no grammar documenting it. 3 | */ 4 | package com.miragesql.miragesql.parser; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/ConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.provider; 2 | 3 | import java.sql.Connection; 4 | 5 | import com.miragesql.miragesql.SqlManager; 6 | import com.miragesql.miragesql.exception.SQLRuntimeException; 7 | 8 | /** 9 | * The interface of the connection provider. 10 | *

11 | * Mirage-SQL uses this interface to get the connection to the database. 12 | * 13 | * @author Naoki Takezoe 14 | * @see SqlManager#setConnectionProvider(ConnectionProvider) 15 | */ 16 | public interface ConnectionProvider { 17 | 18 | /** 19 | * Returns the connection to the database. 20 | * 21 | * @return the connection 22 | * @throws SQLRuntimeException Failed to get the connection 23 | */ 24 | Connection getConnection(); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/DataSourceConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.provider; 2 | 3 | import java.sql.Connection; 4 | import java.sql.SQLException; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import javax.sql.DataSource; 9 | 10 | import com.miragesql.miragesql.exception.SQLRuntimeException; 11 | import com.miragesql.miragesql.util.JdbcUtil; 12 | 13 | /** 14 | * {@link ConnectionProvider} implementation which gets a connection from javax.sql.DataSource. 15 | * 16 | * @author Naoki Takezoe 17 | */ 18 | public class DataSourceConnectionProvider implements ConnectionProvider { 19 | 20 | private static final Logger logger = LoggerFactory.getLogger(DataSourceConnectionProvider.class); 21 | 22 | private DataSource dataSource; 23 | 24 | private ThreadLocal threadLocal = new ThreadLocal<>(); 25 | 26 | public void setDataSource(DataSource dataSource){ 27 | this.dataSource = dataSource; 28 | } 29 | 30 | public void releaseConnection(){ 31 | Connection conn = threadLocal.get(); 32 | if(conn != null){ 33 | JdbcUtil.close(conn); 34 | threadLocal.remove(); 35 | 36 | logger.info("Connection is released."); 37 | 38 | } else { 39 | logger.info("Connection is not used."); 40 | } 41 | } 42 | 43 | // @Override 44 | /**{@inheritDoc}*/ 45 | public Connection getConnection() { 46 | try { 47 | Connection conn = threadLocal.get(); 48 | 49 | if(conn == null){ 50 | conn = dataSource.getConnection(); 51 | threadLocal.set(conn); 52 | logger.info("Get Connection from DataSource."); 53 | } 54 | 55 | return conn; 56 | 57 | } catch(SQLException ex){ 58 | throw new SQLRuntimeException(ex); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/DefaultConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.provider; 2 | 3 | import java.sql.Connection; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import com.miragesql.miragesql.util.JdbcUtil; 8 | 9 | /** 10 | * The simple implementation of {@link ConnectionProvider}. 11 | * 12 | * @author Naoki Takezoe 13 | */ 14 | public class DefaultConnectionProvider implements ConnectionProvider { 15 | 16 | private static final Logger logger = LoggerFactory.getLogger(DefaultConnectionProvider.class); 17 | 18 | private ThreadLocal threadLocal = new ThreadLocal<>(); 19 | 20 | public void setConnection(Connection conn){ 21 | threadLocal.set(conn); 22 | } 23 | 24 | public void releaseConnection(){ 25 | Connection conn = threadLocal.get(); 26 | if(conn != null){ 27 | JdbcUtil.close(conn); 28 | threadLocal.remove(); 29 | 30 | logger.info("Connection is released."); 31 | 32 | } else { 33 | logger.info("Connection is not used."); 34 | } 35 | } 36 | 37 | // @Override 38 | /**{@inheritDoc}*/ 39 | public Connection getConnection() { 40 | return threadLocal.get(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/JNDIDataSourceConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.provider; 2 | 3 | import javax.naming.Context; 4 | import javax.naming.InitialContext; 5 | import javax.naming.NamingException; 6 | import javax.sql.DataSource; 7 | 8 | /** 9 | * {@link ConnectionProvider} implementation which gets a connection from javax.sql.DataSource 10 | * which is obtained from JNDI. 11 | * 12 | * @author Naoki Takezoe 13 | */ 14 | public class JNDIDataSourceConnectionProvider extends DataSourceConnectionProvider { 15 | 16 | public JNDIDataSourceConnectionProvider(String jndiName) throws NamingException { 17 | Context initContext = new InitialContext(); 18 | Context envContext = (Context) initContext.lookup("java:/comp/env"); 19 | DataSource dataSource = (DataSource) envContext.lookup(jndiName); 20 | setDataSource(dataSource); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/JNDIXADataSourceConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.provider; 2 | 3 | import javax.naming.Context; 4 | import javax.naming.InitialContext; 5 | import javax.naming.NamingException; 6 | import javax.sql.XADataSource; 7 | 8 | /** 9 | * {@link ConnectionProvider} implementation which gets a connection from javax.sql.XADataSource 10 | * which is obtained from JNDI. 11 | * 12 | * @author Naoki Takezoe 13 | */ 14 | public class JNDIXADataSourceConnectionProvider extends XADataSourceConnectionProvider { 15 | 16 | public JNDIXADataSourceConnectionProvider(String jndiName) throws NamingException { 17 | Context initContext = new InitialContext(); 18 | Context envContext = (Context) initContext.lookup("java:/comp/env"); 19 | XADataSource xaDataSource = (XADataSource) envContext.lookup(jndiName); 20 | setDataSource(xaDataSource); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/XADataSourceConnectionProvider.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.provider; 2 | 3 | import java.sql.Connection; 4 | import java.sql.SQLException; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import javax.sql.XADataSource; 9 | 10 | import com.miragesql.miragesql.exception.SQLRuntimeException; 11 | import com.miragesql.miragesql.util.JdbcUtil; 12 | 13 | /** 14 | * {@link ConnectionProvider} implementation which gets a connection from javax.sql.XADataSource. 15 | * 16 | * @author Naoki Takezoe 17 | */ 18 | public class XADataSourceConnectionProvider implements ConnectionProvider { 19 | 20 | private static final Logger logger = LoggerFactory.getLogger(XADataSourceConnectionProvider.class); 21 | 22 | private XADataSource xaDataSource; 23 | 24 | private ThreadLocal threadLocal = new ThreadLocal<>(); 25 | 26 | public void setDataSource(XADataSource xaDataSource){ 27 | this.xaDataSource = xaDataSource; 28 | } 29 | 30 | public void releaseConnection(){ 31 | Connection conn = threadLocal.get(); 32 | if(conn != null){ 33 | JdbcUtil.close(conn); 34 | threadLocal.remove(); 35 | 36 | logger.info("Connection is released."); 37 | 38 | } else { 39 | logger.info("Connection is not used."); 40 | } 41 | } 42 | 43 | // @Override 44 | /**{@inheritDoc}*/ 45 | public Connection getConnection() { 46 | try { 47 | Connection conn = threadLocal.get(); 48 | 49 | if(conn == null){ 50 | conn = xaDataSource.getXAConnection().getConnection(); 51 | threadLocal.set(conn); 52 | logger.info("Get Connection from DataSource."); 53 | } 54 | 55 | return conn; 56 | 57 | } catch(SQLException ex){ 58 | throw new SQLRuntimeException(ex); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/provider/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Connection Providers. 3 | */ 4 | package com.miragesql.miragesql.provider; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/session/DialectAutoSelector.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.session; 2 | 3 | import com.miragesql.miragesql.dialect.*; 4 | 5 | public class DialectAutoSelector { 6 | 7 | /** 8 | * Selects the Database Dialect based on the JDBC connection URL. 9 | * 10 | * @param url the JDBC Connection URL 11 | * @return the dialect that maps to a specific JDBC URL. 12 | */ 13 | public static Dialect getDialect(String url){ 14 | if(url.startsWith("jdbc:mysql:")){ 15 | return new MySQLDialect(); 16 | } else if(url.startsWith("jdbc:postgresql:")){ 17 | return new PostgreSQLDialect(); 18 | } else if(url.startsWith("jdbc:oracle:")){ 19 | return new OracleDialect(); 20 | } else if(url.startsWith("jdbc:hsqldb:")){ 21 | return new HyperSQLDialect(); 22 | } else if(url.startsWith("jdbc:h2:")){ 23 | return new H2Dialect(); 24 | } else if(url.startsWith("jdbc:derby:")){ 25 | return new DerbyDialect(); 26 | } else if(url.startsWith("jdbc:sqlite:")){ 27 | return new SQLiteDialect(); 28 | } else if(url.startsWith("jdbc:sqlserver:") || url.startsWith("jdbc:jtds:sqlserver:")){ 29 | return new SQLServerDialect(); 30 | } else if(url.startsWith("jdbc:db2:") || url.startsWith("jdbc:db2j:") 31 | || url.startsWith("jdbc:db2:ids") || url.startsWith("jdbc:informix-sqli:") 32 | || url.startsWith("jdbc:as400:")){ 33 | return new DB2Dialect(); 34 | } 35 | return new StandardDialect(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/session/Session.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.session; 2 | 3 | import com.miragesql.miragesql.SqlManager; 4 | import com.miragesql.miragesql.exception.SessionException; 5 | import com.miragesql.miragesql.filter.OpenSessionInViewFilter; 6 | 7 | /** 8 | * The entry point of Mirage-SQL in stand-alone use. 9 | *

10 | * You can control transactions and access to {@link SqlManager} through this interface. 11 | *

12 | * In addition, you can manage transactions automatically using this interface with {@link OpenSessionInViewFilter}. 13 | * OpenSessionInViewFilter begin and commit transaction per request in web applications. 14 | * 15 | * It rollbacks it transaction if it catches an exception. So you can focus on operation of SqlManager. 16 | * 17 | * @author Naoki Takezoe 18 | */ 19 | public interface Session { 20 | 21 | /** 22 | * Begins the transaction. 23 | * 24 | * @throws SessionException if something goes wrong. 25 | */ 26 | void begin(); 27 | 28 | /** 29 | * Commits the transaction. 30 | * 31 | * @throws SessionException if something goes wrong. 32 | */ 33 | void commit(); 34 | 35 | /** 36 | * Rollbacks the transaction. 37 | * 38 | * @throws SessionException if something goes wrong. 39 | */ 40 | void rollback(); 41 | 42 | /** 43 | * Releases this transaction. 44 | * 45 | * @throws SessionException if something goes wrong. 46 | */ 47 | void release(); 48 | 49 | /** 50 | * Marks the current transaction as rollback-only. 51 | */ 52 | void setRollbackOnly(); 53 | 54 | /** 55 | * Returns whether the current transaction has been marked as rollback-only or not marked. 56 | * 57 | * @return If marked true; otherwise false 58 | */ 59 | boolean isRollbackOnly(); 60 | 61 | /** 62 | * Returns the instance of SqlManager. 63 | * 64 | * @return the instance of SqlManager 65 | * @throws SessionException if something goes wrong. 66 | */ 67 | SqlManager getSqlManager(); 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/session/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides the Session management classes in Mirage-SQL. 3 | */ 4 | package com.miragesql.miragesql.session; -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/AbstractResultSetValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.util.List; 8 | 9 | import com.miragesql.miragesql.bean.PropertyDesc; 10 | 11 | public class AbstractResultSetValueType implements ValueType { 12 | 13 | private int sqlType; 14 | 15 | public AbstractResultSetValueType(int sqlType) { 16 | this.sqlType = sqlType; 17 | } 18 | 19 | // @Override 20 | public Object getDefaultValue() { 21 | return null; 22 | } 23 | 24 | // @Override 25 | public Object get(Class type, ResultSet rs, int index) throws SQLException { 26 | throw new UnsupportedOperationException("not supported"); 27 | } 28 | 29 | // @Override 30 | public Object get(Class type, ResultSet rs, String columnName) throws SQLException { 31 | throw new UnsupportedOperationException("not supported"); 32 | } 33 | 34 | // @Override 35 | public Object get(Class type, CallableStatement cs, int index) throws SQLException { 36 | return cs.getObject(index); 37 | } 38 | 39 | // @Override 40 | public Object get(Class type, CallableStatement cs, String parameterName) throws SQLException { 41 | return cs.getObject(parameterName); 42 | } 43 | 44 | // @Override 45 | public boolean isSupport(Class type, PropertyDesc propertyDesc) { 46 | if (List.class.isAssignableFrom(type)){ 47 | return true; 48 | } 49 | return false; 50 | } 51 | 52 | // @Override 53 | public void registerOutParameter(Class type, CallableStatement cs, int index) throws SQLException { 54 | cs.registerOutParameter(index, sqlType); 55 | } 56 | 57 | // @Override 58 | public void registerOutParameter(Class type, CallableStatement cs, String parameterName) throws SQLException { 59 | cs.registerOutParameter(parameterName, sqlType); 60 | } 61 | 62 | // @Override 63 | public void set(Class type, PreparedStatement stmt, Object value, int index) throws SQLException { 64 | throw new UnsupportedOperationException("not supported"); 65 | } 66 | 67 | public Class getJavaType(int sqlType) { 68 | return null; 69 | } 70 | } -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/BigDecimalValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.math.BigDecimal; 4 | import java.sql.CallableStatement; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | 9 | public class BigDecimalValueType extends AbstractValueType { 10 | 11 | 12 | public BigDecimalValueType() { 13 | super(BigDecimal.class); 14 | } 15 | 16 | public BigDecimal get(Class type, ResultSet rs, int columnIndex) throws SQLException { 17 | return rs.getBigDecimal(columnIndex); 18 | } 19 | 20 | public BigDecimal get(Class type, ResultSet rs, String columnName) throws SQLException { 21 | return rs.getBigDecimal(columnName); 22 | } 23 | 24 | public void set(Class type, PreparedStatement stmt, BigDecimal value, 25 | int index) throws SQLException { 26 | if (value == null){ 27 | setNull(type, stmt, index); 28 | } else { 29 | stmt.setBigDecimal(index, value); 30 | } 31 | } 32 | 33 | public BigDecimal get(Class type, CallableStatement cs, int index) throws SQLException { 34 | return cs.getBigDecimal(index); 35 | } 36 | 37 | public BigDecimal get(Class type, CallableStatement cs, String parameterName) throws SQLException { 38 | return cs.getBigDecimal(parameterName); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/BooleanPrimitiveValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class BooleanPrimitiveValueType extends AbstractValueType { 9 | 10 | 11 | public BooleanPrimitiveValueType() { 12 | super(Boolean.TYPE); 13 | } 14 | 15 | public Boolean get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | return rs.getBoolean(columnIndex); 17 | } 18 | 19 | public Boolean get(Class type, ResultSet rs, String columnName) throws SQLException { 20 | return rs.getBoolean(columnName); 21 | } 22 | 23 | public void set(Class type, PreparedStatement stmt, Boolean value, 24 | int index) throws SQLException { 25 | if (value == null){ 26 | setNull(type, stmt, index); 27 | } else { 28 | stmt.setBoolean(index, value); 29 | } 30 | } 31 | 32 | public Boolean get(Class type, CallableStatement cs, int index) throws SQLException { 33 | boolean value = cs.getBoolean(index); 34 | 35 | if (cs.wasNull()) { 36 | return null; 37 | } 38 | 39 | return value; 40 | } 41 | 42 | public Boolean get(Class type, CallableStatement cs, String parameterName) throws SQLException { 43 | boolean value = cs.getBoolean(parameterName); 44 | 45 | if (cs.wasNull()) { 46 | return null; 47 | } 48 | 49 | return value; 50 | } 51 | 52 | @Override 53 | public Boolean getDefaultValue(){ 54 | return false; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/BooleanValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class BooleanValueType extends AbstractValueType { 9 | 10 | 11 | public BooleanValueType() { 12 | super(Boolean.class); 13 | } 14 | 15 | public Boolean get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | if(rs.getObject(columnIndex) == null){ 17 | return null; 18 | } 19 | return rs.getBoolean(columnIndex); 20 | } 21 | 22 | public Boolean get(Class type, ResultSet rs, String columnName) throws SQLException { 23 | if(rs.getObject(columnName) == null){ 24 | return null; 25 | } 26 | return rs.getBoolean(columnName); 27 | } 28 | 29 | public void set(Class type, PreparedStatement stmt, Boolean value, 30 | int index) throws SQLException { 31 | if (value == null){ 32 | setNull(type, stmt, index); 33 | } else { 34 | stmt.setBoolean(index, value); 35 | } 36 | } 37 | 38 | public Boolean get(Class type, CallableStatement cs, int index) throws SQLException { 39 | boolean value = cs.getBoolean(index); 40 | 41 | if (cs.wasNull()) { 42 | return null; 43 | } 44 | 45 | return value; 46 | } 47 | 48 | public Boolean get(Class type, CallableStatement cs, String parameterName) throws SQLException { 49 | boolean value = cs.getBoolean(parameterName); 50 | 51 | if (cs.wasNull()) { 52 | return null; 53 | } 54 | 55 | return value; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/ByteArrayValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.sql.Blob; 5 | import java.sql.CallableStatement; 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.SQLException; 9 | 10 | import com.miragesql.miragesql.bean.PropertyDesc; 11 | import com.miragesql.miragesql.util.IOUtil; 12 | 13 | public class ByteArrayValueType extends AbstractValueType { 14 | 15 | public static void main(String[] args) { 16 | Class clazz = byte[].class; 17 | System.out.println(clazz.isArray()); 18 | System.out.println(clazz.getComponentType() == Byte.TYPE); 19 | } 20 | 21 | public ByteArrayValueType() { 22 | super(byte[].class); 23 | } 24 | 25 | @Override 26 | public boolean isSupport(Class type, PropertyDesc propertyDesc) { 27 | return type.isArray() && type.getComponentType() == Byte.TYPE; 28 | } 29 | 30 | public byte[] get(Class type, ResultSet rs, int columnIndex) throws SQLException { 31 | if(rs.getObject(columnIndex) == null){ 32 | return null; 33 | } 34 | return IOUtil.readStream(rs.getBinaryStream(columnIndex)); 35 | } 36 | 37 | public byte[] get(Class type, ResultSet rs, String columnName) throws SQLException { 38 | // if(type.isArray() && type.getComponentType() == Byte.TYPE){ 39 | if(rs.getObject(columnName) == null){ 40 | return null; 41 | } 42 | return IOUtil.readStream(rs.getBinaryStream(columnName)); 43 | } 44 | 45 | public void set(Class type, PreparedStatement stmt, byte[] value, 46 | int index) throws SQLException { 47 | if (value == null){ 48 | setNull(type, stmt, index); 49 | } else { 50 | stmt.setBinaryStream(index, new ByteArrayInputStream(value), value.length); 51 | } 52 | } 53 | 54 | public byte[] get(Class type, CallableStatement cs, int index) throws SQLException { 55 | // TODO 56 | Blob blob = cs.getBlob(index); 57 | return IOUtil.readStream(blob.getBinaryStream()); 58 | } 59 | 60 | public byte[] get(Class type, CallableStatement cs, String parameterName) throws SQLException { 61 | Blob blob = cs.getBlob(parameterName); 62 | return IOUtil.readStream(blob.getBinaryStream()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/DoublePrimitiveValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class DoublePrimitiveValueType extends AbstractValueType { 9 | 10 | 11 | public DoublePrimitiveValueType() { 12 | super(Double.TYPE); 13 | } 14 | 15 | public Double get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | return rs.getDouble(columnIndex); 17 | } 18 | 19 | public Double get(Class type, ResultSet rs, String columnName) throws SQLException { 20 | return rs.getDouble(columnName); 21 | } 22 | 23 | public void set(Class type, PreparedStatement stmt, Double value, 24 | int index) throws SQLException { 25 | if (value == null){ 26 | setNull(type, stmt, index); 27 | } else { 28 | stmt.setDouble(index, (Double) value); 29 | } 30 | } 31 | 32 | public Double get(Class type, CallableStatement cs, int index) throws SQLException { 33 | double value = cs.getDouble(index); 34 | 35 | if (cs.wasNull()) { 36 | return null; 37 | } 38 | 39 | return value; 40 | } 41 | 42 | public Double get(Class type, CallableStatement cs, String parameterName) throws SQLException { 43 | double value = cs.getDouble(parameterName); 44 | 45 | if (cs.wasNull()) { 46 | return null; 47 | } 48 | 49 | return value; 50 | } 51 | 52 | @Override 53 | public Double getDefaultValue(){ 54 | return 0d; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/DoubleValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class DoubleValueType extends AbstractValueType { 9 | 10 | 11 | public DoubleValueType() { 12 | super(Double.class); 13 | } 14 | 15 | public Double get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | if(rs.getObject(columnIndex) == null){ 17 | return null; 18 | } 19 | return rs.getDouble(columnIndex); 20 | } 21 | 22 | public Double get(Class type, ResultSet rs, String columnName) throws SQLException { 23 | if(rs.getObject(columnName) == null){ 24 | return null; 25 | } 26 | return rs.getDouble(columnName); 27 | } 28 | 29 | public void set(Class type, PreparedStatement stmt, Double value, 30 | int index) throws SQLException { 31 | if (value == null){ 32 | setNull(type, stmt, index); 33 | } else { 34 | stmt.setDouble(index, (Double) value); 35 | } 36 | } 37 | 38 | public Double get(Class type, CallableStatement cs, int index) throws SQLException { 39 | double value = cs.getDouble(index); 40 | 41 | if (cs.wasNull()) { 42 | return null; 43 | } 44 | 45 | return value; 46 | } 47 | 48 | public Double get(Class type, CallableStatement cs, String parameterName) throws SQLException { 49 | double value = cs.getDouble(parameterName); 50 | 51 | if (cs.wasNull()) { 52 | return null; 53 | } 54 | 55 | return value; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/FloatPrimitiveValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class FloatPrimitiveValueType extends AbstractValueType { 9 | 10 | 11 | public FloatPrimitiveValueType() { 12 | super(Float.TYPE); 13 | } 14 | 15 | public Float get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | return rs.getFloat(columnIndex); 17 | } 18 | 19 | public Float get(Class type, ResultSet rs, String columnName) throws SQLException { 20 | return rs.getFloat(columnName); 21 | } 22 | 23 | public void set(Class type, PreparedStatement stmt, Float value, 24 | int index) throws SQLException { 25 | if (value == null){ 26 | setNull(type, stmt, index); 27 | } else { 28 | stmt.setFloat(index, (Float) value); 29 | } 30 | } 31 | 32 | public Float get(Class type, CallableStatement cs, int index) throws SQLException { 33 | float value = cs.getFloat(index); 34 | 35 | if (cs.wasNull()) { 36 | return null; 37 | } 38 | 39 | return value; 40 | } 41 | 42 | public Float get(Class type, CallableStatement cs, String parameterName) throws SQLException { 43 | float value = cs.getFloat(parameterName); 44 | 45 | if (cs.wasNull()) { 46 | return null; 47 | } 48 | 49 | return value; 50 | } 51 | 52 | @Override 53 | public Float getDefaultValue(){ 54 | return 0f; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/FloatValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class FloatValueType extends AbstractValueType { 9 | 10 | 11 | public FloatValueType() { 12 | super(Float.class); 13 | } 14 | 15 | public Float get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | if(rs.getObject(columnIndex) == null){ 17 | return null; 18 | } 19 | return rs.getFloat(columnIndex); 20 | } 21 | 22 | public Float get(Class type, ResultSet rs, String columnName) throws SQLException { 23 | if(rs.getObject(columnName) == null){ 24 | return null; 25 | } 26 | return rs.getFloat(columnName); 27 | } 28 | 29 | public void set(Class type, PreparedStatement stmt, Float value, 30 | int index) throws SQLException { 31 | if (value == null){ 32 | setNull(type, stmt, index); 33 | } else { 34 | stmt.setFloat(index, (Float) value); 35 | } 36 | } 37 | 38 | public Float get(Class type, CallableStatement cs, int index) throws SQLException { 39 | float value = cs.getFloat(index); 40 | 41 | if (cs.wasNull()) { 42 | return null; 43 | } 44 | 45 | return value; 46 | } 47 | 48 | public Float get(Class type, CallableStatement cs, String parameterName) throws SQLException { 49 | float value = cs.getFloat(parameterName); 50 | 51 | if (cs.wasNull()) { 52 | return null; 53 | } 54 | 55 | return value; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/IntegerPrimitiveValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class IntegerPrimitiveValueType extends AbstractValueType { 9 | 10 | public IntegerPrimitiveValueType() { 11 | super(Integer.TYPE); 12 | } 13 | 14 | public Integer get(Class type, ResultSet rs, int columnIndex) throws SQLException { 15 | return rs.getInt(columnIndex); 16 | } 17 | 18 | public Integer get(Class type, ResultSet rs, String columnName) throws SQLException { 19 | return rs.getInt(columnName); 20 | } 21 | 22 | public void set(Class type, PreparedStatement stmt, Integer value, 23 | int index) throws SQLException { 24 | if (value == null){ 25 | setNull(type, stmt, index); 26 | } else { 27 | stmt.setInt(index, value); 28 | } 29 | } 30 | 31 | public Integer get(Class type, CallableStatement cs, int index) throws SQLException { 32 | int value = cs.getInt(index); 33 | 34 | if (cs.wasNull()) { 35 | return null; 36 | } 37 | 38 | return value; 39 | } 40 | 41 | public Integer get(Class type, CallableStatement cs, String parameterName) throws SQLException { 42 | int value = cs.getInt(parameterName); 43 | 44 | if (cs.wasNull()) { 45 | return null; 46 | } 47 | 48 | return value; 49 | } 50 | 51 | @Override 52 | public Integer getDefaultValue(){ 53 | return 0; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/IntegerValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class IntegerValueType extends AbstractValueType { 9 | 10 | public IntegerValueType() { 11 | super(Integer.class); 12 | } 13 | 14 | public Integer get(Class type, ResultSet rs, int columnIndex) throws SQLException { 15 | if(rs.getObject(columnIndex) == null){ 16 | return null; 17 | } 18 | return rs.getInt(columnIndex); 19 | } 20 | 21 | public Integer get(Class type, ResultSet rs, String columnName) throws SQLException { 22 | if(rs.getObject(columnName) == null){ 23 | return null; 24 | } 25 | return rs.getInt(columnName); 26 | } 27 | 28 | public void set(Class type, PreparedStatement stmt, Integer value, 29 | int index) throws SQLException { 30 | if (value == null){ 31 | setNull(type, stmt, index); 32 | } else { 33 | stmt.setInt(index, value); 34 | } 35 | } 36 | 37 | public Integer get(Class type, CallableStatement cs, int index) throws SQLException { 38 | int value = cs.getInt(index); 39 | 40 | if (cs.wasNull()) { 41 | return null; 42 | } 43 | 44 | return value; 45 | } 46 | 47 | public Integer get(Class type, CallableStatement cs, String parameterName) throws SQLException { 48 | int value = cs.getInt(parameterName); 49 | 50 | if (cs.wasNull()) { 51 | return null; 52 | } 53 | 54 | return value; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/LongPrimitiveValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class LongPrimitiveValueType extends AbstractValueType { 9 | 10 | 11 | public LongPrimitiveValueType() { 12 | super(Long.TYPE); 13 | } 14 | 15 | public Long get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | return rs.getLong(columnIndex); 17 | } 18 | 19 | public Long get(Class type, ResultSet rs, String columnName) throws SQLException { 20 | return rs.getLong(columnName); 21 | } 22 | 23 | public void set(Class type, PreparedStatement stmt, Long value, 24 | int index) throws SQLException { 25 | if (value == null){ 26 | setNull(type, stmt, index); 27 | return; 28 | } else { 29 | stmt.setLong(index, value); 30 | } 31 | } 32 | 33 | public Long get(Class type, CallableStatement cs, int index) throws SQLException { 34 | long value = cs.getLong(index); 35 | 36 | if (cs.wasNull()) { 37 | return null; 38 | } 39 | 40 | return value; 41 | } 42 | 43 | public Long get(Class type, CallableStatement cs, String parameterName) throws SQLException { 44 | long value = cs.getLong(parameterName); 45 | 46 | if (cs.wasNull()) { 47 | return null; 48 | } 49 | 50 | return value; 51 | } 52 | 53 | @Override 54 | public Long getDefaultValue(){ 55 | return 0L; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/LongValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class LongValueType extends AbstractValueType { 9 | 10 | public LongValueType() { 11 | super(Long.class); 12 | } 13 | 14 | public Long get(Class type, ResultSet rs, int columnIndex) throws SQLException { 15 | if(rs.getObject(columnIndex) == null){ 16 | return null; 17 | } 18 | return rs.getLong(columnIndex); 19 | } 20 | 21 | public Long get(Class type, ResultSet rs, String columnName) throws SQLException { 22 | if(rs.getObject(columnName) == null){ 23 | return null; 24 | } 25 | return rs.getLong(columnName); 26 | } 27 | 28 | public void set(Class type, PreparedStatement stmt, Long value, 29 | int index) throws SQLException { 30 | if (value == null){ 31 | setNull(type, stmt, index); 32 | } else { 33 | stmt.setLong(index, (Long) value); 34 | } 35 | } 36 | 37 | public Long get(Class type, CallableStatement cs, int index) throws SQLException { 38 | long value = cs.getLong(index); 39 | 40 | if (cs.wasNull()) { 41 | return null; 42 | } 43 | 44 | return value; 45 | } 46 | 47 | public Long get(Class type, CallableStatement cs, String parameterName) throws SQLException { 48 | long value = cs.getLong(parameterName); 49 | 50 | if (cs.wasNull()) { 51 | return null; 52 | } 53 | 54 | return value; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/ObjectValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class ObjectValueType extends AbstractValueType { 9 | 10 | public ObjectValueType() { 11 | super(Object.class); 12 | } 13 | 14 | public Object get(Class type, ResultSet rs, int columnIndex) throws SQLException { 15 | return rs.getObject(columnIndex); 16 | } 17 | 18 | public Object get(Class type, ResultSet rs, String columnName) throws SQLException { 19 | return rs.getObject(columnName); 20 | } 21 | 22 | public void set(Class type, PreparedStatement stmt, Object value, int index) throws SQLException { 23 | if (value == null) { 24 | setNull(type, stmt, index); 25 | } else { 26 | stmt.setObject(index, value); 27 | } 28 | } 29 | 30 | public Object get(Class type, CallableStatement cs, int index) throws SQLException { 31 | return cs.getObject(index); 32 | } 33 | 34 | public Object get(Class type, CallableStatement cs, String parameterName) throws SQLException { 35 | return cs.getObject(parameterName); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/OracleResultSetValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | 4 | public class OracleResultSetValueType extends AbstractResultSetValueType { 5 | 6 | public static final int CURSOR = -10; 7 | 8 | public OracleResultSetValueType() { 9 | super(CURSOR); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/PostgreResultSetValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.Types; 4 | 5 | public class PostgreResultSetValueType extends AbstractResultSetValueType { 6 | 7 | public PostgreResultSetValueType() { 8 | super(Types.OTHER); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/ShortPrimitiveValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class ShortPrimitiveValueType extends AbstractValueType { 9 | 10 | 11 | public ShortPrimitiveValueType() { 12 | super(Short.TYPE); 13 | } 14 | 15 | public Short get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | return rs.getShort(columnIndex); 17 | } 18 | 19 | public Short get(Class type, ResultSet rs, String columnName) throws SQLException { 20 | return rs.getShort(columnName); 21 | } 22 | 23 | public void set(Class type, PreparedStatement stmt, Short value, 24 | int index) throws SQLException { 25 | if (value == null){ 26 | setNull(type, stmt, index); 27 | return; 28 | } else { 29 | stmt.setShort(index, value); 30 | } 31 | } 32 | 33 | public Short get(Class type, CallableStatement cs, int index) throws SQLException { 34 | Short value = cs.getShort(index); 35 | 36 | if (value != null && cs.wasNull()) { 37 | value = null; 38 | } 39 | 40 | return value; 41 | } 42 | 43 | public Short get(Class type, CallableStatement cs, String parameterName) throws SQLException { 44 | Short value = cs.getShort(parameterName); 45 | 46 | if (value != null && cs.wasNull()) { 47 | value = null; 48 | } 49 | 50 | return value; 51 | } 52 | 53 | @Override 54 | public Short getDefaultValue(){ 55 | return 0; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/ShortValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class ShortValueType extends AbstractValueType { 9 | 10 | 11 | public ShortValueType() { 12 | super(Short.class); 13 | } 14 | 15 | public Short get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | if(rs.getObject(columnIndex) == null){ 17 | return null; 18 | } 19 | return rs.getShort(columnIndex); 20 | } 21 | 22 | public Short get(Class type, ResultSet rs, String columnName) throws SQLException { 23 | if(rs.getObject(columnName) == null){ 24 | return null; 25 | } 26 | return rs.getShort(columnName); 27 | } 28 | 29 | public void set(Class type, PreparedStatement stmt, Short value, 30 | int index) throws SQLException { 31 | if (value == null){ 32 | setNull(type, stmt, index); 33 | return; 34 | } else { 35 | stmt.setShort(index, value); 36 | } 37 | } 38 | 39 | public Short get(Class type, CallableStatement cs, int index) throws SQLException { 40 | short value = cs.getShort(index); 41 | 42 | if (cs.wasNull()) { 43 | return null; 44 | } 45 | 46 | return value; 47 | } 48 | 49 | public Short get(Class type, CallableStatement cs, String parameterName) throws SQLException { 50 | short value = cs.getShort(parameterName); 51 | 52 | if (cs.wasNull()) { 53 | return null; 54 | } 55 | 56 | return value; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/SqlDateValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class SqlDateValueType extends AbstractValueType { 9 | 10 | 11 | public SqlDateValueType() { 12 | super(java.sql.Date.class); 13 | } 14 | 15 | public java.sql.Date get(Class type, ResultSet rs, int columnIndex) throws SQLException { 16 | return rs.getDate(columnIndex); 17 | } 18 | 19 | public java.sql.Date get(Class type, ResultSet rs, String columnName) throws SQLException { 20 | return rs.getDate(columnName); 21 | } 22 | 23 | public void set(Class type, PreparedStatement stmt, java.sql.Date value, 24 | int index) throws SQLException { 25 | if (value == null){ 26 | setNull(type, stmt, index); 27 | } else { 28 | stmt.setDate(index, (java.sql.Date) value); 29 | } 30 | } 31 | 32 | public java.sql.Date get(Class type, CallableStatement cs, int index) throws SQLException { 33 | return cs.getDate(index); 34 | } 35 | 36 | public java.sql.Date get(Class type, CallableStatement cs, String parameterName) throws SQLException { 37 | return cs.getDate(parameterName); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/StringValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class StringValueType extends AbstractValueType { 9 | 10 | public StringValueType() { 11 | super(String.class); 12 | } 13 | 14 | public String get(Class type, ResultSet rs, int columnIndex) throws SQLException { 15 | return rs.getString(columnIndex); 16 | } 17 | 18 | public String get(Class type, ResultSet rs, String columnName) throws SQLException { 19 | return rs.getString(columnName); 20 | } 21 | 22 | public void set(Class type, PreparedStatement stmt, String value, int index) throws SQLException { 23 | if (value == null) { 24 | setNull(type, stmt, index); 25 | } else { 26 | stmt.setString(index, (String) value); 27 | } 28 | } 29 | 30 | public String get(Class type, CallableStatement cs, int index) throws SQLException { 31 | return cs.getString(index); 32 | } 33 | 34 | public String get(Class type, CallableStatement cs, String parameterName) throws SQLException { 35 | return cs.getString(parameterName); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/miragesql/miragesql/type/TimeValueType.java: -------------------------------------------------------------------------------- 1 | package com.miragesql.miragesql.type; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.sql.Time; 8 | 9 | public class TimeValueType extends AbstractValueType