Search in sources :

Example 1 with HerdDBConnection

use of herddb.jdbc.HerdDBConnection in project herddb by diennea.

the class HerdDBCLI method performRestore.

private static void performRestore(String file, String leader, String newschema, Options options, final Statement statement, final Connection connection) throws SQLException, Exception {
    if (file.isEmpty()) {
        println("Please provide --file option");
        failAndPrintHelp(options);
        return;
    }
    Path inputfile = Paths.get(file).toAbsolutePath();
    if (leader.isEmpty() || newschema.isEmpty()) {
        println("options 'newleader' and 'newschema' are required");
        failAndPrintHelp(options);
        return;
    }
    List<String> nodes = new ArrayList<>();
    try (ResultSet rs = statement.executeQuery("SELECT nodeid FROM sysnodes")) {
        while (rs.next()) {
            String nodeid = rs.getString(1);
            nodes.add(nodeid);
        }
    }
    println("Restoring tablespace " + newschema + " with leader " + leader + " from file " + inputfile);
    if (!nodes.contains(leader)) {
        println("There is no node with node id '" + leader + "'");
        println("Valid nodes:");
        for (String nodeid : nodes) {
            println("* " + nodeid);
        }
        return;
    }
    try (InputStream fin = wrapStream(file, Files.newInputStream(inputfile));
        InputStream bin = new BufferedInputStream(fin, 16 * 1024 * 1024)) {
        HerdDBConnection hcon = connection.unwrap(HerdDBConnection.class);
        HDBConnection hdbconnection = hcon.getConnection();
        BackupUtils.restoreTableSpace(newschema, leader, hdbconnection, bin, new ProgressListener() {

            @Override
            public void log(String actionType, String message, Map<String, Object> context) {
                println(message);
            }
        });
    }
    println("Restore finished");
}
Also used : Path(java.nio.file.Path) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HerdDBConnection(herddb.jdbc.HerdDBConnection) HDBConnection(herddb.client.HDBConnection) ProgressListener(herddb.backup.ProgressListener) BufferedInputStream(java.io.BufferedInputStream) ResultSet(java.sql.ResultSet)

Example 2 with HerdDBConnection

use of herddb.jdbc.HerdDBConnection in project herddb by diennea.

the class HerdDBCLI method backupTableSpace.

private static void backupTableSpace(final Statement statement, String schema, String file, String suffix, final Connection connection, int dumpfetchsize) throws Exception, SQLException {
    List<String> tablesToDump = new ArrayList<>();
    try (ResultSet rs = statement.executeQuery("SELECT table_name" + " FROM " + schema + ".systables" + " WHERE systemtable='false'")) {
        while (rs.next()) {
            String tablename = rs.getString(1).toLowerCase();
            tablesToDump.add(tablename);
        }
    }
    int dot = file.lastIndexOf('.');
    String ext = "";
    if (dot >= 0) {
        ext = file.substring(dot);
        file = file.substring(0, dot);
    }
    String finalFile = (suffix == null ? file : file + suffix) + ext;
    Path outputfile = Paths.get(finalFile).toAbsolutePath();
    println("Backup tables " + tablesToDump + " from tablespace " + schema + " to " + outputfile);
    try (OutputStream fout = wrapOutputStream(Files.newOutputStream(outputfile, StandardOpenOption.CREATE_NEW), ext);
        SimpleBufferedOutputStream oo = new SimpleBufferedOutputStream(fout, 16 * 1024 * 1024)) {
        HerdDBConnection hcon = connection.unwrap(HerdDBConnection.class);
        HDBConnection hdbconnection = hcon.getConnection();
        BackupUtils.dumpTableSpace(schema, dumpfetchsize, hdbconnection, oo, new ProgressListener() {

            @Override
            public void log(String actionType, String message, Map<String, Object> context) {
                println(message);
            }
        });
    }
    println("Backup finished for tablespace " + schema);
}
Also used : Path(java.nio.file.Path) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HerdDBConnection(herddb.jdbc.HerdDBConnection) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream) HDBConnection(herddb.client.HDBConnection) ProgressListener(herddb.backup.ProgressListener) ResultSet(java.sql.ResultSet)

