Created model objects and DB layer.

This commit is contained in:
Jonathan Bernard 2015-02-20 01:08:17 -06:00
parent f2dc674181
commit 3e07dc20bf
14 changed files with 262 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
build/ build/
.gradle/ .gradle/
*.sw?

View File

@ -13,6 +13,21 @@ dependencies {
compile 'org.slf4j:slf4j-api:1.7.10' compile 'org.slf4j:slf4j-api:1.7.10'
compile 'ch.qos.logback:logback-core:1.1.2' compile 'ch.qos.logback:logback-core:1.1.2'
compile 'ch.qos.logback:logback-classic:1.1.2' compile 'ch.qos.logback:logback-classic:1.1.2'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'com.zaxxer:HikariCP-java6:2.3.2'
compile 'com.impossibl.pgjdbc-ng:pgjdbc-ng:0.3'
runtime 'org.glassfish.jersey.containers:jersey-container-servlet:2.16'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
/*
compile 'org.hibernate:hibernate-core:4.3.8.Final'
compile 'org.hibernate:hibernate-validator:5.1.3.Final'
copmile 'javax.el:javax.el-api:2.2.4'
compile 'org.glassfish.web:javax.el:2.2.4'
compile 'org.modelmapper:modelmapper:0.7.3'
compile 'org.springframework:spring-context:4.1.4.RELEASE'
compile 'com.mchange:c3p0:0.9.5'
*/
} }

View File

@ -0,0 +1,66 @@
package com.jdbernard.nlsongs.db
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import com.zaxxer.hikari.HikariDataSource
import groovy.sql.GroovyRowResult
import groovy.sql.Sql
import groovy.transform.CompileStatic
import com.jdbernard.nlsongs.model.*
//@CompileStatic
public class NLSongsDB {
private HikariDataSource dataSource
private Sql sql
public NLSongsDB(HikariDataSource dataSource) {
this.dataSource = dataSource
this.sql = new Sql(dataSource) }
public void shutdown() { dataSource.shutdown() }
// ### Services
public Service findService(int id) {
GroovyRowResult row = sql.firstRow(
"SELECT * FROM services WHERE id = ?", [id] as List<Object>)
if (row) return (Service) recordToModel(row, Service)
else return null }
// #### Utility functions
static def recordToModel(def row, Class clazz) {
def model = clazz.newInstance()
row.each { recordKey, v ->
def pts = recordKey.split('_')
def modelKey = pts.length == 1 ? pts[0] :
pts[0] + pts[1..-1].collect { it.capitalize() }.join()
model[modelKey] = v }
return model }
static def modelToRecord(def model) {
def record = [:]
model.properties.each { modelKey, v ->
if (modelKey == "class") return
def recordKey = modelKey.
replaceAll(/(\p{javaUpperCase})/, /_$1/).toLowerCase()
record[recordKey] = v }
return record }
/*
static Object recordToModel(GroovyRowResult row, Class clazz) {
Object model = clazz.newInstance()
row.each { recordKey, v ->
String[] pts = ((String) recordKey).split('_')
String modelKey = pts[0] +
pts[1..-1].collect { it.capitalize() }.join()
model[modelKey] = v }
}
*/
}

View File

@ -0,0 +1,7 @@
package com.jdbernard.nlsongs.model
public class ApiKey implements Serializable {
String key
String description
}

View File

@ -0,0 +1,13 @@
package com.jdbernard.nlsongs.model
public class Performance implements Serializable {
int serviceId
int songId
String pianist
String organist
String bassist
String drummer
String guitarist
String leader
}

View File

@ -0,0 +1,8 @@
package com.jdbernard.nlsongs.model
public class Service implements Serializable {
int id
Date date
ServiceType serviceType
}

View File

@ -0,0 +1,3 @@
package com.jdbernard.nlsongs.model;
public enum ServiceType { SUN_AM, SUN_PM, WED }

View File

@ -0,0 +1,8 @@
package com.jdbernard.nlsongs.model
public class Song implements Serializable {
int id
String name
List<String> artists
}

