Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ btree-test: oldv
sql-test: oldv
$(V) -stats $(BUILD_OPTIONS) vsql/sql_test.v

orm-test: oldv
$(V) -stats $(BUILD_OPTIONS) vsql/orm_test.v

# CLI Tests

cli-test: bin/vsql
Expand Down
61 changes: 61 additions & 0 deletions examples/orm.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import vsql
import time

// CHECK ./vsql/orm_test.v for more advanced usage

fn main() {
os.rm('test.vsql') or {}

example() or { panic(err) }
}

// NOTE for some reason if we declare a @[primary] on a struct field, we can not do delete queries on the tables...
// https://github.com/elliotchance/vsql/issues/200
struct Product {
id int //@[primary]
product_name string @[sql_type: 'varchar(100)']
price f64
}

fn example() ! {
timer := time.new_stopwatch()
mut db := vsql.open('test.vsql')!

sql db {
create table Product
}!

products := [
Product{1, 'Ice Cream', 5.99},
Product{2, 'Ham Sandwhich', 3.47},
Product{3, 'Bagel', 1.25},
]

for product in products {
sql db {
insert product into Product
} or { panic(err) }
}
sql db {
update Product set product_name = 'Cereal' where id == 1
} or { panic(err) }

prod_one := sql db {
select from Product where id == 1
}!

assert prod_one.len == 1

sql db {
delete from Product where product_name == 'Cereal'
} or { panic(err) }

all := sql db {
select from Product
}!

assert all.len == 2

println(timer.elapsed())
}
Loading
Loading