Package org.tmatesoft.sqljet.core.table

Examples of org.tmatesoft.sqljet.core.table.SqlJetDb


        myDBFile = dbFile;
        if (dbFile == null) {
            myTableNamesCombo.setModel(new DefaultComboBoxModel(new Object[] {"<empty>"}));
            myTableNamesCombo.setEnabled(false);
        } else {
            SqlJetDb db = null;
            try {
                db = SqlJetDb.open(dbFile, true);
                myTableNamesCombo.setModel(new DefaultComboBoxModel(db.getSchema().getTableNames().toArray()));
                myTableNamesCombo.setEnabled(true);
            } finally {
                if (db != null) {
                    db.close();
                }
            }
        }
        myTableNamesCombo.setSelectedIndex(0);
        itemStateChanged(null);
View Full Code Here


                if (dbFile == null) {
                    model = new DefaultTableModel();
                } else {
                    // open db and read schema.
                    progress.start("Loading Data...", myPageSize);
                    SqlJetDb db = null;
                    try {
                        db = SqlJetDb.open(dbFile, true);
                        ISqlJetTable table = db.getTable(tableName);
                        model = DataTableModel.createInstance(table, row, myPageSize, progress);
                    } catch (final Throwable th) {
                        model = new DefaultTableModel();
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                myManager.showErrorDialog(th);
                            }
                        });
                    } finally {
                        if (db != null) {
                            try {
                                db.close();
                            } catch (SqlJetException e) {
                            }
                        }
                        progress.finish();
                    }                  
View Full Code Here

    public void open(File dbFile) throws SqlJetException {
        // create model and set to the outline.
        SchemaTreeModel model;
        if (dbFile != null) {
            // open db and read schema.
            SqlJetDb db = null;
            try {
                db = SqlJetDb.open(dbFile, true);
                model = SchemaTreeModel.createInstance(db.getSchema());
            } finally {
                if (db != null) {
                    db.close();
                }
            }
        } else {
            model = SchemaTreeModel.createInstance(null);
        }
View Full Code Here

    public static void main(String[] args) throws SqlJetException {
        File dbFile = new File(DB_NAME);
        dbFile.delete();
       
        // create database, table and two indices:
        SqlJetDb db = SqlJetDb.open(dbFile, true);
        // set DB option that have to be set before running any transactions:
        db.getOptions().setAutovacuum(true);
        // set DB option that have to be set in a transaction:
        db.runTransaction(new ISqlJetTransaction() {
            public Object run(SqlJetDb db) throws SqlJetException {
                db.getOptions().setUserVersion(1);
                return true;
            }
        }, SqlJetTransactionMode.WRITE);
      
           
        db.beginTransaction(SqlJetTransactionMode.WRITE);
        try {           
            String createTableQuery = "CREATE TABLE " + TABLE_NAME + " (" + SECOND_NAME_FIELD + " TEXT NOT NULL PRIMARY KEY , " + FIRST_NAME_FIELD + " TEXT NOT NULL, " + DOB_FIELD + " INTEGER NOT NULL)";
            String createFirstNameIndexQuery = "CREATE INDEX " + FULL_NAME_INDEX + " ON " + TABLE_NAME + "(" +  FIRST_NAME_FIELD + "," + SECOND_NAME_FIELD + ")";
            String createDateIndexQuery = "CREATE INDEX " + DOB_INDEX + " ON " + TABLE_NAME + "(" +  DOB_FIELD + ")";
            System.out.println();
            System.out.println(">DB schema queries:");
            System.out.println();
            System.out.println(createTableQuery);
            System.out.println(createFirstNameIndexQuery);
            System.out.println(createDateIndexQuery);
           
            db.createTable(createTableQuery);
            db.createIndex(createFirstNameIndexQuery);
            db.createIndex(createDateIndexQuery);
        } finally {
            db.commit();
        }
        // close DB and open it again (as part of example code)
       
        db.close();       
        db = SqlJetDb.open(dbFile, true);

        System.out.println();
        System.out.println(">Database schema objects:");
        System.out.println();
        System.out.println(db.getSchema());
        System.out.println(db.getOptions());       
       
        // insert rows:
        db.beginTransaction(SqlJetTransactionMode.WRITE);
        try {
            Calendar calendar = Calendar.getInstance();
            ISqlJetTable table = db.getTable(TABLE_NAME);
            calendar.clear();
            calendar.set(1981, 4, 19);
            table.insert("Prochaskova", "Elena", calendar.getTimeInMillis());
            calendar.set(1967, 5, 19);
            table.insert("Scherbina", "Sergei", calendar.getTimeInMillis());
            calendar.set(1987, 6, 19);
            table.insert("Vadishev", "Semen", calendar.getTimeInMillis());
            calendar.set(1982, 7, 19);
            table.insert("Sinjushkin", "Alexander", calendar.getTimeInMillis());
            calendar.set(1979, 8, 19);
            table.insert("Stadnik", "Dmitry", calendar.getTimeInMillis());
            calendar.set(1977, 9, 19);
            table.insert("Kitaev", "Alexander", calendar.getTimeInMillis());
        } finally {
            db.commit();
        }

        ISqlJetTable table = db.getTable(TABLE_NAME);

        // getting all rows in table, sorted by PK.       
        System.out.println();
        System.out.println(">All employees in order defined by PK (" + table.getPrimaryKeyIndexName() + "):");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.order(table.getPrimaryKeyIndexName()));
        } finally {
            db.commit();
        }

        // getting all rows in table, sorted by PK.       
        System.out.println();
        System.out.println(">All employees in order defined by " + DOB_INDEX + ", reversed:");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.order(table.getPrimaryKeyIndexName()).reverse());
        } finally {
            db.commit();
        }
       
        // getting all rows in table, sorted by index.       
        System.out.println();
        System.out.println(">All employees in order defined by " + FULL_NAME_INDEX + " :");
        System.out.println();

        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.order(FULL_NAME_INDEX));
        } finally {
            db.commit();
        }

        // getting rows in table with exact indexed field value.
        System.out.println();
        System.out.println(">Alexanders:");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.lookup(FULL_NAME_INDEX, "Alexander"));
        } finally {
            db.commit();
        }
       
        // getting rows in table with indexed field value in certain scope.       
        System.out.println();
        System.out.println(">Employees with full name in scope [B, I]:");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.scope(FULL_NAME_INDEX, new Object[] {"B"}, new Object[] {"I"}));
        } finally {
            db.commit();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        calendar.add(Calendar.YEAR, -30);

        System.out.println();
        System.out.println(">Deleting rows of employees older than 30 years old.");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.WRITE);
        try {
            ISqlJetCursor deleteCursor = table.scope(DOB_INDEX,
                     new Object[] {Long.MIN_VALUE},
                     new Object[] {calendar.getTimeInMillis()});
            while (!deleteCursor.eof()) {
                System.out.println("Deleting: " +
                        deleteCursor.getRowId() + " : " +
                        deleteCursor.getString(FIRST_NAME_FIELD) + " " +
                        deleteCursor.getString(SECOND_NAME_FIELD) + " was born on " +
                        formatDate(deleteCursor.getInteger(DOB_FIELD)));
                deleteCursor.delete();
            }
            deleteCursor.close();
        } finally {
            db.commit();
        }

        System.out.println();
        System.out.println(">After deletion in row id order:");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.open());
        } finally {
            db.commit();
        }

        db.beginTransaction(SqlJetTransactionMode.WRITE);
        ISqlJetCursor updateCursor = null;
        try {
            table.insert("Smith", "John", 0);
            calendar.setTime(new Date(System.currentTimeMillis()));
            updateCursor = table.open();
            do {
                updateCursor.update(updateCursor.getValue(SECOND_NAME_FIELD), updateCursor.getValue(FIRST_NAME_FIELD), calendar.getTimeInMillis());
            } while(updateCursor.next());
        } finally {
            updateCursor.close();
            db.commit();
        }

        System.out.println();
        System.out.println(">After insertion of a new record and updating dates (by PK):");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        try {
            printRecords(table.order(table.getPrimaryKeyIndexName()));
            System.out.println();
            System.out.println(">Same in order defined by " + FULL_NAME_INDEX + " :");
            System.out.println();
            printRecords(table.order(FULL_NAME_INDEX));
        } finally {
            db.commit();
        }
      
        System.out.println();
        System.out.println(">Dropping tables and indices:");
        System.out.println();
        db.beginTransaction(SqlJetTransactionMode.WRITE);
        try {
            Set<String> tables = db.getSchema().getTableNames();
            for (String tableName : tables) {
                ISqlJetTableDef tableDef = db.getSchema().getTable(tableName);
                Set<ISqlJetIndexDef> tableIndices = db.getSchema().getIndexes(tableDef.getName());
               
                for (ISqlJetIndexDef indexDef : tableIndices) {
                   
                    if (!indexDef.isImplicit()) {
                        System.out.println("dropping index: " + indexDef.getName());
                        db.dropIndex(indexDef.getName());
                    }
                }
                System.out.println("dropping table: " + tableDef.getName());
                db.dropTable(tableDef.getName());
            }
        } finally {
            db.commit();
        }

        db.close();
    }
View Full Code Here

        final File dbFile2 = File.createTempFile("repCacheFail", null);
        dbFile2.deleteOnExit();

        deflate(new File(DB_ARCHIVE), DB_FILE_NAME, dbFile1, true);

        final SqlJetDb db1 = SqlJetDb.open(dbFile1, false);
        final SqlJetDb db2 = SqlJetDb.open(dbFile2, true);

        db2.runWriteTransaction(new ISqlJetTransaction() {
            public Object run(SqlJetDb db) throws SqlJetException {
                db.createTable("create table rep_cache (hash text not null primary key, "
                        + "                        revision integer not null, "
                        + "                        offset integer not null, "
                        + "                        size integer not null, "
                        + "                        expanded_size integer not null); ");
                return null;
            }
        });

        db1.runReadTransaction(new ISqlJetTransaction() {
            public Object run(SqlJetDb db) throws SqlJetException {
                ISqlJetCursor c = null;
                final Collection<Object> values = new ArrayList<Object>();
                final Collection<Collection<Object>> block = new LinkedList<Collection<Object>>();
                try {
                    c = db.getTable("rep_cache").open();
                    long currentRev = 0;
                    while (!c.eof()) {
                        values.clear();
                        for (int i = 0; i < c.getFieldsCount(); i++) {
                            values.add(c.getValue(i));
                        }
                        long rev = c.getInteger(1);
                        if (rev != currentRev) {
                            db2.runWriteTransaction(new ISqlJetTransaction() {
                                public Object run(SqlJetDb db) throws SqlJetException {
                                    for (Collection<Object> row : block) {
                                        db2.getTable("rep_cache").insert(row.toArray());
                                    }
                                    return null;
                                }
                            });

                            currentRev = rev;
                            block.clear();
                        }
                        block.add(new ArrayList<Object>(values));
                        c.next();
                    }
                    if (!block.isEmpty()) {
                        db2.runWriteTransaction(new ISqlJetTransaction() {
                            public Object run(SqlJetDb db) throws SqlJetException {
                                for (Collection<Object> row : block) {
                                    db2.getTable("rep_cache").insert(row.toArray());
                                }
                                return null;
                            }
                        });
                    }
View Full Code Here

public class MalformedDB {
  public static void main(String[] args) {
    try {
      File dbFile = new File("./src/test/data/db/test.db");
      SqlJetFileUtil.deleteFile(dbFile);
      SqlJetDb db = SqlJetDb.open(dbFile, true);
      db.getOptions().setAutovacuum(true);
      db.runTransaction(new ISqlJetTransaction() {
        public Object run(SqlJetDb db) throws SqlJetException {
          db.getOptions().setUserVersion(1);
          return true;
        }
      }, SqlJetTransactionMode.WRITE);
      db.beginTransaction(SqlJetTransactionMode.WRITE);
      String sql1 = "CREATE TABLE IF NOT EXISTS TESTXX (a int, b int, c int, "
          + "d int, blob blob, PRIMARY KEY (a,b,c,d))";
      String sql2 = "CREATE INDEX IF NOT EXISTS IND on TESTXX (a,b,c,d)";
      db.createTable(sql1);
      db.createIndex(sql2);
      db.commit();
      db.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

    }

    @Test
    public void testReadOnlyBug() throws SqlJetException {

        final SqlJetDb db1 = SqlJetDb.open(file, true);
        try {
            db1.createTable("create table t(a integer primary key, b text)");
        } finally {
            db1.close();
        }

        try {
            final SqlJetDb db2 = SqlJetDb.open(new File("doesnt/exists", file.getName()), false);
            db2.close();
        } catch (SqlJetException e) {
            logger.log(Level.INFO, "Tries to open unexisted file", e);
        }

        final SqlJetDb db3 = SqlJetDb.open(file, true);
        try {
            db3.createTable("create table t1(a integer primary key, b text)");
        } finally {
            db3.close();
        }

    }
View Full Code Here

public class SharedLockNullTest extends AbstractNewDbTest {

  @Test
  public void sharedLockNullTest() throws SqlJetException {
    db.createTable("create table t(t text);");
    final SqlJetDb connect1 = SqlJetDb.open(file, false);
    final SqlJetDb connect2 = SqlJetDb.open(file, true);
    final SqlJetDb connect3 = SqlJetDb.open(file, false);
    try {
      connect1.beginTransaction(SqlJetTransactionMode.READ_ONLY);
      connect2.beginTransaction(SqlJetTransactionMode.WRITE);
      connect2.getTable("t").insert("t");
      try {
        connect2.commit();
      } catch (SqlJetException e) {
        connect3.beginTransaction(SqlJetTransactionMode.READ_ONLY);
      } finally {
        try {
          connect1.rollback();
          connect2.rollback();
          connect3.rollback();
        } catch (Throwable e) {

        }
      }
    } finally {
      try {
        connect1.close();
        connect2.close();
        connect3.close();
      } catch (Throwable e) {

      }
    }
  }
View Full Code Here

    /**
     * @throws SqlJetException
     */
    private void assertDbOpen() throws SqlJetException {
        final SqlJetDb db2 = SqlJetDb.open(file, false);
        try {
            Assert.assertNotNull(db2);
        } finally {
            if (db2 != null) {
                db.close();
View Full Code Here

    private static final String FTS3_DB = "src/test/data/db/fts3.sqlite";

    @Test
    public void fts3() throws SqlJetException {
        final SqlJetDb fts3Db = SqlJetDb.open(new File(FTS3_DB), false);
        try {
            final ISqlJetSchema schema = fts3Db.getSchema();
            final Set<String> virtualTableNames = schema.getVirtualTableNames();
            Assert.assertNotNull(virtualTableNames);
            Assert.assertTrue(virtualTableNames.size() > 0);
        } finally {
            fts3Db.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.tmatesoft.sqljet.core.table.SqlJetDb

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.