From 012b3f0f6481bd03915c572fd4679e0578d8f8d5 Mon Sep 17 00:00:00 2001 From: Shobi Date: Tue, 10 Aug 2021 22:31:09 +0200 Subject: [PATCH] orm: refactor insert part of orm_stmt_gen (#11121) --- vlib/orm/orm.v | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index 6a6afbb7c5..a6831d1846 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -136,24 +136,22 @@ pub fn orm_stmt_gen(table string, para string, kind StmtKind, num bool, qm strin match kind { .insert { - str += 'INSERT INTO $para$table$para (' - for i, field in data.fields { - str += '$para$field$para' - if i < data.fields.len - 1 { - str += ', ' - } - } - str += ') VALUES (' - for i, _ in data.fields { - str += qm + mut values := []string{} + + for _ in 0 .. data.fields.len { + // loop over the length of data.field and generate ?0, ?1 or just ? based on the $num parameter for value placeholders if num { - str += '$c' + values << '$qm$c' c++ - } - if i < data.fields.len - 1 { - str += ', ' + } else { + values << '$qm' } } + + str += 'INSERT INTO $para$table$para (' + str += data.fields.map('$para$it$para').join(', ') + str += ') VALUES (' + str += values.join(', ') str += ')' } .update {