我正在编写一个Grails应用程序,该应用程序主要使用带有服务支持的端点的springws Web服务插件.这些服务将从各种后端数据库中检索数据(即,不通过域类和GORM).我想存储我的服务将用于获取外部文件中的Web服务数据的sql.我在寻找建议:
保存文件的最佳位置在哪里(即,我想将它们放在像grails-app/sql这样明显的地方)和最佳格式(即xml,configslurper等)
抽象检索sql文本的最佳方法,以便我将执行sql的服务不需要知道它们的获取位置和方式.服务将只提供一个sqlid并获取sql.
我最近正在做一个项目,我需要做类似的事情.我创建了以下目录来存储sql文件:
./grails-app/conf/sql
例如,有一个文件./grails-app/conf/sql/hr/FIND_PERSON_BY_ID.sql,其中包含以下内容:
select a.id
, a.first_name
, a.last_name
from person
where id = ?
Run Code Online (Sandbox Code Playgroud)
我创建了一个SqlCatalogService类,它将加载该目录(和子目录)中的所有文件,并将文件名(减去扩展名)和文件文本存储在Map中.该服务有一个get(id)方法,它返回在Map中缓存的sql文本.由于存储在grails-app/conf中的文件/目录放在类路径中,因此SqlCatalogService使用以下代码读入文件:
....
....
Map<String,String> sqlCache = [:]
....
....
void loadSqlCache() {
try {
loadSqlCacheFromDirectory(new File(this.class.getResource("/sql/").getFile()))
} catch (Exception ex) {
log.error(ex)
}
}
void loadSqlCacheFromDirectory(File directory) {
log.info "Loading SQL cache from disk using base directory ${directory.name}"
synchronized(sqlCache) {
if(sqlCache.size() == 0) {
try {
directory.eachFileRecurse { sqlFile ->
if(sqlFile.isFile() && sqlFile.name.toUpperCase().endsWith(".SQL")) {
def sqlKey = sqlFile.name.toUpperCase()[0..-5]
sqlCache[sqlKey] = sqlFile.text
log.debug "added SQL [${sqlKey}] to cache"
}
}
} catch (Exception ex) {
log.error(ex)
}
} else {
log.warn "request to load sql cache and cache not empty: size [${sqlCache.size()}]"
}
}
}
String get(String sqlId) {
def sqlKey = sqlId?.toUpperCase()
log.debug "SQL Id requested: ${sqlKey}"
if(!sqlCache[sqlKey]) {
log.debug "SQL [${sqlKey}] not found in cache, loading cache from disk"
loadSqlCache()
}
return sqlCache[sqlKey]
}
Run Code Online (Sandbox Code Playgroud)
使用各种数据源的服务使用SqlCatalogService通过调用get(id)方法来检索sql:
class PersonService {
def hrDataSource
def sqlCatalogService
private static final String SQL_FIND_PERSON_BY_ID = "FIND_PERSON_BY_ID"
Person findPersonById(String personId) {
try {
def sql = new groovy.sql.Sql(hrDataSource)
def row = sql.firstRow(sqlCatalogService.get(SQL_FIND_PERSON_BY_ID), [personId])
row ? new Person(row) : null
} catch (Exception ex) {
log.error ex.message, ex
throw ex
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们只有几个sql语句,因此将所有文本存储在Map中不是问题.如果要存储大量的sql文件,则可能需要考虑使用Ehcache之类的东西并定义驱逐策略(即最近最少使用或最少使用),并且只将最常用的存储在内存中,并将其余部分保留在磁盘上直到需要.
在此之前,我考虑过使用GORM并将sql文本存储在数据库中.但是决定在文件中使用sql使得开发更容易,因为我们几乎可以直接从我们的sql工具保存sql文件(用问号代替硬编码参数)并且能够让我们的修订控制系统跟踪变化.我不是说上述服务是处理这个问题的最有效或最正确的方法,但它迄今为止对我们的需求起了作用.
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |