package cz.moneta.test.harness.connectors.database;

import cz.moneta.test.harness.connectors.Connector;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnector implements Connector {

    private final String url;
    private final String user;
    private final String password;

    public DatabaseConnector(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    protected Result<Record> executeSql(String sql, SQLDialect sqlDialect) {
        try (Connection connection = DriverManager.getConnection(getUrl(), getUser(), getPassword())) {
            return DSL.using(connection, sqlDialect).fetch(sql);
        } catch (SQLException e) {
            throw new RuntimeException("Error executing native sql database", e);
        }
    }

    public String getUrl() {
        return url;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }
}
