OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.sql.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SQLUpdate {
    private List<SQLField> fields = new ArrayList<>();
    private List<Object> values = new ArrayList<>();
    private SQLTable table;
    private Where where;

    public SQLUpdate(Where where) {
        this.where = where;
    }

    public void add(SQLField field, Object value) {
        if (this.table == null) {
            this.table = field.getTable();
        } else {
            if (!this.table.equals(field.getTable())) {
                throw new IllegalArgumentException(field + " is not in table " + this.table.toString());
            }
        }
        this.fields.add(field);
        this.values.add(value);
    }

    public void set(SQLField field, Object value) {
        if (this.table == null) {
            this.table = field.getTable();
        } else {
            if (!this.table.equals(field.getTable())) {
                throw new IllegalArgumentException(field + " is not in table " + this.table.toString());
            }
        }
        int index = this.fields.indexOf(field);
        if (index < 0) {
            throw new IllegalArgumentException(field + " not in field list");
        }
        this.values.set(index, value);
    }

    public boolean contains(SQLField field) {
        return this.fields.indexOf(field) >= 0;
    }

    public Object getValue(SQLField field) {
        int index = this.fields.indexOf(field);
        if (index < 0) {
            throw new IllegalArgumentException(field + " not in field list");
        }
        return this.values.get(index);
    }

    public String asString() {
        if (this.fields.isEmpty()) {
            throw new IllegalStateException("not fields added");
        }
        final StringBuilder builder = new StringBuilder();
        builder.append("UPDATE ");
        builder.append(this.table.getSQLName());
        builder.append(" SET ");
        //
        int stop = this.fields.size();
        for (int i = 0; i < stop; i++) {
            final SQLField field = this.fields.get(i);
            builder.append(field.getQuotedName());
            builder.append("=");

            Object value = this.values.get(i);
            final Class<?> javaType = field.getType().getJavaType();
            Object str = value;
            if (!javaType.isInstance(value)) {
                str = SQLRowValues.convert(value.getClass(), value, javaType);
            }
            builder.append(field.getType().toString(str));
            if (i < stop - 1) {
                builder.append(',');
            }
        }
        builder.append(" WHERE ");
        builder.append(this.where.getClause());
        return builder.toString();
    }

    /**
     * Update multiple rows with a batch. Rows can be from different tables.
     * 
     * Speed : 5000 /s
     */
    public static long executeMultipleWithBatch(final DBSystemRoot sysRoot, final List<SQLUpdate> queries) throws SQLException {
        if (queries.isEmpty()) {
            throw new IllegalArgumentException("no updates");
        }
        final List<String> l = new ArrayList<>(queries.size());
        for (final SQLUpdate i : queries)
            l.add(i.asString());
        return sysRoot.getDataSource().executeBatch(l, true).get0();
    }

}