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

com.clickhouse.jdbc.JdbcParseHandler Maven / Gradle / Ivy

There is a newer version: 0.6.5
Show newest version
package com.clickhouse.jdbc;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.jdbc.parser.ClickHouseSqlStatement;
import com.clickhouse.jdbc.parser.ParseHandler;
import com.clickhouse.jdbc.parser.StatementType;

public class JdbcParseHandler extends ParseHandler {
    private static final String SETTING_MUTATIONS_SYNC = "mutations_sync";

    public static final ParseHandler INSTANCE = new JdbcParseHandler();

    private void addMutationSetting(String sql, StringBuilder builder, Map positions,
            Map settings, int index) {
        boolean hasSetting = settings != null && !settings.isEmpty();
        String setting = hasSetting ? settings.get(SETTING_MUTATIONS_SYNC) : null;
        if (setting == null) {
            String keyword = "SETTINGS";
            Integer settingsIndex = positions.get(keyword);

            if (settingsIndex == null) {
                builder.append(sql.substring(index)).append(" SETTINGS mutations_sync=1");
                if (hasSetting) {
                    builder.append(',');
                }
            } else {
                builder.append(sql.substring(index, settingsIndex)).append("SETTINGS mutations_sync=1,")
                        .append(sql.substring(settingsIndex + keyword.length()));
            }
        } else {
            builder.append(sql.substring(index));
        }
    }

    private ClickHouseSqlStatement handleDelete(String sql, StatementType stmtType, String cluster, String database,
            String table, String input, String format, String outfile, List parameters,
            Map positions, Map settings, Set tempTables) {
        StringBuilder builder = new StringBuilder();
        int index = positions.get("DELETE");
        if (index > 0) {
            builder.append(sql.substring(0, index));
        }
        index = positions.get("FROM");
        Integer whereIdx = positions.get("WHERE");
        if (whereIdx != null) {
            builder.append("ALTER TABLE ");
            if (!ClickHouseChecker.isNullOrEmpty(database)) {
                builder.append('`').append(database).append('`').append('.');
            }
            builder.append('`').append(table).append('`').append(" DELETE ");
            addMutationSetting(sql, builder, positions, settings, whereIdx);
        } else {
            builder.append("TRUNCATE TABLE").append(sql.substring(index + 4));
        }
        return new ClickHouseSqlStatement(builder.toString(), stmtType, cluster, database, table, input, format,
                outfile, parameters, null, settings, null);
    }

    private ClickHouseSqlStatement handleUpdate(String sql, StatementType stmtType, String cluster, String database,
            String table, String input, String format, String outfile, List parameters,
            Map positions, Map settings, Set tempTables) {
        StringBuilder builder = new StringBuilder();
        int index = positions.get("UPDATE");
        if (index > 0) {
            builder.append(sql.substring(0, index));
        }
        builder.append("ALTER TABLE ");
        index = positions.get("SET");
        if (!ClickHouseChecker.isNullOrEmpty(database)) {
            builder.append('`').append(database).append('`').append('.');
        }
        builder.append('`').append(table).append('`').append(" UPDATE"); // .append(sql.substring(index + 3));
        addMutationSetting(sql, builder, positions, settings, index + 3);
        return new ClickHouseSqlStatement(builder.toString(), stmtType, cluster, database, table, input, format,
                outfile, parameters, null, settings, null);
    }

    @Override
    public ClickHouseSqlStatement handleStatement(String sql, StatementType stmtType, String cluster, String database,
            String table, String input, String format, String outfile, List parameters,
            Map positions, Map settings, Set tempTables) {
        ClickHouseSqlStatement s = null;
        if (stmtType == StatementType.DELETE) {
            s = handleDelete(sql, stmtType, cluster, database, table, input, format, outfile, parameters, positions,
                    settings, tempTables);
        } else if (stmtType == StatementType.UPDATE) {
            s = handleUpdate(sql, stmtType, cluster, database, table, input, format, outfile, parameters, positions,
                    settings, tempTables);
        }
        return s;
    }

    private JdbcParseHandler() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy