mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fmt: fix SQL formatting in ORM expressions - remove trailing spaces (#5996)
This commit is contained in:
parent
28657fe7cb
commit
d7ed3cd8b7
@ -968,16 +968,13 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.write('from ${util.strip_mod_name(node.table_name)}')
|
f.write('from ${util.strip_mod_name(node.table_name)}')
|
||||||
f.write(' ')
|
|
||||||
if node.has_where {
|
if node.has_where {
|
||||||
f.write(' where ')
|
f.write(' where ')
|
||||||
f.expr(node.where_expr)
|
f.expr(node.where_expr)
|
||||||
f.write(' ')
|
|
||||||
}
|
}
|
||||||
if node.has_limit {
|
if node.has_limit {
|
||||||
f.write(' limit ')
|
f.write(' limit ')
|
||||||
f.expr(node.limit_expr)
|
f.expr(node.limit_expr)
|
||||||
f.write(' ')
|
|
||||||
}
|
}
|
||||||
if node.has_offset {
|
if node.has_offset {
|
||||||
f.write(' offset ')
|
f.write(' offset ')
|
||||||
|
45
vlib/v/fmt/tests/orm_keep.vv
Normal file
45
vlib/v/fmt/tests/orm_keep.vv
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user