2019-01-08 19:53:04 -05:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
2019-01-08 19:53:04 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
2019-03-07 03:20:10 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-01-08 19:53:04 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-03-07 03:20:10 -05:00
|
|
|
"github.com/bcicen/jstream"
|
2019-01-08 19:53:04 -05:00
|
|
|
"github.com/minio/minio/pkg/s3select/sql"
|
|
|
|
)
|
|
|
|
|
2019-03-07 03:20:10 -05:00
|
|
|
// RawJSON is a byte-slice that contains valid JSON
|
|
|
|
type RawJSON []byte
|
|
|
|
|
|
|
|
// MarshalJSON instance for []byte that assumes that byte-slice is
|
|
|
|
// already serialized JSON
|
|
|
|
func (b RawJSON) MarshalJSON() ([]byte, error) {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// Record - is JSON record.
|
|
|
|
type Record struct {
|
2019-03-07 03:20:10 -05:00
|
|
|
// Used in Set(), Marshal*()
|
2019-03-09 11:13:37 -05:00
|
|
|
KVS jstream.KVS
|
|
|
|
|
|
|
|
SelectFormat sql.SelectObjectFormat
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get - gets the value for a column name.
|
|
|
|
func (r *Record) Get(name string) (*sql.Value, error) {
|
2019-03-09 11:13:37 -05:00
|
|
|
// Get is implemented directly in the sql package.
|
|
|
|
return nil, errors.New("not implemented here")
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set - sets the value for a column name.
|
2019-03-09 11:13:37 -05:00
|
|
|
func (r *Record) Set(name string, value *sql.Value) error {
|
2019-01-08 19:53:04 -05:00
|
|
|
var v interface{}
|
2019-01-28 20:59:48 -05:00
|
|
|
if b, ok := value.ToBool(); ok {
|
|
|
|
v = b
|
|
|
|
} else if f, ok := value.ToFloat(); ok {
|
|
|
|
v = f
|
|
|
|
} else if i, ok := value.ToInt(); ok {
|
|
|
|
v = i
|
2019-02-04 23:54:45 -05:00
|
|
|
} else if t, ok := value.ToTimestamp(); ok {
|
|
|
|
v = sql.FormatSQLTimestamp(t)
|
2019-01-28 20:59:48 -05:00
|
|
|
} else if s, ok := value.ToString(); ok {
|
|
|
|
v = s
|
|
|
|
} else if value.IsNull() {
|
|
|
|
v = nil
|
|
|
|
} else if b, ok := value.ToBytes(); ok {
|
2019-03-07 03:20:10 -05:00
|
|
|
v = RawJSON(b)
|
2019-01-28 20:59:48 -05:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unsupported sql value %v and type %v", value, value.GetTypeString())
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
name = strings.Replace(name, "*", "__ALL__", -1)
|
2019-03-09 11:13:37 -05:00
|
|
|
r.KVS = append(r.KVS, jstream.KV{Key: name, Value: v})
|
|
|
|
return nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalCSV - encodes to CSV data.
|
|
|
|
func (r *Record) MarshalCSV(fieldDelimiter rune) ([]byte, error) {
|
|
|
|
var csvRecord []string
|
2019-03-09 11:13:37 -05:00
|
|
|
for _, kv := range r.KVS {
|
2019-03-07 03:20:10 -05:00
|
|
|
var columnValue string
|
|
|
|
switch val := kv.Value.(type) {
|
|
|
|
case bool, float64, int64, string:
|
|
|
|
columnValue = fmt.Sprintf("%v", val)
|
|
|
|
case nil:
|
|
|
|
columnValue = ""
|
|
|
|
case RawJSON:
|
|
|
|
columnValue = string([]byte(val))
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Cannot marshal unhandled type")
|
|
|
|
}
|
|
|
|
csvRecord = append(csvRecord, columnValue)
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := csv.NewWriter(buf)
|
|
|
|
w.Comma = fieldDelimiter
|
|
|
|
if err := w.Write(csvRecord); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
if err := w.Error(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := buf.Bytes()
|
|
|
|
return data[:len(data)-1], nil
|
|
|
|
}
|
|
|
|
|
2019-03-09 11:13:37 -05:00
|
|
|
// Raw - returns the underlying representation.
|
|
|
|
func (r *Record) Raw() (sql.SelectObjectFormat, interface{}) {
|
|
|
|
return r.SelectFormat, r.KVS
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// MarshalJSON - encodes to JSON data.
|
|
|
|
func (r *Record) MarshalJSON() ([]byte, error) {
|
2019-03-09 11:13:37 -05:00
|
|
|
return json.Marshal(r.KVS)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the underlying buffer of json data.
|
|
|
|
func (r *Record) Replace(k jstream.KVS) error {
|
|
|
|
r.KVS = k
|
|
|
|
return nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRecord - creates new empty JSON record.
|
2019-03-09 11:13:37 -05:00
|
|
|
func NewRecord(f sql.SelectObjectFormat) *Record {
|
2019-01-08 19:53:04 -05:00
|
|
|
return &Record{
|
2019-03-09 11:13:37 -05:00
|
|
|
KVS: jstream.KVS{},
|
|
|
|
SelectFormat: f,
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
}
|