Example 3 with HerdDBConnection

use of herddb.jdbc.HerdDBConnection in project herddb by diennea.

the class HerdDBCLI method executeStatement.

private static ExecuteStatementResult executeStatement(boolean verbose, boolean ignoreerrors, boolean frommysqldump, boolean rewritestatements, String query, final Statement statement, final TableSpaceMapper tableSpaceMapper, boolean getResults, boolean prettyPrint) throws SQLException, ScriptException {
    query = query.trim();
    if (query.isEmpty() || query.startsWith("--")) {
        return null;
    }
    String formattedQuery = query.toLowerCase();
    if (formattedQuery.endsWith(";")) {
        // mysqldump
        formattedQuery = formattedQuery.substring(0, formattedQuery.length() - 1);
    }
    if (formattedQuery.equals("exit") || formattedQuery.equals("quit")) {
        System.out.println("Connection closed.");
        System.exit(0);
    }
    if (frommysqldump && (formattedQuery.startsWith("lock tables") || formattedQuery.startsWith("unlock tables"))) {
        // mysqldump
        return null;
    }
    Boolean setAutoCommit = null;
    if (formattedQuery.startsWith("autocommit=")) {
        String value = "";
        if (formattedQuery.split("=").length > 1) {
            value = formattedQuery.split("=")[1];
        }
        switch(value) {
            case "true":
                setAutoCommit = true;
                break;
            case "false":
                setAutoCommit = false;
                break;
            default:
                System.out.println("No valid value for autocommit. Only true and false allowed.");
                return null;
        }
    }
    if (verbose) {
        System.out.println("Executing query:" + query);
    }
    try {
        if (setAutoCommit != null) {
            statement.getConnection().setAutoCommit(setAutoCommit);
            System.out.println("Set autocommit=" + setAutoCommit + " executed.");
            return null;
        }
        if (formattedQuery.equals("commit")) {
            statement.getConnection().commit();
            System.out.println("Commit executed.");
            return null;
        }
        if (formattedQuery.equals("rollback")) {
            statement.getConnection().rollback();
            System.out.println("Rollback executed.");
            return null;
        }
        QueryWithParameters rewritten = null;
        if (rewritestatements) {
            rewritten = rewriteQuery(query, tableSpaceMapper, frommysqldump);
        }
        if (rewritten != null) {
            if (rewritten.schema != null) {
                HerdDBConnection connection = statement.getConnection().unwrap(HerdDBConnection.class);
                if (connection != null && !connection.getSchema().equalsIgnoreCase(rewritten.schema)) {
                    changeSchemaAndCommit(connection, rewritten.schema);
                }
            }
            try (PreparedStatement ps = statement.getConnection().prepareStatement(rewritten.query)) {
                int i = 1;
                for (Object o : rewritten.jdbcParameters) {
                    ps.setObject(i++, o);
                }
                boolean resultSet = ps.execute();
                return reallyExecuteStatement(ps, resultSet, verbose, getResults, prettyPrint);
            }
        } else {
            boolean resultSet = statement.execute(query);
            return reallyExecuteStatement(statement, resultSet, verbose, getResults, prettyPrint);
        }
    } catch (SQLException err) {
        if (ignoreerrors) {
            println("ERROR:" + err);
            return null;
        } else {
            throw err;
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) HerdDBConnection(herddb.jdbc.HerdDBConnection)

Aggregations

HerdDBConnection (herddb.jdbc.HerdDBConnection)3 ProgressListener (herddb.backup.ProgressListener)2 HDBConnection (herddb.client.HDBConnection)2 Path (java.nio.file.Path)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 SimpleBufferedOutputStream (herddb.utils.SimpleBufferedOutputStream)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1