vitess-gh/go/sqltypes/proto3.go

123 строки
3.0 KiB
Go
Исходник Обычный вид История

2015-11-10 10:11:15 +03:00
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqltypes
2015-11-12 11:13:54 +03:00
import querypb "github.com/youtube/vitess/go/vt/proto/query"
2015-11-10 10:11:15 +03:00
// This file contains the proto3 conversion functions for the structures
// defined here.
// RowsToProto3 converts [][]Value to proto3.
2015-11-12 11:13:54 +03:00
func RowsToProto3(rows [][]Value) []*querypb.Row {
2015-11-10 10:11:15 +03:00
if len(rows) == 0 {
return nil
}
2015-11-12 11:13:54 +03:00
result := make([]*querypb.Row, len(rows))
2015-11-10 10:11:15 +03:00
for i, r := range rows {
2015-11-12 11:13:54 +03:00
row := &querypb.Row{}
2015-11-10 10:11:15 +03:00
result[i] = row
row.Lengths = make([]int64, 0, len(r))
total := 0
for _, c := range r {
if c.IsNull() {
row.Lengths = append(row.Lengths, -1)
continue
}
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
length := c.Len()
2015-11-10 10:11:15 +03:00
row.Lengths = append(row.Lengths, int64(length))
total += length
}
row.Values = make([]byte, 0, total)
for _, c := range r {
if c.IsNull() {
continue
}
row.Values = append(row.Values, c.Raw()...)
}
}
return result
}
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
// proto3ToRows converts a proto3 rows to [][]Value. The function is private
// because it uses the trusted API.
func proto3ToRows(fields []*querypb.Field, rows []*querypb.Row) [][]Value {
2015-11-10 10:11:15 +03:00
if len(rows) == 0 {
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
return nil
2015-11-10 10:11:15 +03:00
}
result := make([][]Value, len(rows))
for i, r := range rows {
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
result[i] = MakeRowTrusted(fields, r)
2015-11-10 10:11:15 +03:00
}
return result
}
// ResultToProto3 converts Result to proto3.
2015-11-12 11:13:54 +03:00
func ResultToProto3(qr *Result) *querypb.QueryResult {
2015-11-10 10:11:15 +03:00
if qr == nil {
return nil
}
2015-11-12 11:13:54 +03:00
return &querypb.QueryResult{
2015-11-10 10:11:15 +03:00
Fields: qr.Fields,
RowsAffected: qr.RowsAffected,
InsertId: qr.InsertID,
Rows: RowsToProto3(qr.Rows),
}
}
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
// Proto3ToResult converts a proto3 Result to an internal data structure. This function
// should be used only if the field info is populated in qr.
func Proto3ToResult(qr *querypb.QueryResult) *Result {
if qr == nil {
return nil
}
return &Result{
Fields: qr.Fields,
RowsAffected: qr.RowsAffected,
InsertID: qr.InsertId,
Rows: proto3ToRows(qr.Fields, qr.Rows),
}
}
// CustomProto3ToResult converts a proto3 Result to an internal data structure. This function
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
// takes a separate fields input because not all QueryResults contain the field info.
// In particular, only the first packet of streaming queries contain the field info.
func CustomProto3ToResult(fields []*querypb.Field, qr *querypb.QueryResult) *Result {
2015-11-10 10:11:15 +03:00
if qr == nil {
return nil
}
return &Result{
Fields: qr.Fields,
RowsAffected: qr.RowsAffected,
InsertID: qr.InsertId,
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
Rows: proto3ToRows(fields, qr.Rows),
2015-11-10 10:11:15 +03:00
}
}
sqltypes: new Value The new Value implementation is now based on the vitess types. * The inner interface has now been replaced by typ and val. * All Values are expected to be consistent with their types. For example, an Int64 type must contain a number. * The functions that build values generally ensure consistency. * There is a set of 'Trusted' functions that can bypass this consistency check. They should be used with care. * The proto3 conversion functions build the correct Value types based on the field types. * The bson conversion function provides a Repair function that allows you to fix up the types after the fact. This should be deleted after bson is deprecated. * The building of Values from a QueryResult is non-trivial because the field info is not part of the QueryResult for streaming queries. So, the API requires fields to be explicitly passed in. * Fuctions that encode or convert to native types expect Value to be consistent. If not, they panic. * proto3.QueryResult is considered to be trusted. If it contains inconsistent data, it will cause panics. * The EventStreamer has been fixed to ensure that the fields and rows it publishes are trustable: They can used as parameters to the Trusted API. * The Raw() function usage has been minimized. We should see if it can be deprecated. This way, we can make Result truly read-only. There are a few more tweaks that need to be done: * The Proto3ToResult call plumbing was hacked in to make everything work. That part needs cleaning. * The bind vars don't need to be converted to their native types any more.
2015-11-17 23:59:45 +03:00
// ResultsToProto3 converts []Result to proto3.
func ResultsToProto3(qr []Result) []*querypb.QueryResult {
if len(qr) == 0 {
return nil
}
result := make([]*querypb.QueryResult, len(qr))
for i, q := range qr {
result[i] = ResultToProto3(&q)
}
return result
}
2015-11-10 10:11:15 +03:00
// Proto3ToResults converts proto3 results to []Result.
2015-11-12 11:13:54 +03:00
func Proto3ToResults(qr []*querypb.QueryResult) []Result {
2015-11-10 10:11:15 +03:00
if len(qr) == 0 {
return nil
}
result := make([]Result, len(qr))
for i, q := range qr {
result[i] = *Proto3ToResult(q)
2015-11-10 10:11:15 +03:00
}
return result
}