12. テーブル定義(SQL)
CREATE TABLE t_department(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
location VARCHAR(128) NOT NULL
);
CREATE TABLE t_employee(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
job VARCHAR(128) NOT NULL,
hire_date DATE NOT NULL,
salary BIGINT NOT NULL,
department_id INT NOT NULL
);
12
13. テーブル定義(Ktorm)
object KtormDepartments : 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 KtormEmployees : 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 hireDate = date("hire_date").bindTo { it.hireDate }
val salary = long("salary").bindTo { it.salary }
val departmentId = int("department_id").references(KtormDepartments) { it.department }
}
13
14. テーブル定義(Exposed)
object ExposedDepartments : IntIdTable("t_department") {
val name = varchar("name", 50)
val location = varchar("location", 50)
}
object ExposedEmployees : IntIdTable("t_employee") {
val name = varchar("name", 50)
val job = varchar("job", 50).nullable()
val hireDate = date("hire_date")
val salary = long("salary")
val departmentId = reference("department_id", ExposedDepartments.id)
}
14
15. SELECT
// Ktorm
database
.from(KtormEmployees)
.select(KtormEmployees.name)
.where { (KtormEmployees.departmentId notEq 4) and (KtormEmployees.name like "%郎%") }
// Exposed
ExposedEmployees
.slice(ExposedEmployees.name)
.select { (ExposedEmployees.departmentId eq 1) and (ExposedEmployees.name like "%郎%") }
SQL: SELECT t_employee.`name` FROM t_employee WHERE (t_employee.department_id = 1) AND (t_employee.`name` LIKE '%郎%')
Ktorm: SQLそのまんま
Exposed: selectにwhere式を渡す& sliceで取得したい列を指定 15
23. Entity定義(Ktorm)
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 hireDate: LocalDate
var salary: Long
var department: Department
}
23
24. Entity定義(Exposed)
class ExposedDepartment(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ExposedDepartment>(ExposedDepartments)
var name by ExposedDepartments.name
var location by ExposedDepartments.location
}
class ExposedEmployee(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ExposedEmployee>(ExposedEmployees)
var name by ExposedEmployees.name
var job by ExposedEmployees.job
var hireDate by ExposedEmployees.hireDate
var salary by ExposedEmployees.salary
var department by ExposedDepartment referencedOn ExposedEmployees.departmentId
}
24