From 67a50b5b34da0b97be334eaca92e0270910bef18 Mon Sep 17 00:00:00 2001 From: Don Alfons Nisnoni Date: Wed, 6 May 2020 06:10:10 +0800 Subject: [PATCH] mysql: simplify `Result.maps` --- vlib/mysql/result.v | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/vlib/mysql/result.v b/vlib/mysql/result.v index 7a90281b76..65d2b3b588 100644 --- a/vlib/mysql/result.v +++ b/vlib/mysql/result.v @@ -57,24 +57,21 @@ pub fn (r Result) rows() []Row { return rows } +// maps return rows with `map` of columns instead `array` of columns pub fn (r Result) maps() []map[string]string { mut array_map := []map[string]string{} rows := r.rows() + fields := r.fetch_fields() for i in 0..rows.len { - array_map << r.create_map_value(rows[i]) + mut map_val := map[string]string + for j in 0..fields.len { + map_val[fields[j].name] = rows[i].vals[j] + } + array_map << map_val } return array_map } -fn (r Result) create_map_value(row Row) map[string]string { - mut map_ := map[string]string - fields := r.fetch_fields() - for i in 0..fields.len { - map_[fields[i].name] = row.vals[i] - } - return map_ -} - pub fn (r Result) fetch_fields() []Field { mut fields := []Field{} nr_cols := r.num_fields()