Sha256: ac0a0d7a91c02949b9bea5a1214cca983583a4c26c0cb72ed0f781dca88100bf

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

package org.embulk.input.postgresql;

import org.embulk.test.EmbulkTests;
import com.google.common.base.Throwables;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import jnr.ffi.Platform;
import jnr.ffi.Platform.OS;
import org.embulk.config.ConfigSource;
import static java.util.Locale.ENGLISH;

public class PostgreSQLTests
{
    public static ConfigSource baseConfig()
    {
        return EmbulkTests.config("EMBULK_INPUT_POSTGRESQL_TEST_CONFIG");
    }

    public static void execute(String sql)
    {
        ConfigSource config = baseConfig();
        ProcessBuilder pb = new ProcessBuilder("psql", "-w", "--set", "ON_ERROR_STOP=1", "-c", convert(sql));
        pb.environment().put("PGUSER", config.get(String.class, "user"));
        pb.environment().put("PGPASSWORD", config.get(String.class, "password"));
        pb.environment().put("PGDATABASE", config.get(String.class, "database"));
        pb.environment().put("PGPORT", config.get(String.class, "port", "5432"));
        pb.redirectErrorStream(true);
        int code;
        try {
            Process process = pb.start();
            ByteStreams.copy(process.getInputStream(), System.out);
            code = process.waitFor();
        } catch (IOException | InterruptedException ex) {
            throw Throwables.propagate(ex);
        }
        if (code != 0) {
            throw new RuntimeException(String.format(ENGLISH,
                        "Command finished with non-zero exit code. Exit code is %d.", code));
        }
    }

    private static String convert(String sql)
    {
        if (Platform.getNativePlatform().getOS().equals(OS.WINDOWS)) {
            // '"' should be '\"' is Windows
            return sql.replace("\"", "\\\"");
        }
        return sql;
    }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
embulk-input-postgresql-0.9.0 src/test/java/org/embulk/input/postgresql/PostgreSQLTests.java
embulk-input-postgresql-0.8.6 src/test/java/org/embulk/input/postgresql/PostgreSQLTests.java