View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
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    *       https://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, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package examples.animal.data;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.assertj.core.api.Assertions.fail;
20  
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.sql.Connection;
24  import java.sql.DriverManager;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  
29  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
30  import org.apache.ibatis.jdbc.ScriptRunner;
31  import org.apache.ibatis.mapping.Environment;
32  import org.apache.ibatis.session.Configuration;
33  import org.apache.ibatis.session.SqlSession;
34  import org.apache.ibatis.session.SqlSessionFactory;
35  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
36  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Tests for understanding where bind parameters are allowed
42   */
43  class BindingTest {
44  
45      private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
46      private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
47  
48      private SqlSessionFactory sqlSessionFactory;
49  
50      @BeforeEach
51      void setup() throws Exception {
52          Class.forName(JDBC_DRIVER);
53          InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql");
54          assert is != null;
55          try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
56              ScriptRunner sr = new ScriptRunner(connection);
57              sr.setLogWriter(null);
58              sr.runScript(new InputStreamReader(is));
59          }
60  
61          UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
62          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
63          Configuration config = new Configuration(environment);
64          config.addMapper(AnimalDataMapper.class);
65          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
66      }
67  
68      @Test
69      void testBindInSelectList() {
70          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
71              Connection connection = sqlSession.getConnection();
72  
73              PreparedStatement ps = connection.prepareStatement("select brain_weight + ? as calc from AnimalData where id = ?");
74              ps.setDouble(1, 1.0);
75              ps.setInt(2, 1);
76  
77              ResultSet rs = ps.executeQuery();
78              double calculatedWeight = 0.0;
79              if (rs.next()) {
80                  calculatedWeight = rs.getDouble("CALC");
81              }
82  
83              rs.close();
84              ps.close();
85  
86              assertThat(calculatedWeight).isEqualTo(1.005);
87          } catch (SQLException e) {
88              fail("SQL Exception", e);
89          }
90      }
91  
92      @Test
93      void testBindInWeirdWhere() {
94          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
95              Connection connection = sqlSession.getConnection();
96  
97              PreparedStatement ps = connection.prepareStatement("select brain_weight from AnimalData where brain_weight + ? > ? and id = ?");
98              ps.setDouble(1, 1.0);
99              ps.setDouble(2, 1.0);
100             ps.setInt(3, 1);
101 
102             ResultSet rs = ps.executeQuery();
103             double calculatedWeight = 0.0;
104             if (rs.next()) {
105                 calculatedWeight = rs.getDouble("BRAIN_WEIGHT");
106             }
107 
108             rs.close();
109             ps.close();
110 
111             assertThat(calculatedWeight).isEqualTo(.005);
112         } catch (SQLException e) {
113             fail("SQL Exception", e);
114         }
115     }
116 }