5
5
6
6
package repository
7
7
8
+ import NodesTable
9
+ import SQLNodeEntity
10
+ import SQLTreeEntity
11
+ import TreesTable
8
12
import org.jetbrains.exposed.sql.Database
9
13
import org.jetbrains.exposed.sql.SchemaUtils
10
14
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
@@ -16,10 +20,6 @@ import repository.serialization.SerializableNode
16
20
import repository.serialization.SerializableTree
17
21
import repository.serialization.SerializableValue
18
22
import repository.serialization.strategies.Serialization
19
- import repository.sqliteEntities.NodesTable
20
- import repository.sqliteEntities.SQLNodeEntity
21
- import repository.sqliteEntities.SQLTreeEntity
22
- import repository.sqliteEntities.TreesTable
23
23
import trees.AbstractTree
24
24
import trees.nodes.AbstractNode
25
25
@@ -32,13 +32,15 @@ class SQLRepository<T : Comparable<T>,
32
32
33
33
private val typeOfTree = strategy.typeOfTree.toString()
34
34
35
+ // Initializes the database tables for storing trees and nodes.
35
36
init {
36
37
transaction(db) {
37
38
SchemaUtils .create(TreesTable )
38
39
SchemaUtils .create(NodesTable )
39
40
}
40
41
}
41
42
43
+ // Converts an instance of SQLNodeEntity to a SerializableNode object using the serialization strategy.
42
44
private fun SQLNodeEntity.toSerializableNode (): SerializableNode {
43
45
return SerializableNode (
44
46
SerializableValue (data),
@@ -48,6 +50,7 @@ class SQLRepository<T : Comparable<T>,
48
50
)
49
51
}
50
52
53
+ // Deserializes an instance of SQLNodeEntity to a NodeType object using the serialization strategy.
51
54
private fun SQLNodeEntity.deserialize (parent : NodeType ? = null): NodeType ? {
52
55
val node = strategy.createNode(this .toSerializableNode())
53
56
node?.parent = parent
@@ -56,6 +59,7 @@ class SQLRepository<T : Comparable<T>,
56
59
return node
57
60
}
58
61
62
+ // Converts a SerializableNode object to an instance of SQLNodeEntity using the serialization strategy.
59
63
private fun SerializableNode.toEntity (tree : SQLTreeEntity ): SQLNodeEntity = SQLNodeEntity .new {
60
64
this @new.data = this @toEntity.data.value
61
65
this @new.metadata = this @toEntity.metadata.value
@@ -64,20 +68,22 @@ class SQLRepository<T : Comparable<T>,
64
68
this .tree = tree
65
69
}
66
70
71
+ // Converts a SerializableTree object to an instance of SQLTreeEntity.
67
72
private fun SerializableTree.toEntity (): SQLTreeEntity {
68
73
return SQLTreeEntity .new {
69
74
this .name = this @toEntity.name
70
75
this .typeOfTree = this @SQLRepository.typeOfTree
71
76
}
72
77
}
73
78
79
+ // Saves a TreeType object to the database with a given name by deleting any existing tree with the same name and creating a new one.
74
80
override fun save (name : String , tree : TreeType ): Unit = transaction(db) {
75
81
deleteByName(name)
76
82
val entityTree = tree.toSerializableTree(name).toEntity()
77
83
entityTree.root = tree.root?.toSerializableNode()?.toEntity(entityTree)
78
84
}
79
85
80
-
86
+ // Loads a TreeType object from the database by name, if it exists.
81
87
override fun loadByName (name : String ): TreeType ? = transaction(db) {
82
88
SQLTreeEntity .find(
83
89
TreesTable .typeOfTree eq typeOfTree and (TreesTable .name eq name)
@@ -86,6 +92,7 @@ class SQLRepository<T : Comparable<T>,
86
92
}
87
93
}
88
94
95
+ // Deletes a tree from the database by name.
89
96
override fun deleteByName (name : String ): Unit = transaction(db) {
90
97
val treeId = SQLTreeEntity .find(
91
98
TreesTable .typeOfTree eq strategy.typeOfTree.toString() and (TreesTable .name eq name)
@@ -98,6 +105,7 @@ class SQLRepository<T : Comparable<T>,
98
105
}
99
106
}
100
107
108
+ // Returns a list of names of all the trees in the database.
101
109
override fun getNames (): List <String > = transaction(db) {
102
110
SQLTreeEntity .find(TreesTable .typeOfTree eq typeOfTree).map(SQLTreeEntity ::name)
103
111
}
0 commit comments