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();
        }
      }
    }
  }
Example #2
0
 @Override
 public String toString() {
   db.getMutex().enter();
   try {
     StringBuffer buffer = new StringBuffer();
     buffer.append("Tables:\n");
     for (ISqlJetTableDef tableDef : tableDefs.values()) {
       buffer.append(tableDef.toString());
       buffer.append('\n');
     }
     buffer.append("Indexes:\n");
     for (ISqlJetIndexDef indexDef : indexDefs.values()) {
       buffer.append(indexDef.toString());
       buffer.append('\n');
     }
     return buffer.toString();
   } finally {
     db.getMutex().leave();
   }
 }
Example #3
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);
  }
Example #4
0
  @Test
  public void testAutoincrement() throws Exception {

    ISqlJetTableDef createTable =
        db.createTable(
            "CREATE TABLE `JOBLIST` ("
                + "`id` INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "`jobname` VARCHAR(75) NOT NULL,"
                + "`startdate` LONG not null"
                + ");");

    final ISqlJetTable table = db.getTable(createTable.getName());
    db.beginTransaction(SqlJetTransactionMode.EXCLUSIVE);
    try {
      table.insert(null, "jobname", 123456);
      table.insert(null, "jobname", 123456);
    } finally {
      db.commit();
    }

    db.runReadTransaction(
        new ISqlJetTransaction() {
          public Object run(SqlJetDb db) throws SqlJetException {
            ISqlJetCursor open = table.open();
            try {
              while (!open.eof()) {
                long id = open.getInteger("id");
                Assert.assertNotNull(id);
                open.next();
              }
            } finally {
              open.close();
            }
            return null;
          }
        });
  }
Example #5
0
  private ISqlJetIndexDef createIndexSafe(String sql) throws SqlJetException {

    final ParserRuleReturnScope parseIndex = parseIndex(sql);
    final CommonTree ast = (CommonTree) parseIndex.getTree();

    final SqlJetIndexDef indexDef = new SqlJetIndexDef(ast, 0);

    if (null == indexDef.getName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
    final String indexName = indexDef.getName();
    if ("".equals(indexName)) throw new SqlJetException(SqlJetErrorCode.ERROR);

    checkNameReserved(indexName);

    if (indexDefs.containsKey(indexName)) {
      if (indexDef.isKeepExisting()) {
        return indexDefs.get(indexName);
      } else {
        throw new SqlJetException(
            SqlJetErrorCode.ERROR, "Index \"" + indexName + "\" exists already");
      }
    }

    checkNameConflict(SqlJetSchemaObjectType.INDEX, indexName);

    if (null == indexDef.getTableName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
    final String tableName = indexDef.getTableName();
    if ("".equals(tableName)) throw new SqlJetException(SqlJetErrorCode.ERROR);

    final List<ISqlJetIndexedColumn> columns = indexDef.getColumns();
    if (null == columns) throw new SqlJetException(SqlJetErrorCode.ERROR);

    final ISqlJetTableDef tableDef = getTable(tableName);
    if (null == tableDef) throw new SqlJetException(SqlJetErrorCode.ERROR);

    for (final ISqlJetIndexedColumn column : columns) {
      if (null == column.getName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
      final String columnName = column.getName();
      if ("".equals(columnName)) throw new SqlJetException(SqlJetErrorCode.ERROR);
      if (null == tableDef.getColumn(columnName))
        throw new SqlJetException(
            SqlJetErrorCode.ERROR,
            "Column \"" + columnName + "\" not found in table \"" + tableName + "\"");
    }

    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);
    final String createIndexSQL =
        indexDef.isUnique() ? getCreateIndexUniqueSql(parseIndex) : getCreateIndexSql(parseIndex);

    try {

      schemaTable.lock();

      try {

        db.getOptions().changeSchemaVersion();

        final int page = btree.createTable(BTREE_CREATE_INDEX_FLAGS);

        final long rowId =
            schemaTable.insertRecord(INDEX_TYPE, indexName, tableName, page, createIndexSQL);

        indexDef.setPage(page);
        indexDef.setRowId(rowId);
        indexDef.bindColumns(tableDef);
        indexDefs.put(indexName, indexDef);

        final SqlJetBtreeIndexTable indexTable =
            new SqlJetBtreeIndexTable(btree, indexDef.getName(), true);
        try {
          indexTable.reindex(this);
        } finally {
          indexTable.close();
        }
        return indexDef;

      } finally {
        schemaTable.unlock();
      }

    } finally {
      schemaTable.close();
    }
  }