examples: improve the pg and vweb output (#8640)

pull/8652/head
Mohammad Ali Chraghi 2021-02-09 13:01:25 +03:30 committed by GitHub
parent bfee8a528c
commit 86e6b4f926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -2,6 +2,8 @@ module main
import pg import pg
const dash = '----------------------------------------------------------------'
struct Customer { struct Customer {
id int id int
name string name string
@ -24,27 +26,27 @@ fn main() {
println('Total customers: $nr_customers') println('Total customers: $nr_customers')
// V syntax can be used to build queries // V syntax can be used to build queries
println('----------------------------------------------------------------') println(dash)
bg_country := 'Bulgaria' bg_country := 'Bulgaria'
bg_customers := db.select from Customer where country == bg_country && id != 2 bg_customers := db.select from Customer where country == bg_country && id != 2
for customer in bg_customers { for customer in bg_customers {
println('$customer.country | $customer.id - $customer.name') println('$customer.country | $customer.id - $customer.name')
} }
println('----------------------------------------------------------------') println(dash)
ru_customers := db.select from Customer where country == 'Russia' ru_customers := db.select from Customer where country == 'Russia'
for customer in ru_customers { for customer in ru_customers {
println('$customer.country | $customer.id - $customer.name') println('$customer.country | $customer.id - $customer.name')
} }
// by adding `limit 1` we tell V that there will be only one object // by adding `limit 1` we tell V that there will be only one object
println('----------------------------------------------------------------') println(dash)
existing := db.select from Customer where id == 1 limit 1 or { panic(err) } existing := db.select from Customer where id == 1 limit 1 or { panic(err) }
println('Existing customer name: $existing.name') println('Existing customer name: $existing.name')
println('Existing customer full information:') println('Existing customer full information:')
println(existing) println(existing)
println('------------------------------------------------------------------------') println(dash)
q := Customer{} q := Customer{}
// It's easy to handle queries that don't return any data // It's easy to handle queries that don't return any data
if anon := db.select from Customer where id == 12345 && name == q.name && if anon := db.select from Customer where id == 12345 && name == q.name &&

View File

@ -266,8 +266,8 @@ pub fn run<T>(port int) {
} }
pub fn run_app<T>(mut app T, port int) { pub fn run_app<T>(mut app T, port int) {
println('Running a Vweb app on http://localhost:$port')
mut l := net.listen_tcp(port) or { panic('failed to listen') } mut l := net.listen_tcp(port) or { panic('failed to listen') }
println('[Vweb] Running app on http://localhost:$port')
app.Context = Context{ app.Context = Context{
conn: 0 conn: 0
} }
@ -318,7 +318,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
page_gen_start := time.ticks() page_gen_start := time.ticks()
first_line := reader.read_line() or { first_line := reader.read_line() or {
$if debug { $if debug {
eprintln('Failed first_line') // show this only in debug mode, because it always would be shown after a chromium user visits the site eprintln('Failed to read first_line') // show this only in debug mode, because it always would be shown after a chromium user visits the site
} }
return return
} }