diff options
author | Indrajith K L | 2022-12-03 17:00:20 +0530 |
---|---|---|
committer | Indrajith K L | 2022-12-03 17:00:20 +0530 |
commit | f5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch) | |
tree | 2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/examples/database/psql/customer.v | |
download | cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2 cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip |
Diffstat (limited to 'v_windows/v/old/examples/database/psql/customer.v')
-rw-r--r-- | v_windows/v/old/examples/database/psql/customer.v | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/v_windows/v/old/examples/database/psql/customer.v b/v_windows/v/old/examples/database/psql/customer.v new file mode 100644 index 0000000..4538a94 --- /dev/null +++ b/v_windows/v/old/examples/database/psql/customer.v @@ -0,0 +1,63 @@ +module main + +import pg + +const dash = '----------------------------------------------------------------' + +struct Customer { + id int + name string + nr_orders int + country string +} + +fn main() { + /* + db := pg.connect(pg.Config{ + host: 'localhost' //'127.0.0.1' + user: 'postgres' + dbname: 'customerdb' + }) or { + println('failed to connect') + println(err) + return + } + + nr_customers := db.select count from Customer + println('Total customers: $nr_customers') + + // V syntax can be used to build queries + println(dash) + bg_country := 'Bulgaria' + bg_customers := db.select from Customer where country == bg_country && id != 2 + for customer in bg_customers { + println('$customer.country | $customer.id - $customer.name') + } + + println(dash) + ru_customers := db.select from Customer where country == 'Russia' + for customer in ru_customers { + println('$customer.country | $customer.id - $customer.name') + } + + // by adding `limit 1` we tell V that there will be only one object + println(dash) + existing := db.select from Customer where id == 1 limit 1 or { panic(err) } + println('Existing customer name: $existing.name') + println('Existing customer full information:') + println(existing) + + println(dash) + q := Customer{} + // It's easy to handle queries that don't return any data + if anon := db.select from Customer where id == 12345 && name == q.name && + nr_orders > q.nr_orders limit 1 { + println('Non existing customer name: $anon.name') + } + // Insert a new customer + nc := Customer{ + name: 'John Doe' + nr_orders: 10 + } + db.insert(nc)*/ +} |