Search in sources :

Example 46 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class RunScript method process.

private void process(Connection conn, boolean continueOnError, String path, Reader reader, Charset charset) throws SQLException, IOException {
    Statement stat = conn.createStatement();
    ScriptReader r = new ScriptReader(reader);
    while (true) {
        String sql = r.readStatement();
        if (sql == null) {
            break;
        }
        String trim = sql.trim();
        if (trim.length() == 0) {
            continue;
        }
        if (trim.startsWith("@") && StringUtils.toUpperEnglish(trim).startsWith("@INCLUDE")) {
            sql = trim;
            sql = sql.substring("@INCLUDE".length()).trim();
            if (!FileUtils.isAbsolute(sql)) {
                sql = path + SysProperties.FILE_SEPARATOR + sql;
            }
            process(conn, sql, continueOnError, charset);
        } else {
            try {
                if (showResults && !trim.startsWith("-->")) {
                    out.print(sql + ";");
                }
                if (showResults || checkResults) {
                    boolean query = stat.execute(sql);
                    if (query) {
                        ResultSet rs = stat.getResultSet();
                        int columns = rs.getMetaData().getColumnCount();
                        StringBuilder buff = new StringBuilder();
                        while (rs.next()) {
                            buff.append("\n-->");
                            for (int i = 0; i < columns; i++) {
                                String s = rs.getString(i + 1);
                                if (s != null) {
                                    s = StringUtils.replaceAll(s, "\r\n", "\n");
                                    s = StringUtils.replaceAll(s, "\n", "\n-->    ");
                                    s = StringUtils.replaceAll(s, "\r", "\r-->    ");
                                }
                                buff.append(' ').append(s);
                            }
                        }
                        buff.append("\n;");
                        String result = buff.toString();
                        if (showResults) {
                            out.print(result);
                        }
                        if (checkResults) {
                            String expected = r.readStatement() + ";";
                            expected = StringUtils.replaceAll(expected, "\r\n", "\n");
                            expected = StringUtils.replaceAll(expected, "\r", "\n");
                            if (!expected.equals(result)) {
                                expected = StringUtils.replaceAll(expected, " ", "+");
                                result = StringUtils.replaceAll(result, " ", "+");
                                throw new SQLException("Unexpected output for:\n" + sql.trim() + "\nGot:\n" + result + "\nExpected:\n" + expected);
                            }
                        }
                    }
                } else {
                    stat.execute(sql);
                }
            } catch (Exception e) {
                if (continueOnError) {
                    e.printStackTrace(out);
                } else {
                    throw DbException.toSQLException(e);
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ScriptReader(org.h2.util.ScriptReader) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 47 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class Csv method initWrite.

private void initWrite() throws IOException {
    if (output == null) {
        try {
            OutputStream out = FileUtils.newOutputStream(fileName, false);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE);
            output = new BufferedWriter(new OutputStreamWriter(out, characterSet));
        } catch (Exception e) {
            close();
            throw DbException.convertToIOException(e);
        }
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BufferedOutputStream(java.io.BufferedOutputStream) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 48 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class SourceCompiler method javacCompile.

/**
 * Compile the given class. This method tries to use the class
 * "com.sun.tools.javac.Main" if available. If not, it tries to run "javac"
 * in a separate process.
 *
 * @param packageName the package name
 * @param className the class name
 * @param source the source code
 * @return the class file
 */
byte[] javacCompile(String packageName, String className, String source) {
    File dir = new File(COMPILE_DIR);
    if (packageName != null) {
        dir = new File(dir, packageName.replace('.', '/'));
        FileUtils.createDirectories(dir.getAbsolutePath());
    }
    File javaFile = new File(dir, className + ".java");
    File classFile = new File(dir, className + ".class");
    try {
        OutputStream f = FileUtils.newOutputStream(javaFile.getAbsolutePath(), false);
        Writer out = IOUtils.getBufferedWriter(f);
        classFile.delete();
        out.write(source);
        out.close();
        if (JAVAC_SUN != null) {
            javacSun(javaFile);
        } else {
            javacProcess(javaFile);
        }
        byte[] data = new byte[(int) classFile.length()];
        DataInputStream in = new DataInputStream(new FileInputStream(classFile));
        in.readFully(data);
        in.close();
        return data;
    } catch (Exception e) {
        throw DbException.convert(e);
    } finally {
        javaFile.delete();
        classFile.delete();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DataInputStream(java.io.DataInputStream) File(java.io.File) StringWriter(java.io.StringWriter) Writer(java.io.Writer) FileInputStream(java.io.FileInputStream) DbException(org.h2.message.DbException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 49 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class SourceCompiler method exec.

private static int exec(String... args) {
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    try {
        ProcessBuilder builder = new ProcessBuilder();
        // The javac executable allows some of it's flags
        // to be smuggled in via environment variables.
        // But if it sees those flags, it will write out a message
        // to stderr, which messes up our parsing of the output.
        builder.environment().remove("JAVA_TOOL_OPTIONS");
        builder.command(args);
        Process p = builder.start();
        copyInThread(p.getInputStream(), buff);
        copyInThread(p.getErrorStream(), buff);
        p.waitFor();
        String output = new String(buff.toByteArray(), StandardCharsets.UTF_8);
        handleSyntaxError(output);
        return p.exitValue();
    } catch (Exception e) {
        throw DbException.convert(e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DbException(org.h2.message.DbException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 50 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class ValueLob method createFromReader.

private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException {
    try (FileStoreOutputStream out = initLarge(h)) {
        boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null;
        while (true) {
            precision += len;
            byte[] b = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
            out.write(b, 0, b.length);
            remaining -= len;
            if (remaining <= 0) {
                break;
            }
            len = getBufferSize(h, compress, remaining);
            len = IOUtils.readFully(in, buff, len);
            if (len == 0) {
                break;
            }
        }
    }
}
Also used : FileStoreOutputStream(org.h2.store.FileStoreOutputStream)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4