Skip to content

Commit

Permalink
delete and update cache queries
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 17, 2024
1 parent 12a3c31 commit a51f197
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/quasar/dorm/ObjectManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.quasar.dorm.type.base.int
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import org.antlr.v4.runtime.ANTLRInputStream
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.antlr.v4.runtime.RecognitionException
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -143,7 +144,7 @@ class ObjectManager() {
}

fun <T: Any> query(query: String, resultType: Class<T> = DataObject::class.java as Class<T>) : Query<T> {
val tokenStream = CommonTokenStream(OQLLexer(ANTLRInputStream(query)))
val tokenStream = CommonTokenStream(OQLLexer(CharStreams.fromString(query)))

val parser = OQLParser(tokenStream)

Expand Down
146 changes: 123 additions & 23 deletions src/main/kotlin/com/quasar/dorm/persistence/DataObjectMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import com.quasar.dorm.transaction.TransactionState
import com.fasterxml.jackson.databind.ObjectMapper
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import jakarta.persistence.Query
import jakarta.persistence.criteria.CriteriaBuilder
import jakarta.persistence.criteria.ParameterExpression
import jakarta.persistence.criteria.Root
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -163,6 +165,93 @@ class ObjectWriter(private val descriptor: ObjectDescriptor) {
}
}

class AttributeUpdater<T>(attribute: String, type: Class<T>, entityManager: EntityManager) {
// instance data

val entityId : ParameterExpression<Int>
val attributeId : ParameterExpression<String>
val value : ParameterExpression<T>
val updateAttribute : Query

// init

init {
val builder = entityManager.criteriaBuilder

// delete attributes

entityId = builder.parameter(Int::class.java)
attributeId = builder.parameter(String::class.java)
value = builder.parameter(type)

val updateAttributeQuery = builder.createCriteriaUpdate(AttributeEntity::class.java)
val from = updateAttributeQuery.from(AttributeEntity::class.java)

updateAttributeQuery.set(attribute, value);

updateAttributeQuery.where(builder.and(
builder.equal(from.get<Int>("entity"), entityId),
builder.equal(from.get<Int>("attribute"), attributeId)
))

updateAttribute = entityManager.createQuery(updateAttributeQuery)
}

// public

fun update(entity: Int, attribute: String, value: T) {
updateAttribute
.setParameter(entityId, entity)
.setParameter(attributeId, attribute)
.setParameter(this.value, value)
.executeUpdate()
}
}

class ObjectDeleter(entityManager: EntityManager) {
// instance data

val attributeId : ParameterExpression<Int>
val entityId : ParameterExpression<Int>
val deleteAttribute : Query
val deleteEntity : Query

// init

init {
val builder = entityManager.criteriaBuilder

// delete attributes

attributeId = builder.parameter<Int>(Int::class.java)

val deleteAttributeQuery = builder.createCriteriaDelete(AttributeEntity::class.java)
val from = deleteAttributeQuery.from(AttributeEntity::class.java)

deleteAttributeQuery.where(builder.equal(from.get<Int>("entity"), attributeId))

deleteAttribute = entityManager.createQuery(deleteAttributeQuery)

// delete entity

entityId = builder.parameter<Int>(Int::class.java)

val criteriaQueryEntity = builder.createCriteriaDelete(EntityEntity::class.java)
val fromEntity = criteriaQueryEntity.from(EntityEntity::class.java)
criteriaQueryEntity.where(builder.equal(fromEntity.get<Int>("id"), entityId))

deleteEntity = entityManager.createQuery(criteriaQueryEntity)
}

// public

fun delete(obj: DataObject) {
deleteAttribute.setParameter(attributeId, obj.getId()).executeUpdate()
deleteEntity.setParameter(entityId, obj.getId()).executeUpdate()
}
}


