mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
db, json, time, term: change optional to result (#16201)
This commit is contained in:
@@ -6,10 +6,10 @@ import net.conv
|
||||
|
||||
// sql expr
|
||||
|
||||
pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.QueryData) ?[][]orm.Primitive {
|
||||
pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.QueryData) ![][]orm.Primitive {
|
||||
query := orm.orm_select_gen(config, '"', true, '$', 1, where)
|
||||
|
||||
res := pg_stmt_worker(db, query, where, data)?
|
||||
res := pg_stmt_worker(db, query, where, data)!
|
||||
|
||||
mut ret := [][]orm.Primitive{}
|
||||
|
||||
@@ -19,7 +19,7 @@ pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.Qu
|
||||
for row in res {
|
||||
mut row_data := []orm.Primitive{}
|
||||
for i, val in row.vals {
|
||||
field := str_to_primitive(val, config.types[i])?
|
||||
field := str_to_primitive(val, config.types[i])!
|
||||
row_data << field
|
||||
}
|
||||
ret << row_data
|
||||
@@ -30,20 +30,20 @@ pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.Qu
|
||||
|
||||
// sql stmt
|
||||
|
||||
pub fn (db DB) insert(table string, data orm.QueryData) ? {
|
||||
pub fn (db DB) insert(table string, data orm.QueryData) ! {
|
||||
query, converted_data := orm.orm_stmt_gen(table, '"', .insert, true, '$', 1, data,
|
||||
orm.QueryData{})
|
||||
pg_stmt_worker(db, query, converted_data, orm.QueryData{})?
|
||||
pg_stmt_worker(db, query, converted_data, orm.QueryData{})!
|
||||
}
|
||||
|
||||
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ? {
|
||||
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! {
|
||||
query, _ := orm.orm_stmt_gen(table, '"', .update, true, '$', 1, data, where)
|
||||
pg_stmt_worker(db, query, data, where)?
|
||||
pg_stmt_worker(db, query, data, where)!
|
||||
}
|
||||
|
||||
pub fn (db DB) delete(table string, where orm.QueryData) ? {
|
||||
pub fn (db DB) delete(table string, where orm.QueryData) ! {
|
||||
query, _ := orm.orm_stmt_gen(table, '"', .delete, true, '$', 1, orm.QueryData{}, where)
|
||||
pg_stmt_worker(db, query, orm.QueryData{}, where)?
|
||||
pg_stmt_worker(db, query, orm.QueryData{}, where)!
|
||||
}
|
||||
|
||||
pub fn (db DB) last_id() orm.Primitive {
|
||||
@@ -54,19 +54,19 @@ pub fn (db DB) last_id() orm.Primitive {
|
||||
|
||||
// table
|
||||
|
||||
pub fn (db DB) create(table string, fields []orm.TableField) ? {
|
||||
pub fn (db DB) create(table string, fields []orm.TableField) ! {
|
||||
query := orm.orm_table_gen(table, '"', true, 0, fields, pg_type_from_v, false) or { return err }
|
||||
pg_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})?
|
||||
pg_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})!
|
||||
}
|
||||
|
||||
pub fn (db DB) drop(table string) ? {
|
||||
pub fn (db DB) drop(table string) ! {
|
||||
query := 'DROP TABLE "$table";'
|
||||
pg_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})?
|
||||
pg_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})!
|
||||
}
|
||||
|
||||
// utils
|
||||
|
||||
fn pg_stmt_worker(db DB, query string, data orm.QueryData, where orm.QueryData) ?[]Row {
|
||||
fn pg_stmt_worker(db DB, query string, data orm.QueryData, where orm.QueryData) ![]Row {
|
||||
mut param_types := []u32{}
|
||||
mut param_vals := []&char{}
|
||||
mut param_lens := []int{}
|
||||
@@ -185,7 +185,7 @@ fn pg_stmt_match(mut types []u32, mut vals []&char, mut lens []int, mut formats
|
||||
}
|
||||
}
|
||||
|
||||
fn pg_type_from_v(typ int) ?string {
|
||||
fn pg_type_from_v(typ int) !string {
|
||||
str := match typ {
|
||||
orm.type_idx['i8'], orm.type_idx['i16'], orm.type_idx['u8'], orm.type_idx['u16'] {
|
||||
'SMALLINT'
|
||||
@@ -224,7 +224,7 @@ fn pg_type_from_v(typ int) ?string {
|
||||
return str
|
||||
}
|
||||
|
||||
fn str_to_primitive(str string, typ int) ?orm.Primitive {
|
||||
fn str_to_primitive(str string, typ int) !orm.Primitive {
|
||||
match typ {
|
||||
// bool
|
||||
orm.type_idx['bool'] {
|
||||
@@ -279,7 +279,7 @@ fn str_to_primitive(str string, typ int) ?orm.Primitive {
|
||||
}
|
||||
orm.time {
|
||||
if str.contains_any(' /:-') {
|
||||
date_time_str := time.parse(str)?
|
||||
date_time_str := time.parse(str)!
|
||||
return orm.Primitive(date_time_str)
|
||||
}
|
||||
|
||||
|
||||
30
vlib/pg/pg.v
30
vlib/pg/pg.v
@@ -76,7 +76,7 @@ fn C.PQfinish(voidptr)
|
||||
// connect makes a new connection to the database server using
|
||||
// the parameters from the `Config` structure, returning
|
||||
// a connection error when something goes wrong
|
||||
pub fn connect(config Config) ?DB {
|
||||
pub fn connect(config Config) !DB {
|
||||
conninfo := 'host=$config.host port=$config.port user=$config.user dbname=$config.dbname password=$config.password'
|
||||
conn := C.PQconnectdb(conninfo.str)
|
||||
if conn == 0 {
|
||||
@@ -125,8 +125,8 @@ pub fn (db DB) close() {
|
||||
// returns an the first field in the first tuple
|
||||
// converted to an int. If no row is found or on
|
||||
// command failure, an error is returned
|
||||
pub fn (db DB) q_int(query string) ?int {
|
||||
rows := db.exec(query)?
|
||||
pub fn (db DB) q_int(query string) !int {
|
||||
rows := db.exec(query)!
|
||||
if rows.len == 0 {
|
||||
return error('q_int "$query" not found')
|
||||
}
|
||||
@@ -142,8 +142,8 @@ pub fn (db DB) q_int(query string) ?int {
|
||||
// returns an the first field in the first tuple
|
||||
// as a string. If no row is found or on
|
||||
// command failure, an error is returned
|
||||
pub fn (db DB) q_string(query string) ?string {
|
||||
rows := db.exec(query)?
|
||||
pub fn (db DB) q_string(query string) !string {
|
||||
rows := db.exec(query)!
|
||||
if rows.len == 0 {
|
||||
return error('q_string "$query" not found')
|
||||
}
|
||||
@@ -157,37 +157,37 @@ pub fn (db DB) q_string(query string) ?string {
|
||||
|
||||
// q_strings submit a command to the database server and
|
||||
// returns the resulting row set. Alias of `exec`
|
||||
pub fn (db DB) q_strings(query string) ?[]Row {
|
||||
pub fn (db DB) q_strings(query string) ![]Row {
|
||||
return db.exec(query)
|
||||
}
|
||||
|
||||
// exec submit a command to the database server and wait
|
||||
// for the result, returning an error on failure and a
|
||||
// row set on success
|
||||
pub fn (db DB) exec(query string) ?[]Row {
|
||||
pub fn (db DB) exec(query string) ![]Row {
|
||||
res := C.PQexec(db.conn, query.str)
|
||||
return db.handle_error_or_result(res, 'exec')
|
||||
}
|
||||
|
||||
fn rows_first_or_empty(rows []Row) ?Row {
|
||||
fn rows_first_or_empty(rows []Row) !Row {
|
||||
if rows.len == 0 {
|
||||
return error('no row')
|
||||
}
|
||||
return rows[0]
|
||||
}
|
||||
|
||||
pub fn (db DB) exec_one(query string) ?Row {
|
||||
pub fn (db DB) exec_one(query string) !Row {
|
||||
res := C.PQexec(db.conn, query.str)
|
||||
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
||||
if e != '' {
|
||||
return error('pg exec error: "$e"')
|
||||
}
|
||||
row := rows_first_or_empty(res_to_rows(res))?
|
||||
row := rows_first_or_empty(res_to_rows(res))!
|
||||
return row
|
||||
}
|
||||
|
||||
// exec_param_many executes a query with the provided parameters
|
||||
pub fn (db DB) exec_param_many(query string, params []string) ?[]Row {
|
||||
pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
|
||||
unsafe {
|
||||
mut param_vals := []&char{len: params.len}
|
||||
for i in 0 .. params.len {
|
||||
@@ -200,15 +200,15 @@ pub fn (db DB) exec_param_many(query string, params []string) ?[]Row {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (db DB) exec_param2(query string, param string, param2 string) ?[]Row {
|
||||
pub fn (db DB) exec_param2(query string, param string, param2 string) ![]Row {
|
||||
return db.exec_param_many(query, [param, param2])
|
||||
}
|
||||
|
||||
pub fn (db DB) exec_param(query string, param string) ?[]Row {
|
||||
pub fn (db DB) exec_param(query string, param string) ![]Row {
|
||||
return db.exec_param_many(query, [param])
|
||||
}
|
||||
|
||||
fn (db DB) handle_error_or_result(res voidptr, elabel string) ?[]Row {
|
||||
fn (db DB) handle_error_or_result(res voidptr, elabel string) ![]Row {
|
||||
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
||||
if e != '' {
|
||||
C.PQclear(res)
|
||||
@@ -219,7 +219,7 @@ fn (db DB) handle_error_or_result(res voidptr, elabel string) ?[]Row {
|
||||
|
||||
// copy_expert execute COPY commands
|
||||
// https://www.postgresql.org/docs/9.5/libpq-copy.html
|
||||
pub fn (db DB) copy_expert(query string, mut file io.ReaderWriter) ?int {
|
||||
pub fn (db DB) copy_expert(query string, mut file io.ReaderWriter) !int {
|
||||
res := C.PQexec(db.conn, query.str)
|
||||
status := C.PQresultStatus(res)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user