View Javadoc
1   /*
2    *    Copyright 2009-2023 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 org.apache.ibatis.submitted.repeatable;
17  
18  import org.apache.ibatis.annotations.CacheNamespace;
19  import org.apache.ibatis.annotations.Delete;
20  import org.apache.ibatis.annotations.DeleteProvider;
21  import org.apache.ibatis.annotations.Insert;
22  import org.apache.ibatis.annotations.InsertProvider;
23  import org.apache.ibatis.annotations.Options;
24  import org.apache.ibatis.annotations.Select;
25  import org.apache.ibatis.annotations.SelectKey;
26  import org.apache.ibatis.annotations.SelectProvider;
27  import org.apache.ibatis.annotations.Update;
28  import org.apache.ibatis.annotations.UpdateProvider;
29  
30  @CacheNamespace(readWrite = false)
31  public interface Mapper {
32  
33    @Select(value = "SELECT id, name, 'HSQL' as databaseName FROM users WHERE id = #{id}", databaseId = "hsql")
34    @Select(value = "SELECT id, name, 'DERBY' as databaseName FROM users WHERE id = #{id}", databaseId = "derby")
35    @Select("SELECT id, name, 'DEFAULT' as databaseName FROM users WHERE id = #{id}")
36    @Options(useCache = false, databaseId = "hsql")
37    @Options(useCache = false, databaseId = "derby")
38    User getUser(Integer id);
39  
40    @SelectProvider(type = HsqlSqlProvider.class, method = "getUserUsingProvider", databaseId = "hsql")
41    @SelectProvider(type = DerbySqlProvider.class, method = "getUserUsingProvider", databaseId = "derby")
42    @SelectProvider(type = DefaultSqlProvider.class, method = "getUserUsingProvider")
43    @Options(databaseId = "hsql")
44    @Options(databaseId = "derby")
45    @Options(flushCache = Options.FlushCachePolicy.TRUE)
46    User getUserUsingProvider(Integer id);
47  
48    @SelectProvider(type = HsqlSqlProvider.class, method = "getUserUsingProvider", databaseId = "hsql")
49    @Select(value = "SELECT id, name, 'DERBY' as databaseName FROM users WHERE id = #{id}", databaseId = "derby")
50    @Select("SELECT id, name, 'DEFAULT' as databaseName FROM users WHERE id = #{id}")
51    @Options(useCache = false, databaseId = "hsql")
52    @Options(useCache = false, databaseId = "derby")
53    User getUserUsingBoth(Integer id);
54  
55    @Insert(value = "INSERT INTO users (id, name) VALUES(#{id}, #{name} || ' HSQL')", databaseId = "hsql")
56    @Insert(value = "INSERT INTO users (id, name) VALUES(#{id}, #{name} || ' DERBY')", databaseId = "derby")
57    @Insert("INSERT INTO users (id, name) VALUES(#{id}, #{name} || ' DEFAULT')")
58    @SelectKey(statement = "SELECT COUNT(*) + 1 FROM users", keyProperty = "id", before = true, resultType = Integer.class, databaseId = "hsql")
59    @SelectKey(statement = "SELECT COUNT(*) + 1001 FROM users", keyProperty = "id", before = true, resultType = Integer.class, databaseId = "derby")
60    @SelectKey(statement = "SELECT COUNT(*) + 10001 FROM users", keyProperty = "id", before = true, resultType = Integer.class)
61    void insertUser(User user);
62  
63    @InsertProvider(type = HsqlSqlProvider.class, method = "insertUserUsingProvider", databaseId = "hsql")
64    @InsertProvider(type = DerbySqlProvider.class, method = "insertUserUsingProvider", databaseId = "derby")
65    @InsertProvider(type = DefaultSqlProvider.class, method = "insertUserUsingProvider")
66    @SelectKey(statement = "SELECT COUNT(*) + 1 FROM users", keyProperty = "id", before = true, resultType = Integer.class, databaseId = "hsql")
67    @SelectKey(statement = "SELECT COUNT(*) + 1001 FROM users", keyProperty = "id", before = true, resultType = Integer.class, databaseId = "derby")
68    @SelectKey(statement = "SELECT COUNT(*) + 10001 FROM users", keyProperty = "id", before = true, resultType = Integer.class)
69    void insertUserUsingProvider(User user);
70  
71    @Update(value = "UPDATE users SET name = name || ' HSQL' WHERE id = #{id}", databaseId = "hsql")
72    @Update(value = "UPDATE users SET name = name || ' DERBY' WHERE id = #{id}", databaseId = "derby")
73    @Update("UPDATE users SET name = name || ' DEFAULT' WHERE id = #{id}")
74    void updateUserName(Integer id);
75  
76    @UpdateProvider(type = HsqlSqlProvider.class, method = "updateUserNameUsingProvider", databaseId = "hsql")
77    @UpdateProvider(type = DerbySqlProvider.class, method = "updateUserNameUsingProvider", databaseId = "derby")
78    @UpdateProvider(type = DefaultSqlProvider.class, method = "updateUserNameUsingProvider")
79    void updateUserNameUsingProvider(Integer id);
80  
81    @Delete(value = "DELETE FROM users WHERE name LIKE '%HSQL%'", databaseId = "hsql")
82    @Delete(value = "DELETE FROM users WHERE name LIKE '%DERBY%'", databaseId = "derby")
83    @Delete("DELETE FROM users WHERE name LIKE '%DEFAULT%'")
84    void delete();
85  
86    @DeleteProvider(type = HsqlSqlProvider.class, method = "delete", databaseId = "hsql")
87    @DeleteProvider(type = DerbySqlProvider.class, method = "delete", databaseId = "derby")
88    @DeleteProvider(type = DefaultSqlProvider.class, method = "delete")
89    void deleteUsingProvider();
90  
91    @Select("SELECT COUNT(*) FROM users")
92    int count();
93  
94    @Select("SELECT COUNT(*) FROM users WHERE name LIKE '%' || #{dataabse} || '%'")
95    int countByCurrentDatabase(String database);
96  
97    class HsqlSqlProvider {
98      public static String getUserUsingProvider() {
99        return "SELECT id, name, 'HSQL' as databaseName FROM users WHERE id = #{id}";
100     }
101 
102     public static String insertUserUsingProvider() {
103       return "INSERT INTO users (id, name) VALUES(#{id}, #{name} || ' HSQL')";
104     }
105 
106     public static String updateUserNameUsingProvider() {
107       return "UPDATE users SET name = name || ' HSQL' WHERE id = #{id}";
108     }
109 
110     public static String delete() {
111       return "DELETE FROM users WHERE name LIKE '%HSQL%'";
112     }
113 
114     private HsqlSqlProvider() {
115     }
116   }
117 
118   class DerbySqlProvider {
119     public static String getUserUsingProvider() {
120       return "SELECT id, name, 'DERBY' as databaseName FROM users WHERE id = #{id}";
121     }
122 
123     public static String insertUserUsingProvider() {
124       return "INSERT INTO users (id, name) VALUES(#{id}, #{name} || ' DERBY')";
125     }
126 
127     public static String updateUserNameUsingProvider() {
128       return "UPDATE users SET name = name || ' DERBY' WHERE id = #{id}";
129     }
130 
131     public static String delete() {
132       return "DELETE FROM users WHERE name LIKE '%DERBY%'";
133     }
134 
135     private DerbySqlProvider() {
136     }
137   }
138 
139   class DefaultSqlProvider {
140     public static String getUserUsingProvider() {
141       return "SELECT id, name, 'DEFAULT' as databaseName FROM users WHERE id = #{id}";
142     }
143 
144     public static String insertUserUsingProvider() {
145       return "INSERT INTO users (id, name) VALUES(#{id}, #{name} || ' DEFAULT')";
146     }
147 
148     public static String updateUserNameUsingProvider() {
149       return "UPDATE users SET name = name || ' DEFAULT' WHERE id = #{id}";
150     }
151 
152     public static String delete() {
153       return "DELETE FROM users WHERE name LIKE '%DEFAULT%'";
154     }
155 
156     private DefaultSqlProvider() {
157     }
158   }
159 
160 }