Skip to content

Commit

Permalink
orm: enforce that queries always return a Result, a query-resulting a…
Browse files Browse the repository at this point in the history
…rray can be used as a V array in place. (#17871)
  • Loading branch information
walking devel authored Apr 4, 2023
1 parent 9addede commit 8452644
Show file tree
Hide file tree
Showing 67 changed files with 479 additions and 354 deletions.
2 changes: 2 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const (
'vlib/orm/orm_string_interpolation_in_where_test.v',
'vlib/orm/orm_interface_test.v',
'vlib/orm/orm_mut_db_test.v',
'vlib/orm/orm_result_test.v',
'vlib/db/sqlite/sqlite_test.v',
'vlib/db/sqlite/sqlite_orm_test.v',
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
Expand Down Expand Up @@ -205,6 +206,7 @@ const (
'vlib/orm/orm_string_interpolation_in_where_test.v',
'vlib/orm/orm_interface_test.v',
'vlib/orm/orm_mut_db_test.v',
'vlib/orm/orm_result_test.v',
'vlib/v/tests/orm_sub_struct_test.v',
'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_joined_tables_select_test.v',
Expand Down
8 changes: 4 additions & 4 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4850,18 +4850,18 @@ db := sqlite.connect('customers.db')!
// )
sql db {
create table Customer
}
}!
// select count(*) from customers
nr_customers := sql db {
select count from Customer
}
}!
println('number of all customers: ${nr_customers}')
// V syntax can be used to build queries
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}')
Expand All @@ -4874,7 +4874,7 @@ new_customer := Customer{
}
sql db {
insert new_customer into Customer
}
}!
```

For more examples and the docs, see [vlib/orm](https://github.com/vlang/v/tree/master/vlib/orm).
Expand Down
62 changes: 30 additions & 32 deletions examples/database/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ fn sqlite3_array() ! {
sql db {
drop table Parent
drop table Child
}
} or {}
db.close() or {}
}
//

sql db {
create table Parent
}
}!
sql db {
create table Child
}
}!
par := Parent{
name: 'test'
children: [
Expand All @@ -93,10 +93,10 @@ fn sqlite3_array() ! {
}
sql db {
insert par into Parent
}
}!
parent := sql db {
select from Parent where id == 1
}
}!
eprintln(parent)
}

Expand All @@ -113,16 +113,16 @@ fn msql_array() ! {
defer {
sql db {
drop table Parent
}
} or {}
db.close()
}
//

db.query('drop table if exists Parent')!
db.query('drop table if exists Child')!
sql db {
create table Parent
create table Child
}
}!
par := Parent{
name: 'test'
children: [
Expand All @@ -136,10 +136,10 @@ fn msql_array() ! {
}
sql db {
insert par into Parent
}
}!
parent := sql db {
select from Parent where id == 1
}
}!
eprintln(parent)
}

Expand All @@ -151,11 +151,11 @@ fn psql_array() ! {
db.close()
}
db.exec_one('drop table if exists "Parent", "Child"') or { eprintln(err) }
//

sql db {
create table Parent
create table Child
}
}!
par := Parent{
name: 'test'
children: [
Expand All @@ -169,10 +169,10 @@ fn psql_array() ! {
}
sql db {
insert par into Parent
}
}!
parent := sql db {
select from Parent where id == 1
}
}!
eprintln(parent)
}

Expand All @@ -182,19 +182,17 @@ fn sqlite3() ! {
defer {
sql db {
drop table Module
}
sql db {
drop table User
}
} or {}
db.close() or {}
}
//

sql db {
create table Module
}
}!
sql db {
create table User
}
}!
mod := Module{
name: 'test'
nr_downloads: 10
Expand All @@ -206,10 +204,10 @@ fn sqlite3() ! {
}
sql db {
insert mod into Module
}
}!
modul := sql db {
select from Module where id == 1
}
}!
eprintln(modul)
}

Expand All @@ -230,13 +228,13 @@ fn msql() ! {
}
conn.query('DROP TABLE IF EXISTS Module') or { eprintln(err) }
conn.query('DROP TABLE IF EXISTS User') or { eprintln(err) }
//

sql conn {
create table Module
}
}!
sql conn {
create table User
}
}!
mod := Module{
name: 'test'
nr_downloads: 10
Expand All @@ -248,10 +246,10 @@ fn msql() ! {
}
sql conn {
insert mod into Module
}
}!
m := sql conn {
select from Module where id == 1
}
}!
eprintln(m)
}

Expand All @@ -266,7 +264,7 @@ fn psql() ! {
sql db {
create table Module
create table User
}
}!
mod := Module{
name: 'test'
nr_downloads: 10
Expand All @@ -278,13 +276,13 @@ fn psql() ! {
}
sql db {
insert mod into Module
}
}!
modul := sql db {
select from Module where id == 1
}
}!
sql db {
drop table Module
}
}!
eprintln(modul)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/js_dom_draw_bechmark_chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sql db {
create table Task
}
}!
task_model := Task{
title: 'a'
Expand All @@ -55,14 +55,14 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sw.start()
sql db {
insert task_model into Task
}
} or { []Task{} }
sw.stop()
insert_stopwatchs << int(sw.end - sw.start)
}
sql db {
drop table Task
}
}!
response := Response{
insert: insert_stopwatchs
Expand Down
6 changes: 3 additions & 3 deletions examples/js_dom_draw_bechmark_chart/chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sql db {
create table Task
}
} or { panic(err) }
task_model := Task{
title: 'a'
Expand All @@ -42,14 +42,14 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sw.start()
sql db {
insert task_model into Task
}
} or { []Task{} }
sw.stop()
insert_stopwatchs << int(sw.end - sw.start)
}
sql db {
drop table Task
}
} or { panic(err) }
response := Response{
insert: insert_stopwatchs
Expand Down
10 changes: 5 additions & 5 deletions examples/js_dom_draw_bechmark_chart/v_vweb_orm/src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {

sql db {
create table Task
}
} or { panic(err) }

task_model := Task{
title: 'a'
Expand All @@ -62,7 +62,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sw.start()
sql db {
insert task_model into Task
}
} or { panic(err) }
sw.stop()
insert_stopwatchs << int(sw.end - sw.start)
}
Expand All @@ -72,7 +72,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sw.start()
result := sql db {
select from Task
}
} or { []Task{} }
sw.stop()
eprintln(result)
select_stopwatchs << int(sw.end - sw.start)
Expand All @@ -83,14 +83,14 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
sw.start()
sql db {
update Task set title = 'b', status = 'finish' where id == i
}
} or { panic(err) }
sw.stop()
update_stopwatchs << int(sw.end - sw.start)
}

sql db {
drop table Task
}
} or { panic(err) }

response := Response{
insert: insert_stopwatchs
Expand Down
2 changes: 1 addition & 1 deletion examples/vweb_fullstack/src/auth_services.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn (mut app App) service_auth(username string, password string) !string {

users := sql db {
select from User where username == username
}
}!
user := users.first()
if user.username != username {
return error('user not found')
Expand Down
2 changes: 1 addition & 1 deletion examples/vweb_fullstack/src/product_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn (mut app App) service_get_all_products_from(user_id int) ![]Product {

results := sql db {
select from Product where user_id == user_id
}
}!

return results
}
4 changes: 2 additions & 2 deletions examples/vweb_fullstack/src/user_services.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn (mut app App) service_get_all_user() ![]User {

results := sql db {
select from User
}
}!

return results
}
Expand All @@ -59,7 +59,7 @@ fn (mut app App) service_get_user(id int) !User {

results := sql db {
select from User where id == id
}
}!

return results.first()
}
2 changes: 1 addition & 1 deletion examples/vweb_orm_jwt/src/auth_services.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn (mut app App) service_auth(username string, password string) !string {

users := sql db {
select from User where username == username
}
}!

if users.len == 0 {
return error('user not found')
Expand Down
2 changes: 1 addition & 1 deletion examples/vweb_orm_jwt/src/user_controllers.v
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn (mut app App) delete() vweb.Result {

sql db {
drop table User
}
} or { panic(err) }

return app.text('Successfully deleted table')
}
Loading

0 comments on commit 8452644

Please sign in to comment.