diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index 25c5dfb..4fe16f2 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -14,6 +14,7 @@ import com.ljunggren.storm.crud.DeleteQuery; import com.ljunggren.storm.crud.QueryChain; import com.ljunggren.storm.crud.SelectQuery; +import com.ljunggren.storm.crud.UpdateQuery; public class StormRepository implements InvocationHandler { @@ -64,7 +65,9 @@ private QueryChain getQueryChain() { return new SelectQuery().nextChain( - new DeleteQuery()); + new DeleteQuery().nextChain( + new UpdateQuery() + )); } } diff --git a/src/main/java/com/ljunggren/storm/annotation/Update.java b/src/main/java/com/ljunggren/storm/annotation/Update.java new file mode 100644 index 0000000..a43b991 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/annotation/Update.java @@ -0,0 +1,14 @@ +package com.ljunggren.storm.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Update { + + String sql(); + +} diff --git a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java new file mode 100644 index 0000000..87e413d --- /dev/null +++ b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java @@ -0,0 +1,37 @@ +package com.ljunggren.storm.crud; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.sql.Connection; +import java.sql.PreparedStatement; + +import com.ljunggren.storm.annotation.Update; +import com.ljunggren.storm.context.Context; + +public class UpdateQuery extends QueryChain { + + @Override + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + if (annotation.annotationType() == Update.class) { + String sql = ((Update) annotation).sql(); + return executeQuery(sql, context, args, returnType); + } + return nextChain.execute(annotation, context, args, returnType); + } + + private int executeQuery(String sql, Context context, Object[] args, Type returnType) { + Connection connection = null; + try { + connection = context.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql); + setParameters(preparedStatement, args); + return preparedStatement.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } finally { + closeConnection(connection); + } + } + +} diff --git a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java new file mode 100644 index 0000000..91859ff --- /dev/null +++ b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java @@ -0,0 +1,49 @@ +package com.ljunggren.storm.crud; + +import static org.junit.Assert.assertEquals; + +import java.sql.Statement; + +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.Test; + +import com.ljunggren.storm.StormRepository; +import com.ljunggren.storm.TestUser; +import com.ljunggren.storm.annotation.Database; +import com.ljunggren.storm.annotation.Select; +import com.ljunggren.storm.annotation.Update; +import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.context.ContextFactory; + +public class UpdateQueryTest { + + @Database(context = "H2") + private interface UserRepository { + + @Update(sql = "update users set firstname = ? where id = ?") + public int updateFirstName(String name, int id); + + @Select(sql = "select * from users where id = ?") + public TestUser findById(int id); + + } + + @Before + public void setup() throws Exception { + Context context = new ContextFactory().getContext("H2"); + Statement stat = context.getConnection().createStatement(); + String sql = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("com/ljunggren/storm/CREATE_DB.sql"), "UTF-8"); + stat.execute(sql); + } + + @Test + public void updateFirstNameTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + int updates = repository.updateFirstName("Bob", 1); + TestUser user = repository.findById(1); + assertEquals(1, updates); + assertEquals("Bob", user.getFirstName()); + } + +}