View File

@ -0,0 +1,8 @@
package com.jdbernard.nlsongs.model
public class User {
int id
String username
String pwd
}

View File

@ -0,0 +1,42 @@
package com.jdbernard.nlsongs.servlet
import javax.servlet.ServletContext
import javax.servlet.ServletContextEvent
import javax.servlet.ServletContextListener
import com.jdbernard.nlsongs.db.NLSongsDB
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
public final class NLSongsContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
context = event.servletContext
// Create the pooled data source
HikariConfig hcfg = new HikariConfig()
hcfg.username = 'TODO'
hcfg.password = 'TODO'
hcfg.dataSourceClassName = 'TODO'
hcfg.addDataSourceProperty('cachePrepStmts', 'true')
hcfg.addDataSourceProperty('prepStmtCacheSize', '250')
hcfg.addDataSourceProperty('prepStmtCacheSqlLimit', '2048')
hcfg.addDataSourceProperty('useServerPrepStmts', 'true')
HikariDataSource hds = new HikariDataSource(hcfg)
// Create the NLSonsDB instance.
NLSongsDB songsDB = new NLSongsDB(hds)
context.setAttribute('songsDB', songsDB) }
public void contextDestroyed(ServletContextEvent event) {
context = event.servletContext
// Shutdown the Songs DB instance (it will shut down the data source).
NLSongsDB songsDB = context.getAttribute('songsDB')
songsDB.shutdown()
context.removeAttribute('songsDB') }
}

View File

@ -0,0 +1,59 @@
-- # New Life Songs DB
-- @author Jonathan Bernard <jdb@jdb-labs.com>
--
-- PostgreSQL database creation sript.
-- DROP DATABASE IF EXISTS nlsongs;
CREATE DATABASE IF NOT EXISTS nlsongs
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
CONNECTION LIMIT = 1;
-- Services table
DROP TABLE IF EXISTS services;
CREATE TABLE IF NOT EXISTS services (
id SERIAL,
date DATE NOT NULL,
service_type VARCHAR(16) DEFAULT NULL,
PRIMARY KEY (id));
-- Songs table
DROP TABLE IF EXISTS songs;
CREATE TABLE IF NOT EXISTS songs (
id SERIAL,
name VARCHAR(128) NOT NULL,
artists VARCHAR(256) DEFAULT NULL,
PRIMARY KEY (id));
-- performances table
DROP TABLE IF EXISTS performances;
CREATE TABLE IF NOT EXISTS performances (
service_id INTEGER NOT NULL,
song_id INTEGER NOT NULL,
pianist VARCHAR(64) DEFAULT NULL,
organist VARCHAR(64) DEFAULT NULL,
bassist VARCHAR(64) DEFAULT NULL,
drummer VARCHAR(64) DEFAULT NULL,
guitarist VARCHAR(64) DEFAULT NULL,
leader VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (service_id, song_id),
FOREIGN KEY (service_id) REFERENCES services (id),
FOREIGN KEY (song_id) REFERENCES songs (id));
DROP TABLE IF EXISTS api_keys;
CREATE TABLE IF NOT EXISTS api_keys (
key VARCHAR(32) NOT NULL,
description VARCHAR(256) NOT NULL,
PRIMARY KEY (key));
DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users (
id SERIAL,
username VARCHAR(64),
pwd VARCHAR(80));

View File

@ -0,0 +1,32 @@
package com.jdbernard.nlsongs.db
public class GenerateQueries {
public static void main(String[] args) {
}
public static Map<String, Map<String, String> > generateQueries(String ddl) {
def tables = [:]
// Find the table definitions
String tableRegex = /(?ms)(?:CREATE TABLE (?:IF NOT EXISTS )?([^\s]+) \(([^\s]+);.+?)+/
ddl.eachMatch(tableRegex) { matchGroups ->
String tableName = matchGroups[1]
// Parse the column definitions.
// Create new record insert statements.
// Create insert queries.
// Create update queries.
// Create delete queries.
// Create ID lookup queries.
}
}