From d7ed3cd8b777b4887b71f1418c89196e059c7279 Mon Sep 17 00:00:00 2001 From: Enzo Date: Tue, 28 Jul 2020 10:09:42 +0200 Subject: [PATCH] fmt: fix SQL formatting in ORM expressions - remove trailing spaces (#5996) --- vlib/v/fmt/fmt.v | 9 +++----- vlib/v/fmt/tests/orm_keep.vv | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 vlib/v/fmt/tests/orm_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 46e7168bd4..e166dcaf7a 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -968,19 +968,16 @@ pub fn (mut f Fmt) expr(node ast.Expr) { } } f.write('from ${util.strip_mod_name(node.table_name)}') - f.write(' ') if node.has_where { - f.write('where ') + f.write(' where ') f.expr(node.where_expr) - f.write(' ') } if node.has_limit { - f.write('limit ') + f.write(' limit ') f.expr(node.limit_expr) - f.write(' ') } if node.has_offset { - f.write('offset ') + f.write(' offset ') f.expr(node.offset_expr) } f.writeln('') diff --git a/vlib/v/fmt/tests/orm_keep.vv b/vlib/v/fmt/tests/orm_keep.vv new file mode 100644 index 0000000000..7b2643bd2b --- /dev/null +++ b/vlib/v/fmt/tests/orm_keep.vv @@ -0,0 +1,45 @@ +import sqlite + +struct Customer { + id int + name string + nr_orders int + country string +} + +fn find_all_customers(db sqlite.DB) []Customer { + return sql db { + select from Customer + } +} + +fn main() { + db := sqlite.connect('customers.db')? + // select count(*) from Customer + nr_customers := sql db { + select count from Customer + } + println('number of all customers: $nr_customers') + // V syntax can be used to build queries + // db.select returns an array + uk_customers := sql db { + select from Customer where country == 'uk' && nr_orders > 0 + } + println(uk_customers.len) + for customer in uk_customers { + println('$customer.id - $customer.name') + } + // by adding `limit 1` we tell V that there will be only one object + customer := sql db { + select from Customer where id == 1 limit 1 + } + println('$customer.id - $customer.name') + // insert a new customer + new_customer := Customer{ + name: 'Bob' + nr_orders: 10 + } + sql db { + insert new_customer into Customer + } +}