OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 142 Rev 174
Line 17... Line 17...
17
import org.openconcerto.utils.Tuple2;
17
import org.openconcerto.utils.Tuple2;
18
 
18
 
19
import java.sql.ResultSet;
19
import java.sql.ResultSet;
20
import java.sql.SQLException;
20
import java.sql.SQLException;
21
import java.util.ArrayList;
21
import java.util.ArrayList;
-
 
22
import java.util.Collection;
22
import java.util.Collections;
23
import java.util.Collections;
23
import java.util.HashSet;
24
import java.util.HashSet;
24
import java.util.List;
25
import java.util.List;
25
import java.util.Set;
26
import java.util.Set;
-
 
27
import java.util.stream.Collectors;
26
 
28
 
27
import org.apache.commons.dbutils.ResultSetHandler;
29
import org.apache.commons.dbutils.ResultSetHandler;
28
 
30
 
29
public final class SQLRowListRSH implements ResultSetHandler {
31
public final class SQLRowListRSH implements ResultSetHandler {
30
 
32
 
Line 35... Line 37...
35
        // allow to create rows from arbitrary columns (and not just directly from actual fields of
37
        // allow to create rows from arbitrary columns (and not just directly from actual fields of
36
        // the same table)
38
        // the same table)
37
        // ATTN doesn't check that the types of columns are coherent with the types of the fields
39
        // ATTN doesn't check that the types of columns are coherent with the types of the fields
38
        public RSH(final SQLTable t, final List<String> names) {
40
        public RSH(final SQLTable t, final List<String> names) {
39
            this(Tuple2.create(t, names));
41
            this(Tuple2.create(t, names));
-
 
42
            // null are OK (they're ignored)
-
 
43
            final List<String> unknown = names.stream().filter(n -> n != null && !t.getFieldsName().contains(n)).collect(Collectors.toList());
40
            if (!t.getFieldsName().containsAll(names))
44
            if (!unknown.isEmpty())
41
                throw new IllegalArgumentException("Not all names are fields of " + t + " : " + names);
45
                throw new IllegalArgumentException("Not all names are fields of " + t + " : " + unknown);
42
        }
46
        }
43
 
47
 
44
        private RSH(Tuple2<SQLTable, List<String>> names) {
48
        private RSH(final Tuple2<SQLTable, List<String>> names) {
45
            this.names = names;
49
            this.names = names;
46
        }
50
        }
47
 
51
 
48
        @Override
52
        @Override
49
        public List<SQLRow> handle(ResultSet rs) throws SQLException {
53
        public List<SQLRow> handle(final ResultSet rs) throws SQLException {
50
            // since the result will be cached, disallow its modification (e.g.avoid
54
            // since the result will be cached, disallow its modification (e.g.avoid
51
            // ConcurrentModificationException)
55
            // ConcurrentModificationException)
52
            return Collections.unmodifiableList(SQLRow.createListFromRS(this.names.get0(), rs, this.names.get1()));
56
            return Collections.unmodifiableList(SQLRow.createListFromRS(this.names.get0(), rs, this.names.get1()));
53
        }
57
        }
54
 
58
 
Line 56... Line 60...
56
        public int hashCode() {
60
        public int hashCode() {
57
            return this.names.hashCode();
61
            return this.names.hashCode();
58
        }
62
        }
59
 
63
 
60
        @Override
64
        @Override
61
        public boolean equals(Object obj) {
65
        public boolean equals(final Object obj) {
62
            if (this == obj)
66
            if (this == obj)
63
                return true;
67
                return true;
64
            if (obj == null)
68
            if (obj == null)
65
                return false;
69
                return false;
66
            if (getClass() != obj.getClass())
70
            if (getClass() != obj.getClass())
Line 76... Line 80...
76
        if (!t.getTable().isRowable())
80
        if (!t.getTable().isRowable())
77
            throw new IllegalArgumentException("table isn't rowable : " + t);
81
            throw new IllegalArgumentException("table isn't rowable : " + t);
78
        return t;
82
        return t;
79
    }
83
    }
80
 
84
 
81
    static Tuple2<SQLTable, List<String>> getIndexes(SQLSelect sel, final TableRef passedTable, final boolean findTable) {
85
    static Tuple2<SQLTable, List<String>> getIndexes(final SQLSelect sel, final TableRef passedTable, final boolean findTable) {
82
        final List<FieldRef> selectFields = sel.getSelectFields();
86
        final List<FieldRef> selectFields = sel.getSelectFields();
83
        final int size = selectFields.size();
87
        final int size = selectFields.size();
84
        if (size == 0)
88
        if (size == 0)
85
            throw new IllegalArgumentException("empty select : " + sel);
89
            throw new IllegalArgumentException("empty select : " + sel);
86
        TableRef t;
90
        TableRef t;
Line 122... Line 126...
122
     * 
126
     *
123
     * @param sel the select that will produce the result set, must only have one table.
127
     * @param sel the select that will produce the result set, must only have one table.
124
     * @return a handler creating a list of {@link SQLRow}.
128
     * @return a handler creating a list of {@link SQLRow}.
125
     * @deprecated use {@link SQLSelectHandlerBuilder}
129
     * @deprecated use {@link SQLSelectHandlerBuilder}
126
     */
130
     */
-
 
131
    @Deprecated
127
    static public ResultSetHandler createFromSelect(final SQLSelect sel) {
132
    static public ResultSetHandler createFromSelect(final SQLSelect sel) {
128
        return create(getIndexes(sel, null, true));
133
        return create(getIndexes(sel, null, true));
129
    }
134
    }
130
 
135
 
131
    /**
136
    /**
Line 135... Line 140...
135
     * @param sel the select that will produce the result set.
140
     * @param sel the select that will produce the result set.
136
     * @param t the table for which to create rows.
141
     * @param t the table for which to create rows.
137
     * @return a handler creating a list of {@link SQLRow}.
142
     * @return a handler creating a list of {@link SQLRow}.
138
     * @deprecated use {@link SQLSelectHandlerBuilder}
143
     * @deprecated use {@link SQLSelectHandlerBuilder}
139
     */
144
     */
-
 
145
    @Deprecated
140
    static public ResultSetHandler createFromSelect(final SQLSelect sel, final TableRef t) {
146
    static public ResultSetHandler createFromSelect(final SQLSelect sel, final TableRef t) {
141
        return create(getIndexes(sel, t, false));
147
        return create(getIndexes(sel, t, false));
142
    }
148
    }
143
 
149
 
144
    static ResultSetHandler create(final Tuple2<SQLTable, List<String>> names) {
150
    static ResultSetHandler create(final Tuple2<SQLTable, List<String>> names) {
145
        return new RSH(names);
151
        return new RSH(names);
146
    }
152
    }
147
 
153
 
-
 
154
    static public List<SQLRow> fetch(final SQLTable t, final Collection<? extends Number> ids) throws IllegalArgumentException {
-
 
155
        return fetch(t, ids, null);
-
 
156
    }
-
 
157
 
-
 
158
    static public List<SQLRow> fetch(final SQLTable t, final Collection<? extends Number> ids, final Collection<String> fields) throws IllegalArgumentException {
-
 
159
        final SQLSelect sel = new SQLSelect();
-
 
160
        if (fields == null)
-
 
161
            sel.addSelectStar(t);
-
 
162
        else
-
 
163
            sel.addAllSelect(t, fields);
-
 
164
        sel.setWhere(new Where(t.getKey(), ids));
-
 
165
        return execute(sel);
-
 
166
    }
-
 
167
 
148
    /**
168
    /**
149
     * Execute the passed select and return rows. NOTE if there's more than one table in the query
169
     * Execute the passed select and return rows. NOTE if there's more than one table in the query
150
     * {@link #execute(SQLSelect, TableRef)} must be used.
170
     * {@link #execute(SQLSelect, TableRef)} must be used.
151
     * 
171
     *
152
     * @param sel the query to execute.
172
     * @param sel the query to execute.
Line 193... Line 213...
193
    }
213
    }
194
 
214
 
195
    private final SQLTable t;
215
    private final SQLTable t;
196
    private final boolean tableOnly;
216
    private final boolean tableOnly;
197
 
217
 
198
    public SQLRowListRSH(SQLTable t) {
218
    public SQLRowListRSH(final SQLTable t) {
199
        this(t, false);
219
        this(t, false);
200
    }
220
    }
201
 
221
 
202
    public SQLRowListRSH(SQLTable t, final boolean tableOnly) {
222
    public SQLRowListRSH(final SQLTable t, final boolean tableOnly) {
203
        super();
223
        super();
204
        this.t = t;
224
        this.t = t;
205
        this.tableOnly = tableOnly;
225
        this.tableOnly = tableOnly;
206
    }
226
    }
207
 
227
 
-
 
228
    @Override
208
    public Object handle(ResultSet rs) throws SQLException {
229
    public List<SQLRow> handle(final ResultSet rs) throws SQLException {
209
        return SQLRow.createListFromRS(this.t, rs, this.tableOnly);
230
        return SQLRow.createListFromRS(this.t, rs, this.tableOnly);
210
    }
231
    }
211
}
232
}