数据库工具包
| Modifiers | Name | Description |
|---|---|---|
static Integer |
queryMax |
|
static Map |
queryMetaParams |
| Type | Name and description |
|---|---|
static StringBuilder |
appendInListHql(StringBuilder hql, String field, Collection values, Map options = null)拼接可能超出 1000 个值的 in 或 not in 的 hql。 |
static String |
assembleInLisHql(String field, Collection values, Map options = null)拼接可能超出 1000 个值的 in 或 not in 的 hql。 |
static void |
closeConnection(java.sql.Connection conn)释放数据库连接 |
static void |
executeAsyncPersistentThread(Closure callback, Closure callbackException = null)启动一个线程,异步执行闭包,并立即返回 |
static void |
executePersistentThread(Closure callback, Closure callbackException = null)启动一个线程,执行闭包后返回 |
static void |
executePersistentThread(String name, Closure callback, Closure callbackException = null) |
static List |
executeQueryInList(String hql, List queryParams, Map metaParams, Collection inList, Map options = null)安全的 inList 查询 |
static List |
executeQueryInList(String hql, Collection inList, Map options = null) |
static Integer |
executeUpdateInList(String hql, List queryParams, List inList, Map options = null)安全的 inList 更新 |
static boolean |
exists(String datasourceName = null)数据源是否存在 |
static org.hibernate.Session |
flushAndClearCurrentSession(String datasourceName = null)获得当前的 Hibernate Session,并执行 flush()、clear(),以释放缓存、提升性能。 |
static java.sql.Connection |
getConnection(String datasourceName = null)通过 dataSource 获得一个新的 JDBC 数据库连接(和当前 hibernate session 所用的不是一个)。 |
static org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl |
getConnectionProvider(String datasourceName = null)获得数据源的连接管理器 |
static java.sql.Connection |
getConnectionUnproxied(String datasourceName = null) |
static java.sql.Connection |
getCurrentConnection(String datasourceName = null)获得当前 hibernate session 的数据库连接,可以统一进行事务操作与回滚,并且使用后无需关闭。 |
static org.hibernate.Session |
getCurrentSession(String datasourceName = null)获得当前的 Hibernate Session 对象 |
static groovy.sql.Sql |
getCurrentSql(String datasourceName = null, Closure callback = null)获得一个 Groovy Sql 数据库连接,和当前 hibernate session 使用同一个 connection,可以统一进行事务操作与回滚。 |
static groovy.sql.Sql |
getCurrentSql(Closure callback)获得当前数据库连接,并执行闭包 |
static Date |
getCurrentTimestamp()从数据库中取出当前时间 |
static org.springframework.transaction.TransactionStatus |
getCurrentTransactionStatus()获得当前事务(即 withTransaction 的参数 status) |
static String |
getDataSourceNameByDomain(String domainClazz)获取 domain 类所使用的数据源名称后缀。 |
static List<String> |
getDataSourceNames()获得所有数据源名称列表 |
static org.apache.tomcat.jdbc.pool.DataSource |
getDataSourceUnproxied(String datasourceName = null)获得数据源 |
static String |
getDataSourceUrl(String datasourceName = null)获得数据源的数据库链接(URL) |
static String |
getDatabaseName(String datasourceName = null)获得数据库名称。 |
static String |
getDatabaseUrl(String datasourceName = null)@deprecated # getDataSourceUrl |
static org.hibernate.dialect.Dialect |
getDialect(String datasourceName = null)获得数据源的方言对象 |
static String |
getOriginalJdbcUrl(String url) |
static org.hibernate.SessionFactory |
getSessionFactory(String datasourceName = null)获得 Hibernate SessionFactory |
static groovy.sql.Sql |
getSql(String datasourceName = null, Closure callback = null)通过 dataSource 获得一个 Groovy Sql 数据库连接。 |
static groovy.sql.Sql |
getSql(Map args, Closure callback = null)获得一个原始的 Groovy Sql 数据库连接。 |
static groovy.sql.Sql |
getSqlCurrent(String datasourceName = null) |
static groovy.sql.Sql |
getSqlUnproxied(String datasourceName = null) |
static String |
getTableNameByDomain(Class domainClazz)根据 Domain 类,获得映射的数据表名 |
static String |
hqlToSql(String hql, org.hibernate.SessionFactory sessionFactory)将 hql 转换成 sql |
static String |
hqlToSql(String hql, String datasourceName = null)将 hql 转换成 sql |
static void |
interruptPersistentThread(String name)中断线程 |
static boolean |
isDefaultDataSource(String domainClazz)判断 domain 类是否所使用的是系统默认的 DataSource |
static boolean |
isDm(String datasourceName = null)是否是达梦数据库 |
static boolean |
isMysql(String datasourceName = null)是否是 MySQL 数据库 |
static boolean |
isOracle(String datasourceName = null)是否是 Oracle 数据库 |
static boolean |
isPostgreSQL(String datasourceName = null)是否是 PostgreSQL 数据库 |
static boolean |
isSqlserver(String datasourceName = null)是否是 SQLServer 数据库 |
static void |
lock(Closure callback, javax.sql.DataSource dataSourceRef)获得一个基于数据库的、全局的锁 |
static Thread |
startPersistentThread(Long interval, Closure callback, Closure callbackException = null)启动一个线程,并循环执行(类似 runAsync) |
static Thread |
startPersistentThread(String name, Closure callback, Closure callbackException = null) |
static Thread |
startPersistentThread(Closure callback, Closure callbackException = null) |
static Thread |
startPersistentThread(String name, Long interval, Closure callback, Closure callbackException = null)启动一个线程,并执行(类似 runAsync) |
static Boolean |
supportsSequences(String datasourceName = null)数据库是否是用 sequence 来做 ID 生成器,比如 oracle |
static Boolean |
supportsSequences(List<String> datasourceNames)数据库是否是用 sequence 来做 ID 生成器 |
| Methods inherited from class | Name |
|---|---|
class Object |
Object#wait(long, int), Object#wait(long), Object#wait(), Object#equals(Object), Object#toString(), Object#hashCode(), Object#getClass(), Object#notify(), Object#notifyAll() |
拼接可能超出 1000 个值的 in 或 not in 的 hql。
例如:assert DbUtils.appendInListHql(new StringBuilder("select x from Abc"), "id", 1..5, [prefix: "where "]).toString() == "select x from Abc where (id in ('1','2','3','4','5'))"
def queryParams = []
assert DbUtils.appendInListHql(new StringBuilder("and"), "name", 6..8, [params: queryParams]).toString() == "and (name in (?,?,?))"
assert queryParams == 6..8
def hql = DbUtils.appendInListHql(new StringBuilder("and"), "city", 1..1005, [suffix: " and XXX", not: true]).toString() //.contains("or city not in ('1001','1002','1003','1004','1005')) and XXX")
assert hql.startsWith("and (city not in ('1','2','3','4','5',")
assert hql.endsWith(",'999','1000') or city not in ('1001','1002','1003','1004','1005')) and XXX")
hql - 需要拼接的 StringBuilder 对象拼接可能超出 1000 个值的 in 或 not in 的 hql。
例如:assert DbUtils.assembleInLisHql("id", 1..10) == " (id in ('1','2','3','4','5','6','7','8','9','10'))"
String hql = DbUtils.assembleInLisHql("id", 1..1005, [suffix: " and XXX", not: true])
assert hql.contains("(id not in ('1','2','3','4','5',")
assert hql.contains(",'999','1000') or id not in ('1001','1002','1003','1004','1005')) and XXX")
def queryParams = []
assert DbUtils.assembleInLisHql("id", 1..5, [params: queryParams]) == " (id in (?,?,?,?,?))"
assert queryParams == 1..5
field - 属性名values - in 的值列表options.prefix - hql 前缀options.suffix - hql 后缀options.not - 是否是不包含(not in)options.params - 如果不为空,则拼 “?” 号,并且修改 params 添加相对应的值释放数据库连接
启动一个线程,异步执行闭包,并立即返回
等同于调用 startPersistentThread,其参数 interval 设置为 null。
启动一个线程,执行闭包后返回
不同于方法 startPersistentThread,这里会等待线程执行完毕后才返回,并且可以不借助异常闭包直接捕获到异常。
callback - 要执行的闭包callbackException - 异常时执行的闭包,可接收类型为 Throwable 的异常对象;如果为空、但发生异常时,会直接抛出。安全的 inList 查询
通过 hql 做 in 查询时,如果 in 内的内容超过 1000 个,会导致诸如 ORA-01795 的数据库异常,使用本方法可拆成多次执行,并返回执行结果(不查重)。
hql - 需要执行的 hql,其中关键字 "??" 会被替换成 "?,?,..."queryParams - 除 inList 里的参数外的其他查询参数,且在 inList 参数前metaParams - 翻页等参数inList - 列表元素。如果是 id 列表,注意每个元素的类型应为 Long 型而不是 Integer,以免出现 cast 异常options.clazz - 用来实际执行 executeQuery 的 Domain 类。安全的 inList 更新
数据源是否存在
获得当前的 Hibernate Session,并执行 flush()、clear(),以释放缓存、提升性能。
通过 dataSource 获得一个新的 JDBC 数据库连接(和当前 hibernate session 所用的不是一个)。
注意使用完后务必调用 closeConnection 来关闭连接。
获得数据源的连接管理器
获得当前 hibernate session 的数据库连接,可以统一进行事务操作与回滚,并且使用后无需关闭。
获得当前的 Hibernate Session 对象
获得一个 Groovy Sql 数据库连接,和当前 hibernate session 使用同一个 connection,可以统一进行事务操作与回滚。
这里返回的 sql 对象,可以设置额外的属性 fetchSize,用于从数据库中读取大量数据,可以提升10倍以上的性能。
如果没有 callback 参数,使用完成后注意 close 释放资源,避免内存泄漏(数据库连接会自动释放,但是 Sql 对象及其 metaClass 等资源却不会释放)。
获得当前数据库连接,并执行闭包
等同于 getCurrentSql(null, callback) 。
从数据库中取出当前时间
获得当前事务(即 withTransaction 的参数 status)
注:只能在 Service 类中调用。
获取 domain 类所使用的数据源名称后缀。
默认数据源将返回空,否则返回前缀为 _ 的数据源名。
获得所有数据源名称列表
获得数据源
获得数据源的数据库链接(URL)
获得数据库名称。
通过 jdbc URL 计算,如 “jdbc:oracle:....” 返回 oracle。
datasourceName - 数据源名称,如 auditlog 表示数据源 auditlog;或者 JDBC 连接的 URL(以jdbc:开头)获得数据源的方言对象
获得 Hibernate SessionFactory
通过 dataSource 获得一个 Groovy Sql 数据库连接。
这里返回的 sql 对象,可以设置额外的属性 fetchSize,用于从数据库中读取大量数据,可以提升10倍以上的性能。
注意使用完后需要 close。
datasourceName - 数据源名称callback - 获得 sql 对象后运行的闭包,包含参数 sql,执行完成后会自动关闭连接获得一个原始的 Groovy Sql 数据库连接。如果没有回调闭包,注意使用完后 close。
args - 包含数据库连接信息的Map。它的key包括:callback - 回调闭包,执行完后会自动 close。根据 Domain 类,获得映射的数据表名
将 hql 转换成 sql
将 hql 转换成 sql
中断线程
name - 线程名称判断 domain 类是否所使用的是系统默认的 DataSource
是否是达梦数据库
是否是 MySQL 数据库
是否是 Oracle 数据库
是否是 PostgreSQL 数据库
是否是 SQLServer 数据库
获得一个基于数据库的、全局的锁
使用 select .. for update 的方式获得一个锁,如果失败会一直等待下去;执行完回调闭包后自动释放锁。
依赖升级器的数据表,因此应用中必须有升级器类(Updater)、或者依赖包含升级器类的插件(如 bropen-framework)。
启动一个线程,并循环执行(类似 runAsync)
interval - 每次执行之间的等待时间,单位毫秒callback - 要循环执行的闭包callbackException - 异常时执行的闭包,可接收类型为 Throwable 的异常对象启动一个线程,并执行(类似 runAsync)
name - 线程对应的名称interval - 循环执行之间的等待时间,单位毫秒;如果为 null 则不循环执行(执行一遍后退出)callback - 要执行的闭包callbackException - 异常时执行的闭包,可接收类型为 Throwable 的异常对象数据库是否是用 sequence 来做 ID 生成器,比如 oracle
datasourceName - 数据源名称,或者数据库名称(oracle、dm...)数据库是否是用 sequence 来做 ID 生成器
如果数据源不存在,则略过继续检查下一个;如果存在,则直接返回该数据源的检查结果,后续数据源不再检查。
datasourceNames - 数据源名称列表