forked from mybatis/thymeleaf-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SQL provider class that help detecting a template file automatically
Fices mybatisgh-10
- Loading branch information
1 parent
18cd409
commit cf8bccc
Showing
21 changed files
with
779 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/main/java/org/mybatis/scripting/thymeleaf/support/TemplateFilePathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* Copyright 2018-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mybatis.scripting.thymeleaf.support; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
import org.apache.ibatis.builder.annotation.ProviderContext; | ||
import org.apache.ibatis.io.Resources; | ||
|
||
/** | ||
* The SQL provider class that return the SQL template file path. <br> | ||
* <b>IMPORTANT: This class required to use with mybatis 3.5.1+</b> and need to use with SQL provider annotation (such | ||
* as {@link org.apache.ibatis.annotations.SelectProvider} as follow: <br> | ||
* <br> | ||
* | ||
* <pre> | ||
* package com.example.mapper; | ||
* | ||
* public interface BaseMapper<T> { | ||
* | ||
* @Options(useGeneratedKeys = true, keyProperty = "id") | ||
* @InsertProvider(type = TemplateFilePathProvider.class) | ||
* void insert(T entity); | ||
* | ||
* @UpdateProvider(type = TemplateFilePathProvider.class) | ||
* void update(T entity); | ||
* | ||
* @DeleteProvider(type = TemplateFilePathProvider.class) | ||
* void delete(T entity); | ||
* | ||
* @SelectProvider(type = TemplateFilePathProvider.class) | ||
* T findById(Integer id); | ||
* | ||
* } | ||
* </pre> | ||
* | ||
* <pre> | ||
* package com.example.mapper; | ||
* | ||
* public interface NameMapper extends BaseMapper { | ||
* | ||
* @SelectProvider(type = TemplateFilePathProvider.class) | ||
* List<Name> findByConditions(NameConditions conditions); | ||
* | ||
* } | ||
* </pre> | ||
* | ||
* @author Kazuki Shimizu | ||
* @version 1.0.1 | ||
*/ | ||
public class TemplateFilePathProvider { | ||
|
||
private static TemplateFilePathGenerator generator = TemplateFilePathProvider::generatePath; | ||
|
||
/** | ||
* Set custom implementation for {@link TemplateFilePathGenerator}. | ||
* | ||
* @param customGenerator | ||
* a instance for generating a template file path | ||
*/ | ||
public static void setCustomTemplateFilePathGenerator(TemplateFilePathGenerator customGenerator) { | ||
generator = Optional.ofNullable(customGenerator).orElse(TemplateFilePathProvider::generatePath); | ||
} | ||
|
||
/** | ||
* Provide an SQL scripting string(template file path). | ||
* | ||
* <br> | ||
* By default implementation, a template file path resolve following format and priority order. If does not match all, | ||
* it throw an exception that indicate not found a template file. | ||
* <ul> | ||
* <li>com/example/mapper/NameMapper-{methodName}-{databaseId}.sql</li> | ||
* <li>com/example/mapper/NameMapper-{methodName}.sql (fallback using default database)</li> | ||
* <li>com/example/mapper/BaseMapper-{methodName}-{databaseId}.sql (fallback using declaring class of method)</li> | ||
* <li>com/example/mapper/BaseMapper-{methodName}.sql (fallback using declaring class of method and default | ||
* database)</li> | ||
* </ul> | ||
* <br> | ||
* If you want to customize path format, please call the | ||
* {@link #setCustomTemplateFilePathGenerator(TemplateFilePathGenerator)} on application initialize phase. | ||
* | ||
* @param context | ||
* a context of SQL provider | ||
* @return an SQL scripting string(template file path) | ||
*/ | ||
public static String provideSql(ProviderContext context) { | ||
return providePath(context.getMapperType(), context.getMapperMethod(), context.getDatabaseId()); | ||
} | ||
|
||
static String providePath(Class<?> mapperType, Method mapperMethod, String databaseId) { | ||
boolean fallbackDeclaringClass = mapperType != mapperMethod.getDeclaringClass(); | ||
boolean fallbackDatabase = databaseId != null; | ||
String path = generator.generatePath(mapperType, mapperMethod, databaseId); | ||
if (exists(path)) { | ||
return path; | ||
} | ||
if (fallbackDatabase) { | ||
path = generator.generatePath(mapperType, mapperMethod, null); | ||
if (exists(path)) { | ||
return path; | ||
} | ||
} | ||
if (fallbackDeclaringClass) { | ||
path = generator.generatePath(mapperMethod.getDeclaringClass(), mapperMethod, databaseId); | ||
if (exists(path)) { | ||
return path; | ||
} | ||
} | ||
if (fallbackDatabase) { | ||
path = generator.generatePath(mapperMethod.getDeclaringClass(), mapperMethod, null); | ||
if (exists(path)) { | ||
return path; | ||
} | ||
} | ||
throw new IllegalStateException("The SQL template file not found. mapperType:[" + mapperType + "] mapperMethod:[" | ||
+ mapperMethod + "] databaseId:[" + databaseId + "]"); | ||
} | ||
|
||
private static String generatePath(Class<?> type, Method method, String databaseId) { | ||
StringBuilder path = new StringBuilder(); | ||
path.append(type.getName().replace('.', '/')); | ||
path.append("-").append(method.getName()); | ||
if (databaseId != null) { | ||
path.append("-").append(databaseId); | ||
} | ||
path.append(".sql"); | ||
return path.toString(); | ||
} | ||
|
||
private static boolean exists(String path) { | ||
try { | ||
return Resources.getResourceAsFile(path).exists(); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* The interface that implements a function for generating template file path. | ||
*/ | ||
@FunctionalInterface | ||
public interface TemplateFilePathGenerator { | ||
|
||
/** | ||
* Generate a template file path. | ||
* | ||
* @param type | ||
* mapper interface type that specified provider (or declaring interface type of mapper method) | ||
* @param method | ||
* a mapper method that specified provider | ||
* @param databaseId | ||
* a database id that provided from {@link org.apache.ibatis.mapping.DatabaseIdProvider} | ||
* @return a template file path | ||
*/ | ||
String generatePath(Class<?> type, Method method, String databaseId); | ||
|
||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/mybatis/scripting/thymeleaf/support/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2018-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* The package that holds classes for supports development. | ||
*/ | ||
package org.mybatis.scripting.thymeleaf.support; |
Oops, something went wrong.