private void loadTableData(
      final File dbFile, final TableViewer guiTableViewer, final String dbTableName) {
    SqlJetDb db = null;
    try {
      db = SqlJetDb.open(dbFile, true);
      final ISqlJetTable dbTable = db.getTable(dbTableName);
      final ArrayList<DataRow> data = new ArrayList<DataRow>();
      final ISqlJetTableDef tableDef = dbTable.getDefinition();
      final List<String> names = new ArrayList<String>();
      for (final ISqlJetColumnDef column : tableDef.getColumns()) {
        names.add(column.getName());
      }
      final String[] namesArray = names.toArray(new String[names.size()]);

      final Table guiTable = guiTableViewer.getTable();
      createGuiTableColumns(guiTable, namesArray);

      dbTable
          .getDataBase()
          .runReadTransaction(
              new ISqlJetTransaction() {
                @Override
                public Object run(final SqlJetDb db) throws SqlJetException {
                  final ISqlJetCursor cursor = dbTable.open();
                  try {
                    int count = 0;
                    while (!cursor.eof()) {
                      data.add(DataRow.read(cursor, count, namesArray));
                      cursor.next();
                      count++;
                    }
                  } finally {
                    cursor.close();
                  }
                  return null;
                }
              });
      // Populate data and refresh table viewer
      guiTableViewer.setInput(data);
      guiTableViewer.refresh();
    } catch (final Exception e) {
      e.printStackTrace();
    } finally {
      if (db != null) {
        try {
          db.close();
        } catch (final SqlJetException e) {
          e.printStackTrace();
        }
      }
    }
  }
Пример #2
0
  public static TableModel createInstance(
      final ISqlJetTable table, final long fromID, final int pageSize, final IProgress progress)
      throws SqlJetException {
    if (table == null) {
      return new DefaultTableModel();
    }
    final ArrayList<DataRow> data = new ArrayList<DataRow>(pageSize);
    final ISqlJetTableDef tableDef = table.getDefinition();
    final List<String> names = new ArrayList<String>();
    for (ISqlJetColumnDef column : tableDef.getColumns()) {
      names.add(column.getName());
    }
    final String[] namesArray = (String[]) names.toArray(new String[names.size()]);

    table
        .getDataBase()
        .runReadTransaction(
            new ISqlJetTransaction() {
              public Object run(SqlJetDb db) throws SqlJetException {
                ISqlJetCursor cursor = table.open(); // order(table.getPrimaryKeyIndexName());
                try {
                  for (long i = 0; i < fromID && !cursor.eof(); i++) {
                    cursor.next();
                  }
                  int count = 0;
                  while (!cursor.eof() && count < pageSize) {
                    data.add(DataRow.read(cursor, fromID + count, namesArray));
                    progress.current(count);
                    cursor.next();
                    count++;
                  }
                } finally {
                  cursor.close();
                }
                return null;
              }
            });

    return new DataTableModel(data, namesArray, fromID);
  }