ORM fixes
parent
79b26b1654
commit
71c0c4803f
|
@ -1,5 +1,3 @@
|
||||||
/*
|
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import pg
|
import pg
|
||||||
|
@ -12,52 +10,52 @@ struct Customer {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
db := pg.connect(pg.Config{host: '127.0.0.1' user: 'myuser' dbname: 'mydb'})
|
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
|
nr_customers := db.select count from Customer
|
||||||
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('----------------------------------------------------------------')
|
||||||
bg_customers := db.select from Customer where country == 'Bulgaria' && id != 2
|
bg_customers := db.select from Customer where country == 'Bulgaria' && 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('----------------------------------------------------------------')
|
||||||
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('----------------------------------------------------------------')
|
||||||
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('------------------------------------------------------------------------')
|
||||||
q := Customer{}
|
q := Customer{}
|
||||||
for {
|
// It's easy to handle queries that don't return any data
|
||||||
anon := db.select from Customer where id == 12345 && name == q.name && nr_orders > q.nr_orders limit 1 or { eprintln('No such customer. Error: $err') break }
|
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')
|
println('Non existing customer name: $anon.name')
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
// Insert a new customer
|
||||||
// TODO: insert a new customer
|
|
||||||
/*
|
|
||||||
nc := Customer{
|
nc := Customer{
|
||||||
name: 'John Doe'
|
name: 'John Doe'
|
||||||
nr_orders: 10
|
nr_orders: 10
|
||||||
}
|
}
|
||||||
db.insert(nc)
|
db.insert(nc)
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ fn (p mut Parser) select_query(fn_ph int) string {
|
||||||
p.sql_params = []
|
p.sql_params = []
|
||||||
if false {}
|
if false {}
|
||||||
p.sql_types = []
|
p.sql_types = []
|
||||||
|
|
||||||
mut q := 'select '
|
mut q := 'select '
|
||||||
p.check(.key_select)
|
p.check(.key_select)
|
||||||
n := p.check_name()
|
n := p.check_name()
|
||||||
|
@ -91,8 +90,8 @@ fn (p mut Parser) select_query(fn_ph int) string {
|
||||||
// `where` statement
|
// `where` statement
|
||||||
if p.tok == .name && p.lit == 'where' {
|
if p.tok == .name && p.lit == 'where' {
|
||||||
p.next()
|
p.next()
|
||||||
_, expr := p.tmp_expr()
|
|
||||||
p.is_sql = true
|
p.is_sql = true
|
||||||
|
_, expr := p.tmp_expr()
|
||||||
p.is_sql = false
|
p.is_sql = false
|
||||||
q += ' where ' + expr
|
q += ' where ' + expr
|
||||||
}
|
}
|
||||||
|
@ -100,8 +99,8 @@ fn (p mut Parser) select_query(fn_ph int) string {
|
||||||
mut query_one := false
|
mut query_one := false
|
||||||
if p.tok == .name && p.lit == 'limit' {
|
if p.tok == .name && p.lit == 'limit' {
|
||||||
p.next()
|
p.next()
|
||||||
_, limit := p.tmp_expr()
|
|
||||||
p.is_sql = true
|
p.is_sql = true
|
||||||
|
_, limit := p.tmp_expr()
|
||||||
p.is_sql = false
|
p.is_sql = false
|
||||||
q += ' limit ' + limit
|
q += ' limit ' + limit
|
||||||
// `limit 1` means we are getting `?User`, not `[]User`
|
// `limit 1` means we are getting `?User`, not `[]User`
|
||||||
|
|
20
vlib/pg/pg.v
20
vlib/pg/pg.v
|
@ -19,10 +19,10 @@ struct C.PGResult { }
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub:
|
pub:
|
||||||
host string
|
host string
|
||||||
user string
|
user string
|
||||||
password string
|
password string
|
||||||
dbname string
|
dbname string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn C.PQconnectdb(a byteptr) &C.PGconn
|
fn C.PQconnectdb(a byteptr) &C.PGconn
|
||||||
|
@ -31,17 +31,17 @@ fn C.PQgetvalue(voidptr, int, int) byteptr
|
||||||
fn C.PQstatus(voidptr) int
|
fn C.PQstatus(voidptr) int
|
||||||
fn C.PQntuples(voidptr) int
|
fn C.PQntuples(voidptr) int
|
||||||
fn C.PQnfields(voidptr) int
|
fn C.PQnfields(voidptr) int
|
||||||
fn C.PQexec(voidptr) int
|
fn C.PQexec(voidptr) voidptr
|
||||||
fn C.PQexecParams(voidptr) int
|
fn C.PQexecParams(voidptr) voidptr
|
||||||
|
|
||||||
pub fn connect(config pg.Config) DB {
|
pub fn connect(config pg.Config) ?DB {
|
||||||
conninfo := 'host=$config.host user=$config.user dbname=$config.dbname'
|
conninfo := 'host=$config.host user=$config.user dbname=$config.dbname'
|
||||||
conn:=C.PQconnectdb(conninfo.str)
|
conn := C.PQconnectdb(conninfo.str)
|
||||||
status := C.PQstatus(conn)
|
status := C.PQstatus(conn)
|
||||||
|
println("status=$status")
|
||||||
if status != C.CONNECTION_OK {
|
if status != C.CONNECTION_OK {
|
||||||
error_msg := C.PQerrorMessage(conn)
|
error_msg := C.PQerrorMessage(conn)
|
||||||
eprintln('Connection to a PG database failed: ' + string(error_msg))
|
return error ('Connection to a PG database failed: ' + string(error_msg))
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
return DB {conn: conn}
|
return DB {conn: conn}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue