2020-02-02 14:39:57 +03:00
|
|
|
module mysql
|
|
|
|
|
|
|
|
pub struct Result {
|
2022-09-15 07:59:31 +03:00
|
|
|
result &C.MYSQL_RES = unsafe { nil }
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Row {
|
|
|
|
pub mut:
|
|
|
|
vals []string
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
pub struct Field {
|
|
|
|
name string
|
|
|
|
org_name string
|
|
|
|
table string
|
|
|
|
org_table string
|
|
|
|
db string
|
|
|
|
catalog string
|
|
|
|
def string
|
|
|
|
length int
|
|
|
|
max_length int
|
|
|
|
name_length u32
|
|
|
|
org_name_length u32
|
|
|
|
table_length u32
|
|
|
|
org_table_length u32
|
|
|
|
db_length u32
|
|
|
|
catalog_length u32
|
|
|
|
def_length u32
|
|
|
|
flags u32
|
|
|
|
decimals u32
|
|
|
|
charsetnr u32
|
|
|
|
type_ FieldType
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch_row - fetches the next row from a result.
|
2022-04-15 18:25:45 +03:00
|
|
|
pub fn (r Result) fetch_row() &&u8 {
|
2020-05-04 15:58:24 +03:00
|
|
|
return C.mysql_fetch_row(r.result)
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// n_rows - returns the number of rows from a result.
|
2020-06-11 11:07:17 +03:00
|
|
|
pub fn (r Result) n_rows() u64 {
|
2020-06-01 12:26:39 +03:00
|
|
|
return C.mysql_num_rows(r.result)
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// n_fields - returns the number of columns from a result.
|
2020-06-11 11:07:17 +03:00
|
|
|
pub fn (r Result) n_fields() int {
|
2020-05-04 15:58:24 +03:00
|
|
|
return C.mysql_num_fields(r.result)
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// rows - returns array of rows, each containing an array of values,
|
|
|
|
// one for each column.
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (r Result) rows() []Row {
|
2020-04-26 17:25:54 +03:00
|
|
|
mut rows := []Row{}
|
2020-06-11 11:07:17 +03:00
|
|
|
nr_cols := r.n_fields()
|
2020-02-02 14:39:57 +03:00
|
|
|
for rr := r.fetch_row(); rr; rr = r.fetch_row() {
|
|
|
|
mut row := Row{}
|
2020-10-19 21:11:04 +03:00
|
|
|
for i in 0 .. nr_cols {
|
2021-01-09 11:40:21 +03:00
|
|
|
if unsafe { rr[i] == 0 } {
|
2020-02-02 14:39:57 +03:00
|
|
|
row.vals << ''
|
|
|
|
} else {
|
2022-04-15 14:58:56 +03:00
|
|
|
row.vals << mystring(unsafe { &u8(rr[i]) })
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rows << row
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
// maps - returns an array of maps, each containing a set of
|
2021-01-09 11:40:21 +03:00
|
|
|
// field name: field value pairs.
|
2020-05-04 15:58:24 +03:00
|
|
|
pub fn (r Result) maps() []map[string]string {
|
|
|
|
mut array_map := []map[string]string{}
|
|
|
|
rows := r.rows()
|
2020-06-11 11:07:17 +03:00
|
|
|
fields := r.fields()
|
2020-10-19 21:11:04 +03:00
|
|
|
for i in 0 .. rows.len {
|
|
|
|
mut map_val := map[string]string{}
|
|
|
|
for j in 0 .. fields.len {
|
2020-05-06 01:10:10 +03:00
|
|
|
map_val[fields[j].name] = rows[i].vals[j]
|
|
|
|
}
|
|
|
|
array_map << map_val
|
2020-05-04 15:58:24 +03:00
|
|
|
}
|
|
|
|
return array_map
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// fields - returns an array of fields/columns.
|
|
|
|
// The definitions apply primarily for columns of results,
|
|
|
|
// such as those produced by `SELECT` statements.
|
2020-06-11 11:07:17 +03:00
|
|
|
pub fn (r Result) fields() []Field {
|
2020-04-26 17:25:54 +03:00
|
|
|
mut fields := []Field{}
|
2020-06-11 11:07:17 +03:00
|
|
|
nr_cols := r.n_fields()
|
2020-05-04 15:58:24 +03:00
|
|
|
orig_fields := C.mysql_fetch_fields(r.result)
|
2020-10-19 21:11:04 +03:00
|
|
|
for i in 0 .. nr_cols {
|
2021-01-09 11:40:21 +03:00
|
|
|
unsafe {
|
|
|
|
fields << Field{
|
|
|
|
name: mystring(orig_fields[i].name)
|
|
|
|
org_name: mystring(orig_fields[i].org_name)
|
|
|
|
table: mystring(orig_fields[i].table)
|
|
|
|
org_table: mystring(orig_fields[i].org_table)
|
|
|
|
db: mystring(orig_fields[i].db)
|
|
|
|
catalog: mystring(orig_fields[i].catalog)
|
|
|
|
def: resolve_nil_str(orig_fields[i].def)
|
|
|
|
length: orig_fields.length
|
|
|
|
max_length: orig_fields.max_length
|
|
|
|
name_length: orig_fields.name_length
|
|
|
|
org_name_length: orig_fields.org_name_length
|
|
|
|
table_length: orig_fields.table_length
|
|
|
|
org_table_length: orig_fields.org_table_length
|
|
|
|
db_length: orig_fields.db_length
|
|
|
|
catalog_length: orig_fields.catalog_length
|
|
|
|
def_length: orig_fields.def_length
|
|
|
|
flags: orig_fields.flags
|
|
|
|
decimals: orig_fields.decimals
|
|
|
|
charsetnr: orig_fields.charsetnr
|
|
|
|
type_: FieldType(orig_fields.@type)
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// str - serializes the field
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (f Field) str() string {
|
|
|
|
return '
|
2020-04-26 17:25:54 +03:00
|
|
|
{
|
2020-02-02 14:39:57 +03:00
|
|
|
name: "$f.name"
|
|
|
|
org_name: "$f.org_name"
|
|
|
|
table: "$f.table"
|
|
|
|
org_table: "$f.org_table"
|
|
|
|
db: "$f.db"
|
|
|
|
catalog: "$f.catalog"
|
|
|
|
def: "$f.def"
|
|
|
|
length: $f.length
|
|
|
|
max_length: $f.max_length
|
|
|
|
name_length: $f.name_length
|
|
|
|
org_name_length: $f.org_name_length
|
|
|
|
table_length: $f.table_length
|
|
|
|
org_table_length: $f.org_table_length
|
|
|
|
db_length: $f.db_length
|
|
|
|
catalog_length: $f.catalog_length
|
|
|
|
def_length: $f.def_length
|
|
|
|
flags: $f.flags
|
|
|
|
decimals: $f.decimals
|
|
|
|
charsetnr: $f.charsetnr
|
2020-10-19 21:11:04 +03:00
|
|
|
type: $f.type_.str()
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// free - frees the memory used by a result
|
|
|
|
[unsafe]
|
|
|
|
pub fn (r &Result) free() {
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_free_result(r.result)
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|