Skip to content

Commit

Permalink
Merge pull request #37 from Scalified/#36
Browse files Browse the repository at this point in the history
#36 RAW Queries
  • Loading branch information
vbaidak committed Sep 4, 2020
2 parents e69fbbd + c770edc commit 5968526
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 72 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.0

* [**#36** RAW Queries](https://github.com/Scalified/jpa/issues/36)

# 2.0.0

* [**#34** Java EE 8.0 Update](https://github.com/Scalified/jpa/issues/34)
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,28 @@ during stream consuming, the new data will also be included into result set.

### Query DSL

**Query** DSL provides convenient way of stored procedure queries execution
**Query** DSL provides convenient way for queries execution

#### Raw SQL Queries Execution

```java
Jpa jpa;
// ... jpa initialization skipped

// Raw SQL query
String sql = "SELECT * FROM PERSON"

// Executing raw SQL query and mapping results to set
Set<Person> resultSet = jpa.query(query, Person.class).set();

// Executing raw SQL query and mapping results to list
List<Person> resultList = jpa.query(query, Person.class).list();

// Executing raw SQL query and retrieving optional result
Optional<Person> optionalResult = jpa.query(query, Person.class).first();
```

#### Stored Procedures Execution

```java
Jpa jpa;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
allprojects {

group = "com.scalified"
version = "2.0.0"
version = "2.1.0"

val javaeeVersion by extra("8.0")

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
23 changes: 17 additions & 6 deletions jpa/src/main/java/com/scalified/jpa/Jpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.scalified.jpa.dsl.find.JpaFindByEntityClassDsl;
import com.scalified.jpa.dsl.find.JpaFindBySpecificationDsl;
import com.scalified.jpa.dsl.from.JpaFromDsl;
import com.scalified.jpa.dsl.query.JpaSpQueryDsl;
import com.scalified.jpa.dsl.query.JpaQueryDsl;
import com.scalified.jpa.function.CriteriaFunction;
import com.scalified.jpa.sp.SpQuery;
import com.scalified.jpa.specification.Specification;
Expand Down Expand Up @@ -83,14 +83,25 @@ public interface Jpa {
<T> JpaFindBySpecificationDsl<T> find(Specification<T> specification);

/**
* Returns {@link JpaSpQueryDsl} object, which provides the next <b>DSL</b>
* Returns {@link JpaQueryDsl} object, which provides the next <b>DSL</b>
* methods within <b>DSL</b> call chain to execute raw SQL query
*
* @param sql raw SQL query
* @param entityClass class of the result entities
* @param <T> type of the result entities
* @return {@link JpaQueryDsl} object
*/
<T> JpaQueryDsl<T> query(String sql, Class<T> entityClass);

/**
* Returns {@link JpaQueryDsl} object, which provides the next <b>DSL</b>
* methods within <b>DSL</b> call chain to execute stored procedure
*
* @param spQuery stored procedure configuration object
* @param <T> type of result
* @return {@link JpaSpQueryDsl} object
* @param <T> type of the result
* @return {@link JpaQueryDsl} object
*/
<T> JpaSpQueryDsl<T> query(SpQuery<T> spQuery);
<T> JpaQueryDsl<T> query(SpQuery<T> spQuery);

/**
* Returns {@link JpaFromDsl} object, which provides the next <b>DSL</b> methods
Expand Down Expand Up @@ -118,7 +129,7 @@ public interface Jpa {
*
* @param entities a collection of entity instances
* @param <T> type of an entity
* @param <K> type of entities collection
* @param <K> type of the entities collection
* @return {@link JpaEntitiesDsl} object
*/
<T, K extends Collection<T>> JpaEntitiesDsl<T> entities(K entities);
Expand Down
27 changes: 21 additions & 6 deletions jpa/src/main/java/com/scalified/jpa/JpaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import com.scalified.jpa.dsl.find.*;
import com.scalified.jpa.dsl.from.JpaFromDsl;
import com.scalified.jpa.dsl.from.JpaFromDslImpl;
import com.scalified.jpa.dsl.query.JpaSpQueryDsl;
import com.scalified.jpa.dsl.query.JpaQueryDsl;
import com.scalified.jpa.dsl.query.JpaQueryDslImpl;
import com.scalified.jpa.dsl.query.JpaSpQueryDslImpl;
import com.scalified.jpa.function.CriteriaFunction;
import com.scalified.jpa.manager.JpaManager;
Expand Down Expand Up @@ -116,15 +117,29 @@ public <T> JpaFindBySpecificationDsl<T> find(Specification<T> specification) {
}

/**
* Returns {@link JpaSpQueryDsl} object, which provides the next <b>DSL</b>
* Returns {@link JpaQueryDsl} object, which provides the next <b>DSL</b>
* methods within <b>DSL</b> call chain to execute raw SQL query
*
* @param sql raw SQL query
* @param entityClass class of the result entities
* @param <T> type of the result entities
* @return {@link JpaQueryDsl} object
*/
@Override
public <T> JpaQueryDsl<T> query(String sql, Class<T> entityClass) {
return new JpaQueryDslImpl<>(manager, sql, entityClass);
}

/**
* Returns {@link JpaQueryDsl} object, which provides the next <b>DSL</b>
* methods within <b>DSL</b> call chain to execute stored procedure
*
* @param spQuery stored procedure configuration object
* @param <T> type of result
* @return {@link JpaSpQueryDsl} object
* @param <T> type of the result
* @return {@link JpaQueryDsl} object
*/
@Override
public <T> JpaSpQueryDsl<T> query(SpQuery<T> spQuery) {
public <T> JpaQueryDsl<T> query(SpQuery<T> spQuery) {
return new JpaSpQueryDslImpl<>(manager, spQuery);
}

Expand Down Expand Up @@ -160,7 +175,7 @@ public <T> JpaEntityDsl<T> entity(T entity) {
*
* @param entities a collection of entity instances
* @param <T> type of an entity
* @param <K> type of entities collection
* @param <K> type of the entities collection
* @return {@link JpaEntitiesDsl} object
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public interface JpaFindByCriteriaFunctionDsl<T> {
* and after applying the specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @param <R> type of the generic result
* @return some generic result
*/
<R> R some(ResultFunction<T, R> resultFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Set<T> set() {
* and after applying the specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @param <R> type of the generic result
* @return some generic result
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface JpaFindByEntityClassDsl<T> {
* specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @param <R> type of the generic result
* @return some generic result
*/
<R> R some(ResultFunction<T, R> resultFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Set<T> set() {
* specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @param <R> type of the generic result
* @return some generic result
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface JpaFindBySpecificationDsl<T> {
* and after applying the specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @param <R> type of the generic result
* @return some generic result
*/
<R> R some(ResultFunction<T, R> resultFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Set<T> set() {
* and after applying the specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @param <R> type of the generic result
* @return some generic result
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,43 @@

package com.scalified.jpa.dsl.query;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* <b>DSL</b> for executing stored procedures
* <b>DSL</b> for executing queries
*
* @author shell
* @since 2018-08-18
*/
public interface JpaSpQueryDsl<T> {
public interface JpaQueryDsl<T> {

/**
* Returns a list of all results produced by stored procedure execution
* Returns a list of all results produced by query execution
*
* @return a list of all results
*/
List<T> list();

/**
* Returns a set of all results produced by stored procedure execution
* Returns a set of all results produced by query execution
*
* @return a set of all results
*/
Set<T> set();
default Set<T> set() {
return new LinkedHashSet<>(list());
}

/**
* Returns the first result produced by stored procedure execution
* Returns the first result produced by query execution
*
* @return the first result
*/
Optional<T> first();
default Optional<T> first() {
return list().stream()
.findFirst();
}

}
78 changes: 78 additions & 0 deletions jpa/src/main/java/com/scalified/jpa/dsl/query/JpaQueryDslImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2018 Scalified
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package com.scalified.jpa.dsl.query;

import com.scalified.jpa.manager.JpaManager;

import java.util.List;

/**
* A {@link JpaQueryDsl} implementation for raw SQL queries
*
* @author shell
* @since 2018-08-18
*/
public class JpaQueryDslImpl<T> implements JpaQueryDsl<T> {

/**
* An underlying {@link JpaManager}
*/
private final JpaManager manager;

/**
* Raw SQL query
*/
private final String sql;

/**
* Type of the result entities
*/
private final Class<T> entityClass;

/**
* Creates {@link JpaQueryDsl} instance
*
* @param manager an underlying {@link JpaManager}
* @param sql raw SQL query
* @param entityClass class of the result entities
*/
public JpaQueryDslImpl(JpaManager manager, String sql, Class<T> entityClass) {
this.manager = manager;
this.sql = sql;
this.entityClass = entityClass;
}

/**
* Returns a list of all results produced by stored procedure execution
*
* @return a list of all results
*/
@Override
public List<T> list() {
return manager.query(sql, entityClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@
import com.scalified.jpa.manager.JpaManager;
import com.scalified.jpa.sp.SpQuery;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* A {@link JpaSpQueryDsl} implementation
* A {@link JpaQueryDsl} implementation for stored procedures execution
*
* @author shell
* @since 2018-08-18
*/
public class JpaSpQueryDslImpl<T> implements JpaSpQueryDsl<T> {
public class JpaSpQueryDslImpl<T> implements JpaQueryDsl<T> {

/**
* An underlying {@link JpaManager}
Expand All @@ -52,7 +49,7 @@ public class JpaSpQueryDslImpl<T> implements JpaSpQueryDsl<T> {
private final SpQuery<T> query;

/**
* Creates {@link JpaSpQueryDsl} instance
* Creates {@link JpaQueryDsl} instance
*
* @param manager an underlying {@link JpaManager}
* @param query {@link SpQuery} used to execute stored procedure
Expand All @@ -72,25 +69,4 @@ public List<T> list() {
return manager.query(query);
}

/**
* Returns a set of all results produced by stored procedure execution
*
* @return a set of all results
*/
@Override
public Set<T> set() {
return new LinkedHashSet<>(list());
}

/**
* Returns the first result produced by stored procedure execution
*
* @return the first result
*/
@Override
public Optional<T> first() {
return list().stream()
.findFirst();
}

}
Loading

0 comments on commit 5968526

Please sign in to comment.