• 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

93.33
/src/main/java/jp/co/future/uroborosql/expr/spel/SpelExpressionParser.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.expr.spel;
8

9
import java.util.LinkedHashSet;
10
import java.util.Objects;
11
import java.util.Set;
12

13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.springframework.expression.EvaluationException;
16
import org.springframework.expression.spel.ExpressionState;
17
import org.springframework.expression.spel.SpelNode;
18
import org.springframework.expression.spel.ast.PropertyOrFieldReference;
19
import org.springframework.expression.spel.standard.SpelExpression;
20
import org.springframework.expression.spel.support.StandardEvaluationContext;
21

22
import jp.co.future.uroborosql.exception.ExpressionRuntimeException;
23
import jp.co.future.uroborosql.expr.AbstractExpressionParser;
24
import jp.co.future.uroborosql.expr.Expression;
25
import jp.co.future.uroborosql.utils.SqlFunction;
26

27
/**
28
 * SpringExpressionを利用した評価式パーサー
29
 *
30
 * @author H.Sugimoto
31
 */
32
public class SpelExpressionParser extends AbstractExpressionParser {
33
        /** パーサーロガー */
34
        private static final Logger PARSER_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.sql.parser");
1✔
35

36
        /** 評価式のパーサー */
37
        private static final org.springframework.expression.ExpressionParser parser = new org.springframework.expression.spel.standard.SpelExpressionParser();
1✔
38

39
        /** TransformContextに対するプロパティアクセサ */
40
        private TransformContextPropertyAccessor transformContextPropertyAccessor;
41

42
        /**
43
         * コンストラクタ
44
         */
45
        public SpelExpressionParser() {
1✔
46
        }
1✔
47

48
        /**
49
         * {@inheritDoc}
50
         *
51
         * @see jp.co.future.uroborosql.expr.AbstractExpressionParser#initialize()
52
         */
53
        @Override
54
        public void initialize() {
55
                super.initialize();
1✔
56
                transformContextPropertyAccessor = new TransformContextPropertyAccessor(
1✔
57
                                getSqlConfig().getDialect().getSqlFunction());
1✔
58
        }
1✔
59

60
        /**
61
         * {@inheritDoc}
62
         *
63
         * @see jp.co.future.uroborosql.expr.ExpressionParser#parse(java.lang.String)
64
         */
65
        @Override
66
        public Expression parse(final String expression) {
67
                return new SpringElExpression(parser.parseExpression(expression));
1✔
68
        }
69

70
        /**
71
         * SpEL評価式
72
         *
73
         * @author H.Sugimoto
74
         */
75
        private class SpringElExpression implements Expression {
76
                /** 評価式 */
77
                private final org.springframework.expression.Expression expr;
78

79
                /**
80
                 * コンストラクタ
81
                 *
82
                 * @param expr 評価式
83
                 */
84
                public SpringElExpression(final org.springframework.expression.Expression expr) {
1✔
85
                        this.expr = expr;
1✔
86
                }
1✔
87

88
                /**
89
                 * {@inheritDoc}
90
                 *
91
                 * @see jp.co.future.uroborosql.expr.Expression#getValue(java.lang.Object)
92
                 */
93
                @Override
94
                public Object getValue(final Object context) {
95
                        try {
96
                                var ctx = getEvaluationContext(context);
1✔
97
                                return expr.getValue(ctx);
1✔
NEW
98
                        } catch (EvaluationException ex) {
×
99
                                throw new ExpressionRuntimeException("Acquire an object failed.[" + expr.getExpressionString() + "]",
×
100
                                                ex);
101
                        }
102
                }
103

104
                /**
105
                 * StandardEvaluationContextの取得.
106
                 *
107
                 * @param context StandardEvaluationContextに設定するオブジェクト
108
                 * @return
109
                 */
110
                private StandardEvaluationContext getEvaluationContext(final Object context) {
111
                        var ctx = new StandardEvaluationContext(context);
1✔
112
                        ctx.addPropertyAccessor(transformContextPropertyAccessor);
1✔
113
                        return ctx;
1✔
114
                }
115

116
                /**
117
                 * {@inheritDoc}
118
                 *
119
                 * @see jp.co.future.uroborosql.expr.Expression#dumpNode(java.lang.Object)
120
                 */
121
                @Override
122
                public StringBuilder dumpNode(final Object context) {
123
                        var builder = new StringBuilder();
1✔
124
                        if (expr != null) {
1✔
125
                                var spel = (SpelExpression) expr;
1✔
126
                                var root = spel.getAST();
1✔
127
                                Set<PropertyOrFieldReference> props = new LinkedHashSet<>();
1✔
128
                                traverseNode(root, props);
1✔
129

130
                                var ctx = getEvaluationContext(context);
1✔
131
                                var state = new ExpressionState(ctx);
1✔
132
                                for (var prop : props) {
1✔
133
                                        var propName = prop.getName();
1✔
134
                                        if (!SqlFunction.SHORT_NAME.equals(propName)) {
1✔
135
                                                try {
136
                                                        var value = prop.getValue(state);
1✔
137
                                                        builder.append(propName)
1✔
138
                                                                        .append(":[")
1✔
139
                                                                        .append(Objects.toString(value, null))
1✔
140
                                                                        .append("],");
1✔
141
                                                } catch (EvaluationException ex) {
×
142
                                                        // ダンプ処理でシステムが止まっては困るのでログ出力して握りつぶす
NEW
143
                                                        PARSER_LOG.warn(ex.getMessage(), ex);
×
144
                                                }
1✔
145
                                        }
146
                                }
1✔
147
                        }
148
                        return builder;
1✔
149
                }
150

151
                /**
152
                 * {@inheritDoc}
153
                 *
154
                 * @see jp.co.future.uroborosql.expr.Expression#collectParams(java.util.Set)
155
                 */
156
                @Override
157
                public void collectParams(final Set<String> params) {
158
                        var spel = (SpelExpression) expr;
1✔
159
                        var root = spel.getAST();
1✔
160
                        Set<PropertyOrFieldReference> props = new LinkedHashSet<>();
1✔
161
                        traverseNode(root, props);
1✔
162
                        for (var prop : props) {
1✔
163
                                var propName = prop.getName();
1✔
164
                                if (!SqlFunction.SHORT_NAME.equals(propName)) {
1✔
165
                                        params.add(prop.getName());
1✔
166
                                }
167
                        }
1✔
168
                }
1✔
169

170
                /**
171
                 * 評価式の探索
172
                 *
173
                 * @param node ノード
174
                 * @param params プロパティが見つかった場合に格納するSetオブジェクト
175
                 */
176
                private void traverseNode(final SpelNode node, final Set<PropertyOrFieldReference> props) {
177
                        if (node == null) {
1✔
178
                        } else {
179
                                if (node instanceof PropertyOrFieldReference) {
1✔
180
                                        var prop = (PropertyOrFieldReference) node;
1✔
181
                                        props.add(prop);
1✔
182
                                } else {
1✔
183
                                        var childCount = node.getChildCount();
1✔
184
                                        for (var i = 0; i < childCount; i++) {
1✔
185
                                                var child = node.getChild(i);
1✔
186
                                                traverseNode(child, props);
1✔
187
                                        }
188
                                }
189
                        }
190
                }
1✔
191
        }
192
}
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