Skip to content

Commit a954e80

Browse files
committed
Added a fork() method to the SQL interface and implemented a thread local session helper to locally allow overwriting certain settings (currently only the connection used)
1 parent a5f488b commit a954e80

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.javawebstack.orm;
2+
3+
import org.javawebstack.orm.wrapper.SQL;
4+
5+
import java.util.function.Consumer;
6+
7+
public class Session {
8+
9+
private static ThreadLocal<Session> sessions = new ThreadLocal<>();
10+
11+
public static Session current() {
12+
return sessions.get();
13+
}
14+
15+
private SQL connection;
16+
17+
private Session() {
18+
19+
}
20+
21+
public Session via(SQL connection) {
22+
this.connection = connection;
23+
return this;
24+
}
25+
26+
public SQL getConnection() {
27+
return connection;
28+
}
29+
30+
public static void session(Consumer<Session> consumer) {
31+
Session session = begin();
32+
consumer.accept(session);
33+
end();
34+
}
35+
36+
public static Session begin() {
37+
Session session = new Session();
38+
sessions.set(session);
39+
return session;
40+
}
41+
42+
public static void end() {
43+
sessions.remove();
44+
}
45+
46+
}

src/main/java/org/javawebstack/orm/query/Query.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.javawebstack.orm.Model;
44
import org.javawebstack.orm.Repo;
55
import org.javawebstack.orm.SQLMapper;
6+
import org.javawebstack.orm.Session;
67
import org.javawebstack.orm.exception.ORMQueryException;
8+
import org.javawebstack.orm.wrapper.SQL;
79
import org.javawebstack.orm.wrapper.builder.SQLQueryString;
810

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

2022
private final Repo<T> repo;
2123
private final Class<T> model;
24+
private SQL connection;
2225
private List<String> select = new ArrayList<>();
2326
private final QueryGroup<T> where = new QueryGroup<>();
2427
private Integer offset;
@@ -35,6 +38,15 @@ public Query(Class<T> model) {
3538
public Query(Repo<T> repo, Class<T> model) {
3639
this.repo = repo;
3740
this.model = model;
41+
Session session = Session.current();
42+
this.connection = repo.getConnection();
43+
if(session != null && session.getConnection() != null)
44+
this.connection = session.getConnection();
45+
}
46+
47+
public Query<T> via(SQL connection) {
48+
this.connection = connection;
49+
return this;
3850
}
3951

4052
public boolean isWithDeleted() {
@@ -356,9 +368,9 @@ public Query<T> withDeleted() {
356368
}
357369

358370
public void finalDelete() {
359-
SQLQueryString qs = repo.getConnection().builder().buildDelete(this);
371+
SQLQueryString qs = connection.builder().buildDelete(this);
360372
try {
361-
repo.getConnection().write(qs.getQuery(), qs.getParameters().toArray());
373+
connection.write(qs.getQuery(), qs.getParameters().toArray());
362374
} catch (SQLException throwables) {
363375
throw new ORMQueryException(throwables);
364376
}
@@ -385,11 +397,11 @@ public void restore() {
385397
}
386398

387399
public T refresh(T entity) {
388-
SQLQueryString qs = repo.getConnection().builder().buildQuery(this);
400+
SQLQueryString qs = connection.builder().buildQuery(this);
389401
try {
390-
ResultSet rs = repo.getConnection().read(qs.getQuery(), qs.getParameters().toArray());
402+
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
391403
SQLMapper.mapBack(repo, rs, entity);
392-
repo.getConnection().close(rs);
404+
connection.close(rs);
393405
return entity;
394406
} catch (SQLException throwables) {
395407
throw new ORMQueryException(throwables);
@@ -401,20 +413,20 @@ public void update(T entity) {
401413
}
402414

403415
public void update(Map<String, Object> values) {
404-
SQLQueryString queryString = repo.getConnection().builder().buildUpdate(this, values);
416+
SQLQueryString queryString = connection.builder().buildUpdate(this, values);
405417
try {
406-
repo.getConnection().write(queryString.getQuery(), queryString.getParameters().toArray());
418+
connection.write(queryString.getQuery(), queryString.getParameters().toArray());
407419
} catch (SQLException throwables) {
408420
throw new ORMQueryException(throwables);
409421
}
410422
}
411423

412424
public List<T> all() {
413-
SQLQueryString qs = repo.getConnection().builder().buildQuery(this);
425+
SQLQueryString qs = connection.builder().buildQuery(this);
414426
try {
415-
ResultSet rs = repo.getConnection().read(qs.getQuery(), qs.getParameters().toArray());
427+
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
416428
List<T> list = SQLMapper.map(repo, rs, new ArrayList<>());
417-
repo.getConnection().close(rs);
429+
connection.close(rs);
418430
return list;
419431
} catch (SQLException throwables) {
420432
throw new ORMQueryException(throwables);
@@ -437,13 +449,13 @@ public Stream<T> stream() {
437449
}
438450

439451
public int count() {
440-
SQLQueryString qs = repo.getConnection().builder().buildQuery(this.select("count(*)"));
452+
SQLQueryString qs = connection.builder().buildQuery(this.select("count(*)"));
441453
try {
442-
ResultSet rs = repo.getConnection().read(qs.getQuery(), qs.getParameters().toArray());
454+
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
443455
int c = 0;
444456
if (rs.next())
445457
c = rs.getInt(1);
446-
repo.getConnection().close(rs);
458+
connection.close(rs);
447459
return c;
448460
} catch (SQLException throwables) {
449461
throw new ORMQueryException(throwables);

src/main/java/org/javawebstack/orm/wrapper/MySQL.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public MySQL(String host, int port, String database, String username, String pas
3232
this.timeout = timeout * 1000L;
3333
}
3434

35+
public SQL fork() {
36+
return new MySQL(host, port, database, username, password, (int) (timeout / 1000L));
37+
}
38+
3539
public Connection getConnection() {
3640
long now = System.currentTimeMillis();
3741
if (now > lastQuery + timeout) {

src/main/java/org/javawebstack/orm/wrapper/SQL.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public interface SQL {
2222

2323
void removeQueryLogger(QueryLogger logger);
2424

25+
SQL fork();
26+
2527
}

src/main/java/org/javawebstack/orm/wrapper/SQLite.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public SQLite(String file) {
1717
this.file = file;
1818
}
1919

20+
public SQL fork() {
21+
return new SQLite(file);
22+
}
23+
2024
public Connection getConnection() {
2125
try {
2226
if (c == null || c.isClosed()) {

0 commit comments

Comments
 (0)