本文整理匯總了Golang中github.com/cockroachdb/cockroach/pkg/sql/parser.PlaceholderInfo類的典型用法代碼示例。如果您正苦於以下問題:Golang PlaceholderInfo類的具體用法?Golang PlaceholderInfo怎麽用?Golang PlaceholderInfo使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了PlaceholderInfo類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CheckColumnType
// CheckColumnType verifies that a given value is compatible
// with the type requested by the column. If the value is a
// placeholder, the type of the placeholder gets populated.
func CheckColumnType(col ColumnDescriptor, typ parser.Type, pmap *parser.PlaceholderInfo) error {
if typ == parser.TypeNull {
return nil
}
var set parser.Type
switch col.Type.Kind {
case ColumnType_BOOL:
set = parser.TypeBool
case ColumnType_INT:
set = parser.TypeInt
case ColumnType_FLOAT:
set = parser.TypeFloat
case ColumnType_DECIMAL:
set = parser.TypeDecimal
case ColumnType_STRING:
set = parser.TypeString
case ColumnType_BYTES:
set = parser.TypeBytes
case ColumnType_DATE:
set = parser.TypeDate
case ColumnType_TIMESTAMP:
set = parser.TypeTimestamp
case ColumnType_TIMESTAMPTZ:
set = parser.TypeTimestampTZ
case ColumnType_INTERVAL:
set = parser.TypeInterval
default:
return errors.Errorf("unsupported column type: %s", col.Type.Kind)
}
// If the value is a placeholder, then the column check above has
// populated 'set' with a type to assign to it.
if p, pok := typ.(parser.TPlaceholder); pok {
if err := pmap.SetType(p.Name, set); err != nil {
return fmt.Errorf("cannot infer type for placeholder %s from column %q: %s",
p.Name, col.Name, err)
}
} else if !(typ.Equal(set) || (set == parser.TypeBytes && typ == parser.TypeString)) {
// Not a placeholder; check that the value cast has succeeded.
return fmt.Errorf("value type %s doesn't match type %s of column %q",
typ, col.Type.Kind, col.Name)
}
return nil
}
示例2: golangFillQueryArguments
// golangFillQueryArguments populates the placeholder map with
// types and values from an array of Go values.
// TODO: This does not support arguments of the SQL 'Date' type, as there is not
// an equivalent type in Go's standard library. It's not currently needed by any
// of our internal tables.
func golangFillQueryArguments(pinfo *parser.PlaceholderInfo, args []interface{}) {
pinfo.Clear()
for i, arg := range args {
k := fmt.Sprint(i + 1)
if arg == nil {
pinfo.SetValue(k, parser.DNull)
continue
}
// A type switch to handle a few explicit types with special semantics:
// - Datums are passed along as is.
// - Time datatypes get special representation in the database.
var d parser.Datum
switch t := arg.(type) {
case parser.Datum:
d = t
case time.Time:
d = parser.MakeDTimestamp(t, time.Microsecond)
case time.Duration:
d = &parser.DInterval{Duration: duration.Duration{Nanos: t.Nanoseconds()}}
case *inf.Dec:
dd := &parser.DDecimal{}
dd.Set(t)
d = dd
}
if d == nil {
// Handle all types which have an underlying type that can be stored in the
// database.
// Note: if this reflection becomes a performance concern in the future,
// commonly used types could be added explicitly into the type switch above
// for a performance gain.
val := reflect.ValueOf(arg)
switch val.Kind() {
case reflect.Bool:
d = parser.MakeDBool(parser.DBool(val.Bool()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
d = parser.NewDInt(parser.DInt(val.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
d = parser.NewDInt(parser.DInt(val.Uint()))
case reflect.Float32, reflect.Float64:
d = parser.NewDFloat(parser.DFloat(val.Float()))
case reflect.String:
d = parser.NewDString(val.String())
case reflect.Slice:
// Handle byte slices.
if val.Type().Elem().Kind() == reflect.Uint8 {
d = parser.NewDBytes(parser.DBytes(val.Bytes()))
}
}
if d == nil {
panic(fmt.Sprintf("unexpected type %T", arg))
}
}
pinfo.SetValue(k, d)
}
}