1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-08-09 18:10:59 +02:00
parent c67783bcd1
commit 8f8e0dfad7
8 changed files with 256 additions and 44 deletions

View File

@@ -533,17 +533,16 @@ pub fn (ar []int) contains(val int) bool {
return false
}
/*
/*
pub fn (a []string) to_c() voidptr {
char ** res = malloc(sizeof(char*) * a.len);
mut res := malloc(sizeof(byteptr) * a.len)
for i := 0; i < a.len; i++ {
val := a[i]
# res[i] = val.str;
res[i] = val.str
}
return res;
return 0
return res
}
*/
*/
fn is_space(c byte) bool {
return C.isspace(c)
@@ -557,7 +556,6 @@ pub fn (s string) trim_space() string {
if s == '' {
return ''
}
// println('TRIM SPACE "$s"')
mut i := 0
for i < s.len && is_space(s[i]) {
i++

View File

@@ -1,16 +1,46 @@
//import pg
import pg
struct Mod {
struct Modules {
id int
user_id int
name string
url string
nr_downloads int
//nr_downloads int
}
fn test_orm() {
/*
db := pg.connect('vpm', 'alex')
nr_modules := select count from db.modules
//nr_modules := db.select count from modules
//nr_modules := db.select count from Modules where id == 1
nr_modules := db.select count from Modules where
name == 'Bob' && id == 1
println(nr_modules)
mod := db.select from Modules where id = 1 limit 1
println(mod)
mods := db.select from Modules limit 10
for mod in mods {
println(mod)
}
*/
/*
mod := db.retrieve<Module>(1)
mod := db.update Module set name = name + '!' where id > 10
nr_modules := db.select count from Modules
where id > 1 && name == ''
println(nr_modules)
nr_modules := db.select count from modules
nr_modules := db.select from modules
nr_modules := db[:modules].select
*/
/*
mod := select from db.modules where id = 1 limit 1
println(mod.name)
top_mods := select from db.modules where nr_downloads > 1000 order by nr_downloads desc limit 10

View File

@@ -30,7 +30,8 @@ fn C.PQgetvalue(voidptr, int, int) byteptr
fn C.PQstatus(voidptr) int
pub fn connect(dbname, user string) DB {
conninfo := 'host=localhost user=$user dbname=$dbname'
//conninfo := 'host=localhost user=$user dbname=$dbname'
conninfo := 'host=127.0.0.1 user=$user dbname=$dbname'
conn:=C.PQconnectdb(conninfo.str)
status := C.PQstatus(conn)
if status != CONNECTION_OK {
@@ -99,6 +100,21 @@ pub fn (db DB) exec(query string) []pg.Row {
return res_to_rows(res)
}
pub fn (db DB) exec_one(query string) pg.Row {
res := C.PQexec(db.conn, query.str)
e := string(C.PQerrorMessage(db.conn))
if e != '' {
println('pg exec error:')
println(e)
return Row{}
}
rows := res_to_rows(res)
if rows.len == 0 {
return Row{}
}
return rows[0]
}
//
pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
@@ -122,3 +138,4 @@ pub fn (db DB) exec_param(query string, param string) []pg.Row {
return res_to_rows(res)
}