Search in sources :

Example 1 with ISqlJetSchema

use of org.tmatesoft.sqljet.core.schema.ISqlJetSchema in project BibleMultiConverter by schierlm.

the class SQLiteDump method run.

@Override
public void run(String... args) throws Exception {
    SqlJetDb db = SqlJetDb.open(new File(args[0]), false);
    db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), StandardCharsets.UTF_8))) {
        ISqlJetOptions options = db.getOptions();
        bw.write("FLAG\tAutovacuum\t" + options.isAutovacuum());
        bw.newLine();
        bw.write("FLAG\tLegacyFileFormat\t" + options.isLegacyFileFormat());
        bw.newLine();
        bw.write("FLAG\tIncrementalVacuum\t" + options.isIncrementalVacuum());
        bw.newLine();
        bw.write("OPTION\tCacheSize\t" + options.getCacheSize());
        bw.newLine();
        bw.write("OPTION\tEncoding\t" + options.getEncoding());
        bw.newLine();
        bw.write("OPTION\tFileFormat\t" + options.getFileFormat());
        bw.newLine();
        bw.write("OPTION\tSchemaVersion\t" + options.getSchemaVersion());
        bw.newLine();
        bw.write("OPTION\tUserVersion\t" + options.getUserVersion());
        bw.newLine();
        ISqlJetSchema schema = db.getSchema();
        List<String> triggers = new ArrayList<>(schema.getTriggerNames());
        Collections.sort(triggers);
        for (String trigger : triggers) {
            bw.write("TRG\t" + trigger + "\t" + schema.getTrigger(trigger).toSQL());
            bw.newLine();
        }
        List<String> indexes = new ArrayList<>(schema.getIndexNames());
        Collections.sort(indexes);
        for (String index : indexes) {
            bw.write("IDX\t" + index + "\t" + schema.getIndex(index).toSQL());
            bw.newLine();
        }
        List<String> views = new ArrayList<>(schema.getViewNames());
        Collections.sort(views);
        for (String view : views) {
            bw.write("VIEW\t" + view + "\t" + schema.getView(view).toSQL());
            bw.newLine();
        }
        List<String> vTables = new ArrayList<>(schema.getVirtualTableNames());
        Collections.sort(vTables);
        for (String vtbl : vTables) {
            bw.write("VTBL\t" + vtbl + "\t" + schema.getVirtualTable(vtbl).toSQL());
            bw.newLine();
        }
        List<String> tables = new ArrayList<>(schema.getTableNames());
        Collections.sort(tables);
        for (String table : tables) {
            bw.write("TABLE\t" + table + "\t" + schema.getTable(table).toSQL());
            bw.newLine();
            ISqlJetTable tbl = db.getTable(table);
            String primaryIndex = tbl.getPrimaryKeyIndexName();
            ISqlJetCursor cursor = primaryIndex == null ? tbl.open() : tbl.order(primaryIndex);
            int count = cursor.getFieldsCount();
            while (!cursor.eof()) {
                for (int i = 0; i < count; i++) {
                    bw.write("  " + cursor.getFieldType(i).name().substring(0, 1) + "\t" + escape("" + cursor.getValue(i)));
                    bw.newLine();
                }
                bw.write("  -----");
                bw.newLine();
                cursor.next();
            }
            cursor.close();
        }
    }
    db.commit();
    db.close();
}
Also used : SqlJetDb(org.tmatesoft.sqljet.core.table.SqlJetDb) ArrayList(java.util.ArrayList) ISqlJetOptions(org.tmatesoft.sqljet.core.table.ISqlJetOptions) ISqlJetSchema(org.tmatesoft.sqljet.core.schema.ISqlJetSchema) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) ISqlJetTable(org.tmatesoft.sqljet.core.table.ISqlJetTable) OutputStreamWriter(java.io.OutputStreamWriter) ISqlJetCursor(org.tmatesoft.sqljet.core.table.ISqlJetCursor) File(java.io.File)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 ISqlJetSchema (org.tmatesoft.sqljet.core.schema.ISqlJetSchema)1 ISqlJetCursor (org.tmatesoft.sqljet.core.table.ISqlJetCursor)1 ISqlJetOptions (org.tmatesoft.sqljet.core.table.ISqlJetOptions)1 ISqlJetTable (org.tmatesoft.sqljet.core.table.ISqlJetTable)1 SqlJetDb (org.tmatesoft.sqljet.core.table.SqlJetDb)1