Permalink
Browse files

dayday up

  • Loading branch information...
1 parent 7b00ba2 commit 9e824ec9903962a6a007ef198723bd688f14d194 @leonlee committed Dec 18, 2012
View
@@ -34,6 +34,7 @@ subprojects {
compile "org.mozilla:rhino:$rhinoVsn"
groovy "org.codehaus.groovy:groovy:$groovyVsn"
+ compile "org.codehaus.groovy:groovy-sql:$groovyVsn"
compile "org.msgpack:msgpack:$msgpackVsn"
compile "org.slf4j:slf4j-log4j12:$slf4jVsn"
@@ -4,5 +4,24 @@ package org.riderzen.ogs.common
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-7
*/
-class BaseModel implements IModel{
+class BaseModel {
+ static def transientAttributes = ['class', 'metaClass', 'transientAttributes', 'tableName']
+
+ BaseModel() {
+ }
+
+ static def excludeAttributes(fields) {
+ transientAttributes.addAll(fields)
+ }
+
+ def pAttributes() {
+ def propMap = properties;
+
+ transientAttributes?.each {
+ propMap.remove(it)
+ }
+
+ propMap
+ }
+
}
@@ -0,0 +1,101 @@
+package org.riderzen.ogs.common
+
+import groovy.sql.Sql
+import org.slf4j.LoggerFactory
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-18
+ */
+class DBHelper {
+ def static logger = LoggerFactory.getLogger(DBHelper.class)
+
+ static def save(model) {
+ if (!model.metaClass.hasProperty('tableName')) {
+ logger.error("No tableName property found in $model")
+ throw new NoPropertyFoundException('tableName')
+ }
+
+ def tableName = model.tableName
+
+ logger.debug("working on table $tableName")
+
+ def sql = getShardSql(model)
+ def dataSet = sql.dateSet(tableName)
+ dataSet.add(model.pAttributes())
+ }
+
+ static def batchSave(models) {
+ if (!models) return
+ def sql = getShardSql(models[0])
+
+ sql.withBatch { stmt ->
+ models.each { it ->
+ stmt.addBatch(generateInsertSql(it))
+ }
+ }
+ }
+
+ static def find(model) {
+ def sql = getShardSql(model)
+ def whereClause;
+ def params = new HashMap();
+ if (model.primaryKeys?.size() > 1) {
+ def whereBuilder = new StringBuilder(" where true ")
+ model.primaryKeys.each {
+ whereBuilder.append("and ${it}=:${it} ")
+ params.put(it, model.getProperty(it))
+ }
+ whereClause = whereBuilder.toString()
+ } else {
+ whereClause = "${model.primaryKeys[0]}=:id"
+ params.put('id', model.getProperty(model.primaryKeys[0]))
+ }
+ def row = sql.firstRow("select * from ${tableName} $whereClause", params)
+
+ model.pAttributes().each { key, value ->
+ model[key] = row[key]
+ }
+ model
+ }
+
+
+ def static String generateInsertSql(model) {
+ StringBuilder buffer = new StringBuilder("insert into ")
+ buffer.append(model.tableName)
+ buffer.append(" (")
+ StringBuilder paramBuffer = new StringBuilder()
+ boolean first = true
+ for (String column : model.pAttributes().keySet()) {
+ if (first) {
+ first = false
+ paramBuffer.append("?")
+ } else {
+ buffer.append(", ")
+ paramBuffer.append(", ?")
+ }
+ buffer.append(column)
+ }
+ buffer.append(") values (")
+ buffer.append(paramBuffer.toString())
+ buffer.append(")")
+
+ buffer.toString()
+ }
+
+ static def getShardSql(model) {
+ def sql = defaultSql()
+ if (model.metaClass.hasProperty('shardBy')) {
+ def shardBy = model.shardBy
+
+ logger.debug("shadBy: $shardBy")
+
+ sql = getShardSql(shardBy)
+ }
+ sql
+ }
+
+ static def defaultSql() {
+ return null;
+ }
+}
@@ -1,15 +0,0 @@
-package org.riderzen.ogs.common
-
-/**
- * User: Leon Lee <mail.lgq@gmail.com>
- * Date: 12-12-17
- */
-public interface IModel {
- void save()
-
- IModel get(id)
-
- void update()
-
- void delete()
-}
@@ -0,0 +1,11 @@
+package org.riderzen.ogs.common
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-18
+ */
+class ModelException extends Exception{
+ ModelException(String s) {
+ super(s)
+ }
+}
@@ -0,0 +1,11 @@
+package org.riderzen.ogs.common
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-18
+ */
+class NoPropertyFoundException extends Exception {
+ NoPropertyFoundException(name) {
+ super("No $name property found")
+ }
+}

0 comments on commit 9e824ec

Please sign in to comment.