diff --git a/README.md b/README.md index 4fb427d..d4b68a4 100644 --- a/README.md +++ b/README.md @@ -5,42 +5,68 @@ - src/main/resources/context.json ```json - [ - { - "name" : "H2", - "driver" : "org.h2.Driver", - "url" : "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", - "username" : "", - "password" : "" - } - ] +[ + { + "name" : "H2", + "driver" : "org.h2.Driver", + "url" : "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", + "username" : "", + "password" : "" + } +] ``` Interface ```java - @Database(context = "H2") - private interface UserRepository { +@Database(context = "H2") +private interface UserRepository { - @Select(sql = "select * from users") - public List fetchAll(); + @Select(sql = "select * from users") + public List fetchAll(); - @Delete(sql = "delete from users where id = ?") - public int delete(int id); + @Delete(sql = "delete from users where id = ?") + public int delete(int id); - } +} ``` Use ```java - UserRepository repository = StormRepository.newInstance(UserRepository.class); - List users = repository.fetchAll(); +UserRepository repository = StormRepository.newInstance(UserRepository.class); +List users = repository.fetchAll(); ``` Mapping ```java - @ColumnProperty(name = "employee_id") - private int employeeID; -``` \ No newline at end of file +@ColumnProperty(name = "employee_id") +private int employeeID; +``` + +## Annotations ## + +Select + +```java +@Select(sql = "select * from users") +``` + +Delete + +```java +@Delete(sql = "delete from users where id = ?") +``` + +Update + +```java +@Update(sql = "update users set firstname = ? where id = ?") +``` + +Insert + +```java +@Insert(sql = "insert into users (firstname, lastname, employee_id) values (?, ?, ?)") +``` diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index 4fe16f2..e479970 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -12,6 +12,7 @@ import com.ljunggren.storm.context.Context; import com.ljunggren.storm.context.ContextFactory; import com.ljunggren.storm.crud.DeleteQuery; +import com.ljunggren.storm.crud.InsertQuery; import com.ljunggren.storm.crud.QueryChain; import com.ljunggren.storm.crud.SelectQuery; import com.ljunggren.storm.crud.UpdateQuery; @@ -66,8 +67,9 @@ private QueryChain getQueryChain() { return new SelectQuery().nextChain( new DeleteQuery().nextChain( - new UpdateQuery() - )); + new UpdateQuery().nextChain( + new InsertQuery() + ))); } } diff --git a/src/main/java/com/ljunggren/storm/annotation/Insert.java b/src/main/java/com/ljunggren/storm/annotation/Insert.java new file mode 100644 index 0000000..1a09230 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/annotation/Insert.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 Insert { + + String sql(); + +} diff --git a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java new file mode 100644 index 0000000..be782bf --- /dev/null +++ b/src/main/java/com/ljunggren/storm/crud/InsertQuery.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.Insert; +import com.ljunggren.storm.context.Context; + +public class InsertQuery extends QueryChain { + + @Override + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + if (annotation.annotationType() == Insert.class) { + String sql = ((Insert) annotation).sql(); + return executeQuery(sql, context, args, returnType); + } + return nextChain.execute(annotation, context, args, returnType); + } + + private Object 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 null; + } finally { + closeConnection(connection); + } + } + +} diff --git a/src/main/java/com/ljunggren/storm/mapper/MapperChain.java b/src/main/java/com/ljunggren/storm/mapper/MapperChain.java index 7a634a5..f6268f4 100644 --- a/src/main/java/com/ljunggren/storm/mapper/MapperChain.java +++ b/src/main/java/com/ljunggren/storm/mapper/MapperChain.java @@ -36,7 +36,9 @@ protected Object instantiateObject(Class clazz) { try { - return clazz.newInstance(); + if (ReflectionUtils.hasNoArgsContrustor(clazz)) { + return clazz.newInstance(); + } } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } diff --git a/src/main/java/com/ljunggren/storm/mapper/PrimitiveMapper.java b/src/main/java/com/ljunggren/storm/mapper/PrimitiveMapper.java index cb2f078..5c1c268 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() ? mapToObject(resultSet, clazz, null) : instantiateObject(clazz); + return resultSet.next() ? mapToObject(resultSet, clazz, null) : null; } } diff --git a/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java b/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java index d1e3664..dd52920 100644 --- a/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java +++ b/src/main/java/com/ljunggren/storm/utils/ReflectionUtils.java @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.List; import java.util.Set; +import java.util.stream.Stream; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.reflect.FieldUtils; @@ -84,4 +85,9 @@ return false; } + public static boolean hasNoArgsContrustor(Class clazz) { + return Stream.of(clazz.getConstructors()) + .allMatch(constructor -> constructor.getParameterCount() == 0); + } + } diff --git a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java new file mode 100644 index 0000000..1a44ffd --- /dev/null +++ b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java @@ -0,0 +1,50 @@ +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.Insert; +import com.ljunggren.storm.annotation.Select; +import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.context.ContextFactory; + +public class InsertQueryTest { + + @Database(context = "H2") + private interface UserRepository { + + @Insert(sql = "insert into users (firstname, lastname, employee_id) values (?, ?, ?)") + public int add(String firstName, String lastName, int employeeID); + + @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 test() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + long beforeCount = repository.count(); + int inserts = repository.add("Jane", "Doe", 104); + long afterCount = repository.count(); + assertEquals(1, inserts); + assertTrue(beforeCount < afterCount); + } + +} diff --git a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java index 6b18dcc..4124745 100644 --- a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java @@ -68,6 +68,13 @@ } @Test + public void findByIdNoResultTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + TestUser user = repository.findById(999); + assertNull(user); + } + + @Test public void findByFirstAndLastNameTest() { UserRepository repository = StormRepository.newInstance(UserRepository.class); TestUser user = repository.findByFirstAndLastName("Gage", "Ljunggren"); diff --git a/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java index 9a45f29..df69b9d 100644 --- a/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java +++ b/src/test/java/com/ljunggren/storm/utils/ReflectionUtilsTest.java @@ -11,6 +11,7 @@ import org.junit.Test; +import com.ljunggren.storm.TestUser; import com.ljunggren.storm.utils.ReflectionUtils; public class ReflectionUtilsTest { @@ -26,6 +27,7 @@ Set intergerSet; int primitiveInteger; Integer objectInteger; + TestUser testUser; @Test public void getOwnerTypeNullTest() { @@ -212,4 +214,18 @@ assertFalse(ReflectionUtils.isSet(fieldType)); } + @Test + public void hasNoArgsConstructorTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("testUser"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.hasNoArgsContrustor((Class) fieldType)); + } + + @Test + public void hasNoArgsConstructorFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.hasNoArgsContrustor((Class) fieldType)); + } + } diff --git a/src/test/resources/com/ljunggren/storm/CREATE_DB.sql b/src/test/resources/com/ljunggren/storm/CREATE_DB.sql index c71ddfc..6109774 100644 --- a/src/test/resources/com/ljunggren/storm/CREATE_DB.sql +++ b/src/test/resources/com/ljunggren/storm/CREATE_DB.sql @@ -1,13 +1,13 @@ drop table users if exists; create table users ( - id int, + id int auto_increment, firstname varchar(20), lastname varchar(20), employee_id int ); -insert into users (id, firstname, lastname, employee_id) values(1, 'Alex', 'Ljunggren', 100); -insert into users (id, firstname, lastname, employee_id) values(2, 'Christie', 'Ljunggren', 101); -insert into users (id, firstname, lastname, employee_id) values(3, 'Gage', 'Ljunggren', 102); -insert into users (id, firstname, lastname, employee_id) values(4, 'John', 'Doe', 103); +insert into users (firstname, lastname, employee_id) values('Alex', 'Ljunggren', 100); +insert into users (firstname, lastname, employee_id) values('Christie', 'Ljunggren', 101); +insert into users (firstname, lastname, employee_id) values('Gage', 'Ljunggren', 102); +insert into users (firstname, lastname, employee_id) values('John', 'Doe', 103);