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 csv
|
|
|
|
|
|
|
|
import (
|
2019-03-07 03:20:10 -05:00
|
|
|
"encoding/json"
|
2019-03-09 11:13:37 -05:00
|
|
|
"errors"
|
2019-01-08 19:53:04 -05:00
|
|
|
"fmt"
|
2019-08-31 03:07:40 -04:00
|
|
|
"io"
|
2019-01-08 19:53:04 -05:00
|
|
|
|
2019-03-07 03:20:10 -05:00
|
|
|
"github.com/bcicen/jstream"
|
2020-02-07 05:55:36 -05:00
|
|
|
csv "github.com/minio/minio/pkg/csvparser"
|
2019-01-08 19:53:04 -05:00
|
|
|
"github.com/minio/minio/pkg/s3select/sql"
|
|
|
|
)
|
|
|
|
|
2019-09-13 17:18:35 -04:00
|
|
|
// Record - is a CSV record.
|
2019-01-08 19:53:04 -05:00
|
|
|
type Record struct {
|
|
|
|
columnNames []string
|
|
|
|
csvRecord []string
|
|
|
|
nameIndexMap map[string]int64
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
// Get - gets the value for a column name. CSV fields do not have any
|
|
|
|
// defined type (other than the default string). So this function
|
|
|
|
// always returns fields using sql.FromBytes so that the type
|
|
|
|
// specified/implied by the query can be used, or can be automatically
|
|
|
|
// converted based on the query.
|
2019-01-08 19:53:04 -05:00
|
|
|
func (r *Record) Get(name string) (*sql.Value, error) {
|
|
|
|
index, found := r.nameIndexMap[name]
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("column %v not found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if index >= int64(len(r.csvRecord)) {
|
2019-01-28 20:59:48 -05:00
|
|
|
// No value found for column 'name', hence return null
|
|
|
|
// value
|
|
|
|
return sql.FromNull(), nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
return sql.FromBytes([]byte(r.csvRecord[index])), nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set - sets the value for a column name.
|
2020-02-13 17:03:52 -05:00
|
|
|
func (r *Record) Set(name string, value *sql.Value) (sql.Record, error) {
|
2019-01-08 19:53:04 -05:00
|
|
|
r.columnNames = append(r.columnNames, name)
|
|
|
|
r.csvRecord = append(r.csvRecord, value.CSVString())
|
2020-02-13 17:03:52 -05:00
|
|
|
return r, nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
// Reset data in record.
|
|
|
|
func (r *Record) Reset() {
|
|
|
|
if len(r.columnNames) > 0 {
|
|
|
|
r.columnNames = r.columnNames[:0]
|
|
|
|
}
|
|
|
|
if len(r.csvRecord) > 0 {
|
|
|
|
r.csvRecord = r.csvRecord[:0]
|
|
|
|
}
|
|
|
|
for k := range r.nameIndexMap {
|
|
|
|
delete(r.nameIndexMap, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:33:14 -04:00
|
|
|
// Clone the record.
|
|
|
|
func (r *Record) Clone(dst sql.Record) sql.Record {
|
|
|
|
other, ok := dst.(*Record)
|
2019-09-16 20:26:27 -04:00
|
|
|
if !ok {
|
2019-09-27 15:33:14 -04:00
|
|
|
other = &Record{}
|
2019-09-16 20:26:27 -04:00
|
|
|
}
|
2019-09-27 15:33:14 -04:00
|
|
|
if len(other.columnNames) > 0 {
|
|
|
|
other.columnNames = other.columnNames[:0]
|
|
|
|
}
|
|
|
|
if len(other.csvRecord) > 0 {
|
|
|
|
other.csvRecord = other.csvRecord[:0]
|
|
|
|
}
|
|
|
|
other.columnNames = append(other.columnNames, r.columnNames...)
|
|
|
|
other.csvRecord = append(other.csvRecord, r.csvRecord...)
|
|
|
|
return other
|
2019-09-16 20:26:27 -04:00
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
// WriteCSV - encodes to CSV data.
|
2020-04-01 18:39:34 -04:00
|
|
|
func (r *Record) WriteCSV(writer io.Writer, opts sql.WriteCSVOpts) error {
|
2019-08-31 03:07:40 -04:00
|
|
|
w := csv.NewWriter(writer)
|
2020-04-01 18:39:34 -04:00
|
|
|
w.Comma = opts.FieldDelimiter
|
|
|
|
w.AlwaysQuote = opts.AlwaysQuote
|
|
|
|
w.Quote = opts.Quote
|
|
|
|
w.QuoteEscape = opts.QuoteEscape
|
2019-01-08 19:53:04 -05:00
|
|
|
if err := w.Write(r.csvRecord); err != nil {
|
2019-08-31 03:07:40 -04:00
|
|
|
return err
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
if err := w.Error(); err != nil {
|
2019-08-31 03:07:40 -04:00
|
|
|
return err
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
return nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
// WriteJSON - encodes to JSON data.
|
|
|
|
func (r *Record) WriteJSON(writer io.Writer) error {
|
2019-03-07 03:20:10 -05:00
|
|
|
var kvs jstream.KVS = make([]jstream.KV, len(r.columnNames))
|
|
|
|
for i := 0; i < len(r.columnNames); i++ {
|
|
|
|
kvs[i] = jstream.KV{Key: r.columnNames[i], Value: r.csvRecord[i]}
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
2019-08-31 03:07:40 -04:00
|
|
|
return json.NewEncoder(writer).Encode(kvs)
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-03-09 11:13:37 -05:00
|
|
|
// Raw - returns the underlying data with format info.
|
|
|
|
func (r *Record) Raw() (sql.SelectObjectFormat, interface{}) {
|
|
|
|
return sql.SelectFmtCSV, r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace - is not supported for CSV
|
2020-02-13 17:03:52 -05:00
|
|
|
func (r *Record) Replace(_ interface{}) error {
|
2019-03-09 11:13:37 -05:00
|
|
|
return errors.New("Replace is not supported for CSV")
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// NewRecord - creates new CSV record.
|
|
|
|
func NewRecord() *Record {
|
|
|
|
return &Record{}
|
|
|
|
}
|