blob: e9e1909d840476684adda7ef718d5dbc3e79aae1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
module mysql
pub struct Result {
result &C.MYSQL_RES
}
pub struct Row {
pub mut:
vals []string
}
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.
pub fn (r Result) fetch_row() &&byte {
return C.mysql_fetch_row(r.result)
}
// n_rows - returns the number of rows from a result.
pub fn (r Result) n_rows() u64 {
return C.mysql_num_rows(r.result)
}
// n_fields - returns the number of columns from a result.
pub fn (r Result) n_fields() int {
return C.mysql_num_fields(r.result)
}
// rows - returns array of rows, each containing an array of values,
// one for each column.
pub fn (r Result) rows() []Row {
mut rows := []Row{}
nr_cols := r.n_fields()
for rr := r.fetch_row(); rr; rr = r.fetch_row() {
mut row := Row{}
for i in 0 .. nr_cols {
if unsafe { rr[i] == 0 } {
row.vals << ''
} else {
row.vals << mystring(unsafe { &byte(rr[i]) })
}
}
rows << row
}
return rows
}
// maps - returns an array of maps, each containing a set of
// field name: field value pairs.
pub fn (r Result) maps() []map[string]string {
mut array_map := []map[string]string{}
rows := r.rows()
fields := r.fields()
for i in 0 .. rows.len {
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
}
// fields - returns an array of fields/columns.
// The definitions apply primarily for columns of results,
// such as those produced by `SELECT` statements.
pub fn (r Result) fields() []Field {
mut fields := []Field{}
nr_cols := r.n_fields()
orig_fields := C.mysql_fetch_fields(r.result)
for i in 0 .. nr_cols {
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)
}
}
}
return fields
}
// str - serializes the field
pub fn (f Field) str() string {
return '
{
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
type: $f.type_.str()
}
'
}
// free - frees the memory used by a result
[unsafe]
pub fn (r &Result) free() {
C.mysql_free_result(r.result)
}
|