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

leeonky / test-charm-java / 150

07 Mar 2025 12:58AM UTC coverage: 74.287% (-0.08%) from 74.367%
150

push

circleci

leeonky
Try to fix ci

7919 of 10660 relevant lines covered (74.29%)

0.74 hits per line

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

94.83
/DAL-extension-jdbc/src/main/java/com/github/leeonky/dal/extensions/jdbc/DataBase.java
1
package com.github.leeonky.dal.extensions.jdbc;
2

3
import com.github.leeonky.dal.runtime.ProxyObject;
4
import com.github.leeonky.util.Suppressor;
5

6
import java.sql.Connection;
7
import java.sql.ResultSet;
8
import java.sql.ResultSetMetaData;
9
import java.sql.SQLException;
10
import java.util.*;
11
import java.util.function.Supplier;
12

13
public class DataBase implements AutoCloseable, ProxyObject {
14
    public final Connection connection;
15
    public final DataBaseBuilder builder;
16
    public Set<String> queriedTables = new LinkedHashSet<>();
1✔
17

18
    public DataBase(Connection connection, DataBaseBuilder builder) {
1✔
19
        this.connection = connection;
1✔
20
        this.builder = builder;
1✔
21
    }
1✔
22

23
    public Set<String> allTableNames() {
24
        return new LinkedHashSet<String>() {{
1✔
25
            addAll(builder.tablesProvider().apply(Suppressor.get(connection::createStatement)));
1✔
26
            addAll(queriedTables);
1✔
27
        }};
1✔
28
    }
29

30
    public Table<?> table(String name) {
31
        queriedTables.add(name);
1✔
32
        return new Table<>(name);
1✔
33
    }
34

35
    public String getUrl() {
36
        return Suppressor.get(() -> connection.getMetaData().getURL());
1✔
37
    }
38

39
    @Override
40
    public void close() throws Exception {
41
        connection.close();
×
42
    }
×
43

44
    @Override
45
    public Object getValue(Object property) {
46
        return table((String) property);
1✔
47
    }
48

49
    @Override
50
    public Set<?> getPropertyNames() {
51
        return allTableNames();
1✔
52
    }
53

54
    public class Table<T extends Table<T>> implements Iterable<Row<T>>, CanWhere<T> {
55
        private final String name;
56
        protected final Query query;
57

58
        public Table(String name) {
59
            this(name, new Query(name, name + ".*"));
1✔
60
        }
1✔
61

62
        private Table(String name, Query query) {
1✔
63
            this.name = name;
1✔
64
            this.query = query;
1✔
65
        }
1✔
66

67
        public String name() {
68
            return name;
1✔
69
        }
70

71
        @Override
72
        public Iterator<DataBase.Row<T>> iterator() {
73
            return Suppressor.get(() -> query(query()));
1✔
74
        }
75

76
        @SuppressWarnings("unchecked")
77
        protected T createInstance(Query query) {
78
            return (T) new Table<>(name, query);
1✔
79
        }
80

81
        protected Iterator<DataBase.Row<T>> query(Query query) throws SQLException {
82
            ResultSet resultSet = query.execute(connection);
1✔
83
            return new Iterator<DataBase.Row<T>>() {
1✔
84
                private final Set<String> columns = new LinkedHashSet<String>() {{
1✔
85
                    ResultSetMetaData metaData = resultSet.getMetaData();
1✔
86
                    int columnCount = metaData.getColumnCount();
1✔
87
                    for (int i = 1; i <= columnCount; ++i)
1✔
88
                        add(metaData.getColumnLabel(i).toLowerCase());
1✔
89
                }};
1✔
90

91
                @Override
92
                public boolean hasNext() {
93
                    return Suppressor.get(resultSet::next);
1✔
94
                }
95

96
                @Override
97
                public DataBase.Row<T> next() {
98
                    return Suppressor.get(this::getRow);
1✔
99
                }
100

101
                @SuppressWarnings("unchecked")
102
                private DataBase.Row<T> getRow() throws SQLException {
103
                    return new Row<>((T) Table.this, new LinkedHashMap<String, Object>() {{
1✔
104
                        for (String column : columns)
1✔
105
                            put(column, resultSet.getObject(column));
1✔
106
                    }});
1✔
107
                }
108
            };
109
        }
110

111
        public Query query() {
112
            return query;
1✔
113
        }
114

115
        public T select(String select) {
116
            return createInstance(query.select(select));
1✔
117
        }
118

119
        @Override
120
        public T where(String clause) {
121
            return createInstance(query.where(clause));
1✔
122
        }
123

124
        public T where(String clause, Map<String, Object> parameters) {
125
            return createInstance(query.where(clause).parameters(parameters));
×
126
        }
127
    }
128

129
    public class Row<T extends Table<T>> implements ProxyObject {
130
        protected final T table;
131
        protected Map<String, Object> data;
132

133
        public Row(T table, Map<String, Object> data) {
1✔
134
            this.table = table;
1✔
135
            this.data = data;
1✔
136
        }
1✔
137

138
        public Object value(String column) {
139
            if (data().containsKey(column))
1✔
140
                return data().get(column);
1✔
141
            return builder.callRowMethod(this, column);
1✔
142
        }
143

144
        public Set<String> columns() {
145
            return data().keySet();
1✔
146
        }
147

148
        public Map<String, Object> data() {
149
            return data;
1✔
150
        }
151

152
        public boolean empty() {
153
            return data() == null;
1✔
154
        }
155

156
        public LinkedRow belongsTo(String parentTable) {
157
            return join(parentTable).asParent().exactlyOne();
1✔
158
        }
159

160
        public LinkedTable join(String anotherTable) {
161
            return new LinkedTable(this, anotherTable);
1✔
162
        }
163

164
        public LinkedRow hasOne(String childTableOrManyToMany) {
165
            return join(childTableOrManyToMany).asChildren().exactlyOne();
1✔
166
        }
167

168
        public LinkedTable hasMany(String childTableOrManyToMany) {
169
            return join(childTableOrManyToMany).asChildren();
1✔
170
        }
171

172
        public T table() {
173
            return table;
1✔
174
        }
175

176
        @Override
177
        public Object getValue(Object property) {
178
            return value((String) property);
1✔
179
        }
180

181
        @Override
182
        public Set<?> getPropertyNames() {
183
            return columns();
×
184
        }
185

186
        @Override
187
        public boolean isNull() {
188
            return empty();
1✔
189
        }
190
    }
191

192
    public class LinkedTable extends Table<LinkedTable> implements Association<LinkedTable> {
193
        protected final Row<? extends Table<?>> row;
194

195
        public LinkedTable(Row<? extends Table<?>> row, String name) {
1✔
196
            super(name);
1✔
197
            this.row = row;
1✔
198
            query.parameters().putAll(row.table().query().parameters());
1✔
199
            query.parameters().putAll(row.data());
1✔
200
        }
1✔
201

202
        public LinkedTable(Row<? extends Table<?>> row, String name, Query query) {
1✔
203
            super(name, query);
1✔
204
            this.row = row;
1✔
205
        }
1✔
206

207
        public LinkedTable defaultParameterColumn(String parameterColumn) {
208
            return createInstance(query.defaultParameterColumn(parameterColumn));
1✔
209
        }
210

211
        public LinkedTable defaultLinkColumn(String linkColumn) {
212
            return createInstance(query.defaultLinkColumn(linkColumn));
1✔
213
        }
214

215
        @Override
216
        public LinkedTable on(String condition) {
217
            return createInstance(query.on(condition));
1✔
218
        }
219

220
        @Override
221
        protected LinkedTable createInstance(Query query) {
222
            return new LinkedTable(row, name(), query);
1✔
223
        }
224

225
        public DataBase.LinkedRow exactlyOne() {
226
            return new DataBase.LinkedRow(this, () -> {
1✔
227
                Iterator<Row<LinkedTable>> iterator = iterator();
1✔
228
                if (iterator.hasNext()) {
1✔
229
                    DataBase.Row<LinkedTable> row = iterator.next();
1✔
230
                    if (iterator.hasNext())
1✔
231
                        throw new RuntimeException("Result set has multiple records");
1✔
232
                    return row.data();
1✔
233
                }
234
                return null;
1✔
235
            });
236
        }
237

238
        public LinkedTable asParent() {
239
            return defaultLinkColumn(builder.resolveReferencedColumn(this, row.table()))
1✔
240
                    .defaultParameterColumn(builder.resolveJoinColumn(this, row.table()));
1✔
241
        }
242

243
        public LinkedTable asChildren() {
244
            return defaultLinkColumn(builder.resolveJoinColumn(row.table(), this))
1✔
245
                    .defaultParameterColumn(builder.resolveReferencedColumn(row.table(), this));
1✔
246
        }
247

248
        @Override
249
        public LinkedTable through(String table) {
250
            LinkedTable throughTable = row.join(table).asChildren();
1✔
251
            String joinColumn = builder.resolveJoinColumn(this, throughTable);
1✔
252
            return new LinkedThroughTable(this, throughTable.select(joinColumn));
1✔
253
        }
254

255
        @Override
256
        public LinkedTable through(String table, String joinColumn) {
257
            return new LinkedThroughTable(this, row.join(table).asChildren().select(joinColumn));
1✔
258
        }
259
    }
260

261
    public class LinkedThroughTable extends LinkedTable {
262
        private final LinkedTable thoughTable;
263
        private final String referencedColumn;
264

265
        public LinkedThroughTable(LinkedTable linkedTable, LinkedTable thoughTable) {
1✔
266
            super(linkedTable.row, linkedTable.name(), linkedTable.query().on(null));
1✔
267
            this.thoughTable = thoughTable;
1✔
268
            referencedColumn = linkedTable.query().linkColumn() == null ?
1✔
269
                    builder.resolveReferencedColumn(linkedTable, thoughTable) : linkedTable.query().linkColumn();
1✔
270
        }
1✔
271

272
        public LinkedThroughTable(LinkedTable thoughTable, String referencedColumn,
273
                                  Row<? extends Table<?>> row, String name, Query query) {
1✔
274
            super(row, name, query);
1✔
275
            this.thoughTable = thoughTable;
1✔
276
            this.referencedColumn = referencedColumn;
1✔
277
        }
1✔
278

279
        @Override
280
        protected Iterator<Row<LinkedTable>> query(Query query) throws SQLException {
281
            return super.query(query.where(String.format("%s in (%s)", referencedColumn, thoughTable.query().buildSql())));
1✔
282
        }
283

284
        @Override
285
        protected LinkedTable createInstance(Query query) {
286
            return new LinkedThroughTable(thoughTable, referencedColumn, row, name(), query);
×
287
        }
288

289
        @Override
290
        public LinkedTable on(String condition) {
291
            return new LinkedThroughTable(thoughTable.on(condition), referencedColumn, row, name(), query);
1✔
292
        }
293
    }
294

295
    public class LinkedRow extends Row<LinkedTable> implements CanWhere<LinkedRow>, Association<LinkedRow> {
296
        protected Supplier<Map<String, Object>> query;
297

298
        public LinkedRow(LinkedTable table, Supplier<Map<String, Object>> query) {
1✔
299
            super(table, null);
1✔
300
            this.query = query;
1✔
301
        }
1✔
302

303
        @Override
304
        public Map<String, Object> data() {
305
            if (data == null)
1✔
306
                data = query.get();
1✔
307
            return super.data();
1✔
308
        }
309

310
        @Override
311
        public LinkedRow where(String clause) {
312
            return table().where(clause).exactlyOne();
1✔
313
        }
314

315
        @Override
316
        public LinkedRow on(String condition) {
317
            return table().on(condition).exactlyOne();
1✔
318
        }
319

320
        @Override
321
        public LinkedRow through(String table) {
322
            return table().through(table).exactlyOne();
1✔
323
        }
324

325
        @Override
326
        public LinkedRow through(String table, String joinColumn) {
327
            return table().through(table, joinColumn).exactlyOne();
×
328
        }
329
    }
330
}
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

© 2026 Coveralls, Inc