• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

future-architect / uroborosql / #893

17 Aug 2025 05:16PM UTC coverage: 88.615% (+0.1%) from 88.472%
#893

Pull #364

HidekiSugimoto189
add null guard
Pull Request #364: Add ConnectionSupplier Fix schema name option.

130 of 142 new or added lines in 5 files covered. (91.55%)

2 existing lines in 1 file now uncovered.

8204 of 9258 relevant lines covered (88.62%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/main/java/jp/co/future/uroborosql/connection/JdbcConnectionSupplierImpl.java
1
/**
2
 * Copyright (c) 2017-present, Future Corporation
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
package jp.co.future.uroborosql.connection;
8

9
import java.sql.Connection;
10
import java.sql.DriverManager;
11
import java.sql.SQLException;
12
import java.util.Objects;
13

14
import jp.co.future.uroborosql.exception.UroborosqlSQLException;
15

16
/**
17
 * JDBCドライバーを使用したコネクション供給クラス<br>
18
 * 指定されたプロパティをもとにコネクションを都度生成する
19
 *
20
 * @author H.Sugimoto
21
 */
22
public class JdbcConnectionSupplierImpl implements ConnectionSupplier {
23

24
        /** デフォルトDB接続情報 */
25
        private final JdbcConnectionContext defaultConnectionContext;
26

27
        /**
28
         * コンストラクタ
29
         *
30
         * @param url JDBC URL(必須)
31
         * @param user 接続ユーザ名
32
         * @param password 接続パスワード
33
         */
34
        @Deprecated
35
        public JdbcConnectionSupplierImpl(final String url, final String user, final String password) {
36
                this(ConnectionContextBuilder.jdbc(url, user, password));
1✔
37
        }
1✔
38

39
        /**
40
         * コンストラクタ
41
         *
42
         * @param url JDBC URL(必須)
43
         * @param user 接続ユーザ名
44
         * @param password 接続パスワード
45
         * @param schema JDBCスキーマ名
46
         */
47
        @Deprecated
48
        public JdbcConnectionSupplierImpl(final String url, final String user, final String password, final String schema) {
49
                this(ConnectionContextBuilder.jdbc(url, user, password, schema));
1✔
50
        }
1✔
51

52
        /**
53
         * コンストラクタ
54
         *
55
         * @param url JDBC URL(必須)
56
         * @param user 接続ユーザ名
57
         * @param password 接続パスワード
58
         * @param schema JDBCスキーマ名
59
         * @param autoCommit 自動コミットするかどうか
60
         * @param readOnly 参照のみかどうか
61
         */
62
        @Deprecated
63
        public JdbcConnectionSupplierImpl(final String url, final String user, final String password, final String schema,
64
                        final boolean autoCommit, final boolean readOnly) {
65
                this(ConnectionContextBuilder.jdbc(url, user, password, schema).autoCommit(autoCommit)
1✔
66
                                .readOnly(readOnly));
1✔
67
        }
1✔
68

69
        /**
70
         * コンストラクタ
71
         *
72
         * @param connectionContext DB接続情報
73
         */
74
        public JdbcConnectionSupplierImpl(final JdbcConnectionContext connectionContext) {
1✔
75
                this.defaultConnectionContext = connectionContext;
1✔
76
        }
1✔
77

78
        /**
79
         * {@inheritDoc}
80
         *
81
         * @see jp.co.future.uroborosql.connection.ConnectionSupplier#getConnection()
82
         */
83
        @Override
84
        public Connection getConnection() {
85
                return getConnection(defaultConnectionContext);
1✔
86
        }
87

88
        /**
89
         * {@inheritDoc}
90
         *
91
         * @see jp.co.future.uroborosql.connection.ConnectionSupplier#getConnection(jp.co.future.uroborosql.connection.ConnectionContext)
92
         */
93
        @Override
94
        public Connection getConnection(final ConnectionContext ctx) {
95
                if (!(ctx instanceof JdbcConnectionContext)) {
1✔
96
                        throw new IllegalArgumentException("ctx must be of type JdbcConnectionContext.");
1✔
97
                }
98
                JdbcConnectionContext jdbcCtx = (JdbcConnectionContext) ctx;
1✔
99
                try {
100
                        Connection original = DriverManager.getConnection(jdbcCtx.url(), jdbcCtx.toProperties());
1✔
101
                        Connection connection;
102
                        if (jdbcCtx.fixSchema()) {
1✔
103
                                String schemaName = jdbcCtx.fixedSchemaName();
1✔
104
                                if (schemaName == null) {
1✔
105
                                        schemaName = original.getSchema();
1✔
106
                                        if (schemaName != null) {
1✔
107
                                                jdbcCtx.fixedSchemaName(schemaName);
1✔
108
                                        }
109
                                }
110
                                connection = new SchemaFixedConnectionWrapper(original, schemaName);
1✔
111
                        } else {
1✔
112
                                connection = new MetadataCachedConnectionWrapper(original, jdbcCtx.cacheSchema());
1✔
113
                                String schema = jdbcCtx.schema();
1✔
114
                                if (schema != null && !Objects.equals(connection.getSchema(), schema)) {
1✔
NEW
115
                                        connection.setSchema(schema);
×
116
                                }
117
                        }
118
                        if (jdbcCtx.autoCommit() != connection.getAutoCommit()) {
1✔
119
                                connection.setAutoCommit(jdbcCtx.autoCommit());
1✔
120
                        }
121
                        if (jdbcCtx.readOnly() != connection.isReadOnly()) {
1✔
122
                                connection.setReadOnly(jdbcCtx.readOnly());
1✔
123
                        }
124
                        int transactionIsolation = jdbcCtx.transactionIsolation();
1✔
125
                        if (transactionIsolation > 0 && transactionIsolation != connection.getTransactionIsolation()) {
1✔
126
                                connection.setTransactionIsolation(transactionIsolation);
1✔
127
                        }
128
                        return connection;
1✔
129
                } catch (SQLException ex) {
×
130
                        throw new UroborosqlSQLException("Connection[" + jdbcCtx.url() + "] can not be acquired.", ex);
×
131
                }
132
        }
133

134
        /**
135
         * JDBCスキーマ名を設定
136
         *
137
         * @param schema スキーマ名
138
         */
139
        @Deprecated
140
        public void setSchema(final String schema) {
141
                setDefaultSchema(schema);
1✔
142
        }
1✔
143

144
        /**
145
         * デフォルトのDB接続情報にJDBCスキーマ名を設定
146
         *
147
         * @param schema スキーマ名
148
         */
149
        public void setDefaultSchema(final String schema) {
150
                defaultConnectionContext.schema(schema);
1✔
151
        }
1✔
152

153
        /**
154
         * デフォルトのDB接続情報にAutoCommitオプションの指定
155
         *
156
         * @param autoCommit AutoCommitを行う場合は<code>true</code>
157
         */
158
        public void setDefaultAutoCommit(final boolean autoCommit) {
159
                defaultConnectionContext.autoCommit(autoCommit);
1✔
160
        }
1✔
161

162
        /**
163
         * デフォルトのDB接続情報にReadOnlyオプションを指定
164
         *
165
         * @param readOnly readOnlyを指定する場合は<code>true</code>
166
         */
167
        public void setDefaultReadOnly(final boolean readOnly) {
168
                defaultConnectionContext.readOnly(readOnly);
1✔
169
        }
1✔
170

171
        /**
172
         * デフォルトのDB接続情報にtransactionIsolationオプションを指定
173
         *
174
         * @see Connection#TRANSACTION_READ_UNCOMMITTED
175
         * @see Connection#TRANSACTION_READ_COMMITTED
176
         * @see Connection#TRANSACTION_REPEATABLE_READ
177
         * @see Connection#TRANSACTION_SERIALIZABLE
178
         *
179
         * @param transactionIsolation transactionIsolationオプション
180
         */
181
        public void setDefaultTransactionIsolation(final int transactionIsolation) {
182
                defaultConnectionContext.transactionIsolation(transactionIsolation);
1✔
183
        }
1✔
184

185
        /**
186
         * デフォルトのDB接続情報にスキーマ名のキャッシュオプションを指定
187
         *
188
         * @param cache スキーマ名をキャッシュする場合は<code>true</code>
189
         */
190
        public void setDefaultCacheSchema(final boolean cache) {
191
                defaultConnectionContext.cacheSchema(cache);
1✔
192
        }
1✔
193

194
        /**
195
         * デフォルトのDB接続情報にスキーマ名の固定オプションを指定
196
         *
197
         * @param fixed スキーマ名を固定する場合は<code>true</code>
198
         */
199
        public void setDefaultFixSchema(final boolean fixed) {
200
                defaultConnectionContext.fixSchema(fixed);
1✔
201
        }
1✔
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc