diff --git a/src/main/kotlin/org/sirius/dorm/ObjectManager.kt b/src/main/kotlin/org/sirius/dorm/ObjectManager.kt index 8d69908..52e7a31 100644 --- a/src/main/kotlin/org/sirius/dorm/ObjectManager.kt +++ b/src/main/kotlin/org/sirius/dorm/ObjectManager.kt @@ -18,6 +18,7 @@ import jakarta.persistence.PersistenceContext import org.antlr.v4.runtime.CharStreams import org.antlr.v4.runtime.CommonTokenStream import org.antlr.v4.runtime.RecognitionException +import org.sirius.common.type.base.long import org.sirius.dorm.query.parser.OQLParser import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -31,7 +32,7 @@ class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) { private val properties = ArrayList>() init { - attribute("id", int(), true) + attribute("id", long(), true) } // fluent diff --git a/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt b/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt index 72b1d0c..241f63c 100644 --- a/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt +++ b/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt @@ -9,7 +9,7 @@ import jakarta.persistence.* import org.sirius.dorm.`object`.DataObject import java.io.Serializable -data class PropertyId(private val entity: Int = 0, private val attribute: String = "") : Serializable +data class PropertyId(private val entity: Long = 0L, private val attribute: String = "") : Serializable @Entity @Table(name="PROPERTY", @@ -45,9 +45,9 @@ data class PropertyEntity( @ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REMOVE]) @JoinTable( - name = "RELATION", - joinColumns = [JoinColumn(name = "FROM_ENTITY"), JoinColumn(name = "FROM")], - inverseJoinColumns = [JoinColumn(name = "TO_ENTITY"), JoinColumn(name = "TO")] + name = "RELATIONS", + joinColumns = [JoinColumn(name = "FROM_ENTITY"), JoinColumn(name = "FROM_")], + inverseJoinColumns = [JoinColumn(name = "TO_ENTITY"), JoinColumn(name = "TO_")] ) val targets : MutableSet = HashSet(), diff --git a/src/test/kotlin/org/sirius/dorm/AbstractTest.kt b/src/test/kotlin/org/sirius/dorm/AbstractTest.kt index 6942f9b..5fd61dd 100644 --- a/src/test/kotlin/org/sirius/dorm/AbstractTest.kt +++ b/src/test/kotlin/org/sirius/dorm/AbstractTest.kt @@ -5,6 +5,12 @@ package org.sirius.dorm * All rights reserved */ +import jakarta.persistence.EntityManager +import jakarta.persistence.PersistenceContext +import org.hibernate.Session +import org.hibernate.stat.Statistics +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach import org.sirius.common.tracer.TraceLevel import org.sirius.common.tracer.Tracer import org.sirius.common.tracer.trace.ConsoleTrace @@ -12,16 +18,12 @@ import org.sirius.common.type.base.* import org.sirius.dorm.model.Multiplicity import org.sirius.dorm.model.ObjectDescriptor import org.sirius.dorm.`object`.DataObject -import jakarta.persistence.EntityManager -import jakarta.persistence.PersistenceContext -import org.hibernate.Session -import org.hibernate.stat.Statistics -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeEach import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Import +import org.springframework.jdbc.core.JdbcTemplate +import org.springframework.stereotype.Component @Configuration() @@ -29,6 +31,71 @@ import org.springframework.context.annotation.Import class TestConfiguration { } +data class ColumnType(val col: String, val type: Class<*>, val format: String) + +@Component +class TablePrinter { + // instance + + @Autowired + lateinit var jdbcTemplate: JdbcTemplate + + val entity : Array = arrayOf( + ColumnType("ID ", Long::class.java, "%4d"), + ColumnType("TYPE ", String::class.java, "%-10s") + ) + + val relations : Array = arrayOf( + ColumnType("FROM_ENTITY", String::class.java, "%-11s"), + ColumnType("FROM_", Long::class.java, "%5d"), + ColumnType("TO_ENTITY", String::class.java, "%-11s"), + ColumnType("TO_", Long::class.java, "%5d") + ) + + val property : Array = arrayOf( + ColumnType("ENTITY ", Long::class.java,"%-10d"), + ColumnType("ATTRIBUTE ", String::class.java, "%-10s"), + ColumnType("TYPE ", String::class.java, "%-10s"), + ColumnType("STRING_VALUE", String::class.java, "%-11s"), + ColumnType("INT_VALUE ", Int::class.java, "%-10d"), + ColumnType("DOUBLE_VALUE", Double::class.java, "%-12f") + ) + + // public + + fun printAll() { + print("ENTITY", entity) + print("PROPERTY", property) + print("RELATIONS", relations) + } + + fun print(entity: String, columns: Array ) { + println(entity) + for ( col in columns) + print(col.col + " | ") + println() + + jdbcTemplate.query>("SELECT * FROM ${entity} ") { rs, _ -> + columns.map { column -> when (column.type) { + Long::class.java -> String.format(column.format, rs.getLong(column.col.strip())) + Int::class.java -> String.format(column.format, rs.getInt(column.col.strip())) + Double::class.java -> String.format(column.format, rs.getDouble(column.col.strip())) + String::class.java -> String.format(column.format, rs.getString(column.col.strip())) + else -> { + ""; + } + } + } + }.forEach { row -> + for ( col in row ) { + print(col + " | ") + } + println() + } + + } +} + @SpringBootTest(classes =[TestConfiguration::class]) class AbstractTest { @Autowired @@ -38,6 +105,9 @@ class AbstractTest { protected var personDescriptor : ObjectDescriptor? = null + @Autowired + lateinit var printer: TablePrinter + init { Tracer(ConsoleTrace(), "%t{yyyy-MM-dd HH:mm:ss,SSS} %l{-10s} [%p] %m") .setTraceLevel("", TraceLevel.OFF) @@ -47,6 +117,10 @@ class AbstractTest { lateinit var statistics : Statistics; + protected fun printTables() { + printer.printAll() + } + //@BeforeEach fun clearStats() { val session = entityManager.unwrap(Session::class.java) @@ -136,7 +210,7 @@ class AbstractTest { return query.execute().getResultList() } - protected fun createPerson(name: String, age: Int) : Int{ + protected fun createPerson(name: String, age: Int) : Long { objectManager.begin() try { val person = objectManager.create(personDescriptor!!) diff --git a/src/test/kotlin/org/sirius/dorm/CreateUpdateDeleteTests.kt b/src/test/kotlin/org/sirius/dorm/CreateUpdateDeleteTests.kt index 7f55f34..03b9053 100644 --- a/src/test/kotlin/org/sirius/dorm/CreateUpdateDeleteTests.kt +++ b/src/test/kotlin/org/sirius/dorm/CreateUpdateDeleteTests.kt @@ -15,6 +15,8 @@ class CreateUpdateDeleteTests: AbstractTest() { fun testOne() { val id = createPerson("Andi", 58) + printTables() + withTransaction { val person = objectManager.findById(personDescriptor!!, id)!! diff --git a/src/test/kotlin/org/sirius/dorm/RelationTests.kt b/src/test/kotlin/org/sirius/dorm/RelationTests.kt index 49899b6..fbee490 100644 --- a/src/test/kotlin/org/sirius/dorm/RelationTests.kt +++ b/src/test/kotlin/org/sirius/dorm/RelationTests.kt @@ -8,18 +8,20 @@ package org.sirius.dorm import org.sirius.dorm.`object`.DataObject import org.junit.jupiter.api.Test import org.sirius.common.type.base.* +import org.sirius.dorm.model.Cascade import org.sirius.dorm.model.Multiplicity import org.sirius.dorm.model.ObjectDescriptor import org.sirius.dorm.`object`.MultiValuedRelation import org.sirius.dorm.`object`.Relation import org.sirius.dorm.`object`.SingleValuedRelation +import org.sirius.dorm.query.eq import kotlin.test.assertEquals class RelationTests: AbstractTest() { @Test fun testOneToOne() { - var id = 0 + var id = 0L withTransaction { val andi = objectManager.create(personDescriptor!!) @@ -69,7 +71,7 @@ class RelationTests: AbstractTest() { // test - var id = 0 + var id = 0L withTransaction { val person = objectManager.create(descriptor) @@ -114,6 +116,8 @@ class RelationTests: AbstractTest() { //person.relation("children").add(child) } + printTables() + // reread withTransaction { @@ -131,8 +135,6 @@ class RelationTests: AbstractTest() { val x = iter.next() val y = iter.next() - - println() //assertEquals("Nika", children[0]["name"]) } } @@ -141,11 +143,10 @@ class RelationTests: AbstractTest() { fun testValidateRelation() { // create schema - withTransaction { objectManager.type("product") .attribute("name", string()) - .relation("part", "part", Multiplicity.ZERO_OR_MANY, "product") + .relation("parts", "part", Multiplicity.ZERO_OR_MANY, "product", Cascade.DELETE) .register() objectManager.type("part") @@ -179,6 +180,53 @@ class RelationTests: AbstractTest() { } assertEquals(true, caughtError) + + // now really + + var id = 0L + withTransaction { + val product = objectManager.create(productDescriptor) + + product["name"] = "Car" + + val part = objectManager.create(partDescriptor) + + part["name"] = "Motor" + + part["product"] = product + + id = product.id + } + + printTables() + + // try delete + + withTransaction { + val product = objectManager.findById(productDescriptor, id)!! + + objectManager.delete(product) + } + + // check + + withTransaction { + val queryManager = objectManager.queryManager() + val part = queryManager.from(partDescriptor) + + // no where + + val query = queryManager + .create() + .select(part) + .from(part) + + val result = query.executor() + .execute() + .getResultList() + + assertEquals(0, result.size) + } } @Test @@ -197,7 +245,7 @@ class RelationTests: AbstractTest() { // test - var id = 0 + var id = 0L withTransaction { val person = objectManager.create(descriptor!!) @@ -216,6 +264,8 @@ class RelationTests: AbstractTest() { person.relation("children").add(child) } + printTables() + // reread withTransaction { diff --git a/src/test/kotlin/org/sirius/dorm/TypeTests.kt b/src/test/kotlin/org/sirius/dorm/TypeTests.kt index da9303b..5805a66 100644 --- a/src/test/kotlin/org/sirius/dorm/TypeTests.kt +++ b/src/test/kotlin/org/sirius/dorm/TypeTests.kt @@ -76,4 +76,10 @@ class TypeTests { assert(double().greaterEqual(0.toDouble()).isValid(1.toDouble())) assert(!double().greaterEqual(0.toDouble()).isValid(-1.toDouble())) } + + @Test + fun testChar() { + assert(!character().isValid(1)) + assert(character().isValid('a')) + } } \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 39bd9a3..c5a7397 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -7,26 +7,27 @@ spring: sql: init: mode: always - logging: - level: - org: - x_hibernate: - orm: - jdbc: - bind: trace - type: - descriptor: - sql: trace jpa: properties: hibernate: format_sql: false generate_statistics: false - show-sql: true + show-sql: false defer-datasource-initialization: true hibernate: ddl-auto: update naming: implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - strategy: org.hibernate.cfg.ImprovedNamingStrategy \ No newline at end of file + strategy: org.hibernate.cfg.ImprovedNamingStrategy +logging: + level: + org: + xhibernate: + sql: debug + orm: + jdbc: + bind: trace + type: + descriptor: + sql: trace \ No newline at end of file