Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
}
}