diff --git a/README.md b/README.md index ec9ddaa..4fb427d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,5 @@ ## Storm - Object-Relational Mapper ## -Interface - -```java - @Database(context = "H2") - private interface UserRepository { - - @Select(sql = "select * from users") - public List fetchAll(); - - @Select(sql = "select * from users where id = ?") - public User findById(int id); - - @Select(sql = "select firstname || ' ' || lastname from users") - public List fullNames(); - - } -``` - Context - src/main/resources/context.json @@ -34,15 +16,26 @@ ] ``` +Interface + +```java + @Database(context = "H2") + private interface UserRepository { + + @Select(sql = "select * from users") + public List fetchAll(); + + @Delete(sql = "delete from users where id = ?") + public int delete(int id); + + } +``` + Use ```java UserRepository repository = StormRepository.newInstance(UserRepository.class); - List users = repository.fetchAll(); - User user = repository.findById(12); - List fullNames = repository.fullNames(); - ``` Mapping diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index e5d939f..25c5dfb 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -11,6 +11,7 @@ import com.ljunggren.storm.annotation.Database; import com.ljunggren.storm.context.Context; import com.ljunggren.storm.context.ContextFactory; +import com.ljunggren.storm.crud.DeleteQuery; import com.ljunggren.storm.crud.QueryChain; import com.ljunggren.storm.crud.SelectQuery; @@ -62,7 +63,8 @@ } private QueryChain getQueryChain() { - return new SelectQuery(); + return new SelectQuery().nextChain( + new DeleteQuery()); } } diff --git a/src/main/java/com/ljunggren/storm/annotation/Delete.java b/src/main/java/com/ljunggren/storm/annotation/Delete.java new file mode 100644 index 0000000..ce360cb --- /dev/null +++ b/src/main/java/com/ljunggren/storm/annotation/Delete.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 Delete { + + String sql(); + +} diff --git a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java new file mode 100644 index 0000000..8a46779 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.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.Delete; +import com.ljunggren.storm.context.Context; + +public class DeleteQuery extends QueryChain { + + @Override + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + if (annotation.annotationType() == Delete.class) { + String sql = ((Delete) 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/main/java/com/ljunggren/storm/crud/QueryChain.java b/src/main/java/com/ljunggren/storm/crud/QueryChain.java index a2a6718..d114f96 100644 --- a/src/main/java/com/ljunggren/storm/crud/QueryChain.java +++ b/src/main/java/com/ljunggren/storm/crud/QueryChain.java @@ -2,6 +2,9 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; import com.ljunggren.storm.context.Context; @@ -16,4 +19,22 @@ public abstract Object execute(Annotation annotation, Context context, Object[] args, Type returnType); + protected void setParameters(PreparedStatement preparedStatement, Object[] args) throws SQLException { + if (args != null) { + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i + 1, args[i]); + } + } + } + + protected void closeConnection(Connection connection) { + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } diff --git a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java index dbefd99..a181506 100644 --- a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java @@ -5,7 +5,6 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.SQLException; import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; @@ -13,19 +12,17 @@ public class SelectQuery extends QueryChain { - private Class annotationClass = Select.class; - @Override public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { - if (annotation.annotationType() == annotationClass) { - return executeQuery(annotation, context, args, returnType); + if (annotation.annotationType() == Select.class) { + String sql = ((Select) annotation).sql(); + return executeQuery(sql, context, args, returnType); } return nextChain.execute(annotation, context, args, returnType); } - private Object executeQuery(Annotation annotation, Context context, Object[] args, Type returnType) { + private Object executeQuery(String sql, Context context, Object[] args, Type returnType) { Connection connection = null; - String sql = ((Select) annotation).sql(); try { connection = context.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); @@ -40,22 +37,4 @@ } } - private void setParameters(PreparedStatement preparedStatement, Object[] args) throws SQLException { - if (args != null) { - for (int i = 0; i < args.length; i++) { - preparedStatement.setObject(i + 1, args[i]); - } - } - } - - private void closeConnection(Connection connection) { - if (connection != null) { - try { - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - } - } diff --git a/src/main/java/com/ljunggren/storm/mapper/MapperChain.java b/src/main/java/com/ljunggren/storm/mapper/MapperChain.java index 18e8f4e..7a634a5 100644 --- a/src/main/java/com/ljunggren/storm/mapper/MapperChain.java +++ b/src/main/java/com/ljunggren/storm/mapper/MapperChain.java @@ -44,12 +44,14 @@ } protected Object mapToObject(ResultSet resultSet, Class clazz, List fields) { + if (ReflectionUtils.isPrimitive(clazz) || ReflectionUtils.isString(clazz)) { + return mapToPrimitive(resultSet); + } Object object = instantiateObject(clazz); - return (ReflectionUtils.isPrimitive(clazz) || ReflectionUtils.isString(clazz)) ? - mapToPrimitive(resultSet, object) : mapToObject(resultSet, object, fields); + return mapToObject(resultSet, object, fields); } - private Object mapToPrimitive(ResultSet resultSet, Object object) { + private Object mapToPrimitive(ResultSet resultSet) { return getColumnValue(resultSet, 1); } diff --git a/src/main/java/com/ljunggren/storm/mapper/PrimitiveMapper.java b/src/main/java/com/ljunggren/storm/mapper/PrimitiveMapper.java index cb255f2..cb2f078 100644 --- a/src/main/java/com/ljunggren/storm/mapper/PrimitiveMapper.java +++ b/src/main/java/com/ljunggren/storm/mapper/PrimitiveMapper.java @@ -18,7 +18,7 @@ } private Object map(ResultSet resultSet, Class clazz) throws SQLException { - return resultSet.next() ? getColumnValue(resultSet, 1) : instantiateObject(clazz); + return resultSet.next() ? mapToObject(resultSet, clazz, null) : instantiateObject(clazz); } - + } diff --git a/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java b/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java index dc66ffa..d1e3664 100644 --- a/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java +++ b/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java @@ -44,6 +44,14 @@ return ClassUtils.isPrimitiveOrWrapper((Class) type); } + public static boolean isInteger(Type type) { + if (isPrimitive(type)) { + return type.getTypeName().equals(Integer.class.getName()) || + type.getTypeName().equals(int.class.getName()); + } + return false; + } + public static boolean isParameterized(Type type) { return type instanceof ParameterizedType; } diff --git a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java new file mode 100644 index 0000000..e789f18 --- /dev/null +++ b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java @@ -0,0 +1,61 @@ +package com.ljunggren.storm.crud; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +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.annotation.Database; +import com.ljunggren.storm.annotation.Delete; +import com.ljunggren.storm.annotation.Select; +import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.context.ContextFactory; + +public class DeleteQueryTest { + + @Database(context = "H2") + private interface UserRepository { + + @Delete(sql = "delete from users where id = ?") + public int delete(int id); + + @Delete(sql = "delete from users") + public int deleteAll(); + + @Select(sql = "select count(*) from users") + public long count(); + + } + + @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 deleteByIdTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + long beforeCount = repository.count(); + int deletedCount = repository.delete(1); + long afterCount = repository.count(); + assertEquals(1, deletedCount); + assertTrue(beforeCount > afterCount); + } + + @Test + public void deleteAllTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + repository.deleteAll(); + long count = repository.count(); + assertEquals(0, count); + } + +} diff --git a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java index d0c2143..6b18dcc 100644 --- a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java @@ -17,6 +17,7 @@ import com.ljunggren.storm.annotation.Database; import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.context.ContextFactory; public class SelectQueryTest { @@ -44,10 +45,7 @@ @Before public void setup() throws Exception { - Context context = Context.builder() - .url("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") - .driver("org.h2.Driver") - .build(); + 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); diff --git a/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java index 9d7fb66..9a45f29 100644 --- a/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java +++ b/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java @@ -24,6 +24,8 @@ Boolean objectBoolean; String string; Set intergerSet; + int primitiveInteger; + Integer objectInteger; @Test public void getOwnerTypeNullTest() { @@ -113,6 +115,20 @@ } @Test + public void isPrimitiveIntegerTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveInteger"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isInteger(fieldType)); + } + + @Test + public void isObjectIntegerTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("objectInteger"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isInteger(fieldType)); + } + + @Test public void isPrimitiveFalseTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); Type fieldType = field.getGenericType();