Package org.tmatesoft.sqljet.core.table

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


        }
        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;
            }
        });
View Full Code Here


        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):");
View Full Code Here

            }
        });

        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;
                            }
                        });
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
                return null;
            }
        });
View Full Code Here

    db.createTable("CREATE TABLE record (a NONE NOT NULL)");
    ISqlJetTable table = db.getTable("record");
    table.insert("10");
    db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    try {
      ISqlJetCursor cursor = table.open();
      boolean more = !cursor.eof();
      while (more) {
        String s = cursor.getString(0);
        Assert.assertEquals(2, s.length());
        more = cursor.next();
      }
    } finally {
      db.commit();
    }
  }
View Full Code Here

    db.beginTransaction(SqlJetTransactionMode.EXCLUSIVE);

    try {

      // Remove all rows
      ISqlJetCursor curseur = null;
      final ISqlJetTable tableDossiers = db.getTable("test");
      curseur = tableDossiers.open();
      while (!curseur.eof()) {
        curseur.delete();
      }
      curseur.close();

      // Commit & close
      db.commit();

    } catch (SqlJetException e) {
View Full Code Here

    private void assertInsertedOrReplaced(final ISqlJetTable table,
            final long actual, final Object ... key) throws SqlJetException {
        db.runReadTransaction(new ISqlJetTransaction() {
            public Object run(SqlJetDb db) throws SqlJetException {
                ISqlJetCursor lookup = table.lookup(null, key);
                Assert.assertEquals(actual, lookup.getInteger("time"));
                return null;
            }
        });
    }
View Full Code Here

    }

    private void assertCount(final ISqlJetTable table, final int count) throws SqlJetException {
        db.runReadTransaction(new ISqlJetTransaction() {
            public Object run(SqlJetDb db) throws SqlJetException {
                ISqlJetCursor open = table.open();
                Assert.assertEquals(count, open.getRowCount());
                return null;
            }
        });
    }
View Full Code Here

    // begin a read transaction before reading data (this is required by SqlJet).
    persistenceDb.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    try
    {
      classLogger.log(Level.CONFIG, "Getting Group ID {0} to merge Customer ID {1}.", new Object[] {1, 1});
      ISqlJetCursor foundRow = customerTable.lookup(customerTable.getPrimaryKeyIndexName(), 1);
      if(foundRow != null && foundRow.first())
      {
        int rowCount = intFromLong(foundRow.getRowCount());
        classLogger.log(Level.CONFIG, "Reading {0} Customers.", rowCount);
      }
    }
    finally
    {
View Full Code Here

    @Test
    public void testRowCountIsStateless() throws SqlJetException {
        db.runReadTransaction(new ISqlJetTransaction() {
            public Object run(SqlJetDb db) throws SqlJetException {
                SqlJetScope scope = new SqlJetScope(new Object[] {"AB"}, new Object[] {"BC"});
                ISqlJetCursor cursor = db.getTable("table").scope("names_idx", scope);
                Assert.assertTrue(!cursor.eof());
                long count = cursor.getRowCount();
                Assert.assertTrue(count > 0);
                Assert.assertTrue(!cursor.eof());
                Assert.assertTrue(!cursor.eof());
                Assert.assertTrue(cursor.getRowCount() > 0);
                cursor.close();

                cursor = db.getTable("table").scope("names_idx", scope);
                // now at 0.
               
                count = cursor.getRowCount();
                Assert.assertTrue(count > 0);
                Assert.assertTrue(!cursor.eof());
                Assert.assertTrue(cursor.getRowCount() > 0);
                Assert.assertTrue(!cursor.eof());
               
                Assert.assertTrue(cursor.next());
                // now at 1.
                Assert.assertFalse(cursor.eof());
                Assert.assertTrue(cursor.getRowCount() == 3);
                Assert.assertTrue(cursor.next());
               
                // now at 2.
                Assert.assertFalse(cursor.eof());
                Assert.assertFalse(cursor.eof());
                Assert.assertTrue(cursor.getRowCount() == 3);
                Assert.assertFalse(cursor.next());
                // now at eof
                Assert.assertTrue(cursor.eof());

                Assert.assertTrue(cursor.previous());
                // at 2.
                Assert.assertTrue(cursor.getRowCount() == 3);
                Assert.assertFalse(cursor.eof());

                Assert.assertTrue(cursor.previous());
                // at 1.
                Assert.assertTrue(cursor.getRowCount() == 3);
                Assert.assertFalse(cursor.eof());
                Assert.assertTrue(cursor.previous());
                // at 0.
                Assert.assertFalse(cursor.previous());
                Assert.assertTrue(cursor.eof());
                Assert.assertTrue(cursor.getRowCount() == 3);
               
                Assert.assertTrue(cursor.first());
                Assert.assertFalse(cursor.eof());
                Assert.assertTrue(cursor.getRowCount() == 3);
                Assert.assertTrue(cursor.last());
                Assert.assertFalse(cursor.eof());
                Assert.assertTrue(cursor.getRowCount() == 3);
               
                cursor.close();
                return null;
            }
        });
    }
View Full Code Here

    // begin a read transaction before reading data (this is required by SqlJet).
    persistenceDb.beginTransaction(SqlJetTransactionMode.WRITE);
    try
    {
      classLogger.log(Level.CONFIG, "Getting Group ID {0} to merge Customer ID {1}.", new Object[] {1, 1});
      ISqlJetCursor foundRow = customerTable.lookup(customerTable.getPrimaryKeyIndexName(), 1);
      if(foundRow != null && foundRow.first())
      {
        foundRow.delete();
      }
    }
    finally
    {
      persistenceDb.commit();
View Full Code Here

TOP

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

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.