Skip to content

adding some sanity things #3

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
24 changes: 13 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
<groupId>com.gullerya</groupId>
<version>1.0-SNAPSHOT</version>

<name>SQL DSL (Java)</name>
<name>SQL DSL</name>
<description>
SQL queries builder and executor, implemented in Java. Featured by strongly typed validations, simplicity of
use.
SQL queries builder and executor.
JPA standard annotations compliant, featured by strongly typed validations, very simple to use.
</description>
<url>https://github.com/gullerya/sql-dsl-java</url>

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<name>ISC License</name>
<url>https://opensource.org/licenses/ISC</url>
</license>
</licenses>

Expand All @@ -40,13 +40,15 @@

<properties>
<jpa>2.2</jpa>
<slf4j>1.7.32</slf4j>
<slf4j>1.7.36</slf4j>

<!--BUILD-->
<jdk.version>17</jdk.version>
<maven-clean-plugin>3.1.0</maven-clean-plugin>
<maven-resources-plugin>3.2.0</maven-resources-plugin>
<maven-compiler-plugin>3.8.1</maven-compiler-plugin>

<!--PACK-->
<maven-jar-plugin>3.2.0</maven-jar-plugin>
<maven-source-plugin>3.2.0</maven-source-plugin>
<maven-javadoc-plugin>3.3.1</maven-javadoc-plugin>
Expand All @@ -59,14 +61,14 @@

<!--QUALITY-->
<skipTests>true</skipTests>
<log4j-slf4j-impl>2.14.1</log4j-slf4j-impl>
<junit>5.8.1</junit>
<log4j-slf4j-impl>2.17.0</log4j-slf4j-impl>
<junit>5.8.2</junit>
<spotbugs>4.4.2</spotbugs>
<maven-surefire-plugin>3.0.0-M5</maven-surefire-plugin>
<jacoco-maven-plugin>0.8.7</jacoco-maven-plugin>
<hikaricp>5.0.0</hikaricp>
<mssql-jdbc>9.4.0.jre16</mssql-jdbc>
<postgresql>42.3.1</postgresql>
<hikaricp>5.0.1</hikaricp>
<postgresql>42.3.3</postgresql>
<mssql-jdbc>10.2.0.jre17</mssql-jdbc>
</properties>

<dependencies>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/gullerya/sqldsl/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import com.gullerya.sqldsl.api.statements.Select;

import java.util.Map;

public interface Join {

<T, K> void inner(Select.SelectDownstream<T> selectionA, Select.SelectDownstream<K> selectionB);
static Map<String, ?> inner(Select.SelectDownstream<?>... joiners) {
return null;
}

<T, K> void toward(Class<T> mainEntity, Select.SelectDownstream<T> selectionA, Select.SelectDownstream<K> selectionB);
static Map<String, ?> toward(Select.SelectDownstream<?> primaryEntity, Select.SelectDownstream<?>... joiners) {
return null;
}

}
6 changes: 0 additions & 6 deletions src/main/java/com/gullerya/sqldsl/UpdateTermAction.java

This file was deleted.

