All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.curs.celesta.dbutils.h2.RecVersionCheckTrigger Maven / Gradle / Ivy

The newest version!
package ru.curs.celesta.dbutils.h2;

import org.h2.api.Trigger;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by ioann on 03.05.2017.
 */
public class RecVersionCheckTrigger implements Trigger {

    int indexOfRecVersionColumn;

    @Override
    public void init(Connection connection, String schemaName, String triggerName, String tableName,
                     boolean before, int type) throws SQLException {
        String sql = "SELECT ordinal_position - 1 "
                + "FROM information_schema.columns "
                + "WHERE table_schema = '%s' "
                + "AND table_name = '%s' "
                + "AND column_name = 'recversion'";
        sql = String.format(sql, schemaName, tableName);


        try (Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {

            if (resultSet.next()) {
                indexOfRecVersionColumn = resultSet.getInt(1);
            } else {
                throw new SQLException(String.format("Can't find recversion column for %s.%s", schemaName, tableName));
            }
        }
    }

    @Override
    public void fire(Connection connection, Object[] oldRow, Object[] newRow) throws SQLException {
        Integer oldRecVersion = (Integer) oldRow[indexOfRecVersionColumn];
        Integer newRecVersion = (Integer) newRow[indexOfRecVersionColumn];

        if (oldRecVersion.equals(newRecVersion)) {
            newRow[indexOfRecVersionColumn] = ++newRecVersion;
        } else {
            throw new SQLException("record version check failure");
        }
    }

    @Override
    public void close() {

    }

    @Override
    public void remove() {

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy