-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquery_result.go
More file actions
124 lines (109 loc) · 4.5 KB
/
Copy pathquery_result.go
File metadata and controls
124 lines (109 loc) · 4.5 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
package lbug
// #include "lbug.h"
// #include <stdlib.h>
import "C"
import (
"fmt"
"unsafe"
)
// QueryResult represents the result of a query, which can be used to iterate
// over the result set.
// QueryResult is returned by the `Query` and `Execute` methods of Connection.
type QueryResult struct {
cQueryResult C.lbug_query_result
connection *Connection
isClosed bool
columnNames []string
}
// ToString returns the string representation of the QueryResult.
// The string representation contains the column names and the tuples in the
// result set.
func (queryResult *QueryResult) ToString() string {
cString := C.lbug_query_result_to_string(&queryResult.cQueryResult)
str := C.GoString(cString)
C.free(unsafe.Pointer(cString))
return str
}
// Close releases the underlying C resources for the QueryResult.
// MUST be called when done to prevent resource leaks.
func (queryResult *QueryResult) Close() {
if queryResult.isClosed {
return
}
C.lbug_query_result_destroy(&queryResult.cQueryResult)
queryResult.isClosed = true
}
// ResetIterator resets the iterator of the QueryResult. After calling this method, the `Next`
// method can be called to iterate over the result set from the beginning.
func (queryResult *QueryResult) ResetIterator() {
C.lbug_query_result_reset_iterator(&queryResult.cQueryResult)
}
// GetColumnNames returns the column names of the QueryResult as a slice of strings.
func (queryResult *QueryResult) GetColumnNames() []string {
if queryResult.columnNames != nil {
return queryResult.columnNames
}
numColumns := int64(C.lbug_query_result_get_num_columns(&queryResult.cQueryResult))
columns := make([]string, 0, numColumns)
for i := int64(0); i < numColumns; i++ {
var outColumn *C.char
C.lbug_query_result_get_column_name(&queryResult.cQueryResult, C.uint64_t(i), &outColumn)
defer C.lbug_destroy_string(outColumn)
columns = append(columns, C.GoString(outColumn))
}
queryResult.columnNames = columns
return columns
}
// GetNumberOfColumns returns the number of columns in the QueryResult.
func (queryResult *QueryResult) GetNumberOfColumns() uint64 {
return uint64(C.lbug_query_result_get_num_columns(&queryResult.cQueryResult))
}
// GetNumberOfRows returns the number of rows in the QueryResult.
func (queryResult *QueryResult) GetNumberOfRows() uint64 {
if queryResult.columnNames != nil {
return uint64(len(queryResult.columnNames))
}
return uint64(C.lbug_query_result_get_num_tuples(&queryResult.cQueryResult))
}
// HasNext returns true if there is at least one more tuple in the result set.
func (queryResult *QueryResult) HasNext() bool {
return bool(C.lbug_query_result_has_next(&queryResult.cQueryResult))
}
// Next returns the next tuple in the result set.
func (queryResult *QueryResult) Next() (*FlatTuple, error) {
tuple := &FlatTuple{}
tuple.queryResult = queryResult
status := C.lbug_query_result_get_next(&queryResult.cQueryResult, &tuple.cFlatTuple)
if status != C.LbugSuccess {
return tuple, fmt.Errorf("failed to get next tuple with status %d", status)
}
return tuple, nil
}
// HasNextQueryResult returns true not all the query results is consumed when
// multiple query statements are executed.
func (queryResult *QueryResult) HasNextQueryResult() bool {
return bool(C.lbug_query_result_has_next_query_result(&queryResult.cQueryResult))
}
// NextQueryResult returns the next query result when multiple query statements are executed.
func (queryResult *QueryResult) NextQueryResult() (*QueryResult, error) {
nextQueryResult := &QueryResult{}
status := C.lbug_query_result_get_next_query_result(&queryResult.cQueryResult, &nextQueryResult.cQueryResult)
if status != C.LbugSuccess {
return nextQueryResult, fmt.Errorf("failed to get next query result with status %d", status)
}
return nextQueryResult, nil
}
// GetCompilingTime returns the compiling time of the query in milliseconds.
func (queryResult *QueryResult) GetCompilingTime() float64 {
var cQuerySummary C.lbug_query_summary
C.lbug_query_result_get_query_summary(&queryResult.cQueryResult, &cQuerySummary)
defer C.lbug_query_summary_destroy(&cQuerySummary)
return float64(C.lbug_query_summary_get_compiling_time(&cQuerySummary))
}
// GetExecutionTime returns the execution time of the query in milliseconds.
func (queryResult *QueryResult) GetExecutionTime() float64 {
var cQuerySummary C.lbug_query_summary
C.lbug_query_result_get_query_summary(&queryResult.cQueryResult, &cQuerySummary)
defer C.lbug_query_summary_destroy(&cQuerySummary)
return float64(C.lbug_query_summary_get_execution_time(&cQuerySummary))
}