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
174 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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.SQLException;
17
import java.util.ArrayList;
18
import java.util.List;
19
 
20
public class SQLUpdate {
21
    private List<SQLField> fields = new ArrayList<>();
22
    private List<Object> values = new ArrayList<>();
23
    private SQLTable table;
24
    private Where where;
25
 
26
    public SQLUpdate(Where where) {
27
        this.where = where;
28
    }
29
 
30
    public void add(SQLField field, Object value) {
31
        if (this.table == null) {
32
            this.table = field.getTable();
33
        } else {
34
            if (!this.table.equals(field.getTable())) {
35
                throw new IllegalArgumentException(field + " is not in table " + this.table.toString());
36
            }
37
        }
38
        this.fields.add(field);
39
        this.values.add(value);
40
    }
41
 
42
    public void set(SQLField field, Object value) {
43
        if (this.table == null) {
44
            this.table = field.getTable();
45
        } else {
46
            if (!this.table.equals(field.getTable())) {
47
                throw new IllegalArgumentException(field + " is not in table " + this.table.toString());
48
            }
49
        }
50
        int index = this.fields.indexOf(field);
51
        if (index < 0) {
52
            throw new IllegalArgumentException(field + " not in field list");
53
        }
54
        this.values.set(index, value);
55
    }
56
 
57
    public boolean contains(SQLField field) {
58
        return this.fields.indexOf(field) >= 0;
59
    }
60
 
61
    public Object getValue(SQLField field) {
62
        int index = this.fields.indexOf(field);
63
        if (index < 0) {
64
            throw new IllegalArgumentException(field + " not in field list");
65
        }
66
        return this.values.get(index);
67
    }
68
 
69
    public String asString() {
70
        if (this.fields.isEmpty()) {
71
            throw new IllegalStateException("not fields added");
72
        }
73
        final StringBuilder builder = new StringBuilder();
74
        builder.append("UPDATE ");
75
        builder.append(this.table.getSQLName());
76
        builder.append(" SET ");
77
        //
78
        int stop = this.fields.size();
79
        for (int i = 0; i < stop; i++) {
80
            final SQLField field = this.fields.get(i);
81
            builder.append(field.getQuotedName());
82
            builder.append("=");
83
 
84
            Object value = this.values.get(i);
85
            final Class<?> javaType = field.getType().getJavaType();
86
            Object str = value;
87
            if (!javaType.isInstance(value)) {
88
                str = SQLRowValues.convert(value.getClass(), value, javaType);
89
            }
90
            builder.append(field.getType().toString(str));
91
            if (i < stop - 1) {
92
                builder.append(',');
93
            }
94
        }
95
        builder.append(" WHERE ");
96
        builder.append(this.where.getClause());
97
        return builder.toString();
98
    }
99
 
100
    /**
101
     * Update multiple rows with a batch. Rows can be from different tables.
102
     *
103
     * Speed : 5000 /s
104
     */
105
    public static long executeMultipleWithBatch(final DBSystemRoot sysRoot, final List<SQLUpdate> queries) throws SQLException {
106
        if (queries.isEmpty()) {
107
            throw new IllegalArgumentException("no updates");
108
        }
109
        final List<String> l = new ArrayList<>(queries.size());
110
        for (final SQLUpdate i : queries)
111
            l.add(i.asString());
112
        return sysRoot.getDataSource().executeBatch(l, true).get0();
113
    }
114
 
115
}