Created
August 17, 2011 21:37
-
-
Save brianm/1152705 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.skife.jdbi.v2.docs; | |
import org.skife.jdbi.v2.SQLStatement; | |
import org.skife.jdbi.v2.sqlobject.Binder; | |
import org.skife.jdbi.v2.sqlobject.BinderFactory; | |
import org.skife.jdbi.v2.sqlobject.BindingAnnotation; | |
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizer; | |
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizerFactory; | |
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizingAnnotation; | |
import java.lang.annotation.Annotation; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.reflect.Method; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
@Retention(RetentionPolicy.RUNTIME) | |
@SqlStatementCustomizingAnnotation(BindIn.CustomizerFactory.class) | |
@BindingAnnotation(BindIn.BindingFactory.class) | |
public @interface BindIn | |
{ | |
String value(); | |
public static final class CustomizerFactory implements SqlStatementCustomizerFactory | |
{ | |
public SqlStatementCustomizer createForMethod(Annotation annotation, Class sqlObjectType, Method method) | |
{ | |
throw new UnsupportedOperationException("Not Yet Implemented!"); | |
} | |
public SqlStatementCustomizer createForType(Annotation annotation, Class sqlObjectType) | |
{ | |
throw new UnsupportedOperationException("Not Yet Implemented!"); | |
} | |
public SqlStatementCustomizer createForParameter(Annotation annotation, | |
Class sqlObjectType, | |
Method method, | |
Object arg) | |
{ | |
Collection<?> coll = (Collection<?>) arg; | |
BindIn in = (BindIn) annotation; | |
final String key = in.value(); | |
final List<String> ids = new ArrayList<String>(); | |
for (int idx = 0; idx < coll.size(); idx++) { | |
ids.add("__" + key + "_" + idx); | |
} | |
StringBuilder names = new StringBuilder(); | |
for (Iterator<String> i = ids.iterator(); i.hasNext();) { | |
names.append(":").append(i.next()); | |
if (i.hasNext()) { | |
names.append(","); | |
} | |
} | |
final String ns = names.toString(); | |
return new SqlStatementCustomizer() | |
{ | |
public void apply(SQLStatement q) throws SQLException | |
{ | |
q.define(key, ns); | |
} | |
}; | |
} | |
} | |
public static class BindingFactory implements BinderFactory | |
{ | |
public Binder build(Annotation annotation) | |
{ | |
final BindIn in = (BindIn) annotation; | |
final String key = in.value(); | |
return new Binder() | |
{ | |
public void bind(SQLStatement q, Annotation bind, Object arg) | |
{ | |
Iterable<?> coll = (Iterable<?>) arg; | |
int idx = 0; | |
for (Object s : coll) { | |
q.bind("__" + key + "_" + idx++, s); | |
} | |
} | |
}; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group DAO; | |
findIdsForNames(names) ::= << | |
select id from something where name in(<names>) order by id | |
>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.skife.jdbi.v2.docs; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.skife.jdbi.v2.DBI; | |
import org.skife.jdbi.v2.Handle; | |
import org.skife.jdbi.v2.sqlobject.Bind; | |
import org.skife.jdbi.v2.sqlobject.SqlQuery; | |
import org.skife.jdbi.v2.sqlobject.customizers.Define; | |
import org.skife.jdbi.v2.sqlobject.stringtemplate.ExternalizedSqlViaStringTemplate3; | |
import java.util.Arrays; | |
import java.util.List; | |
import static java.util.Arrays.asList; | |
import static org.hamcrest.CoreMatchers.equalTo; | |
import static org.junit.Assert.assertThat; | |
public class TestInClauseExpansion | |
{ | |
private DBI dbi; | |
private Handle handle; | |
@Before | |
public void setUp() throws Exception | |
{ | |
dbi = new DBI("jdbc:h2:mem:test"); | |
handle = dbi.open(); | |
handle.execute("create table something( id integer primary key, name varchar(100) )"); | |
} | |
@After | |
public void tearDown() throws Exception | |
{ | |
handle.close(); | |
} | |
@Test | |
public void testInClauseExpansion() throws Exception | |
{ | |
handle.execute("insert into something (name, id) values ('Brian', 1), ('Jeff', 2), ('Tom', 3)"); | |
DAO dao = handle.attach(DAO.class); | |
assertThat(dao.findIdsForNames(asList("Brian", "Jeff")), equalTo(asList(1, 2))); | |
} | |
@ExternalizedSqlViaStringTemplate3 | |
public static interface DAO | |
{ | |
@SqlQuery | |
public List<Integer> findIdsForNames(@BindIn("names") List<String> names); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment