diff --git a/.github/badges/jacoco.svg b/.github/badges/jacoco.svg
index f5179a5..abc8b4a 100644
--- a/.github/badges/jacoco.svg
+++ b/.github/badges/jacoco.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/README.md b/README.md
index 7ec3dab..cfb709c 100644
--- a/README.md
+++ b/README.md
@@ -5,30 +5,29 @@
[![branches](.github/badges/branches.svg)](branches.svg)
## Why?
-Have you tried the latest/greatest ORM frameworks out there yet?
+Have you tried the latest/greatest ORM frameworks out there yet?
-There are more than I can count, each one of them makes some kind of promise to you.
Most of them want to remove you from the RDBMS layer as much as possible. Some use meta-programming
to generate the model in runtime, some are statically typed and require us to define the schema
in the application layer so that a rich DSL layer can be used.
-Reflecting on this, you may find yourself needing to be close to SQL and all that
+Reflecting on this, you may find yourself needing to stay close to SQL and all that
it has to offer in flexibility and transparency. Tailor each query for optimal retrieval, using
-your knowledge of to put together a performant query. At the same time, have a good enough api to
-help you with the boring,silly stuff. Just enough to help with the needed
-insert,update and simple select statements. Find that middle ground of convention and flexibility.
-How low level can we stay and still be productive with RDBMS? This little library was born from
-having to work with so many frameworks, between `spring-data` and `rails` and many other
-specialized libraries.
+your knowledge to put together a performant query. At the same time, have a good enough api to
+help you with the boring stuff. Just enough to help with the needed
+insert, update and simple select statements. Find that middle ground of convention and flexibility.
+How low level can we stay and still be productive with RDBMS? This little library was created as a
+reflection on other frameworks I worked with `spring-data`, `rails` and the likes, among others.
## How?
-Clearly, we start of with the fact that we are on the JVM. Taking advantage of one of the most
+Starting of with the fact that we are on the JVM and using Java. Taking advantage of one of the most
prolific frameworks out there, which is `spring-jdbc`. An already slim, low level library.
Between `spring-jdbc` and Springs next flagship library `spring-data`,
this little project sits right in between. We don't do any meta-programming (autogenerate interfaces),
we don't generate any clever queries. Instead, we help with the most boring parts and leave the power
-and flexibility and a level of complexity to you. With some good guidelines, best practices and test cases, you may find this library useful.
+and flexibility and a level of complexity to you. With some good guidelines, best practices and test cases,
+you may find this library useful.
## Details
@@ -66,7 +65,9 @@ Beyond that, extend the class and define your desired helper methods for additio
Most mapping needs can be delegated over to `PersistableRowMapper`. Only if you want to take full control of
how every column, field and foreign relationships is mapped, you will want to implement
`PersistableMapper` and go from there. For an advanced example of this, see
-[ProposalMapper](src/test/java/org/viablespark/persistence/ProposalMapper.java)
+[ProposalMapper](src/test/java/org/viablespark/persistence/ProposalMapper.java) This is the most laborious
+part of the library, queries will need to match entities fields, so often you will have to spell out
+the select statement with `column as renamed`. A tradeoff I can live with to retain fine control.
### Test Cases
diff --git a/build.gradle.kts b/build.gradle.kts
index 68b9cee..77d855d 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -18,7 +18,7 @@ plugins {
}
group = "org.viablespark"
-version = "1.3.0"
+version = "1.4.0"
repositories {
mavenCentral()
@@ -75,7 +75,7 @@ publishing {
create("maven") {
groupId = "org.viablespark"
artifactId = "goodenough-jdbc"
- version = "1.3.0"
+ version = "1.4.0"
from(components["java"])
pom {
diff --git a/src/main/java/org/viablespark/persistence/BaseRepository.java b/src/main/java/org/viablespark/persistence/BaseRepository.java
index f778a75..dd862c3 100644
--- a/src/main/java/org/viablespark/persistence/BaseRepository.java
+++ b/src/main/java/org/viablespark/persistence/BaseRepository.java
@@ -59,18 +59,18 @@ public Optional save(E entity) throws SQLException {
public void delete(E entity) {
String sql = "DELETE FROM " + deriveEntityName(entity.getClass())
- + " WHERE " + entity.getKey().getPrimaryKey().key + "=? ";
+ + " WHERE " + entity.getKey().primaryKey().getKey() + "=? ";
- jdbc.update(sql, entity.getKey().getPrimaryKey().value);
+ jdbc.update(sql, entity.getKey().primaryKey().getValue());
}
public Optional get(Key key, Class cls) throws NoSuchElementException {
List list = jdbc.query(
- "SELECT " + WithSql.getSQLSelectClause(cls, key.getPrimaryKey().key)
+ "SELECT " + WithSql.getSQLSelectClause(cls, key.primaryKey().getKey())
+ " FROM " + deriveEntityName(cls)
- + " WHERE " + key.getPrimaryKey().key + "=?",
+ + " WHERE " + key.primaryKey().getKey() + "=?",
new PersistableRowMapper<>(cls),
- key.getPrimaryKey().value);
+ key.primaryKey().getValue());
Optional res = list.stream().findFirst();
res.ifPresent(e -> e.setKey(key));
diff --git a/src/main/java/org/viablespark/persistence/Key.java b/src/main/java/org/viablespark/persistence/Key.java
index cba36d0..15a4c39 100644
--- a/src/main/java/org/viablespark/persistence/Key.java
+++ b/src/main/java/org/viablespark/persistence/Key.java
@@ -24,8 +24,11 @@ public class Key implements Serializable {
private final Map> keys = new LinkedHashMap<>();
+ public Key() {
+ }
+
public Key(Pair primaryKey) {
- keys.put(primaryKey.key, primaryKey);
+ keys.put(primaryKey.getKey(), primaryKey);
}
public static Key of(String key, Long value){
@@ -47,7 +50,7 @@ public Key add(String name, Long key) {
return this;
}
- public int getCount() {
+ public int count() {
return keys.size();
}
@@ -55,6 +58,10 @@ public Collection> getKeys() {
return keys.values();
}
+ public void setKeys(Collection> _keys){
+ _keys.forEach( pair -> keys.put(pair.getKey(),pair));
+ }
+
public Pair getAt(int index) {
int counter=0;
for (Entry> entry : keys.entrySet()) {
@@ -71,7 +78,7 @@ public Long getKey(String name) {
if( found == null ){
throw new RuntimeException("Key " + name + " not found. Other keys?" + this);
}
- return found.value;
+ return found.getValue();
}
public Optional> contains(String strKey) {
@@ -88,11 +95,11 @@ public boolean equals(Object other) {
return false;
}
- if (((Key) other).getCount() != getCount()) {
+ if (((Key) other).count() != count()) {
return false;
}
- for (int i = 0; i < getCount(); i++) {
+ for (int i = 0; i < count(); i++) {
if (!getAt(i).equals(((Key) other).getAt(i))) {
return false;
}
@@ -104,13 +111,13 @@ public boolean equals(Object other) {
@Override
public int hashCode() {
int hash = 0;
- for (int i = 0; i < getCount(); i++) {
+ for (int i = 0; i < count(); i++) {
hash += getAt(i).hashCode();
}
return hash;
}
- public Pair getPrimaryKey() {
+ public Pair primaryKey() {
return getAt(0);
}
@@ -118,7 +125,7 @@ public Pair getPrimaryKey() {
public String toString() {
StringBuilder str = new StringBuilder("Key(s) ");
for (Pair key : keys.values()) {
- str.append(key.key).append("=").append(key.value).append(" ");
+ str.append(key.getKey()).append("=").append(key.getValue()).append(" ");
}
return str.toString();
}
diff --git a/src/main/java/org/viablespark/persistence/Pair.java b/src/main/java/org/viablespark/persistence/Pair.java
index 62d68c8..fb802f3 100644
--- a/src/main/java/org/viablespark/persistence/Pair.java
+++ b/src/main/java/org/viablespark/persistence/Pair.java
@@ -17,14 +17,33 @@
public class Pair {
- public final T key;
- public final Y value;
+ private T key;
+ private Y value;
public Pair(T key, Y value) {
this.key = key;
this.value = value;
}
+ public Pair() {
+ }
+
+ public T getKey() {
+ return key;
+ }
+
+ public void setKey(T key) {
+ this.key = key;
+ }
+
+ public Y getValue() {
+ return value;
+ }
+
+ public void setValue(Y value) {
+ this.value = value;
+ }
+
public static Pair of(T key, Y value) {
return new Pair<>(key, value);
}
diff --git a/src/main/java/org/viablespark/persistence/Persistable.java b/src/main/java/org/viablespark/persistence/Persistable.java
index d32783f..5614cd8 100644
--- a/src/main/java/org/viablespark/persistence/Persistable.java
+++ b/src/main/java/org/viablespark/persistence/Persistable.java
@@ -20,7 +20,7 @@ public interface Persistable {
void setKey(Key key);
default Long getId() {
- return getKey().getPrimaryKey().value;
+ return getKey().primaryKey().getValue();
}
default boolean isNew() {
diff --git a/src/main/java/org/viablespark/persistence/dsl/SqlQuery.java b/src/main/java/org/viablespark/persistence/dsl/SqlQuery.java
index 95d1172..9e60c14 100644
--- a/src/main/java/org/viablespark/persistence/dsl/SqlQuery.java
+++ b/src/main/java/org/viablespark/persistence/dsl/SqlQuery.java
@@ -81,20 +81,20 @@ public String sql() {
}
Optional whereResult = where.stream()
- .map(p -> p.key)
+ .map(Pair::getKey)
.reduce((acc, a) -> "" + acc + " AND " + a);
String whereStr = whereResult.map(s -> "WHERE " + s).orElse(" ");
Optional condResult = conditions.stream()
- .map(p -> p.key)
+ .map(Pair::getKey)
.reduce((acc, a) -> "" + acc + " " + a);
String condStr = condResult.orElse("");
- String orderStr = orderBy != null ? " ORDER BY " +orderBy.key + " " + orderBy.value.toString() : "";
+ String orderStr = orderBy != null ? " ORDER BY " +orderBy.getKey() + " " + orderBy.getValue().toString() : "";
- String limitStr = limit != null ? " LIMIT "+limit.key : "";
+ String limitStr = limit != null ? " LIMIT "+limit.getKey() : "";
return whereStr + condStr + orderStr + limitStr;
}
@@ -104,8 +104,8 @@ public Object[] values() {
return rawValues;
}
List