Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions vlib/db/sqlite/sqlite.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,27 @@ pub fn (db &DB) str() string {

pub struct Row {
pub mut:
vals []string
vals []string
names []string
}

// get_string returns the value for the given column name, or '' if the column is not found
// or if the corresponding value index is out of range.
pub fn (r &Row) get_string(col_name string) string {
for i, name in r.names {
if name == col_name {
if i < r.vals.len {
return r.vals[i]
}
return ''
}
}
return ''
}

// get_int returns the integer value for the given column name, or 0 if the column is not found.
pub fn (r &Row) get_int(col_name string) int {
return r.get_string(col_name).int()
}

pub type Params = []string | [][]string
Expand Down Expand Up @@ -246,6 +266,11 @@ pub fn (db &DB) exec_map(query string) ![]map[string]string {
C.sqlite3_finalize(stmt)
}
nr_cols := C.sqlite3_column_count(stmt)
mut col_names := []string{cap: nr_cols}
for i in 0 .. nr_cols {
col_char := unsafe { &u8(C.sqlite3_column_name(stmt, i)) }
col_names << if col_char != unsafe { nil } { unsafe { tos_clone(col_char) } } else { '' }
}
mut res := 0
mut rows := []map[string]string{}
for {
Expand All @@ -256,13 +281,7 @@ pub fn (db &DB) exec_map(query string) ![]map[string]string {
mut row := map[string]string{}
for i in 0 .. nr_cols {
val := unsafe { &u8(C.sqlite3_column_text(stmt, i)) }
col_char := unsafe { &u8(C.sqlite3_column_name(stmt, i)) }
col := unsafe { tos_clone(col_char) }
if val == &u8(unsafe { nil }) {
row[col] = ''
} else {
row[col] = unsafe { tos_clone(val) }
}
row[col_names[i]] = if val != unsafe { nil } { unsafe { tos_clone(val) } } else { '' }
}
rows << row
}
Expand All @@ -285,14 +304,21 @@ pub fn (db &DB) exec(query string) ![]Row {
C.sqlite3_finalize(stmt)
}
nr_cols := C.sqlite3_column_count(stmt)
mut col_names := []string{cap: nr_cols}
for i in 0 .. nr_cols {
col_char := unsafe { &u8(C.sqlite3_column_name(stmt, i)) }
col_names << if col_char != unsafe { nil } { unsafe { tos_clone(col_char) } } else { '' }
}
mut res := 0
mut rows := []Row{}
for {
res = C.sqlite3_step(stmt)
if res != sqlite_row {
break
}
mut row := Row{}
mut row := Row{
names: col_names
}
for i in 0 .. nr_cols {
val := unsafe { &u8(C.sqlite3_column_text(stmt, i)) }
if val == &u8(unsafe { nil }) {
Expand Down Expand Up @@ -372,6 +398,11 @@ pub fn (db &DB) exec_param_many(query string, params Params) ![]Row {

mut rows := []Row{}
nr_cols := C.sqlite3_column_count(stmt)
mut col_names := []string{cap: nr_cols}
for i in 0 .. nr_cols {
col_char := unsafe { &u8(C.sqlite3_column_name(stmt, i)) }
col_names << if col_char != unsafe { nil } { unsafe { tos_clone(col_char) } } else { '' }
}

if params is []string {
for i, param in params {
Expand All @@ -381,7 +412,9 @@ pub fn (db &DB) exec_param_many(query string, params Params) ![]Row {
}
}
for {
mut row := Row{}
mut row := Row{
names: col_names
}
code = C.sqlite3_step(stmt)
if is_error(code) {
return db.error_message(code, query)
Expand All @@ -402,7 +435,9 @@ pub fn (db &DB) exec_param_many(query string, params Params) ![]Row {
} else if params is [][]string {
// Rows to process
for params_row in params {
mut row := Row{}
mut row := Row{
names: col_names
}
// Param values to bind
for i, param in params_row {
code = C.sqlite3_bind_text(stmt, i + 1, voidptr(param.str), param.len,
Expand Down
Loading