/** * Open a handle and attach a new sql object of the specified type to that handle. Be sure to close the * sql object (via a close() method, or calling {@link IDBI#close(Object)} * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ public <SqlObjectType> SqlObjectType open(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.open(this, sqlObjectType); }
/** * Open a handle and attach a new sql object of the specified type to that handle. Be sure to close the * sql object (via a close() method, or calling {@link IDBI#close(Object)} * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ @Override public <SqlObjectType> SqlObjectType open(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.open(this, sqlObjectType); }
@Override public <SqlObjectType> SqlObjectType open(final Class<SqlObjectType> sqlObjectType) { if (delegate == null) { throw new IllegalStateException("No delegate has been set!"); } return wrapping ? SqlObjectBuilder.open(this, sqlObjectType) : delegate.open(sqlObjectType); }
@Test public void testIterator() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Iterator<Something> itty = spiffy.findByIdRange(2, 10); Set<Something> all = new HashSet<Something>(); while (itty.hasNext()) { all.add(itty.next()); } assertEquals(2, all.size()); assertTrue(all.contains(new Something(7, "Tim"))); assertTrue(all.contains(new Something(3, "Diego"))); }
@Test public void testList() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); List<Something> all = spiffy.findTwoByIds(3, 7); assertEquals(2, all.size()); assertTrue(all.contains(new Something(7, "Tim"))); assertTrue(all.contains(new Something(3, "Diego"))); }
@Test public void testList() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); List<Something> all = spiffy.findTwoByIds(3, 7); assertEquals(2, all.size()); assertTrue(all.contains(new Something(7, "Tim"))); assertTrue(all.contains(new Something(3, "Diego"))); }
@Test public void testSingleValue() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Something s = spiffy.findById(7); assertEquals("Tim", s.getName()); }
@Test public void testSingleValue() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Something s = spiffy.findById(7); assertEquals("Tim", s.getName()); }
@Test public void testWithExplicitMapper() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); Spiffy2 spiffy = SqlObjectBuilder.open(dbi, Spiffy2.class); Something s = spiffy.findByIdWithExplicitMapper(7) .first(); assertEquals("Tim", s.getName()); }
@Test public void testWithExplicitMapper() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); Spiffy2 spiffy = SqlObjectBuilder.open(dbi, Spiffy2.class); Something s = spiffy.findByIdWithExplicitMapper(7) .first(); assertEquals("Tim", s.getName()); }
@Test public void testFoo() throws Exception { handle.execute("insert into something (id, name) values (2, 'Martin')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Something s = spiffy.findSame(new Something(2, "Unknown")); assertEquals("Martin", s.getName()); spiffy.close(); }
@Test public void testInsertWithVoidReturn() throws Exception { Inserter i = SqlObjectBuilder.open(dbi, Inserter.class); // this is what is under test here i.insertWithVoidReturn(2, "Diego"); String name = handle.createQuery("select name from something where id = 2").mapTo(String.class).first(); assertEquals("Diego", name); i.close(); }
@Test public void testFoo() throws Exception { handle.execute("insert into something (id, name) values (2, 'Martin')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Something s = spiffy.findSame(new Something(2, "Unknown")); assertEquals("Martin", s.getName()); spiffy.close(); }
@Test public void testInsertWithVoidReturn() throws Exception { Inserter i = SqlObjectBuilder.open(dbi, Inserter.class); // this is what is under test here i.insertWithVoidReturn(2, "Diego"); String name = handle.createQuery("select name from something where id = 2").map(StringMapper.FIRST).first(); assertEquals("Diego", name); i.close(); }
@Test public void testInsert() throws Exception { Inserter i = SqlObjectBuilder.open(dbi, Inserter.class); // this is what is under test here int rows_affected = i.insert(2, "Diego"); String name = handle.createQuery("select name from something where id = 2").map(StringMapper.FIRST).first(); assertEquals(1, rows_affected); assertEquals("Diego", name); i.close(); }
@Test public void testWithRegisteredMapper() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); dbi.registerMapper(new SomethingMapper()); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Something s = spiffy.findById(7) .first(); assertEquals("Tim", s.getName()); }
@Test public void testInsert() throws Exception { Inserter i = SqlObjectBuilder.open(dbi, Inserter.class); // this is what is under test here int rows_affected = i.insert(2, "Diego"); String name = handle.createQuery("select name from something where id = 2").mapTo(String.class).first(); assertEquals(1, rows_affected); assertEquals("Diego", name); i.close(); }
@Test public void testWithRegisteredMapper() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); dbi.registerMapper(new SomethingMapper()); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Something s = spiffy.findById(7) .first(); assertEquals("Tim", s.getName()); }
@Test public void testTransactionIsolationActuallyHappens() throws Exception { TransactionStuff txl = SqlObjectBuilder.attach(handle, TransactionStuff.class); TransactionStuff tx2 = SqlObjectBuilder.open(dbi, TransactionStuff.class); txl.insert(8, "Mike"); txl.begin(); txl.updateName(8, "Miker"); assertEquals("Miker", txl.byId(8).getName()); assertEquals("Mike", tx2.byId(8).getName()); txl.commit(); assertEquals("Miker", tx2.byId(8).getName()); tx2.close(); }
@Test public void testTransactionIsolationActuallyHappens() throws Exception { TransactionStuff txl = SqlObjectBuilder.attach(handle, TransactionStuff.class); TransactionStuff tx2 = SqlObjectBuilder.open(dbi, TransactionStuff.class); txl.insert(8, "Mike"); txl.begin(); txl.updateName(8, "Miker"); assertEquals("Miker", txl.byId(8).getName()); assertEquals("Mike", tx2.byId(8).getName()); txl.commit(); assertEquals("Miker", tx2.byId(8).getName()); tx2.close(); }