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

future-architect / uroborosql / #892

17 Aug 2025 05:09PM UTC coverage: 87.81% (-0.5%) from 88.265%
#892

Pull #365

HidekiSugimoto189
add null guard
Pull Request #365: Add ConnectionSupplier Fix schema name option for master.

77 of 146 new or added lines in 5 files covered. (52.74%)

1 existing line in 1 file now uncovered.

8911 of 10148 relevant lines covered (87.81%)

0.88 hits per line

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

92.86
/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 connectionContext DB接続情報
31
         */
32
        public JdbcConnectionSupplierImpl(final JdbcConnectionContext connectionContext) {
1✔
33
                this.defaultConnectionContext = connectionContext;
1✔
34
        }
1✔
35

36
        /**
37
         * {@inheritDoc}
38
         *
39
         * @see jp.co.future.uroborosql.connection.ConnectionSupplier#getConnection()
40
         */
41
        @Override
42
        public Connection getConnection() {
43
                return getConnection(defaultConnectionContext);
1✔
44
        }
45

46
        /**
47
         * {@inheritDoc}
48
         *
49
         * @see jp.co.future.uroborosql.connection.ConnectionSupplier#getConnection(jp.co.future.uroborosql.connection.ConnectionContext)
50
         */
51
        @Override
52
        public Connection getConnection(final ConnectionContext ctx) {
53
                if (!(ctx instanceof JdbcConnectionContext)) {
1✔
54
                        throw new IllegalArgumentException("ctx must be of type JdbcConnectionContext.");
1✔
55
                }
56
                var jdbcCtx = (JdbcConnectionContext) ctx;
1✔
57
                try {
58
                        var original = DriverManager.getConnection(jdbcCtx.url(), jdbcCtx.toProperties());
1✔
59
                        Connection connection;
60
                        if (jdbcCtx.fixSchema()) {
1✔
61
                                var schemaName = jdbcCtx.fixedSchemaName();
1✔
62
                                if (schemaName == null) {
1✔
63
                                        schemaName = original.getSchema();
1✔
64
                                        if (schemaName != null) {
1✔
65
                                                jdbcCtx.fixedSchemaName(schemaName);
1✔
66
                                        }
67
                                }
68
                                connection = new SchemaFixedConnectionWrapper(original, schemaName);
1✔
69
                        } else {
1✔
70
                                connection = new MetadataCachedConnectionWrapper(original, jdbcCtx.cacheSchema());
1✔
71
                                var schema = jdbcCtx.schema();
1✔
72
                                if (schema != null && !Objects.equals(connection.getSchema(), schema)) {
1✔
NEW
73
                                        connection.setSchema(schema);
×
74
                                }
75
                        }
76

77
                        if (jdbcCtx.autoCommit() != connection.getAutoCommit()) {
1✔
78
                                connection.setAutoCommit(jdbcCtx.autoCommit());
1✔
79
                        }
80
                        if (jdbcCtx.readOnly() != connection.isReadOnly()) {
1✔
81
                                connection.setReadOnly(jdbcCtx.readOnly());
1✔
82
                        }
83
                        var transactionIsolation = jdbcCtx.transactionIsolation();
1✔
84
                        if (transactionIsolation > 0 && transactionIsolation != connection.getTransactionIsolation()) {
1✔
85
                                connection.setTransactionIsolation(transactionIsolation);
1✔
86
                        }
87
                        return connection;
1✔
88
                } catch (SQLException ex) {
×
89
                        throw new UroborosqlSQLException("Connection[" + jdbcCtx.url() + "] can not be acquired.", ex);
×
90
                }
91
        }
92

93
        /**
94
         * デフォルトのDB接続情報にJDBCスキーマ名を設定
95
         *
96
         * @param schema スキーマ名
97
         */
98
        public void setDefaultSchema(final String schema) {
99
                defaultConnectionContext.schema(schema);
1✔
100
        }
1✔
101

102
        /**
103
         * デフォルトのDB接続情報にAutoCommitオプションの指定
104
         *
105
         * @param autoCommit AutoCommitを行う場合は<code>true</code>
106
         */
107
        public void setDefaultAutoCommit(final boolean autoCommit) {
108
                defaultConnectionContext.autoCommit(autoCommit);
1✔
109
        }
1✔
110

111
        /**
112
         * デフォルトのDB接続情報にReadOnlyオプションを指定
113
         *
114
         * @param readOnly readOnlyを指定する場合は<code>true</code>
115
         */
116
        public void setDefaultReadOnly(final boolean readOnly) {
117
                defaultConnectionContext.readOnly(readOnly);
1✔
118
        }
1✔
119

120
        /**
121
         * デフォルトのDB接続情報にtransactionIsolationオプションを指定
122
         *
123
         * @see Connection#TRANSACTION_READ_UNCOMMITTED
124
         * @see Connection#TRANSACTION_READ_COMMITTED
125
         * @see Connection#TRANSACTION_REPEATABLE_READ
126
         * @see Connection#TRANSACTION_SERIALIZABLE
127
         *
128
         * @param transactionIsolation transactionIsolationオプション
129
         */
130
        public void setDefaultTransactionIsolation(final int transactionIsolation) {
131
                defaultConnectionContext.transactionIsolation(transactionIsolation);
1✔
132
        }
1✔
133

134
        /**
135
         * デフォルトのDB接続情報にスキーマ名のキャッシュオプションを指定
136
         *
137
         * @param cache スキーマ名をキャッシュする場合は<code>true</code>
138
         */
139
        public void setDefaultCacheSchema(final boolean cache) {
140
                defaultConnectionContext.cacheSchema(cache);
1✔
141
        }
1✔
142

143
        /**
144
         * デフォルトのDB接続情報にスキーマ名の固定オプションを指定
145
         *
146
         * @param fixed スキーマ名を固定する場合は<code>true</code>
147
         */
148
        public void setDefaultFixSchema(final boolean fixed) {
149
                defaultConnectionContext.fixSchema(fixed);
1✔
150
        }
1✔
151
}
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