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

future-architect / uroborosql / #768

10 Sep 2024 05:58PM UTC coverage: 90.35% (-0.9%) from 91.21%
#768

Pull #332

HidekiSugimoto189
Changed to use slf4j v2.0 API
Also added suppressLogging API
Pull Request #332: Enable per-SQL-ID log suppression (#322)

382 of 585 new or added lines in 32 files covered. (65.3%)

9 existing lines in 7 files now uncovered.

8885 of 9834 relevant lines covered (90.35%)

0.9 hits per line

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

88.71
/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.springframework.expression.EvaluationException;
14
import org.springframework.expression.spel.ExpressionState;
15
import org.springframework.expression.spel.SpelNode;
16
import org.springframework.expression.spel.ast.PropertyOrFieldReference;
17
import org.springframework.expression.spel.standard.SpelExpression;
18
import org.springframework.expression.spel.support.StandardEvaluationContext;
19

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

25
/**
26
 * SpringExpressionを利用した評価式パーサー
27
 *
28
 * @author H.Sugimoto
29
 */
30
public class SpelExpressionParser extends AbstractExpressionParser {
31
        /** 評価式のパーサー */
32
        private static final org.springframework.expression.ExpressionParser parser = new org.springframework.expression.spel.standard.SpelExpressionParser();
1✔
33

34
        /** TransformContextに対するプロパティアクセサ */
35
        private TransformContextPropertyAccessor transformContextPropertyAccessor;
36

37
        /**
38
         * コンストラクタ
39
         */
40
        public SpelExpressionParser() {
1✔
41
        }
1✔
42

43
        /**
44
         * {@inheritDoc}
45
         *
46
         * @see jp.co.future.uroborosql.expr.AbstractExpressionParser#initialize()
47
         */
48
        @Override
49
        public void initialize() {
50
                super.initialize();
1✔
51
                transformContextPropertyAccessor = new TransformContextPropertyAccessor(
1✔
52
                                getSqlConfig().getDialect().getSqlFunction());
1✔
53
        }
1✔
54

55
        /**
56
         * {@inheritDoc}
57
         *
58
         * @see jp.co.future.uroborosql.expr.ExpressionParser#parse(java.lang.String)
59
         */
60
        @Override
61
        public Expression parse(final String expression) {
62
                return new SpringElExpression(parser.parseExpression(expression));
1✔
63
        }
64

65
        /**
66
         * SpEL評価式
67
         *
68
         * @author H.Sugimoto
69
         */
70
        private class SpringElExpression implements Expression {
71
                /** 評価式 */
72
                private final org.springframework.expression.Expression expr;
73

74
                /**
75
                 * コンストラクタ
76
                 *
77
                 * @param expr 評価式
78
                 */
79
                public SpringElExpression(final org.springframework.expression.Expression expr) {
1✔
80
                        this.expr = expr;
1✔
81
                }
1✔
82

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

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

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

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

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

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