sjeiwei / sqljet

Automatically exported from code.google.com/p/sqljet
0 stars 0 forks source link

Unique index does not work correctly #147

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In trunk version of SqlJet this test case fails.

public class SqlJetIndexText
{
  private File dbFile;
  private SqlJetDb db;
  @Before
  public void setUp() throws Exception {
    dbFile = new File("D:\\jetdbtest.db");
    db = SqlJetDb.open(dbFile, true);
    db.createTable("CREATE TABLE IF NOT EXISTS names (name_id INTEGER PRIMARY KEY AUTOINCREMENT,unique_name TEXT NOT NULL)");
    db.createIndex("CREATE UNIQUE INDEX IF NOT EXISTS names_unique_name_idx ON names(unique_name)");

  }

  @After
  public void tearDown() throws Exception {
    db.close();
    dbFile.delete();
  }

  @Test
  public void testUniqueIndex() throws Exception
  {

    db.runTransaction(new ISqlJetTransaction() {
      @Override
      public Object run(SqlJetDb arg0) throws SqlJetException {
        ISqlJetTable table = db.getTable("names");
        table.insertOr(SqlJetConflictAction.REPLACE, null, "same name");
        table.insertOr(SqlJetConflictAction.REPLACE, null, "same name");

        return null;
      }
    }, SqlJetTransactionMode.WRITE); //I would expect transaction fails or one record in table. But in table are two records!

    Integer count = (Integer)db.runTransaction(new ISqlJetTransaction() {
      @Override
      public Object run(SqlJetDb arg0) throws SqlJetException {
        ISqlJetTable table = db.getTable("names");
        ISqlJetCursor cur = table.order(null);
        Integer counter = 0;
        if(!cur.eof())
        {
          do
          {
            ++counter;
          }while(cur.next());

        };
        return counter;
      }
    }, SqlJetTransactionMode.READ_ONLY);

    assertEquals(Integer.valueOf(1),count); //count is 2 but correctly is 1

  }
}

Original issue reported on code.google.com by jhruby....@gmail.com on 20 Jan 2011 at 12:51

GoogleCodeExporter commented 9 years ago

Original comment by sergey.s...@gmail.com on 20 Jan 2011 at 12:53

GoogleCodeExporter commented 9 years ago
Fixed in 'trunk'

Original comment by sergey.s...@gmail.com on 21 Jan 2011 at 12:42

GoogleCodeExporter commented 9 years ago
What is behavior of this fix?

Original comment by jhruby....@gmail.com on 21 Jan 2011 at 3:21

GoogleCodeExporter commented 9 years ago
It replaces row which exists in unique index.

Original comment by sergey.s...@gmail.com on 21 Jan 2011 at 3:28

GoogleCodeExporter commented 9 years ago
Thanks.

Original comment by jhruby....@gmail.com on 21 Jan 2011 at 3:32