Skip to content

Commit 8452644

Browse files
author
walking devel
authored
orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871)
1 parent 9addede commit 8452644

67 files changed

Lines changed: 479 additions & 354 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/tools/vtest-self.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ const (
130130
'vlib/orm/orm_string_interpolation_in_where_test.v',
131131
'vlib/orm/orm_interface_test.v',
132132
'vlib/orm/orm_mut_db_test.v',
133+
'vlib/orm/orm_result_test.v',
133134
'vlib/db/sqlite/sqlite_test.v',
134135
'vlib/db/sqlite/sqlite_orm_test.v',
135136
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
@@ -205,6 +206,7 @@ const (
205206
'vlib/orm/orm_string_interpolation_in_where_test.v',
206207
'vlib/orm/orm_interface_test.v',
207208
'vlib/orm/orm_mut_db_test.v',
209+
'vlib/orm/orm_result_test.v',
208210
'vlib/v/tests/orm_sub_struct_test.v',
209211
'vlib/v/tests/orm_sub_array_struct_test.v',
210212
'vlib/v/tests/orm_joined_tables_select_test.v',

doc/docs.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4850,18 +4850,18 @@ db := sqlite.connect('customers.db')!
48504850
// )
48514851
sql db {
48524852
create table Customer
4853-
}
4853+
}!
48544854
48554855
// select count(*) from customers
48564856
nr_customers := sql db {
48574857
select count from Customer
4858-
}
4858+
}!
48594859
println('number of all customers: ${nr_customers}')
48604860
48614861
// V syntax can be used to build queries
48624862
uk_customers := sql db {
48634863
select from Customer where country == 'uk' && nr_orders > 0
4864-
}
4864+
}!
48654865
println(uk_customers.len)
48664866
for customer in uk_customers {
48674867
println('${customer.id} - ${customer.name}')
@@ -4874,7 +4874,7 @@ new_customer := Customer{
48744874
}
48754875
sql db {
48764876
insert new_customer into Customer
4877-
}
4877+
}!
48784878
```
48794879

48804880
For more examples and the docs, see [vlib/orm](https://github.com/vlang/v/tree/master/vlib/orm).

examples/database/orm.v

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ fn sqlite3_array() ! {
7070
sql db {
7171
drop table Parent
7272
drop table Child
73-
}
73+
} or {}
7474
db.close() or {}
7575
}
76-
//
76+
7777
sql db {
7878
create table Parent
79-
}
79+
}!
8080
sql db {
8181
create table Child
82-
}
82+
}!
8383
par := Parent{
8484
name: 'test'
8585
children: [
@@ -93,10 +93,10 @@ fn sqlite3_array() ! {
9393
}
9494
sql db {
9595
insert par into Parent
96-
}
96+
}!
9797
parent := sql db {
9898
select from Parent where id == 1
99-
}
99+
}!
100100
eprintln(parent)
101101
}
102102

@@ -113,16 +113,16 @@ fn msql_array() ! {
113113
defer {
114114
sql db {
115115
drop table Parent
116-
}
116+
} or {}
117117
db.close()
118118
}
119-
//
119+
120120
db.query('drop table if exists Parent')!
121121
db.query('drop table if exists Child')!
122122
sql db {
123123
create table Parent
124124
create table Child
125-
}
125+
}!
126126
par := Parent{
127127
name: 'test'
128128
children: [
@@ -136,10 +136,10 @@ fn msql_array() ! {
136136
}
137137
sql db {
138138
insert par into Parent
139-
}
139+
}!
140140
parent := sql db {
141141
select from Parent where id == 1
142-
}
142+
}!
143143
eprintln(parent)
144144
}
145145

@@ -151,11 +151,11 @@ fn psql_array() ! {
151151
db.close()
152152
}
153153
db.exec_one('drop table if exists "Parent", "Child"') or { eprintln(err) }
154-
//
154+
155155
sql db {
156156
create table Parent
157157
create table Child
158-
}
158+
}!
159159
par := Parent{
160160
name: 'test'
161161
children: [
@@ -169,10 +169,10 @@ fn psql_array() ! {
169169
}
170170
sql db {
171171
insert par into Parent
172-
}
172+
}!
173173
parent := sql db {
174174
select from Parent where id == 1
175-
}
175+
}!
176176
eprintln(parent)
177177
}
178178

@@ -182,19 +182,17 @@ fn sqlite3() ! {
182182
defer {
183183
sql db {
184184
drop table Module
185-
}
186-
sql db {
187185
drop table User
188-
}
186+
} or {}
189187
db.close() or {}
190188
}
191-
//
189+
192190
sql db {
193191
create table Module
194-
}
192+
}!
195193
sql db {
196194
create table User
197-
}
195+
}!
198196
mod := Module{
199197
name: 'test'
200198
nr_downloads: 10
@@ -206,10 +204,10 @@ fn sqlite3() ! {
206204
}
207205
sql db {
208206
insert mod into Module
209-
}
207+
}!
210208
modul := sql db {
211209
select from Module where id == 1
212-
}
210+
}!
213211
eprintln(modul)
214212
}
215213

@@ -230,13 +228,13 @@ fn msql() ! {
230228
}
231229
conn.query('DROP TABLE IF EXISTS Module') or { eprintln(err) }
232230
conn.query('DROP TABLE IF EXISTS User') or { eprintln(err) }
233-
//
231+
234232
sql conn {
235233
create table Module
236-
}
234+
}!
237235
sql conn {
238236
create table User
239-
}
237+
}!
240238
mod := Module{
241239
name: 'test'
242240
nr_downloads: 10
@@ -248,10 +246,10 @@ fn msql() ! {
248246
}
249247
sql conn {
250248
insert mod into Module
251-
}
249+
}!
252250
m := sql conn {
253251
select from Module where id == 1
254-
}
252+
}!
255253
eprintln(m)
256254
}
257255

@@ -266,7 +264,7 @@ fn psql() ! {
266264
sql db {
267265
create table Module
268266
create table User
269-
}
267+
}!
270268
mod := Module{
271269
name: 'test'
272270
nr_downloads: 10
@@ -278,13 +276,13 @@ fn psql() ! {
278276
}
279277
sql db {
280278
insert mod into Module
281-
}
279+
}!
282280
modul := sql db {
283281
select from Module where id == 1
284-
}
282+
}!
285283
sql db {
286284
drop table Module
287-
}
285+
}!
288286
eprintln(modul)
289287
}
290288

examples/js_dom_draw_bechmark_chart/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
4444
4545
sql db {
4646
create table Task
47-
}
47+
}!
4848
4949
task_model := Task{
5050
title: 'a'
@@ -55,14 +55,14 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
5555
sw.start()
5656
sql db {
5757
insert task_model into Task
58-
}
58+
} or { []Task{} }
5959
sw.stop()
6060
insert_stopwatchs << int(sw.end - sw.start)
6161
}
6262
6363
sql db {
6464
drop table Task
65-
}
65+
}!
6666
6767
response := Response{
6868
insert: insert_stopwatchs

examples/js_dom_draw_bechmark_chart/chart/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
3131
3232
sql db {
3333
create table Task
34-
}
34+
} or { panic(err) }
3535
3636
task_model := Task{
3737
title: 'a'
@@ -42,14 +42,14 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
4242
sw.start()
4343
sql db {
4444
insert task_model into Task
45-
}
45+
} or { []Task{} }
4646
sw.stop()
4747
insert_stopwatchs << int(sw.end - sw.start)
4848
}
4949
5050
sql db {
5151
drop table Task
52-
}
52+
} or { panic(err) }
5353
5454
response := Response{
5555
insert: insert_stopwatchs

examples/js_dom_draw_bechmark_chart/v_vweb_orm/src/main.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
5050

5151
sql db {
5252
create table Task
53-
}
53+
} or { panic(err) }
5454

5555
task_model := Task{
5656
title: 'a'
@@ -62,7 +62,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
6262
sw.start()
6363
sql db {
6464
insert task_model into Task
65-
}
65+
} or { panic(err) }
6666
sw.stop()
6767
insert_stopwatchs << int(sw.end - sw.start)
6868
}
@@ -72,7 +72,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
7272
sw.start()
7373
result := sql db {
7474
select from Task
75-
}
75+
} or { []Task{} }
7676
sw.stop()
7777
eprintln(result)
7878
select_stopwatchs << int(sw.end - sw.start)
@@ -83,14 +83,14 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
8383
sw.start()
8484
sql db {
8585
update Task set title = 'b', status = 'finish' where id == i
86-
}
86+
} or { panic(err) }
8787
sw.stop()
8888
update_stopwatchs << int(sw.end - sw.start)
8989
}
9090

9191
sql db {
9292
drop table Task
93-
}
93+
} or { panic(err) }
9494

9595
response := Response{
9696
insert: insert_stopwatchs

examples/vweb_fullstack/src/auth_services.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn (mut app App) service_auth(username string, password string) !string {
3636

3737
users := sql db {
3838
select from User where username == username
39-
}
39+
}!
4040
user := users.first()
4141
if user.username != username {
4242
return error('user not found')

examples/vweb_fullstack/src/product_service.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn (mut app App) service_get_all_products_from(user_id int) ![]Product {
3737

3838
results := sql db {
3939
select from Product where user_id == user_id
40-
}
40+
}!
4141

4242
return results
4343
}

examples/vweb_fullstack/src/user_services.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn (mut app App) service_get_all_user() ![]User {
4242

4343
results := sql db {
4444
select from User
45-
}
45+
}!
4646

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

6060
results := sql db {
6161
select from User where id == id
62-
}
62+
}!
6363

6464
return results.first()
6565
}

examples/vweb_orm_jwt/src/auth_services.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn (mut app App) service_auth(username string, password string) !string {
3333

3434
users := sql db {
3535
select from User where username == username
36-
}
36+
}!
3737

3838
if users.len == 0 {
3939
return error('user not found')

0 commit comments

Comments
 (0)