GithubHelp home page GithubHelp logo

Comments (3)

mvysny avatar mvysny commented on August 22, 2024

Full example:

import org.ktorm.database.Database
import org.ktorm.database.Transaction
import org.ktorm.dsl.*
import org.ktorm.entity.*
import org.ktorm.schema.*
import java.time.LocalDate

object Departments : Table<Department>("t_department") {
    val id = int("id").primaryKey().bindTo { it.id }
    val name = varchar("name").bindTo { it.name }
    val location = varchar("location").bindTo { it.location }
}

object Employees : Table<Employee>("t_employee") {
    val id = int("id").primaryKey().bindTo { it.id }
    val name = varchar("name").bindTo { it.name }
    val job = varchar("job").bindTo { it.job }
    val managerId = int("manager_id").bindTo { it.manager?.id }
    val hireDate = date("hire_date").bindTo { it.hireDate }
    val salary = long("salary").bindTo { it.salary }
    val departmentId = int("department_id").references(Departments) { it.department }
}

interface Department : Entity<Department> {
    companion object : Entity.Factory<Department>()
    val id: Int
    var name: String
    var location: String
}

interface Employee : Entity<Employee> {
    companion object : Entity.Factory<Employee>()
    val id: Int
    var name: String
    var job: String
    var manager: Employee?
    var hireDate: LocalDate
    var salary: Long
    var department: Department
}

val Database.departments get() = this.sequenceOf(Departments)
val Database.employees get() = this.sequenceOf(Employees)

fun Transaction.ddl(ddl: String) {
    connection.prepareStatement(ddl).executeUpdate()
}

fun main() {
    val database = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", user = "root", password = "***")

    database.useTransaction {
        it.ddl(
            """create table T_EMPLOYEE(
  ID int not null primary key auto_increment,
  name varchar(128) not null,
  job varchar(128) not null,
  manager_id int null,
  hire_date date not null,
  salary bigint not null,
  department_id int not null
)"""
        )
        it.ddl("""
            create table t_department(
              id int not null primary key auto_increment,
              name varchar(128) not null,
              location varchar(128) not null
            )
        """.trimIndent())

        for (row in database.from(Employees).select()) {
            println(row[Employees.name])
        }

        val employee = database.employees.find { it.job eq "vince" }
        println(employee)
    }
}

from ktorm.

vincentlauvlwj avatar vincentlauvlwj commented on August 22, 2024

H2 treats quoted names and unquoted names differently, quoted names are case-sensitve while unquoted names are case-insensitive, so name and "name" are different identifiers, see https://stackoverflow.com/questions/10789994/make-h2-treat-quoted-name-and-unquoted-name-as-the-same

There are 2 options to solve the problem.

Option 1: Use upper-case unquoted names.

CREATE TABLE T_EMPLOYEE(
  ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(128) NOT NULL,
  JOB VARCHAR(128) NOT NULL,
  MANAGER_ID INT NULL,
  HIRE_DATE DATE NOT NULL,
  SALARY BIGINT NOT NULL,
  DEPARTMENT_ID INT NOT NULL
);
object Employees : Table<Employee>("T_EMPLOYEE") {
    val id = int("ID").primaryKey().bindTo { it.id }
    val name = varchar("NAME").bindTo { it.name }
    val job = varchar("JOB").bindTo { it.job }
    val managerId = int("MANAGER_ID").bindTo { it.manager?.id }
    val hireDate = date("HIRE_DATE").bindTo { it.hireDate }
    val salary = long("SALARY").bindTo { it.salary }
    val departmentId = int("DEPARTMENT_ID").references(Departments) { it.department }
}

Option 2: Always use quoted names and set alwaysQuoteIdentifiers = true.

create table "t_employee"(
  "id" int not null primary key auto_increment,
  "name" varchar(128) not null,
  "job" varchar(128) not null,
  "manager_id" int null,
  "hire_date" date not null,
  "salary" bigint not null,
  "department_id" int not null
);
object Employees : Table<Employee>("t_employee") {
    val id = int("id").primaryKey().bindTo { it.id }
    val name = varchar("name").bindTo { it.name }
    val job = varchar("job").bindTo { it.job }
    val managerId = int("manager_id").bindTo { it.manager?.id }
    val hireDate = date("hire_date").bindTo { it.hireDate }
    val salary = long("salary").bindTo { it.salary }
    val departmentId = int("department_id").references(Departments) { it.department }
}
val database = Database.connect("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1", alwaysQuoteIdentifiers = true)

from ktorm.

mvysny avatar mvysny commented on August 22, 2024

Excellent find, I was unaware of this fact, thank you so much for your reply. Tried first option and it worked really well for me. This kind of feels like more of a quirk at H2's side; so I'll close this ticket.

from ktorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.