forked from Philio/GoMySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.go
More file actions
143 lines (126 loc) · 2.41 KB
/
result.go
File metadata and controls
143 lines (126 loc) · 2.41 KB
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
// 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
// if non-nil, the Result came from a Statement, not
// via Client.Query.
s *Statement
// 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
}
func (r *Result) getRow() (eof bool, err os.Error) {
if r.s != nil {
return r.s.getRow()
}
return r.c.getRow()
}
// 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.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) {
if r.s != nil {
err = r.s.FreeResult()
} else {
err = r.c.FreeResult()
}
return
}