Changes to make the core ORM database agnostic.

This commit is contained in:
Jonathan Bernard 2016-02-10 05:16:22 -06:00
parent 6a73b1ca8c
commit 6e6defe544
2 changed files with 33 additions and 17 deletions

View File

@ -4,6 +4,9 @@ import com.zaxxer.hikari.HikariConfig
public class ConfigWrapper {
public static final String LIBRARY_DIR_KEY = "library.dir"
public static final String DB_CONFIG_FILE_KEY = "database.config.file"
Properties configProps
Map<String, String> env = System.getenv()
@ -15,7 +18,7 @@ public class ConfigWrapper {
if (tryLoadConfigFile(new File(env.HOME, ".wdiwtlt.properties"))) return
try {
ConfigWrapper.getResourceAsStream("/wdiwtlt.default.properties")
ConfigWrapper.getResourceAsStream("/com/jdbernard/wdiwtlt/db/default.properties")
.withStream { tryLoadConfig(is) } }
catch (Exception e) {} }
@ -24,8 +27,8 @@ public class ConfigWrapper {
public String getLibraryRootPath() {
// 1. Check config properties
if (configProps && configProps["library.dir"])
return configProps["library.dir"]
if (configProps && configProps[LIBRARY_DIR_KEY])
return configProps[LIBRARY_DIR_KEY]
// 2. Check environment variable
if (env.WDIWTLT_LIBRARY_DIR)
@ -40,8 +43,8 @@ public class ConfigWrapper {
if (configProps) {
// 1.1 Look for a reference to a dedicated config file
if (configProps["database.config.file"]) {
File cf = new File(configProps["database.config.file"])
if (configProps[DB_CONFIG_FILE_KEY]) {
File cf = new File(configProps[DB_CONFIG_FILE_KEY])
if (cf.exists() && cf.isFile())
cf.withInputStream { props.load(it) } }

View File

@ -127,17 +127,30 @@ public class ORM {
sql.execute(query, [model.id])
return sql.updateCount }
public def associate(String linkTable, Integer firstId, Integer secondId) {
def query = "INSERT INTO $linkTable VALUES (?, ?) ON CONFLICT DO NOTHING"
def params = [firstId, secondId]
logger.debug("Creating association.\n\tSQL: {}\n\tPARAMS: {}",
query, params)
return sql.execute(query, params) }
public def associate(Class modelClass1, Class modelClass2 , Integer firstId, Integer secondId) {
String linkTable = pluralize(nameFromModel(modelClass1.simpleName)) +
"_" + pluralize(nameFromModel(modelClass2.simpleName))
String col1 = nameFromModel(modelClass1) + "_id"
String col2 = nameFromModel(modelClass2) + "_id"
withTransaction {
def query = """\
SELECT * FROM $linkTable
WHERE "${col1}"_id = ? AND "${col2}" = ?"""
def params = [firstId, secondId]
// Look first for an existing association before creating one.
logger.debug("Selecting association.\n\tSQL: {}\n\tPARAMS: {}",
query, params)
if (sql.firstRow(query, params)) { return 0 }
else {
query = """INSERT INTO $linkTable ("$col1", "$col2") VALUES (?, ?)"""
logger.debug("Selecting association.\n\tSQL: {}\n\tPARAMS: {}",
query, params)
return sql.execute(query, params) } } }
public def associate(def m1, def m2) {
return associate(pluralize(nameFromModel(m1.class.simpleName)) +
"_" + pluralize(nameFromModel(m2.class.simpleName)),
m1.id, m2.id) }
return associate(m1.class, m2.class, m1.id, m2.id) }
/// ### Album-specific methods
public Album getAlbumById(int id) { return getById(id, Album) }
@ -192,7 +205,7 @@ public class ORM {
}
public def addAlbumArtist(int albumId, int artistId) {
return associate("artists_albums", artistId, albumId) }
return associate(Artist, Album, artistId, albumId) }
/// ### Bookmark-specific methods
public Bookmark getBookmarkById(int id) { return getById(id, Bookmark) }
@ -210,10 +223,10 @@ public class ORM {
return files ? files[0] : null }
public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
return associate("albums_media_files", albumId, mediaFileId) }
return associate(Album, MediaFile, albumId, mediaFileId) }
public def associateMediaFileWithArtist(int mediaFileId, int artistId) {
return associate("artists_media_files", artistId, mediaFileId) }
return associate(Album, MediaFile, artistId, mediaFileId) }
public def incrementPlayCount(int mediaFileId) {
def query = "UPDATE media_files SET play_count = play_count + 1 WHERE ID = ?"