-
Notifications
You must be signed in to change notification settings - Fork 37
/
result.go
128 lines (113 loc) · 2.15 KB
/
result.go
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
// GoMySQL - A MySQL client library for Go
//
// Copyright 2010-2011 Phil Bayfield. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mysql
import "os"
// Result struct
type Result struct {
// Pointer to the client
c *Client
// Fields
fieldCount uint64
fieldPos uint64
fields []*Field
// Rows
rowPos uint64
rows []Row
// Storage
mode byte
allRead bool
}
// Field type
type Field struct {
Database string
Table string
Name string
Length uint32
Type FieldType
Flags FieldFlag
Decimals uint8
}
// Row types
type Row []interface{}
type Map map[string]interface{}
// Get field count
func (r *Result) FieldCount() uint64 {
return r.fieldCount
}
// Fetch the next field
func (r *Result) FetchField() *Field {
// Check if all fields have been fetched
if r.fieldPos < uint64(len(r.fields)) {
// Increment and return current field
r.fieldPos++
return r.fields[r.fieldPos-1]
}
return nil
}
// Fetch all fields
func (r *Result) FetchFields() []*Field {
return r.fields
}
// Get row count
func (r *Result) RowCount() uint64 {
// Stored mode
if r.mode == RESULT_STORED {
return uint64(len(r.rows))
}
return 0
}
// Fetch a row
func (r *Result) FetchRow() Row {
// Stored result
if r.mode == RESULT_STORED {
// Check if all rows have been fetched
if r.rowPos < uint64(len(r.rows)) {
// Increment position and return current row
r.rowPos++
return r.rows[r.rowPos-1]
}
}
// Used result
if r.mode == RESULT_USED {
if r.allRead == false {
eof, err := r.c.getRow()
if err != nil {
return nil
}
if eof {
r.allRead = true
} else {
return r.rows[0]
}
}
}
return nil
}
// Fetch a map
func (r *Result) FetchMap() Map {
// Fetch row
row := r.FetchRow()
if row != nil {
rowMap := make(Map)
for key, val := range row {
rowMap[r.fields[key].Name] = val
}
return rowMap
}
return nil
}
// Fetch all rows
func (r *Result) FetchRows() []Row {
if r.mode == RESULT_STORED {
return r.rows
}
return nil
}
// Free the result
func (r *Result) Free() (err os.Error) {
err = r.c.FreeResult()
return
}