orm: refactor insert part of orm_stmt_gen (#11121)

pull/11138/head
Shobi 2021-08-10 22:31:09 +02:00 committed by GitHub
parent da53f818df
commit 012b3f0f64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 14 deletions

View File

@ -136,24 +136,22 @@ pub fn orm_stmt_gen(table string, para string, kind StmtKind, num bool, qm strin
match kind { match kind {
.insert { .insert {
str += 'INSERT INTO $para$table$para (' mut values := []string{}
for i, field in data.fields {
str += '$para$field$para' for _ in 0 .. data.fields.len {
if i < data.fields.len - 1 { // loop over the length of data.field and generate ?0, ?1 or just ? based on the $num parameter for value placeholders
str += ', '
}
}
str += ') VALUES ('
for i, _ in data.fields {
str += qm
if num { if num {
str += '$c' values << '$qm$c'
c++ c++
} } else {
if i < data.fields.len - 1 { values << '$qm'
str += ', '
} }
} }
str += 'INSERT INTO $para$table$para ('
str += data.fields.map('$para$it$para').join(', ')
str += ') VALUES ('
str += values.join(', ')
str += ')' str += ')'
} }
.update { .update {