Search in sources :

Example 1 with TableDumpReport

use of org.entando.entando.aps.system.init.model.TableDumpReport in project entando-core by entando.

the class DatabaseDumper method dumpTableData.

protected void dumpTableData(String tableName, String dataSourceName, DataSource dataSource, DataSourceDumpReport report, String backupSubFolder) throws ApsSystemException {
    String filename = tableName + ".sql";
    File tempFile = null;
    FileWriter fileWriter = null;
    BufferedWriter bufferWriter = null;
    try {
        tempFile = this.createEmptyTempFile(filename);
        fileWriter = new FileWriter(tempFile.getAbsolutePath());
        bufferWriter = new BufferedWriter(fileWriter);
        TableDumpReport tableDumpReport = TableDataUtils.dumpTable(bufferWriter, dataSource, tableName);
        report.addTableReport(dataSourceName, tableDumpReport);
    } catch (IOException t) {
        _logger.error("Error dumping table '{}' - datasource '{}'", tableName, dataSourceName, t);
        throw new ApsSystemException("Error dumping table '" + tableName + "' - datasource '" + dataSourceName + "'", t);
    } finally {
        try {
            if (null != bufferWriter) {
                bufferWriter.close();
            }
            if (null != fileWriter) {
                fileWriter.close();
            }
        } catch (IOException t2) {
            _logger.error("Error closing FileWriter and BufferedWriter of file '{}'", filename, t2);
            throw new ApsSystemException("Error closing FileWriter and BufferedWriter", t2);
        }
    }
    this.finalizeDumpFile(filename, dataSourceName, backupSubFolder, tempFile);
}
Also used : FileWriter(java.io.FileWriter) TableDumpReport(org.entando.entando.aps.system.init.model.TableDumpReport) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 2 with TableDumpReport

use of org.entando.entando.aps.system.init.model.TableDumpReport in project entando-core by entando.

the class TableDataUtils method dumpTable.

public static TableDumpReport dumpTable(BufferedWriter br, DataSource dataSource, String tableName) throws ApsSystemException {
    TableDumpReport report = new TableDumpReport(tableName);
    StringBuilder scriptPrefix = new StringBuilder("INSERT INTO ").append(tableName).append(" (");
    Connection conn = null;
    Statement stat = null;
    ResultSet res = null;
    long start = System.currentTimeMillis();
    try {
        conn = dataSource.getConnection();
        String query = "SELECT * FROM " + tableName;
        stat = conn.createStatement();
        res = stat.executeQuery(query);
        ResultSetMetaData metaData = res.getMetaData();
        int columnCount = metaData.getColumnCount();
        int[] types = new int[columnCount];
        for (int i = 0; i < columnCount; i++) {
            int indexColumn = i + 1;
            if (metaData.isAutoIncrement(indexColumn)) {
                continue;
            }
            scriptPrefix.append(metaData.getColumnName(indexColumn).toLowerCase()).append(",");
            types[i] = metaData.getColumnType(indexColumn);
        }
        scriptPrefix.deleteCharAt(scriptPrefix.lastIndexOf(","));
        scriptPrefix.append(") VALUES (");
        int rows = 0;
        while (res.next()) {
            StringBuilder newRecord = new StringBuilder(scriptPrefix);
            for (int i = 0; i < columnCount; i++) {
                if (metaData.isAutoIncrement(i + 1)) {
                    continue;
                }
                Object value = getColumnValue(res, i, types);
                if (value == null) {
                    newRecord.append("NULL");
                } else {
                    String outputValue = value.toString();
                    outputValue = StringEscapeUtils.escapeSql(outputValue);
                    if (isDataNeedsQuotes(types[i])) {
                        newRecord.append("'").append(outputValue).append("'");
                    } else {
                        newRecord.append(outputValue);
                    }
                }
                newRecord.append(",");
            }
            newRecord.deleteCharAt(newRecord.lastIndexOf(","));
            newRecord.append(");\n");
            br.write(newRecord.toString());
            rows++;
        }
        report.setRows(rows);
    } catch (Throwable t) {
        _logger.error("Error creating backup", t);
        throw new ApsSystemException("Error creating backup", t);
    } finally {
        try {
            if (res != null) {
                res.close();
            }
        } catch (Throwable t) {
            _logger.error("Error while closing the resultset", t);
        }
        try {
            if (stat != null) {
                stat.close();
            }
        } catch (Throwable t) {
            _logger.error("Error while closing the statement", t);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Throwable t) {
            _logger.error("Error closing the connection", t);
        }
    }
    long time = System.currentTimeMillis() - start;
    report.setRequiredTime(time);
    return report;
}
Also used : Statement(java.sql.Statement) TableDumpReport(org.entando.entando.aps.system.init.model.TableDumpReport) Connection(java.sql.Connection) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet)

Aggregations

ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 TableDumpReport (org.entando.entando.aps.system.init.model.TableDumpReport)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1