Skip to content
Snippets Groups Projects
Commit 4b1f9050 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Work on sql serialization

parent 26b4b35e
No related branches found
No related tags found
No related merge requests found
Showing
with 216 additions and 1 deletion
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="HydraSettings">
<option name="hydraStorePath" value="$PROJECT_DIR$/.hydra/idea" />
<option name="noOfCores" value="2" />
<option name="projectRoot" value="$PROJECT_DIR$" />
<option name="sourcePartitioner" value="auto" />
</component>
</project>
\ No newline at end of file
package de.tobias.playpad.server.sql;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnName
{
/**
* Name of the table column.
*
* @return name
*/
String value();
Class<? extends SqlResultHandler> handler() default StringSqlResultHandler.class;
}
package de.tobias.playpad.server.sql;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ForeignKey
{
}
package de.tobias.playpad.server.sql;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Id
{
}
package de.tobias.playpad.server.sql;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Table
{
String value();
}
......@@ -4,21 +4,28 @@ import java.util.UUID
import de.tobias.playpad.server.json._
import de.tobias.playpad.server.project.settings.PadSettings
import de.tobias.playpad.server.sql.{ColumnName, ForeignKey, Id, Table}
/**
* Created by tobias on 17.02.17.
*/
@Table("Pad")
class Pad() {
@JsonName(value = "id", handler = classOf[UUIDSerializerHandler])
@Id
@ColumnName("id")
var id: UUID = UUID.randomUUID()
@JsonName("name")
@ColumnName("name")
var name: String = _
@JsonName(value = "position", handler = classOf[IntSerializerHandler])
@ColumnName("position")
var position: Int = _
@JsonName("contentType")
@ColumnName("content_type")
var contentType: String = _
@JsonCollection(value = "paths", `type` = classOf[Path])
var paths: List[Path] = List()
......@@ -27,5 +34,9 @@ class Pad() {
var padSettings: PadSettings = _
@JsonParent
@ForeignKey
var page: Page = _
override def toString = s"Pad($id, $name, $position, $contentType, $paths, $padSettings, $page)"
}
......@@ -5,7 +5,7 @@ import java.util.UUID
import de.tobias.playpad.server.project.utils.SqlDef._
import de.tobias.playpad.server.project.{Pad, Page}
import de.tobias.playpad.server.sql.SqlSerializer
/**
* Created by tobias on 17.02.17.
*/
......@@ -18,6 +18,10 @@ class PadLoader(val connection: Connection) {
var pads: List[Pad] = List()
val padss = new SqlSerializer().queryObj(classOf[Pad], connection)
println(padss)
while (result.next()) {
val pad = new Pad()
pad.id = UUID.fromString(result.getString(PAD_ID))
......
package de.tobias.playpad.server.sql
trait SqlResultHandler {
def fromResult(o: Any): Any
def toQuery(o: Any): Any
}
package de.tobias.playpad.server.sql
import java.sql.{Connection, ResultSet}
import java.util.UUID
class SqlSerializer {
def queryObj[T](clazz: Class[T], key: UUID, connection: Connection, keyName: String = null): T = {
val obj = clazz.newInstance()
if (clazz.isAnnotationPresent(classOf[Table])) {
val table = clazz.getAnnotation(classOf[Table]).value()
val columnName = if (keyName != null) {
keyName
} else {
getKeyName(clazz)
}
val stmt = connection.prepareStatement(s"SELECT * FROM $table WHERE $columnName = ?")
stmt.setString(1, key.toString)
val result = stmt.executeQuery()
if (result.first()) {
clazz.getDeclaredFields
.filter(f => f.isAnnotationPresent(classOf[ColumnName]))
.foreach(f => {
f.setAccessible(true)
f.set(obj, result.getObject(f.getAnnotation(classOf[ColumnName]).value()))
})
}
result.close()
stmt.close()
}
obj
}
private def getKeyName[T](clazz: Class[T]): String = {
val field = clazz.getDeclaredFields
.filter(f => f.isAnnotationPresent(classOf[ColumnName]))
.find(f => f.isAnnotationPresent(classOf[Id]))
if (field.isDefined) {
field.get.getAnnotation(classOf[ColumnName]).value()
} else {
null
}
}
// def queryObj[T](clazz: Class[T], key: UUID, connection: Connection, keyName: String): List[T] = {
// if (clazz.isAnnotationPresent(classOf[Table])) {
// val table = clazz.getAnnotation(classOf[Table]).value()
//
// val stmt = connection.prepareStatement(s"SELECT * FROM $table WHERE $keyName = ?")
// stmt.setString(1, key.toString)
// val result = stmt.executeQuery()
//
// val list = getResult(clazz, result)
//
// result.close()
// stmt.close()
// list
// } else {
// null
// }
// }
def queryObj[T](clazz: Class[T], connection: Connection): List[T] = {
if (clazz.isAnnotationPresent(classOf[Table])) {
val table = clazz.getAnnotation(classOf[Table]).value()
val stmt = connection.prepareStatement(s"SELECT * FROM $table")
val result = stmt.executeQuery()
val list = getResult(clazz, result)
result.close()
stmt.close()
list
} else {
null
}
}
private def getResult[T](clazz: Class[T], result: ResultSet): List[T] = {
var list = List[T]()
while (result.next()) {
val obj = clazz.newInstance()
clazz.getDeclaredFields
.filter(f => f.isAnnotationPresent(classOf[ColumnName]))
.foreach(f => {
f.setAccessible(true)
if (f.getType.equals(classOf[UUID])) {
f.set(obj, UUID.fromString(result.getString(f.getAnnotation(classOf[ColumnName]).value())))
} else {
val column = f.getAnnotation(classOf[ColumnName])
val columnName = column.value()
f.set(obj, column.handler().newInstance().fromResult(result.getObject(columnName)))
}
})
list = obj :: list
}
list
}
}
package de.tobias.playpad.server.sql
class StringSqlResultHandler extends SqlResultHandler {
override def fromResult(o: Any): Any = o
override def toQuery(o: Any): Any = o
}
package de.tobias.playpad.server.sql
import java.util.UUID
class UUIDSqlResultHandler extends SqlResultHandler {
override def fromResult(o: Any): Any = {
o match {
case string: String => UUID.fromString(string)
case _ => null
}
}
override def toQuery(o: Any): Any = {
o match {
case uuid: UUID => uuid.toString
case _ => null
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment