OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
182 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.model;
15
 
16
import java.sql.ResultSet;
17
import java.sql.SQLException;
18
import java.util.ArrayList;
19
import java.util.Collections;
20
import java.util.HashMap;
21
import java.util.HashSet;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Objects;
25
import java.util.function.Function;
26
 
27
import org.apache.commons.dbutils.RowProcessor;
28
 
29
import net.jcip.annotations.Immutable;
30
 
31
@Immutable
32
public final class SQLFieldRowProcessor implements RowProcessor {
33
 
34
    private final List<String> names;
35
    private final int rsOffset;
36
    private Boolean uniqueNames;
37
 
38
    public SQLFieldRowProcessor(final SQLTable table, final List<String> fields) {
39
        this(0, fields, table::getField);
40
    }
41
 
42
    public SQLFieldRowProcessor(final List<? extends FieldRef> fields) {
43
        this(0, fields);
44
    }
45
 
46
    public SQLFieldRowProcessor(final int rsOffset, final List<? extends FieldRef> fields) {
47
        this(rsOffset, fields, FieldRef::getField);
48
    }
49
 
50
    private <F> SQLFieldRowProcessor(final int rsOffset, final List<? extends F> fields, final Function<? super F, SQLField> func) {
51
        final List<String> names = new ArrayList<>(fields.size());
52
        for (final F ref : fields) {
53
            if (ref == null) {
54
                names.add(null);
55
            } else {
56
                final SQLField f = func.apply(ref);
57
                names.add(f.getName());
58
            }
59
        }
60
        this.names = Collections.unmodifiableList(names);
61
        this.rsOffset = rsOffset;
62
        this.uniqueNames = null;
63
    }
64
 
65
    public final List<String> getFieldNames() {
66
        return this.names;
67
    }
68
 
69
    public final boolean areNamesUnique() {
70
        if (this.uniqueNames == null) {
71
            this.uniqueNames = new HashSet<>(this.names).size() == this.names.size();
72
        }
73
        return this.uniqueNames.booleanValue();
74
    }
75
 
76
    public final Object getObject(final ResultSet rs, final int rsIndex) throws SQLException {
77
        return rs.getObject(rsIndex + this.rsOffset);
78
    }
79
 
80
    @Override
81
    public Object[] toArray(ResultSet rs) throws SQLException {
82
        final int count = this.names.size();
83
        final Object[] result = new Object[count];
84
        for (int i = 0; i < count; i++) {
85
            result[i] = getObject(rs, i + 1);
86
        }
87
        return result;
88
    }
89
 
90
    @SuppressWarnings("rawtypes")
91
    @Override
92
    public Object toBean(ResultSet rs, Class type) throws SQLException {
93
        throw new UnsupportedOperationException();
94
    }
95
 
96
    @SuppressWarnings("rawtypes")
97
    @Override
98
    public List toBeanList(ResultSet rs, Class type) throws SQLException {
99
        throw new UnsupportedOperationException();
100
    }
101
 
102
    @Override
103
    public Map<String, Object> toMap(ResultSet rs) throws SQLException {
104
        final int count = this.names.size();
105
        final Map<String, Object> result = new HashMap<>();
106
        for (int i = 0; i < count; i++) {
107
            final String name = this.names.get(i);
108
            if (name != null) {
109
                result.put(name, getObject(rs, i + 1));
110
            }
111
        }
112
        return result;
113
    }
114
 
115
    @Override
116
    public int hashCode() {
117
        return Objects.hash(this.names, this.rsOffset);
118
    }
119
 
120
    @Override
121
    public boolean equals(Object obj) {
122
        if (this == obj)
123
            return true;
124
        if (obj == null)
125
            return false;
126
        if (getClass() != obj.getClass())
127
            return false;
128
        final SQLFieldRowProcessor other = (SQLFieldRowProcessor) obj;
129
        return this.rsOffset == other.rsOffset && this.names.equals(other.names);
130
    }
131
}