Package tech.muyan.utils
Class QueryHelper
- java.lang.Object
-
- tech.muyan.utils.QueryHelper
-
public class QueryHelper extends java.lang.Object
A utility class that provides methods for executing database queries using Groovy's Sql class.
-
-
Constructor Summary
Constructors Constructor Description QueryHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
withNewSession(groovy.lang.Closure<T> closure)
Executes a closure within the context of a new database session.static <T> T
withSession(groovy.lang.Closure<T> closure)
Executes a closure within the context of an existing database session.static <T> T
withSql(java.util.function.Function<groovy.sql.Sql,T> function)
Executes a function that takes a Sql instance as a parameter and returns a result.static <T> T
withTransaction(groovy.lang.Closure<T> closure)
Executes a closure within the context of a transaction using an existing database session.
-
-
-
Method Detail
-
withSession
public static <T> T withSession(groovy.lang.Closure<T> closure)
Executes a closure within the context of an existing database session.- Type Parameters:
T
- the return type of the closure- Parameters:
closure
- the closure to be executed- Returns:
- the result of executing the closure
- Throws:
java.lang.IllegalStateException
- if the method is called directly from the library
-
withNewSession
public static <T> T withNewSession(groovy.lang.Closure<T> closure)
Executes a closure within the context of a new database session.- Type Parameters:
T
- the return type of the closure- Parameters:
closure
- the closure to be executed- Returns:
- the result of executing the closure
- Throws:
java.lang.IllegalStateException
- if the method is called directly from the library
-
withTransaction
public static <T> T withTransaction(groovy.lang.Closure<T> closure)
Executes a closure within the context of a transaction using an existing database session.- Type Parameters:
T
- the return type of the closure- Parameters:
closure
- the closure to be executed- Returns:
- the result of executing the closure
- Throws:
java.lang.IllegalStateException
- if the method is called directly from the library
-
withSql
public static <T> T withSql(java.util.function.Function<groovy.sql.Sql,T> function)
Executes a function that takes a Sql instance as a parameter and returns a result.- Type Parameters:
T
- the return type of the function- Parameters:
function
- the function to be executed- Returns:
- the result of executing the function
- Throws:
java.lang.IllegalStateException
- if the method is called directly from the library
-
-