@Override public int getTaskIdHistoryCount(Optional<String> requestId, Optional<String> deployId, Optional<String> runId, Optional<String> host, Optional<ExtendedTaskState> lastTaskStatus, Optional<Long> startedBefore, Optional<Long> startedAfter, Optional<Long> updatedBefore, Optional<Long> updatedAfter) { final Map<String, Object> binds = new HashMap<>(); final StringBuilder sqlBuilder = new StringBuilder(GET_TASK_ID_HISTORY_COUNT_QUERY); applyTaskIdHistoryBaseQuery(sqlBuilder, binds, requestId, deployId, runId, host, lastTaskStatus, startedBefore, startedAfter, updatedBefore, updatedAfter); final String sql = sqlBuilder.toString(); LOG.trace("Generated sql for task search count: {}, binds: {}", sql, binds); final Query<Integer> query = getHandle().createQuery(sql).mapTo(Integer.class); for (Map.Entry<String, Object> entry : binds.entrySet()) { query.bind(entry.getKey(), entry.getValue()); } return query.first(); }
@Override public List<SingularityTaskIdHistory> getTaskIdHistory(Optional<String> requestId, Optional<String> deployId, Optional<String> runId, Optional<String> host, Optional<ExtendedTaskState> lastTaskStatus, Optional<Long> startedBefore, Optional<Long> startedAfter, Optional<Long> updatedBefore, Optional<Long> updatedAfter, Optional<OrderDirection> orderDirection, Optional<Integer> limitStart, Integer limitCount) { final Map<String, Object> binds = new HashMap<>(); final StringBuilder sqlBuilder = new StringBuilder(GET_TASK_ID_HISTORY_QUERY); applyTaskIdHistoryBaseQuery(sqlBuilder, binds, requestId, deployId, runId, host, lastTaskStatus, startedBefore, startedAfter, updatedBefore, updatedAfter); sqlBuilder.append(" ORDER BY startedAt "); sqlBuilder.append(orderDirection.or(OrderDirection.DESC).name()); if (!requestId.isPresent()) { sqlBuilder.append(", requestId "); sqlBuilder.append(orderDirection.or(OrderDirection.DESC).name()); } // NOTE: PG, MySQL are both compatible with OFFSET LIMIT syntax, while only MySQL understands LIMIT offset, limit. if (limitCount != null ){ sqlBuilder.append(" LIMIT :limitCount"); binds.put("limitCount", limitCount); } if (limitStart.isPresent()) { sqlBuilder.append(" OFFSET :limitStart "); binds.put("limitStart", limitStart.get()); } final String sql = sqlBuilder.toString(); LOG.trace("Generated sql for task search: {}, binds: {}", sql, binds); final Query<SingularityTaskIdHistory> query = getHandle().createQuery(sql).mapTo(SingularityTaskIdHistory.class); for (Map.Entry<String, Object> entry : binds.entrySet()) { query.bind(entry.getKey(), entry.getValue()); } return query.list(); }
@Override public Integer withHandle(Handle handle) throws Exception { return handle.createQuery("select 2 + 2").mapTo(Integer.class).first(); } });
@Override public void call(IDBI dbi) { final Handle h = DBIUtil.getHandle(dbi); int count = h.createQuery("select count(*) from something").mapTo(Integer.class).first(); assertEquals(0, count); } });
@Override public Object withHandle(final Handle h) throws Exception { h.execute("create"); h.execute("insert", 1, "Brian"); String brian = h.createQuery("findNameById").bind("0", 1).mapTo(String.class).first(); assertThat(brian).isEqualTo("Brian"); return null; } });
@Test public void testZipping() throws Exception { UsesBatching b = handle.attach(UsesBatching.class); List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5); List<String> names = Arrays.asList("David", "Tim", "Mike"); b.zipArgumentsTogether(ids, names); assertThat(b.size(), equalTo(3)); List<String> ins_names = handle.createQuery("select distinct name from something order by name") .mapTo(String.class) .list(); assertThat(ins_names, equalTo(Arrays.asList("David", "Mike", "Tim"))); }
@Override public String withHandle(Handle handle) throws Exception { handle.execute("insert into something (id, name) values (8, 'Mike')"); return handle.createQuery("select name from something where id = 8").mapTo(String.class).first(); } });
@Test public void testBindConstantValue() throws Exception { UsesBatching b = handle.attach(UsesBatching.class); List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5); b.withConstantValue(ids, "Johan"); assertThat(b.size(), equalTo(5)); List<String> names = handle.createQuery("select distinct name from something") .mapTo(String.class) .list(); assertThat(names, equalTo(Arrays.asList("Johan"))); }
@Override public Something withHandle(Handle handle) throws Exception { handle.insert("insert into something (id, name) values (18, 'Sam')"); return handle.createQuery("select id, name from something where id = :id") .bind("id", 18) .mapTo(Something.class) .first(); } });
@Override public Something withHandle(Handle handle) throws Exception { handle.insert("insert into something (id, name) values (18, 'Sam')"); return handle.createQuery("select id, name from something where id = :id") .bind("id", 18) .mapTo(Something.class) .first(); } });
@Test public void testOnList() throws Exception { h.registerContainerFactory(new ImmutableListContainerFactory()); h.execute("insert into something (id, name) values (1, 'Coda')"); h.execute("insert into something (id, name) values (2, 'Brian')"); ImmutableList<String> rs = h.createQuery("select name from something order by id") .mapTo(String.class) .list(ImmutableList.class); assertThat(rs, equalTo(ImmutableList.of("Coda", "Brian"))); }
@Test public void testDefines() throws Exception { handle.attach(Kangaroo.class).weirdInsert("something", "id", "name", 5, "Bouncer"); String name = handle.createQuery("select name from something where id = 5") .mapTo(String.class) .first(); assertThat(name, equalTo("Bouncer")); }
@Test public void testBaz() throws Exception { Wombat wombat = handle.attach(Wombat.class); wombat.insert(new Something(7, "Henning")); String name = handle.createQuery("select name from something where id = 7") .mapTo(String.class) .first(); assertThat(name, equalTo("Henning")); }
@Test public void testMapToEnum() throws Exception { Handle h = openHandle(); h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); List<SomethingElse.Name> results = h.createQuery("select name from something order by id") .mapTo(SomethingElse.Name.class) .list(); assertEquals(SomethingElse.Name.eric, results.get(0)); assertEquals(SomethingElse.Name.brian, results.get(1)); }
@Test public void testDefines() throws Exception { handle.attach(Wombat.class).weirdInsert("something", "id", "name", 5, "Bouncer"); handle.attach(Wombat.class).weirdInsert("something", "id", "name", 6, "Bean"); String name = handle.createQuery("select name from something where id = 5") .mapTo(String.class) .first(); assertThat(name, equalTo("Bouncer")); }
@Test public void testDoesNotExist() throws Exception { h.execute("insert into something (id, name) values (1, 'Coda')"); h.registerContainerFactory(new MaybeContainerFactory()); Maybe<String> rs = h.createQuery("select name from something where id = :id") .bind("id", 2) .mapTo(String.class) .first(Maybe.class); assertThat(rs.isKnown(), equalTo(false)); }
@Test public void testExists() throws Exception { h.execute("insert into something (id, name) values (1, 'Coda')"); h.registerContainerFactory(new MaybeContainerFactory()); Maybe<String> rs = h.createQuery("select name from something where id = :id") .bind("id", 1) .mapTo(String.class) .first(Maybe.class); assertThat(rs.isKnown(), equalTo(true)); assertThat(rs.getValue(), equalTo("Coda")); }
@Test public void testAPIWorks() throws Exception { Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(7, "Bill"); String bill = handle.createQuery("select name from something where id = 7").mapTo(String.class).first(); assertEquals("Bill", bill); }
@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 testInsert() throws Exception { Spiffy s = handle.attach(Spiffy.class); s.insert(new Something(2, "Bean")); String name = handle.createQuery("select name from something where id = 2").mapTo(String.class).first(); assertEquals("Bean", name); }