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

future-architect / uroborosql / #743

28 Jun 2024 04:17PM UTC coverage: 90.988% (+0.5%) from 90.484%
#743

push

web-flow
add paramIfNotEmpty method (#318)

* v0.x to master merge

* fix nanoseconds diff (java8 to java11)

* eclipse cleanup and  optimize import

* eclipse format

* optimize Imports

* remove unused annotation

* library version up

* migrate v0.x to master
- update java version 8 to 11
- update junit4 to junit5
- library version update

* fix test failed

* remove unused annotation

* fixed bug

* use var

* fix java8 coding style

* Refactoring TransactionContextManager

* Refactoring SqlAgent

* 途中コミット

* fix testcase

* fix review

* fix javadoc comments

* merge v0.x PR

* cleanup code

* cleanup test code

* change build status badge

* - agent.query, update, proc, batch にそれぞれSupplierを引数にとるメソッドを追加
- SqlQuery.paramとSqlEntityUpdate.setにFunctionを受け取るメソッドを追加

* testcaseの整形

* - SqlFluent と ExtractionCondition の分離
- Arrays.asList -> List.of への変更
- テストの整形

* - v0.x系の不具合対応の追いつき
- ログ出力の整理

* - SqlKindの整理(ENTITY_XXXの追加)
- REPL_LOGの追加
- Deprecatedメソッドの削除(SqlAgent)
- SqlAgent, ExecutionContextでsetterをfluent APIに変更

* DB接続URLの修正

* add and fix testcases.

* add event testcases.

* fix typo

* add paramIfNotEmpty method and StringUtils rename ObjectUtils

* fix review comments.

* remove unused import

1695 of 1958 new or added lines in 97 files covered. (86.57%)

26 existing lines in 10 files now uncovered.

8249 of 9066 relevant lines covered (90.99%)

0.91 hits per line

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

82.72
/src/main/java/jp/co/future/uroborosql/parameter/Parameter.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.parameter;
8

9
import java.sql.JDBCType;
10
import java.sql.PreparedStatement;
11
import java.sql.SQLException;
12
import java.sql.SQLType;
13
import java.util.Map;
14
import java.util.Objects;
15

16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18
import org.slf4j.MDC;
19

20
import jp.co.future.uroborosql.parameter.mapper.BindParameterMapperManager;
21
import jp.co.future.uroborosql.utils.ObjectUtils;
22

23
/**
24
 * パラメータオブジェクト。<br>
25
 * SQLへバインドするパラメータを保持する。<br>
26
 *
27
 * @author H.Sugimoto
28
 */
29
public class Parameter {
30
        /** ロガー */
31
        private static final Logger LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.log");
1✔
32

33
        /** SQLロガー */
34
        private static final Logger SQL_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.sql");
1✔
35

36
        /** 未設定のSQLType */
37
        protected static final SQLType SQL_TYPE_NOT_SET = null;
1✔
38

39
        /**
40
         * パラメータ名
41
         */
42
        protected final String parameterName;
43

44
        /**
45
         * パラメータ値
46
         */
47
        protected final Object value;
48

49
        /**
50
         * SQL型
51
         */
52
        protected final SQLType sqlType;
53

54
        /**
55
         * コンストラクタ。
56
         *
57
         * @param parameterName パラメータ名
58
         * @param value 値
59
         */
60
        public Parameter(final String parameterName, final Object value) {
61
                this(parameterName, value, SQL_TYPE_NOT_SET);
1✔
62
        }
1✔
63

64
        /**
65
         * コンストラクタ。
66
         *
67
         * @param parameterName パラメータ名
68
         * @param value 値
69
         * @param sqlType {@link java.sql.SQLType} で表される型
70
         */
71
        public Parameter(final String parameterName, final Object value, final SQLType sqlType) {
1✔
72
                this.parameterName = parameterName;
1✔
73
                this.value = value;
1✔
74
                this.sqlType = sqlType;
1✔
75
        }
1✔
76

77
        /**
78
         * コンストラクタ。
79
         *
80
         * @param parameterName パラメータ名
81
         * @param value 値
82
         * @param sqlType {@link java.sql.Types} で表される型
83
         */
84
        public Parameter(final String parameterName, final Object value, final int sqlType) {
1✔
85
                this.parameterName = parameterName;
1✔
86
                this.value = value;
1✔
87
                this.sqlType = toSqlType(sqlType);
1✔
88
        }
1✔
89

90
        /**
91
         * サブパラメータを生成する。 パラメータ値がBeanの場合、プロパティ名に対応するフィールド値をパラメータ値とする サブパラメータを作成して返す。
92
         *
93
         * @param propertyName プロパティ名
94
         * @return パラメータ
95
         */
96
        @SuppressWarnings("rawtypes")
97
        public Parameter createSubParameter(final String propertyName) {
98
                var subParameterName = parameterName + "." + propertyName;
1✔
99
                Object subValue = null;
1✔
100
                if (value != null) {
1✔
101
                        if (value instanceof Map) {
1✔
102
                                subValue = ((Map) value).get(propertyName);
×
103
                                if (subValue == null) {
×
NEW
104
                                        if (LOG.isWarnEnabled()) {
×
NEW
105
                                                LOG.warn("Set subparameter value to NULL because property can not be accessed.[{}]",
×
106
                                                                subParameterName);
107
                                        }
108
                                }
109
                        } else {
110
                                try {
111
                                        // フィールドアクセスで値の取得を実施
112
                                        var field = value.getClass().getDeclaredField(propertyName);
1✔
113
                                        field.setAccessible(true);
1✔
114
                                        subValue = field.get(value);
1✔
115
                                } catch (NoSuchFieldException ex) {
1✔
116
                                        // メソッドアクセスで値の取得を実施
117
                                        try {
118
                                                var prefix = boolean.class.equals(value.getClass()) ? "is" : "get";
1✔
119
                                                var method = value.getClass()
1✔
120
                                                                .getMethod(prefix + ObjectUtils.capitalize(propertyName));
1✔
121
                                                subValue = method.invoke(value);
1✔
NEW
122
                                        } catch (Exception ex2) {
×
NEW
123
                                                if (LOG.isWarnEnabled()) {
×
NEW
124
                                                        LOG.warn("Set subparameter value to NULL because property can not be accessed.[{}]",
×
125
                                                                        subParameterName, ex2);
126
                                                }
127
                                        }
1✔
NEW
128
                                } catch (Exception ex) {
×
NEW
129
                                        if (LOG.isWarnEnabled()) {
×
UNCOV
130
                                                LOG.warn("Set subparameter value to NULL because property can not be accessed.[{}]",
×
131
                                                                subParameterName, ex);
132
                                        }
133
                                }
1✔
134
                        }
135
                }
136

137
                return new Parameter(subParameterName, subValue);
1✔
138
        }
139

140
        /**
141
         * ステートメントへパラメータ値をバインド。
142
         *
143
         * @param preparedStatement ステートメント
144
         * @param index パラメータインデックス
145
         * @param parameterMapperManager パラメータ変換管理クラス
146
         * @return 次のパラメータインデックス
147
         * @throws SQLException SQL例外
148
         */
149
        public int setParameter(final PreparedStatement preparedStatement, final int index,
150
                        final BindParameterMapperManager parameterMapperManager) throws SQLException {
151
                return setInParameter(preparedStatement, index, parameterMapperManager);
1✔
152
        }
153

154
        /**
155
         * ステートメントへ入力パラメータ値をバインド。
156
         *
157
         * @param preparedStatement ステートメント
158
         * @param index パラメータインデックス
159
         * @param parameterMapperManager パラメータ変換管理クラス
160
         * @return 次のパラメータインデックス
161
         * @throws SQLException SQL例外
162
         */
163
        protected int setInParameter(final PreparedStatement preparedStatement, final int index,
164
                        final BindParameterMapperManager parameterMapperManager) throws SQLException {
165
                var parameterIndex = index;
1✔
166
                if (value instanceof Iterable) {
1✔
167
                        for (var e : (Iterable<?>) value) {
1✔
168
                                setParameterObject(preparedStatement, parameterIndex, e, parameterMapperManager);
1✔
169

170
                                parameterLog(parameterIndex);
1✔
171
                                parameterIndex++;
1✔
172
                        }
1✔
173
                } else {
174
                        setParameterObject(preparedStatement, parameterIndex, value, parameterMapperManager);
1✔
175

176
                        parameterLog(parameterIndex);
1✔
177
                        parameterIndex++;
1✔
178
                }
179

180
                return parameterIndex;
1✔
181
        }
182

183
        /**
184
         * パラメータ設定ログ出力。
185
         *
186
         * @param index パラメータインデックス
187
         */
188
        protected void parameterLog(final int index) {
189
                if (SQL_LOG.isInfoEnabled() && Boolean.FALSE.toString().equals(MDC.get("SuppressParameterLogOutput"))) {
1✔
190
                        SQL_LOG.info("Set the parameter.[INDEX[{}], {}]", index, this);
1✔
191
                }
192
        }
1✔
193

194
        /**
195
         * パラメータ名取得。
196
         *
197
         * @return パラメータ名
198
         */
199
        public String getParameterName() {
200
                return parameterName;
1✔
201
        }
202

203
        /**
204
         * パラメータ値取得。
205
         *
206
         * @return パラメータ値
207
         */
208
        public Object getValue() {
209
                return value;
1✔
210
        }
211

212
        /**
213
         * {@inheritDoc}
214
         *
215
         * @see java.lang.Object#toString()
216
         */
217
        @Override
218
        public String toString() {
219
                return "Parameter name[" + parameterName + "], Value[" + value + "], Class["
1✔
220
                                + (value == null ? "NULL" : value.getClass().getSimpleName())
1✔
221
                                + (Objects.equals(sqlType, SQL_TYPE_NOT_SET) ? "]" : "], SQL type[" + sqlType.getName() + "]");
1✔
222
        }
223

224
        /**
225
         * {@link java.sql.Types} の値を {@link java.sql.SQLType} に変換する
226
         * @param sqlType {@link java.sql.Types} の値
227
         * @return {@link java.sql.SQLType} の値
228
         */
229
        private SQLType toSqlType(final int sqlType) {
230
                for (var type : JDBCType.values()) {
1✔
231
                        if (type.getVendorTypeNumber().intValue() == sqlType) {
1✔
232
                                return type;
1✔
233
                        }
234
                }
235
                //下位互換のため、念のため生成して返す
236
                return new SQLType() {
×
237
                        @Override
238
                        public Integer getVendorTypeNumber() {
239
                                return sqlType;
×
240
                        }
241

242
                        @Override
243
                        public String getVendor() {
244
                                return "unknown";
×
245
                        }
246

247
                        @Override
248
                        public String getName() {
249
                                return "unknown name:" + sqlType;
×
250
                        }
251
                };
252
        }
253

254
        /**
255
         * PreparedStatementへのパラメータセット処理
256
         *
257
         * @param preparedStatement PreparedStatement
258
         * @param parameterIndex index
259
         * @param param オリジナルパラメータ
260
         * @param parameterMapperManager パラメータ変換管理クラス
261
         * @throws SQLException SQL例外
262
         */
263
        private void setParameterObject(final PreparedStatement preparedStatement, final int parameterIndex,
264
                        final Object param, final BindParameterMapperManager parameterMapperManager) throws SQLException {
265
                //JDBCの受け付ける型に変換
266
                var jdbcParam = parameterMapperManager.toJdbc(param, preparedStatement.getConnection());
1✔
267
                if (Objects.equals(sqlType, SQL_TYPE_NOT_SET)) {
1✔
268
                        if (jdbcParam instanceof java.sql.Array) {
1✔
269
                                preparedStatement.setArray(parameterIndex, (java.sql.Array) jdbcParam);
1✔
270
                        } else {
271
                                if (jdbcParam != null) {
1✔
272
                                        preparedStatement.setObject(parameterIndex, jdbcParam);
1✔
273
                                } else {
274
                                        preparedStatement.setNull(parameterIndex, JDBCType.NULL.getVendorTypeNumber());
1✔
275
                                }
276
                        }
277
                } else {
278
                        int targetSqlType = sqlType.getVendorTypeNumber();//各JDBCの対応状況が怪しいのでintで扱う
1✔
279
                        if (jdbcParam != null) {
1✔
280
                                if (jdbcParam instanceof java.sql.Array) {
1✔
281
                                        preparedStatement.setArray(parameterIndex, (java.sql.Array) jdbcParam);
1✔
282
                                } else {
283
                                        preparedStatement.setObject(parameterIndex, jdbcParam, targetSqlType);
1✔
284
                                }
285
                        } else {
286
                                preparedStatement.setNull(parameterIndex, targetSqlType);
1✔
287
                        }
288
                }
289
        }
1✔
290
}
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

© 2025 Coveralls, Inc