fmt: fix SQL formatting in ORM expressions - remove trailing spaces (#5996)

pull/5940/head
Enzo 2020-07-28 10:09:42 +02:00 committed by GitHub
parent 28657fe7cb
commit d7ed3cd8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 6 deletions

View File

@ -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('')

View File

@ -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
}
}