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

future-architect / uroborosql / #785

25 Sep 2024 03:13PM UTC coverage: 90.073% (-0.5%) from 90.525%
#785

Pull #337

HidekiSugimoto189
revise log level.
Pull Request #337: revise log level.

25 of 36 new or added lines in 6 files covered. (69.44%)

36 existing lines in 8 files now uncovered.

8683 of 9640 relevant lines covered (90.07%)

0.9 hits per line

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

70.33
/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 jp.co.future.uroborosql.log.support.ServiceLoggingSupport;
17
import jp.co.future.uroborosql.log.support.SqlLoggingSupport;
18
import jp.co.future.uroborosql.parameter.mapper.BindParameterMapperManager;
19
import jp.co.future.uroborosql.utils.ObjectUtils;
20

21
/**
22
 * パラメータオブジェクト。<br>
23
 * SQLへバインドするパラメータを保持する。<br>
24
 *
25
 * @author H.Sugimoto
26
 */
27
public class Parameter implements ServiceLoggingSupport, SqlLoggingSupport {
28
        /** 未設定のSQLType */
29
        protected static final SQLType SQL_TYPE_NOT_SET = null;
1✔
30

31
        /**
32
         * パラメータ名
33
         */
34
        protected final String parameterName;
35

36
        /**
37
         * パラメータ値
38
         */
39
        protected final Object value;
40

41
        /**
42
         * SQL型
43
         */
44
        protected final SQLType sqlType;
45

46
        /**
47
         * コンストラクタ。
48
         *
49
         * @param parameterName パラメータ名
50
         * @param value 値
51
         */
52
        public Parameter(final String parameterName, final Object value) {
53
                this(parameterName, value, SQL_TYPE_NOT_SET);
1✔
54
        }
1✔
55

56
        /**
57
         * コンストラクタ。
58
         *
59
         * @param parameterName パラメータ名
60
         * @param value 値
61
         * @param sqlType {@link java.sql.SQLType} で表される型
62
         */
63
        public Parameter(final String parameterName, final Object value, final SQLType sqlType) {
1✔
64
                this.parameterName = parameterName;
1✔
65
                this.value = value;
1✔
66
                this.sqlType = sqlType;
1✔
67
        }
1✔
68

69
        /**
70
         * コンストラクタ。
71
         *
72
         * @param parameterName パラメータ名
73
         * @param value 値
74
         * @param sqlType {@link java.sql.Types} で表される型
75
         */
76
        public Parameter(final String parameterName, final Object value, final int sqlType) {
1✔
77
                this.parameterName = parameterName;
1✔
78
                this.value = value;
1✔
79
                this.sqlType = toSqlType(sqlType);
1✔
80
        }
1✔
81

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

131
                return new Parameter(subParameterName, subValue);
1✔
132

133
        }
134

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

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

165
                                parameterLog(parameterIndex);
1✔
166
                                parameterIndex++;
1✔
167
                        }
1✔
168
                } else {
169
                        setParameterObject(preparedStatement, parameterIndex, value, parameterMapperManager);
1✔
170

171
                        parameterLog(parameterIndex);
1✔
172
                        parameterIndex++;
1✔
173
                }
174

175
                return parameterIndex;
1✔
176
        }
177

178
        /**
179
         * パラメータ設定ログ出力。
180
         *
181
         * @param index パラメータインデックス
182
         */
183
        protected void parameterLog(final int index) {
184
                if (SQL_LOG.isDebugEnabled() && !isSuppressParameterLogging()) {
1✔
NEW
185
                        debugWith(SQL_LOG)
×
UNCOV
186
                                        .setMessage("Set the parameter.[INDEX[{}], {}]")
×
UNCOV
187
                                        .addArgument(index)
×
UNCOV
188
                                        .addArgument(this)
×
UNCOV
189
                                        .log();
×
190
                }
191
        }
1✔
192

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

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

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

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

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

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

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