+
Skip to content

Fix for issue #206 - insert with schema #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ subprojects { project ->
name = "Antony Denyer"
email = "git@antonydenyer.co.uk"
}
developer {
id = "sinzed"
name = "Saeed Zahedi"
email = "saeedzhd@gmail.com"
}
developer {
id = "smn-dv"
name = "Simon Schoof"
email = "simon.schoof@hey.com"
}
}
scm {
url = "https://github.com/kotlin-orm/ktorm.git"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ public abstract class SqlFormatter(

override fun visitInsert(expr: InsertExpression): InsertExpression {
writeKeyword("insert into ")
write("${expr.table.name.quoted} (")
visitTable(expr.table)
write(" (")
for ((i, assignment) in expr.assignments.withIndex()) {
if (i > 0) write(", ")
checkColumnName(assignment.column.name)
Expand All @@ -550,7 +551,8 @@ public abstract class SqlFormatter(

override fun visitInsertFromQuery(expr: InsertFromQueryExpression): InsertFromQueryExpression {
writeKeyword("insert into ")
write("${expr.table.name.quoted} (")
visitTable(expr.table)
write(" (")
for ((i, column) in expr.columns.withIndex()) {
if (i > 0) write(", ")
checkColumnName(column.name)
Expand Down
21 changes: 21 additions & 0 deletions ktorm-core/src/test/kotlin/org/ktorm/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ open class BaseTest {
var department: Department
}

interface Customer : Entity<Customer> {
companion object : Entity.Factory<Customer>()
var id: Int?
var name: String
var email: String
var phoneNumber: String
}

open class Departments(alias: String?) : Table<Department>("t_department", alias) {
companion object : Departments(null)
override fun aliased(alias: String) = Departments(alias)
Expand All @@ -97,7 +105,20 @@ open class BaseTest {
val department = departmentId.referenceTable as Departments
}

open class Customers(alias: String?) : Table<Customer>("t_customer", alias, schema="COMPANY") {
companion object : Customers(null)
override fun aliased(alias: String) = Customers(alias)

val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val email = varchar("email").bindTo { it.email }
val phoneNumber = varchar("phoneNumber").bindTo { it.phoneNumber }

}

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

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

val Database.customers get() = this.sequenceOf(Customers)
}
11 changes: 11 additions & 0 deletions ktorm-core/src/test/kotlin/org/ktorm/dsl/DmlTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class DmlTest : BaseTest() {
assert(database.employees.count() == 5)
}

@Test
fun testInsertWithSchema() {
database.insert(Customers) {
set(it.name, "steve")
set(it.email, "steve@job.com")
set(it.phoneNumber, "0123456789")
}

assert(database.customers.count() == 1)
}

@Test
fun testBatchInsert() {
database.batchInsert(Employees) {
Expand Down
4 changes: 3 additions & 1 deletion ktorm-core/src/test/resources/drop-data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

drop table if exists "t_department";
drop table if exists "t_employee";
drop table if exists "t_employee0";
drop table if exists "t_employee0";
drop table if exists COMPANY."t_customer";
drop schema if exists COMPANY;
10 changes: 9 additions & 1 deletion ktorm-core/src/test/resources/init-data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

create schema COMPANY;
create table "t_department"(
"id" int not null primary key auto_increment,
"name" varchar(128) not null,
Expand All @@ -16,6 +17,13 @@ create table "t_employee"(
"department_id" int not null
);

create table COMPANY."t_customer" (
"id" int not null primary key auto_increment,
"name" varchar(128) not null,
"email" varchar(128) not null,
"phoneNumber" varchar(128) not null,
);

insert into "t_department"("name", "location") values ('tech', 'Guangzhou');
insert into "t_department"("name", "location") values ('finance', 'Beijing');

Expand Down Expand Up @@ -49,4 +57,4 @@ insert into "t_employee0"("name", "job", "manager_id", "hire_date", "salary", "d
insert into "t_employee0"("name", "job", "manager_id", "hire_date", "salary", "department_id")
values ('tom', 'director', null, '2018-01-01', 200, 2);
insert into "t_employee0"("name", "job", "manager_id", "hire_date", "salary", "department_id")
values ('penny', 'assistant', 3, '2019-01-01', 100, 2);
values ('penny', 'assistant', 3, '2019-01-01', 100, 2);
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public open class MySqlFormatter(

protected open fun visitInsertOrUpdate(expr: InsertOrUpdateExpression): InsertOrUpdateExpression {
writeKeyword("insert into ")
write("${expr.table.name.quoted} (")
visitTable(expr.table)
write(" (")
for ((i, assignment) in expr.assignments.withIndex()) {
if (i > 0) write(", ")
checkColumnName(assignment.column.name)
Expand All @@ -97,7 +98,8 @@ public open class MySqlFormatter(

protected open fun visitBulkInsert(expr: BulkInsertExpression): BulkInsertExpression {
writeKeyword("insert into ")
write("${expr.table.name.quoted} (")
visitTable(expr.table)
write(" (")
for ((i, assignment) in expr.assignments[0].withIndex()) {
if (i > 0) write(", ")
checkColumnName(assignment.column.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public open class PostgreSqlFormatter(

protected open fun visitInsertOrUpdate(expr: InsertOrUpdateExpression): InsertOrUpdateExpression {
writeKeyword("insert into ")
write("${expr.table.name.quoted} (")
visitTable(expr.table)
write(" (")
for ((i, assignment) in expr.assignments.withIndex()) {
if (i > 0) write(", ")
checkColumnName(assignment.column.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SQLiteTest : BaseTest() {
}

override fun destroy() {
super.destroy()
execSqlScript("drop-sqlite-data.sql")
connection.close()
}

Expand Down
4 changes: 4 additions & 0 deletions ktorm-support-sqlite/src/test/resources/drop-sqlite-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

drop table if exists "t_department";
drop table if exists "t_employee";
drop table if exists "t_employee0";
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载