Skip to content

Support the _databaseId variable in sql template #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Map;

import freemarker.template.SimpleScalar;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
Expand All @@ -40,13 +41,15 @@ public class FreeMarkerSqlSource implements SqlSource {
private final Template template;
private final Configuration configuration;
private final Version incompatibleImprovementsVersion;
private final String databaseId;

public static final String GENERATED_PARAMS_KEY = "__GENERATED__";

public FreeMarkerSqlSource(Template template, Configuration configuration, Version incompatibleImprovementsVersion) {
this.template = template;
this.configuration = configuration;
this.incompatibleImprovementsVersion = incompatibleImprovementsVersion;
this.databaseId = configuration.getDatabaseId();
}

/**
Expand All @@ -56,9 +59,12 @@ public FreeMarkerSqlSource(Template template, Configuration configuration, Versi
protected Object preProcessDataContext(Object dataContext, boolean isMap) {
if (isMap) {
((Map<String, Object>) dataContext).put(MyBatisParamDirective.DEFAULT_KEY, new MyBatisParamDirective());
((Map<String, Object>) dataContext).put(MyBatisParamDirective.DATABASE_ID_KEY,new SimpleScalar(this.databaseId));
} else {
((ParamObjectAdapter) dataContext).putAdditionalParam(MyBatisParamDirective.DEFAULT_KEY,
new MyBatisParamDirective());
((ParamObjectAdapter) dataContext).putAdditionalParam(MyBatisParamDirective.DATABASE_ID_KEY,
new SimpleScalar(this.databaseId));
}
return dataContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
*/
public class MyBatisParamDirective implements TemplateDirectiveModel {
public static String DEFAULT_KEY = "p";
public static String DATABASE_ID_KEY = "_databaseId";

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2015-2023 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
*
* https://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.freemarker;

import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Optional;

public interface PreparedDatabaseIdParamsMapper {
@Lang(FreeMarkerLanguageDriver.class)
@Select("preparedDatabaseIdTest.ftl")
Optional<Name> getDatabaseIdTest();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2015-2023 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
*
* https://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.freemarker;

import java.io.Reader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.hsqldb.jdbc.JDBCDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Optional;

/**
* Test of using FreeMarker to generate prepared statements parameters.
*
* @author s-nakao
*/
class PreparedDatabaseIdParamsTest {
private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");

JDBCDataSource dataSource = new JDBCDataSource();
dataSource.setUrl("jdbc:hsqldb:mem:db5");
dataSource.setUser("sa");
dataSource.setPassword("");

try (Connection conn = dataSource.getConnection()) {
try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.setErrorLogWriter(null);
runner.runScript(reader);
conn.commit();
}
}

TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);

// You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
// after this to use FreeMarker driver by default.
Configuration configuration = new Configuration(environment);

// set databaseId. default null
// If it is a property, please refer to the following
// https://mybatis.org/mybatis-3/ja/configuration.html#databaseIdProvider.
configuration.setDatabaseId("hsqldb");

configuration.addMapper(PreparedDatabaseIdParamsMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

@Test
void testInCall() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PreparedDatabaseIdParamsMapper mapper = sqlSession.getMapper(PreparedDatabaseIdParamsMapper.class);
Optional<Name> nameList = mapper.getDatabaseIdTest();
Assertions.assertEquals(true, nameList.isPresent());
Assertions.assertEquals("Fred", nameList.get().getFirstName());
Assertions.assertEquals("Flintstone", nameList.get().getLastName());
}
}
}
23 changes: 23 additions & 0 deletions src/test/resources/sql/preparedDatabaseIdTest.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<#--

Copyright 2015-2023 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

https://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.

-->
select
*
from
names
where
<#if '${_databaseId}' == 'hsqldb'>firstName = 'Fred' and lastName = 'Flintstone'</#if>