@Component
class DataObjectMapper() {
// instance data
Expand All @@ -176,13 +265,34 @@ class DataObjectMapper() {
private val reader = ConcurrentHashMap<String, ObjectReader>()
private val jsonReader = ConcurrentHashMap<String, JSONReader>()
private val writer = ConcurrentHashMap<String, ObjectWriter>()
private val updater = ConcurrentHashMap<String, AttributeUpdater<Any>>()
private val deleter = ConcurrentHashMap<String, ObjectDeleter>()

// public

fun update(obj: DataObject) {
val builder: CriteriaBuilder = entityManager.criteriaBuilder

// update attributes
// new

val properties = obj.type.properties
for ( index in 1..obj.values.size - 1) {
if ( obj.values[index] != obj.state!!.snapshot!![index]) {
val property = properties[index]

when ( property.type.baseType) {
String::class.java -> updater4("stringValue", String::class.java).update(obj.getId(), property.name, obj.values[index] as String)
Integer::class.java -> updater4("intValue", Integer::class.java).update(obj.getId(), property.name, obj.values[index] as Integer)
Int::class.java -> updater4("intValue", Integer::class.java).update(obj.getId(), property.name, obj.values[index] as Integer)

else -> {
throw Error("ouch")
}
}
}
} // for

/* update attributes
val criteriaQuery = builder.createQuery(AttributeEntity::class.java)
val attributeEntity = criteriaQuery.from(AttributeEntity::class.java)
Expand All @@ -203,6 +313,7 @@ class DataObjectMapper() {
if ( obj.values[index] != obj.state!!.snapshot!![index])
writer.update(obj, index, attribute)
} // for
*/

// update entity

Expand All @@ -219,28 +330,8 @@ class DataObjectMapper() {
}

fun delete(obj: DataObject) {
if ( obj.getId() < 0)
return

val builder = entityManager.criteriaBuilder

// delete attributes

val criteriaQuery = builder.createCriteriaDelete(AttributeEntity::class.java)
val from = criteriaQuery.from(AttributeEntity::class.java)

criteriaQuery.where(builder.equal(from.get<Int>("entity"), obj.getId()))

entityManager.createQuery(criteriaQuery).executeUpdate()

// delete entity

val criteriaQueryEntity = builder.createCriteriaDelete(EntityEntity::class.java)
val fromEntity = criteriaQueryEntity.from(EntityEntity::class.java)

criteriaQueryEntity.where(builder.equal(fromEntity.get<Int>("id"), obj.getId()))

entityManager.createQuery(criteriaQueryEntity).executeUpdate()
if ( obj.getId() >= 0)
deleter4(obj.type).delete(obj)
}

fun create(obj: DataObject) {
Expand All @@ -264,6 +355,7 @@ class DataObjectMapper() {
val obj = jsonReader4(objectDescriptor).read(node)

obj.setId(entity.id) // TODO ?

// set state

state.register(ObjectState(obj, Status.MANAGED))
Expand Down Expand Up @@ -309,4 +401,12 @@ class DataObjectMapper() {
private fun writer4(objectDescriptor: ObjectDescriptor) : ObjectWriter {
return writer.getOrPut(objectDescriptor.name) { -> ObjectWriter(objectDescriptor) }
}

private fun <T> updater4(attribute: String, type: Class<T>) : AttributeUpdater<T> {
return AttributeUpdater(attribute, type, entityManager)//TODO updater.getOrPut(objectDescriptor.name) { -> ObjectUpdater(objectDescriptor) }
}

private fun deleter4(objectDescriptor: ObjectDescriptor) : ObjectDeleter {
return ObjectDeleter(entityManager)//TODO deleter.getOrPut(objectDescriptor.name) { -> ObjectDeleter(entityManager) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ abstract class EXPR {
}
}


abstract class VALUE(val value: Any) : EXPR() {
abstract fun <T: Any> resolve(query: Query<T>) : Value
}

class PARAMETER(private val name: String) : VALUE("null") { // TODO
class PARAMETER(private val name: String) : VALUE(name) {
// override

override fun <T: Any> resolve(query: Query<T>) : Value {
Expand Down Expand Up @@ -69,10 +68,10 @@ class SELECT() {

val query = queryManager
.create() // object query
.select(*select.map({path -> path.buildPath(alias)}).toTypedArray())
.select(*select.map { path -> path.buildPath(alias) }.toTypedArray())
.from(from!!)

if ( where != null)
if ( where !== null)
query.where(where!!.build(this, query))

return query as Query<T>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.sql.init.mode=always

spring.jpa.show-sql=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

0 comments on commit a51f197

Please sign in to comment.