33import org .javawebstack .orm .Model ;
44import org .javawebstack .orm .Repo ;
55import org .javawebstack .orm .SQLMapper ;
6+ import org .javawebstack .orm .Session ;
67import org .javawebstack .orm .exception .ORMQueryException ;
8+ import org .javawebstack .orm .wrapper .SQL ;
79import org .javawebstack .orm .wrapper .builder .SQLQueryString ;
810
911import 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 );
0 commit comments