9 changes: 1 addition & 8 deletions src/main/java/com/gullerya/sqldsl/api/clauses/Where.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ public interface Where<S> {

S where(WhereClause where);

final class WhereFieldValuePair {
public final String column;
public final Object value;

private WhereFieldValuePair(String column, Object value) {
this.column = column;
this.value = value;
}
final record WhereFieldValuePair(String column, Object value) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface Delete<T> {
* @param whereClause where clause; MUST NOT be NULL
* @return number of affected rows
*/
int deleteAll(Where.WhereClause whereClause);
int delete(Where.WhereClause whereClause);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Set;

import com.gullerya.sqldsl.SelectTermAction;
import com.gullerya.sqldsl.api.clauses.GroupBy;
import com.gullerya.sqldsl.api.clauses.Having;
import com.gullerya.sqldsl.api.clauses.OrderBy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.gullerya.sqldsl;
package com.gullerya.sqldsl.api.statements;

import java.util.List;

public interface SelectTermAction<T> {
interface SelectTermAction<T> {

/**
* read a single entity
Expand All @@ -21,20 +21,20 @@ public interface SelectTermAction<T> {
List<T> readAll();

/**
* read all entities matching query, if any
* read a limited number of entities matching query, if any
* - no offset applied
*
* @param limit limit number; MUST be greater than 0
* @return list of entities; MAY be an EMPTY list; MAY NOT be NULL
*/
List<T> readAll(int limit);
List<T> read(int limit);

/**
* read all entities matching query, if any
* read a limited number of entities matching query, if any
*
* @param offset offset number; MUST be greater than 0
* @param limit limit number; MUST be greater than 0
* @return list of entities; MAY be an EMPTY list; MAY NOT be NULL
*/
List<T> readAll(int offset, int limit);
List<T> read(int offset, int limit);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gullerya.sqldsl.api.statements;

import com.gullerya.sqldsl.Literal;
import com.gullerya.sqldsl.UpdateTermAction;
import com.gullerya.sqldsl.api.clauses.Where;

public interface Update<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.gullerya.sqldsl.api.statements;

interface UpdateTermAction {

int all();
}
14 changes: 8 additions & 6 deletions src/main/java/com/gullerya/sqldsl/impl/EntityDALImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
import com.gullerya.sqldsl.EntityDAL;
import com.gullerya.sqldsl.Literal;
import com.gullerya.sqldsl.api.clauses.Where;
import com.gullerya.sqldsl.api.statements.Delete;
import com.gullerya.sqldsl.api.statements.Insert;
import com.gullerya.sqldsl.api.statements.Select;
import com.gullerya.sqldsl.api.statements.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EntityDALImpl<T> implements EntityDAL<T> {
private static final Logger logger = LoggerFactory.getLogger(EntityDALImpl.class);
private final ESConfig<T> config;

public EntityDALImpl(Class<T> entityType, DataSource ds) throws ReflectiveOperationException {
Expand All @@ -30,8 +29,8 @@ public int deleteAll() {
}

@Override
public int deleteAll(Where.WhereClause whereClause) {
return new StatementDeleteImpl<>(config).deleteAll(whereClause);
public int delete(Where.WhereClause whereClause) {
return new StatementDeleteImpl<>(config).delete(whereClause);
}

@Override
Expand Down Expand Up @@ -61,6 +60,9 @@ public UpdateDownstream update(T entity, Literal... literals) {

record ESConfig<ET>(DataSource ds, EntityMetaProc<ET> em) {
<R> R prepareStatementAndDo(String sql, PreparedStatementAction<R> psAction) {
if (logger.isDebugEnabled()) {
logger.debug(sql);
}
try (Connection c = ds.getConnection(); PreparedStatement s = c.prepareStatement(sql)) {
return psAction.execute(s);
} catch (SQLException sqle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public int deleteAll() {
}

@Override
public int deleteAll(Where.WhereClause where) {
public int delete(Where.WhereClause where) {
validateWhereClause(config.em(), where);
return internalDelete(where);
}
Expand All @@ -30,8 +30,8 @@ private int internalDelete(Where.WhereClause where) {
int i = 0;
for (Where.WhereFieldValuePair parameter : parametersCollector) {
i++;
FieldMetaProc fm = config.em().byColumn.get(parameter.column);
Object pv = fm.translateFieldToColumn(parameter.value);
FieldMetaProc fm = config.em().byColumn.get(parameter.column());
Object pv = fm.translateFieldToColumn(parameter.value());
s.setObject(i, pv);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ public List<T> readAll() {
}

@Override
public List<T> readAll(int limit) {
public List<T> read(int limit) {
if (limit == 0) {
throw new IllegalArgumentException("limit MUST be greater than 0");
}
return internalRead(null, limit);
}

@Override
public List<T> readAll(int offset, int limit) {
public List<T> read(int offset, int limit) {
if (offset == 0) {
throw new IllegalArgumentException("offset MUST be greater than 0 ('read' methods without offset exists)");
}
Expand Down Expand Up @@ -160,8 +160,8 @@ private List<T> internalRead(Integer offset, Integer limit) {
int i = 0;
for (WhereFieldValuePair parameter : parametersCollector) {
i++;
FieldMetaProc fm = config.em().byColumn.get(parameter.column);
Object pv = fm.translateFieldToColumn(parameter.value);
FieldMetaProc fm = config.em().byColumn.get(parameter.column());
Object pv = fm.translateFieldToColumn(parameter.value());
s.setObject(i, pv);
}
try (ResultSet rs = s.executeQuery()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ private int internalUpdate(WhereClause where) {
}
if (where != null) {
for (Where.WhereFieldValuePair parameter : parametersCollector) {
FieldMetaProc fm = config.em().byColumn.get(parameter.column);
fm.setColumnValue(s, ++i, parameter.value);
FieldMetaProc fm = config.em().byColumn.get(parameter.column());
fm.setColumnValue(s, ++i, parameter.value());
}
}
return s.executeUpdate();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module com.gullerya.sqldsl {
requires java.persistence;
requires java.sql;
requires org.slf4j;

exports com.gullerya.sqldsl;
exports com.gullerya.sqldsl.api.clauses;
exports com.gullerya.sqldsl.api.statements;
}
75 changes: 75 additions & 0 deletions src/test/java/com/gullerya/sqldsl/statements/JoinTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.gullerya.sqldsl.statements;

import com.gullerya.sqldsl.DBUtils;
import com.gullerya.sqldsl.Join;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.persistence.*;
import javax.sql.DataSource;
import java.time.LocalDate;
import java.util.*;


public class JoinTest {
private static final String TABLE_NAME_A = "JoinTestTableA";
private static final String TABLE_NAME_B = "JoinTestTableB";
private static final String TABLE_NAME_C = "JoinTestTableC";
private static final DataSource dataSource = DBUtils.getDataSource();

@BeforeAll
public static void prepare() throws Exception {
for (String tableName : Arrays.asList(TABLE_NAME_A, TABLE_NAME_B, TABLE_NAME_C)) {
dataSource.getConnection()
.prepareStatement(
"DROP TABLE IF EXISTS \"" + tableName + "\";" +
"CREATE TABLE \"" + tableName + "\" (" +
"id BIGINT," +
"state INT," +
"ready BOOLEAN," +
"first_name VARCHAR(128)," +
"last_name VARCHAR(128)," +
"birthday DATE," +
"created TIMESTAMP NOT NULL DEFAULT TIMEZONE('UTC', CURRENT_TIMESTAMP)," +
"updated TIMESTAMP," +
"locales VARCHAR(128)" +
");"
)
.execute();
}
}

@Test
public void basicE2EInner() {
Join.inner();
}

@Test
public void basicE2ETowards() {
Join.toward();
}

@Entity
@Table(name = TABLE_NAME_A)
public static final class User {

@Column(updatable = false, nullable = false)
public long id;
@Column(name = "first_name")
public String firstName;
@Column(name = "last_name")
public String lastName;
@Column
public LocalDate birthday;

public User() {
}

User(Long id, String firstName, String lastName, LocalDate birthday) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.birthday = birthday;
}
}
}
Loading