Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a5f488b
Incremented version to 1.0.2
JanHolger Nov 6, 2021
e316723
Update pom.xml
JulianFun123 Nov 8, 2021
b2290a5
Added parent class support for models.
x7airworker Nov 29, 2021
1aabe50
Added test case for non-abstract parent
x7airworker Nov 29, 2021
d9c3c2e
Added support for n amount of super classes
x7airworker Nov 30, 2021
1cace0b
Splitted table and column analysis into two methods
x7airworker Dec 2, 2021
c939910
Merge pull request #31 from JavaWebStack/feature/parentClass
JanHolger Dec 2, 2021
d46558d
Order of analyzeColumns and analyzeTable was wrong
x7airworker Dec 2, 2021
a6880f7
Merge pull request #32 from JavaWebStack/feature/parentClass
JanHolger Dec 2, 2021
a954e80
Added a fork() method to the SQL interface and implemented a thread l…
JanHolger Feb 14, 2022
a798304
Merge remote-tracking branch 'origin/dev' into dev
JanHolger Feb 14, 2022
75d8596
Added grouping for the Accessible in order to improve security.
JanHolger Mar 21, 2022
804d8aa
Fixed issue with empty where in combination with the accessible
JanHolger Mar 21, 2022
abe4f8b
Fixed issue with empty where in combination with the accessible
JanHolger Mar 21, 2022
4ea3b03
Fixed #34 (refresh throwing exception) and optimized imports
JanHolger May 1, 2022
a63f495
Merge remote-tracking branch 'origin/dev' into dev
JanHolger May 1, 2022
1b1a25c
Implemented #36 (don't render empty groups)
JanHolger May 1, 2022
9d146e8
Implemented #35 (render empty IN as 1=2)
JanHolger May 1, 2022
ce66dbe
Updated abstract-data to 1.0.2
JanHolger May 1, 2022
cf52c50
Fixed create not using the session system
JanHolger May 18, 2022
9f73d83
Added hotfix for missing braces in DefaultMapper and added support fo…
JanHolger Aug 13, 2022
309e612
Fixed data type for abstract data json mapper
JanHolger Aug 19, 2022
a0a1548
Upgraded abstract-data
JanHolger Oct 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added a fork() method to the SQL interface and implemented a thread l…
…ocal session helper to locally allow overwriting certain settings (currently only the connection used)
  • Loading branch information
JanHolger committed Feb 14, 2022
commit a954e800ab3a3b709dd4948acc4b9c7d3de171b0
46 changes: 46 additions & 0 deletions src/main/java/org/javawebstack/orm/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.javawebstack.orm;

import org.javawebstack.orm.wrapper.SQL;

import java.util.function.Consumer;

public class Session {

private static ThreadLocal<Session> sessions = new ThreadLocal<>();

public static Session current() {
return sessions.get();
}

private SQL connection;

private Session() {

}

public Session via(SQL connection) {
this.connection = connection;
return this;
}

public SQL getConnection() {
return connection;
}

public static void session(Consumer<Session> consumer) {
Session session = begin();
consumer.accept(session);
end();
}

public static Session begin() {
Session session = new Session();
sessions.set(session);
return session;
}

public static void end() {
sessions.remove();
}

}
38 changes: 25 additions & 13 deletions src/main/java/org/javawebstack/orm/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.javawebstack.orm.Model;
import org.javawebstack.orm.Repo;
import org.javawebstack.orm.SQLMapper;
import org.javawebstack.orm.Session;
import org.javawebstack.orm.exception.ORMQueryException;
import org.javawebstack.orm.wrapper.SQL;
import org.javawebstack.orm.wrapper.builder.SQLQueryString;

import java.sql.ResultSet;
Expand All @@ -19,6 +21,7 @@ public class Query<T extends Model> {

private final Repo<T> repo;
private final Class<T> model;
private SQL connection;
private List<String> select = new ArrayList<>();
private final QueryGroup<T> where = new QueryGroup<>();
private Integer offset;
Expand All @@ -35,6 +38,15 @@ public Query(Class<T> model) {
public Query(Repo<T> repo, Class<T> model) {
this.repo = repo;
this.model = model;
Session session = Session.current();
this.connection = repo.getConnection();
if(session != null && session.getConnection() != null)
this.connection = session.getConnection();
}

public Query<T> via(SQL connection) {
this.connection = connection;
return this;
}

public boolean isWithDeleted() {
Expand Down Expand Up @@ -356,9 +368,9 @@ public Query<T> withDeleted() {
}

public void finalDelete() {
SQLQueryString qs = repo.getConnection().builder().buildDelete(this);
SQLQueryString qs = connection.builder().buildDelete(this);
try {
repo.getConnection().write(qs.getQuery(), qs.getParameters().toArray());
connection.write(qs.getQuery(), qs.getParameters().toArray());
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
Expand All @@ -385,11 +397,11 @@ public void restore() {
}

public T refresh(T entity) {
SQLQueryString qs = repo.getConnection().builder().buildQuery(this);
SQLQueryString qs = connection.builder().buildQuery(this);
try {
ResultSet rs = repo.getConnection().read(qs.getQuery(), qs.getParameters().toArray());
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
SQLMapper.mapBack(repo, rs, entity);
repo.getConnection().close(rs);
connection.close(rs);
return entity;
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
Expand All @@ -401,20 +413,20 @@ public void update(T entity) {
}

public void update(Map<String, Object> values) {
SQLQueryString queryString = repo.getConnection().builder().buildUpdate(this, values);
SQLQueryString queryString = connection.builder().buildUpdate(this, values);
try {
repo.getConnection().write(queryString.getQuery(), queryString.getParameters().toArray());
connection.write(queryString.getQuery(), queryString.getParameters().toArray());
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}

public List<T> all() {
SQLQueryString qs = repo.getConnection().builder().buildQuery(this);
SQLQueryString qs = connection.builder().buildQuery(this);
try {
ResultSet rs = repo.getConnection().read(qs.getQuery(), qs.getParameters().toArray());
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
List<T> list = SQLMapper.map(repo, rs, new ArrayList<>());
repo.getConnection().close(rs);
connection.close(rs);
return list;
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
Expand All @@ -437,13 +449,13 @@ public Stream<T> stream() {
}

public int count() {
SQLQueryString qs = repo.getConnection().builder().buildQuery(this.select("count(*)"));
SQLQueryString qs = connection.builder().buildQuery(this.select("count(*)"));
try {
ResultSet rs = repo.getConnection().read(qs.getQuery(), qs.getParameters().toArray());
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
int c = 0;
if (rs.next())
c = rs.getInt(1);
repo.getConnection().close(rs);
connection.close(rs);
return c;
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/javawebstack/orm/wrapper/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public MySQL(String host, int port, String database, String username, String pas
this.timeout = timeout * 1000L;
}

public SQL fork() {
return new MySQL(host, port, database, username, password, (int) (timeout / 1000L));
}

public Connection getConnection() {
long now = System.currentTimeMillis();
if (now > lastQuery + timeout) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/javawebstack/orm/wrapper/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface SQL {

void removeQueryLogger(QueryLogger logger);

SQL fork();

}
4 changes: 4 additions & 0 deletions src/main/java/org/javawebstack/orm/wrapper/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public SQLite(String file) {
this.file = file;
}

public SQL fork() {
return new SQLite(file);
}

public Connection getConnection() {
try {
if (c == null || c.isClosed()) {
Expand Down