• 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.24
/src/main/java/jp/co/future/uroborosql/client/command/DescCommand.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.client.command;
8

9
import java.io.UnsupportedEncodingException;
10
import java.nio.charset.Charset;
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.Map;
15
import java.util.Objects;
16
import java.util.Properties;
17

18
import org.jline.reader.LineReader;
19
import org.jline.terminal.Terminal;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

23
import jp.co.future.uroborosql.client.completer.TableNameCompleter;
24
import jp.co.future.uroborosql.config.SqlConfig;
25
import jp.co.future.uroborosql.utils.ObjectUtils;
26

27
/**
28
 * Describe table Command
29
 *
30
 * @author H.Sugimoto
31
 */
32
public class DescCommand extends ReplCommand {
33
        /** REPLロガー */
34
        private static final Logger REPL_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.repl");
1✔
35

36
        /** DESCで表示する項目 */
37
        private static final String[] DESC_COLUMN_LABELS = { "TABLE_NAME", "COLUMN_NAME", "TYPE_NAME", "COLUMN_SIZE",
1✔
38
                        "DECIMAL_DIGITS", "IS_NULLABLE", "COLUMN_DEF", "REMARKS" };
39

40
        /**
41
         * Constructor
42
         */
43
        @SuppressWarnings("unchecked")
44
        public DescCommand() {
45
                super(false, TableNameCompleter.class);
1✔
46
        }
1✔
47

48
        /**
49
         * {@inheritDoc}
50
         *
51
         * @see jp.co.future.uroborosql.client.command.ReplCommand#execute(org.jline.reader.LineReader, java.lang.String[], jp.co.future.uroborosql.config.SqlConfig, java.util.Properties)
52
         */
53
        @Override
54
        public boolean execute(final LineReader reader, final String[] parts, final SqlConfig sqlConfig,
55
                        final Properties props) {
56
                var writer = reader.getTerminal().writer();
1✔
57

58
                var tableNamePattern = parts.length > 1 ? parts[parts.length - 1] : "%";
1✔
59

60
                try {
61
                        var conn = sqlConfig.getConnectionSupplier().getConnection();
1✔
62
                        var md = conn.getMetaData();
1✔
63

64
                        var columns = new ArrayList<Map<String, String>>();
1✔
65
                        var labelLength = new HashMap<String, Integer>();
1✔
66
                        for (var label : DESC_COLUMN_LABELS) {
1✔
67
                                labelLength.put(label, label.length());
1✔
68
                        }
69
                        try (var rs = md.getColumns(conn.getCatalog(), conn.getSchema(), tableNamePattern, null)) {
1✔
70
                                while (rs.next()) {
1✔
71
                                        var column = new HashMap<String, String>();
1✔
72
                                        for (var label : DESC_COLUMN_LABELS) {
1✔
73
                                                var value = Objects.toString(rs.getString(label), "");
1✔
74
                                                column.put(label, value);
1✔
75
                                                labelLength.compute(label,
1✔
76
                                                                (k, v) -> v == null ? getByteLength(value)
1✔
77
                                                                                : v.compareTo(getByteLength(value)) >= 0 ? v : getByteLength(value));
1✔
78
                                        }
79
                                        columns.add(column);
1✔
80
                                }
1✔
81
                        }
82

83
                        // ラベル
84
                        writer.print("-");
1✔
85
                        for (var label : DESC_COLUMN_LABELS) {
1✔
86
                                writer.print(ObjectUtils.rightPad("", labelLength.get(label), "-"));
1✔
87
                                writer.print("-");
1✔
88
                        }
89
                        writer.println();
1✔
90
                        writer.print("|");
1✔
91
                        for (var label : DESC_COLUMN_LABELS) {
1✔
92
                                writer.print(ObjectUtils.rightPad(label, labelLength.get(label)));
1✔
93
                                writer.print("|");
1✔
94
                        }
95
                        writer.println();
1✔
96
                        // カラムデータ
97
                        String tableName = null;
1✔
98
                        var breakFlag = false;
1✔
99
                        for (var column : columns) {
1✔
100
                                if (tableName == null || !tableName.equalsIgnoreCase(column.get("TABLE_NAME"))) {
1✔
101
                                        tableName = column.get("TABLE_NAME");
1✔
102
                                        breakFlag = true;
1✔
103
                                }
104
                                if (breakFlag) {
1✔
105
                                        writer.print("-");
1✔
106
                                        for (var label : DESC_COLUMN_LABELS) {
1✔
107
                                                writer.print(ObjectUtils.rightPad("", labelLength.get(label), "-"));
1✔
108
                                                writer.print("-");
1✔
109
                                        }
110
                                        writer.println();
1✔
111
                                        breakFlag = false;
1✔
112
                                }
113

114
                                writer.print("|");
1✔
115
                                for (var label : DESC_COLUMN_LABELS) {
1✔
116
                                        var val = column.get(label);
1✔
117
                                        if (ObjectUtils.isNumeric(val)) {
1✔
118
                                                writer.print(ObjectUtils.leftPad(val, labelLength.get(label)));
1✔
119
                                        } else {
120
                                                writer.print(ObjectUtils.rightPad(val, labelLength.get(label)));
1✔
121
                                        }
122
                                        writer.print("|");
1✔
123
                                }
124
                                writer.println();
1✔
125
                        }
1✔
126
                        writer.print("-");
1✔
127
                        for (var label : DESC_COLUMN_LABELS) {
1✔
128
                                writer.print(ObjectUtils.rightPad("", labelLength.get(label), "-"));
1✔
129
                                writer.print("-");
1✔
130
                        }
131
                        writer.println();
1✔
132
                } catch (SQLException ex) {
×
NEW
133
                        REPL_LOG.error(ex.getMessage(), ex);
×
134
                }
1✔
135
                writer.flush();
1✔
136

137
                return true;
1✔
138
        }
139

140
        /**
141
         * オブジェクトの文字列表現のバイト数(デフォルトエンコーディング)を取得する
142
         *
143
         * @param val 計算対象オブジェクト
144
         * @return バイト数
145
         */
146
        private int getByteLength(final Object val) {
147
                if (val == null) {
1✔
148
                        return 0;
×
149
                }
150
                var str = val.toString();
1✔
151
                try {
152
                        return str.getBytes(Charset.defaultCharset().displayName()).length;
1✔
153
                } catch (UnsupportedEncodingException ex) {
×
154
                        return 1;
×
155
                }
156
        }
157

158
        /**
159
         * {@inheritDoc}
160
         *
161
         * @see jp.co.future.uroborosql.client.command.ReplCommand#showHelp(org.jline.terminal.Terminal)
162
         */
163
        @Override
164
        public void showHelp(final Terminal terminal) {
165
                terminal.writer().println("\t" + this.toString() + "\t: describe table.");
1✔
166
                terminal.writer().println("\t\tex) desc [table name]<Enter> : Show table description.");
1✔
167
        }
1✔
168

169
}
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