|
|
@ -3,11 +3,18 @@ package encoding
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/rqlite/rqlite/command"
|
|
|
|
"github.com/rqlite/rqlite/command"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
// ErrTypesColumnsLengthViolation is returned when a results
|
|
|
|
|
|
|
|
// object doesn't have the same number of types and columns
|
|
|
|
|
|
|
|
ErrTypesColumnsLengthViolation = errors.New("types and columns are different lengths")
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Result represents the outcome of an operation that changes rows.
|
|
|
|
// Result represents the outcome of an operation that changes rows.
|
|
|
|
type Result struct {
|
|
|
|
type Result struct {
|
|
|
|
LastInsertID int64 `json:"last_insert_id,omitempty"`
|
|
|
|
LastInsertID int64 `json:"last_insert_id,omitempty"`
|
|
|
@ -27,7 +34,7 @@ type Rows struct {
|
|
|
|
|
|
|
|
|
|
|
|
// AssociativeRows represents the outcome of an operation that returns query data.
|
|
|
|
// AssociativeRows represents the outcome of an operation that returns query data.
|
|
|
|
type AssociativeRows struct {
|
|
|
|
type AssociativeRows struct {
|
|
|
|
Types []string `json:"types,omitempty"`
|
|
|
|
Types map[string]string `json:"types,omitempty"`
|
|
|
|
Rows []map[string]interface{} `json:"rows,omitempty"`
|
|
|
|
Rows []map[string]interface{} `json:"rows,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Time float64 `json:"time,omitempty"`
|
|
|
|
Time float64 `json:"time,omitempty"`
|
|
|
@ -45,6 +52,10 @@ func NewResultFromExecuteResult(e *command.ExecuteResult) (*Result, error) {
|
|
|
|
|
|
|
|
|
|
|
|
// NewRowsFromQueryRows returns an API Rows object from a QueryRows
|
|
|
|
// NewRowsFromQueryRows returns an API Rows object from a QueryRows
|
|
|
|
func NewRowsFromQueryRows(q *command.QueryRows) (*Rows, error) {
|
|
|
|
func NewRowsFromQueryRows(q *command.QueryRows) (*Rows, error) {
|
|
|
|
|
|
|
|
if len(q.Columns) != len(q.Types) {
|
|
|
|
|
|
|
|
return nil, ErrTypesColumnsLengthViolation
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
values := make([][]interface{}, len(q.Values))
|
|
|
|
values := make([][]interface{}, len(q.Values))
|
|
|
|
if err := NewValuesFromQueryValues(values, q.Values); err != nil {
|
|
|
|
if err := NewValuesFromQueryValues(values, q.Values); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -60,6 +71,10 @@ func NewRowsFromQueryRows(q *command.QueryRows) (*Rows, error) {
|
|
|
|
|
|
|
|
|
|
|
|
// NewAssociativeRowsFromQueryRows returns an associative API object from a QueryRows
|
|
|
|
// NewAssociativeRowsFromQueryRows returns an associative API object from a QueryRows
|
|
|
|
func NewAssociativeRowsFromQueryRows(q *command.QueryRows) (*AssociativeRows, error) {
|
|
|
|
func NewAssociativeRowsFromQueryRows(q *command.QueryRows) (*AssociativeRows, error) {
|
|
|
|
|
|
|
|
if len(q.Columns) != len(q.Types) {
|
|
|
|
|
|
|
|
return nil, ErrTypesColumnsLengthViolation
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
values := make([][]interface{}, len(q.Values))
|
|
|
|
values := make([][]interface{}, len(q.Values))
|
|
|
|
if err := NewValuesFromQueryValues(values, q.Values); err != nil {
|
|
|
|
if err := NewValuesFromQueryValues(values, q.Values); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -74,7 +89,13 @@ func NewAssociativeRowsFromQueryRows(q *command.QueryRows) (*AssociativeRows, er
|
|
|
|
rows[i] = m
|
|
|
|
rows[i] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
types := make(map[string]string)
|
|
|
|
|
|
|
|
for i := range q.Types {
|
|
|
|
|
|
|
|
types[q.Columns[i]] = q.Types[i]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &AssociativeRows{
|
|
|
|
return &AssociativeRows{
|
|
|
|
|
|
|
|
Types: types,
|
|
|
|
Rows: rows,
|
|
|
|
Rows: rows,
|
|
|
|
Error: q.Error,
|
|
|
|
Error: q.Error,
|
|
|
|
Time: q.Time,
|
|
|
|
Time: q.Time,
|
|
|
|