Search in sources :

Example 36 with Out

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

the class TestCsv method testRead.

private void testRead() throws Exception {
    String fileName = getBaseDir() + "/test.csv";
    FileUtils.delete(fileName);
    OutputStream out = FileUtils.newOutputStream(fileName, false);
    byte[] b = ("a,b,c,d\n201,-2,0,18\n, \"abc\"\"\" ," + ",\"\"\n 1 ,2 , 3, 4 \n5, 6, 7, 8").getBytes();
    out.write(b, 0, b.length);
    out.close();
    ResultSet rs = new Csv().read(fileName, null, "UTF8");
    ResultSetMetaData meta = rs.getMetaData();
    assertEquals(4, meta.getColumnCount());
    assertEquals("A", meta.getColumnLabel(1));
    assertEquals("B", meta.getColumnLabel(2));
    assertEquals("C", meta.getColumnLabel(3));
    assertEquals("D", meta.getColumnLabel(4));
    assertTrue(rs.next());
    assertEquals("201", rs.getString(1));
    assertEquals("-2", rs.getString(2));
    assertEquals("0", rs.getString(3));
    assertEquals("18", rs.getString(4));
    assertTrue(rs.next());
    assertEquals(null, rs.getString(1));
    assertEquals("abc\"", rs.getString(2));
    assertEquals(null, rs.getString(3));
    assertEquals("", rs.getString(4));
    assertTrue(rs.next());
    assertEquals("1", rs.getString(1));
    assertEquals("2", rs.getString(2));
    assertEquals("3", rs.getString(3));
    assertEquals("4", rs.getString(4));
    assertTrue(rs.next());
    assertEquals("5", rs.getString(1));
    assertEquals("6", rs.getString(2));
    assertEquals("7", rs.getString(3));
    assertEquals("8", rs.getString(4));
    assertFalse(rs.next());
    // a,b,c,d
    // 201,-2,0,18
    // 201,2,0,18
    // 201,2,0,18
    // 201,2,0,18
    // 201,2,0,18
    // 201,2,0,18
    FileUtils.delete(fileName);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Csv(org.h2.tools.Csv) ResultSet(java.sql.ResultSet)

Example 37 with Out

use of org.h2.dev.util.BitStream.Out in project elastic-core-maven by OrdinaryDude.

the class Helper method executeQuery.

public static String executeQuery(String line) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baos);
    out.println(line);
    try {
        Shell shell = new Shell();
        shell.setErr(out);
        shell.setOut(out);
        shell.runTool(Db.db.getConnection(), "-sql", line);
    } catch (SQLException e) {
        out.println(e.toString());
    }
    return new String(baos.toByteArray());
}
Also used : PrintStream(java.io.PrintStream) Shell(org.h2.tools.Shell) SQLException(java.sql.SQLException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 38 with Out

use of org.h2.dev.util.BitStream.Out in project elastic-core-maven by OrdinaryDude.

the class DbShellServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
    resp.setHeader("Pragma", "no-cache");
    resp.setDateHeader("Expires", 0);
    if (!API.isAllowed(req.getRemoteHost())) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    String body = null;
    if (!API.disableAdminPassword) {
        if (API.adminPassword.isEmpty()) {
            body = errorNoPasswordIsConfigured;
        } else {
            try {
                API.verifyPassword(req);
                if ("true".equals(req.getParameter("showShell"))) {
                    body = form.replace("{adminPassword}", URLEncoder.encode(req.getParameter("adminPassword"), "UTF-8"));
                }
            } catch (ParameterException exc) {
                String desc = (String) ((JSONObject) JSONValue.parse(JSON.toString(exc.getErrorResponse()))).get("errorDescription");
                body = String.format(passwordFormTemplate, "<p style=\"color:red\">" + desc + "</p>");
            }
        }
    }
    if (body != null) {
        try (PrintStream out = new PrintStream(resp.getOutputStream())) {
            out.print(header);
            out.print(body);
            out.print(barter);
        }
        return;
    }
    String line = Convert.nullToEmpty(req.getParameter("line"));
    try (PrintStream out = new PrintStream(resp.getOutputStream())) {
        out.println("\n> " + line);
        try {
            Shell shell = new Shell();
            shell.setErr(out);
            shell.setOut(out);
            shell.runTool(Db.db.getConnection(), "-sql", line);
        } catch (SQLException e) {
            out.println(e.toString());
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Shell(org.h2.tools.Shell) JSONObject(org.json.simple.JSONObject) SQLException(java.sql.SQLException)

Example 39 with Out

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

the class Optimizer method calculateBruteForceSome.

private void calculateBruteForceSome() {
    int bruteForce = getMaxBruteForceFilters(filters.length);
    TableFilter[] list = new TableFilter[filters.length];
    Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce);
    for (int x = 0; !canStop(x) && p.next(); x++) {
        // find out what filters are not used yet
        for (TableFilter f : filters) {
            f.setUsed(false);
        }
        for (int i = 0; i < bruteForce; i++) {
            list[i].setUsed(true);
        }
        // fill the remaining elements with the unused elements (greedy)
        for (int i = bruteForce; i < filters.length; i++) {
            double costPart = -1.0;
            int bestPart = -1;
            for (int j = 0; j < filters.length; j++) {
                if (!filters[j].isUsed()) {
                    if (i == filters.length - 1) {
                        bestPart = j;
                        break;
                    }
                    list[i] = filters[j];
                    Plan part = new Plan(list, i + 1, condition);
                    double costNow = part.calculateCost(session);
                    if (costPart < 0 || costNow < costPart) {
                        costPart = costNow;
                        bestPart = j;
                    }
                }
            }
            filters[bestPart].setUsed(true);
            list[i] = filters[bestPart];
        }
        testPlan(list);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) Plan(org.h2.table.Plan)

Example 40 with Out

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

the class ScriptBase method openOutput.

/**
 * Open the output stream.
 */
void openOutput() {
    String file = getFileName();
    if (file == null) {
        return;
    }
    if (isEncrypted()) {
        initStore();
        out = new FileStoreOutputStream(store, this, compressionAlgorithm);
        // always use a big buffer, otherwise end-of-block is written a lot
        out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
    } else {
        OutputStream o;
        try {
            o = FileUtils.newOutputStream(file, false);
        } catch (IOException e) {
            throw DbException.convertIOException(e, null);
        }
        out = new BufferedOutputStream(o, Constants.IO_BUFFER_SIZE);
        out = CompressTool.wrapOutputStream(out, compressionAlgorithm, SCRIPT_SQL);
    }
}
Also used : FileStoreOutputStream(org.h2.store.FileStoreOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

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