kotlin-orm / ktorm

A lightweight ORM framework for Kotlin with strong-typed SQL DSL and sequence APIs.
https://www.ktorm.org
Apache License 2.0
2.13k stars 147 forks source link

Document DDL #568

Open mvysny opened 3 months ago

mvysny commented 3 months ago

Would be great if README.md documented how to run DDLs; it would help running an example code quickly on a H2 in-memory database. I only found database.executeUpdate() but I have no idea which SqlExpression to use for a DDL statement.

mvysny commented 3 months ago

Workaround: use JDBC:

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

    database.useTransaction {
        it.connection.prepareStatement("""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
);""").executeUpdate()
    }

    for (row in database.from(Employees).select()) {
        println(row[Employees.name])
    }
}
sidian123 commented 2 months ago

you also can see see this function: org.ktorm.database.Database#useConnection