本文整理汇总了Golang中github.com/cockroachdb/cockroach/pkg/sql/parser.DBool函数的典型用法代码示例。如果您正苦于以下问题:Golang DBool函数的具体用法?Golang DBool怎么用?Golang DBool使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DBool函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
示例2: DecodeTableValue
// DecodeTableValue decodes a value encoded by EncodeTableValue.
func DecodeTableValue(a *DatumAlloc, valType parser.Type, b []byte) (parser.Datum, []byte, error) {
_, dataOffset, _, typ, err := encoding.DecodeValueTag(b)
if err != nil {
return nil, b, err
}
if typ == encoding.Null {
return parser.DNull, b[dataOffset:], nil
}
switch valType {
case parser.TypeBool:
var x bool
b, x, err = encoding.DecodeBoolValue(b)
// No need to chunk allocate DBool as MakeDBool returns either
// parser.DBoolTrue or parser.DBoolFalse.
return parser.MakeDBool(parser.DBool(x)), b, err
case parser.TypeInt:
var i int64
b, i, err = encoding.DecodeIntValue(b)
return a.NewDInt(parser.DInt(i)), b, err
case parser.TypeFloat:
var f float64
b, f, err = encoding.DecodeFloatValue(b)
return a.NewDFloat(parser.DFloat(f)), b, err
case parser.TypeDecimal:
var d *inf.Dec
b, d, err = encoding.DecodeDecimalValue(b)
dd := a.NewDDecimal(parser.DDecimal{})
dd.Set(d)
return dd, b, err
case parser.TypeString:
var data []byte
b, data, err = encoding.DecodeBytesValue(b)
return a.NewDString(parser.DString(data)), b, err
case parser.TypeBytes:
var data []byte
b, data, err = encoding.DecodeBytesValue(b)
return a.NewDBytes(parser.DBytes(data)), b, err
case parser.TypeDate:
var i int64
b, i, err = encoding.DecodeIntValue(b)
return a.NewDDate(parser.DDate(i)), b, err
case parser.TypeTimestamp:
var t time.Time
b, t, err = encoding.DecodeTimeValue(b)
return a.NewDTimestamp(parser.DTimestamp{Time: t}), b, err
case parser.TypeTimestampTZ:
var t time.Time
b, t, err = encoding.DecodeTimeValue(b)
return a.NewDTimestampTZ(parser.DTimestampTZ{Time: t}), b, err
case parser.TypeInterval:
var d duration.Duration
b, d, err = encoding.DecodeDurationValue(b)
return a.NewDInterval(parser.DInterval{Duration: d}), b, err
default:
return nil, nil, errors.Errorf("TODO(pmattis): decoded index value: %s", valType)
}
}
示例3: ShowColumns
// ShowColumns of a table.
// Privileges: Any privilege on table.
// Notes: postgres does not have a SHOW COLUMNS statement.
// mysql only returns columns you have privileges on.
func (p *planner) ShowColumns(n *parser.ShowColumns) (planNode, error) {
tn, err := n.Table.NormalizeWithDatabaseName(p.session.Database)
if err != nil {
return nil, err
}
desc, err := p.mustGetTableDesc(tn)
if err != nil {
return nil, err
}
if err := p.anyPrivilege(desc); err != nil {
return nil, err
}
columns := ResultColumns{
{Name: "Field", Typ: parser.TypeString},
{Name: "Type", Typ: parser.TypeString},
{Name: "Null", Typ: parser.TypeBool},
{Name: "Default", Typ: parser.TypeString},
}
return &delayedNode{
p: p,
name: "SHOW COLUMNS FROM " + tn.String(),
columns: columns,
constructor: func(p *planner) (planNode, error) {
v := p.newContainerValuesNode(columns, 0)
for i, col := range desc.Columns {
defaultExpr := parser.DNull
if e := desc.Columns[i].DefaultExpr; e != nil {
defaultExpr = parser.NewDString(*e)
}
newRow := parser.DTuple{
parser.NewDString(desc.Columns[i].Name),
parser.NewDString(col.Type.SQLString()),
parser.MakeDBool(parser.DBool(desc.Columns[i].Nullable)),
defaultExpr,
}
if err := v.rows.AddRow(newRow); err != nil {
v.rows.Close()
return nil, err
}
}
return v, nil
},
}, nil
}
示例4: MakePrimaryIndexKey
// MakePrimaryIndexKey creates a key prefix that corresponds to a table row
// (in the primary index); it is intended for tests.
//
// The value types must match the primary key columns (or a prefix of them);
// supported types are: - Datum
// - bool (converts to DBool)
// - int (converts to DInt)
// - string (converts to DString)
func MakePrimaryIndexKey(desc *TableDescriptor, vals ...interface{}) (roachpb.Key, error) {
index := &desc.PrimaryIndex
if len(vals) > len(index.ColumnIDs) {
return nil, errors.Errorf("got %d values, PK has %d columns", len(vals), len(index.ColumnIDs))
}
datums := make([]parser.Datum, len(vals))
for i, v := range vals {
switch v := v.(type) {
case bool:
datums[i] = parser.MakeDBool(parser.DBool(v))
case int:
datums[i] = parser.NewDInt(parser.DInt(v))
case string:
datums[i] = parser.NewDString(v)
case parser.Datum:
datums[i] = v
default:
return nil, errors.Errorf("unexpected value type %T", v)
}
// Check that the value type matches.
colID := index.ColumnIDs[i]
for _, c := range desc.Columns {
if c.ID == colID {
if t := DatumTypeToColumnKind(datums[i].ResolvedType()); t != c.Type.Kind {
return nil, errors.Errorf("column %d of type %s, got value of type %s", i, c.Type.Kind, t)
}
break
}
}
}
// Create the ColumnID to index in datums slice map needed by
// MakeIndexKeyPrefix.
colIDToRowIndex := make(map[ColumnID]int)
for i := range vals {
colIDToRowIndex[index.ColumnIDs[i]] = i
}
keyPrefix := MakeIndexKeyPrefix(desc, index.ID)
key, _, err := EncodeIndexKey(desc, index, colIDToRowIndex, datums, keyPrefix)
if err != nil {
return nil, err
}
return roachpb.Key(key), nil
}
示例5: ShowIndex
// ShowIndex returns all the indexes for a table.
// Privileges: Any privilege on table.
// Notes: postgres does not have a SHOW INDEXES statement.
// mysql requires some privilege for any column.
func (p *planner) ShowIndex(n *parser.ShowIndex) (planNode, error) {
tn, err := n.Table.NormalizeWithDatabaseName(p.session.Database)
if err != nil {
return nil, err
}
desc, err := p.mustGetTableDesc(tn)
if err != nil {
return nil, err
}
if err := p.anyPrivilege(desc); err != nil {
return nil, err
}
columns := ResultColumns{
{Name: "Table", Typ: parser.TypeString},
{Name: "Name", Typ: parser.TypeString},
{Name: "Unique", Typ: parser.TypeBool},
{Name: "Seq", Typ: parser.TypeInt},
{Name: "Column", Typ: parser.TypeString},
{Name: "Direction", Typ: parser.TypeString},
{Name: "Storing", Typ: parser.TypeBool},
}
return &delayedNode{
p: p,
name: "SHOW INDEX FROM " + tn.String(),
columns: columns,
constructor: func(p *planner) (planNode, error) {
v := p.newContainerValuesNode(columns, 0)
appendRow := func(index sqlbase.IndexDescriptor, colName string, sequence int,
direction string, isStored bool) error {
newRow := parser.DTuple{
parser.NewDString(tn.Table()),
parser.NewDString(index.Name),
parser.MakeDBool(parser.DBool(index.Unique)),
parser.NewDInt(parser.DInt(sequence)),
parser.NewDString(colName),
parser.NewDString(direction),
parser.MakeDBool(parser.DBool(isStored)),
}
_, err := v.rows.AddRow(newRow)
return err
}
for _, index := range append([]sqlbase.IndexDescriptor{desc.PrimaryIndex}, desc.Indexes...) {
sequence := 1
for i, col := range index.ColumnNames {
if err := appendRow(index, col, sequence, index.ColumnDirections[i].String(), false); err != nil {
v.rows.Close()
return nil, err
}
sequence++
}
for _, col := range index.StoreColumnNames {
if err := appendRow(index, col, sequence, "N/A", true); err != nil {
v.rows.Close()
return nil, err
}
sequence++
}
}
return v, nil
},
}, nil
}
示例6: dumpTable
//.........这里部分代码省略.........
// pk holds the last values of the fetched primary keys
var pk []driver.Value
q := fmt.Sprintf(bs, "")
for {
rows, err := conn.Query(q, pk)
if err != nil {
return err
}
cols := rows.Columns()
pkcols := cols[:len(index)]
cols = cols[len(index):]
inserts := make([][]string, 0, limit)
i := 0
for i < limit {
vals := make([]driver.Value, len(cols)+len(pkcols))
if err := rows.Next(vals); err == io.EOF {
break
} else if err != nil {
return err
}
if pk == nil {
q = fmt.Sprintf(bs, wbuf.String())
}
pk = vals[:len(index)]
vals = vals[len(index):]
ivals := make([]string, len(vals))
// Values need to be correctly encoded for INSERT statements in a text file.
for si, sv := range vals {
switch t := sv.(type) {
case nil:
ivals[si] = "NULL"
case bool:
ivals[si] = parser.MakeDBool(parser.DBool(t)).String()
case int64:
ivals[si] = parser.NewDInt(parser.DInt(t)).String()
case float64:
ivals[si] = parser.NewDFloat(parser.DFloat(t)).String()
case string:
ivals[si] = parser.NewDString(t).String()
case []byte:
switch ct := coltypes[cols[si]]; ct {
case "INTERVAL":
ivals[si] = fmt.Sprintf("'%s'", t)
case "BYTES":
ivals[si] = parser.NewDBytes(parser.DBytes(t)).String()
default:
// STRING and DECIMAL types can have optional length
// suffixes, so only examine the prefix of the type.
if strings.HasPrefix(coltypes[cols[si]], "STRING") {
ivals[si] = parser.NewDString(string(t)).String()
} else if strings.HasPrefix(coltypes[cols[si]], "DECIMAL") {
ivals[si] = string(t)
} else {
panic(errors.Errorf("unknown []byte type: %s, %v: %s", t, cols[si], coltypes[cols[si]]))
}
}
case time.Time:
var d parser.Datum
ct := coltypes[cols[si]]
switch ct {
case "DATE":
d = parser.NewDDateFromTime(t, time.UTC)
case "TIMESTAMP":
d = parser.MakeDTimestamp(t, time.Nanosecond)
case "TIMESTAMP WITH TIME ZONE":
示例7: DecodeTableKey
// DecodeTableKey decodes a table key/value.
func DecodeTableKey(
a *DatumAlloc, valType parser.Type, key []byte, dir encoding.Direction,
) (parser.Datum, []byte, error) {
if (dir != encoding.Ascending) && (dir != encoding.Descending) {
return nil, nil, errors.Errorf("invalid direction: %d", dir)
}
var isNull bool
if key, isNull = encoding.DecodeIfNull(key); isNull {
return parser.DNull, key, nil
}
var rkey []byte
var err error
switch valType {
case parser.TypeBool:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
// No need to chunk allocate DBool as MakeDBool returns either
// parser.DBoolTrue or parser.DBoolFalse.
return parser.MakeDBool(parser.DBool(i != 0)), rkey, err
case parser.TypeInt:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return a.NewDInt(parser.DInt(i)), rkey, err
case parser.TypeFloat:
var f float64
if dir == encoding.Ascending {
rkey, f, err = encoding.DecodeFloatAscending(key)
} else {
rkey, f, err = encoding.DecodeFloatDescending(key)
}
return a.NewDFloat(parser.DFloat(f)), rkey, err
case parser.TypeDecimal:
var d *inf.Dec
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDecimalAscending(key, nil)
} else {
rkey, d, err = encoding.DecodeDecimalDescending(key, nil)
}
dd := a.NewDDecimal(parser.DDecimal{})
dd.Set(d)
return dd, rkey, err
case parser.TypeString:
var r string
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeUnsafeStringAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeUnsafeStringDescending(key, nil)
}
return a.NewDString(parser.DString(r)), rkey, err
case parser.TypeBytes:
var r []byte
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
return a.NewDBytes(parser.DBytes(r)), rkey, err
case parser.TypeDate:
var t int64
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeVarintAscending(key)
} else {
rkey, t, err = encoding.DecodeVarintDescending(key)
}
return a.NewDDate(parser.DDate(t)), rkey, err
case parser.TypeTimestamp:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return a.NewDTimestamp(parser.DTimestamp{Time: t}), rkey, err
case parser.TypeTimestampTZ:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return a.NewDTimestampTZ(parser.DTimestampTZ{Time: t}), rkey, err
case parser.TypeInterval:
var d duration.Duration
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDurationAscending(key)
} else {
rkey, d, err = encoding.DecodeDurationDescending(key)
}
return a.NewDInterval(parser.DInterval{Duration: d}), rkey, err
default:
return nil, nil, errors.Errorf("TODO(pmattis): decoded index key: %s", valType)
//.........这里部分代码省略.........
示例8: UnmarshalColumnValue
// UnmarshalColumnValue decodes the value from a key-value pair using the type
// expected by the column. An error is returned if the value's type does not
// match the column's type.
func UnmarshalColumnValue(
a *DatumAlloc, kind ColumnType_Kind, value *roachpb.Value,
) (parser.Datum, error) {
if value == nil {
return parser.DNull, nil
}
switch kind {
case ColumnType_BOOL:
v, err := value.GetBool()
if err != nil {
return nil, err
}
return parser.MakeDBool(parser.DBool(v)), nil
case ColumnType_INT:
v, err := value.GetInt()
if err != nil {
return nil, err
}
return a.NewDInt(parser.DInt(v)), nil
case ColumnType_FLOAT:
v, err := value.GetFloat()
if err != nil {
return nil, err
}
return a.NewDFloat(parser.DFloat(v)), nil
case ColumnType_DECIMAL:
v, err := value.GetDecimal()
if err != nil {
return nil, err
}
dd := a.NewDDecimal(parser.DDecimal{})
dd.Set(v)
return dd, nil
case ColumnType_STRING:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
return a.NewDString(parser.DString(v)), nil
case ColumnType_BYTES:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
return a.NewDBytes(parser.DBytes(v)), nil
case ColumnType_DATE:
v, err := value.GetInt()
if err != nil {
return nil, err
}
return a.NewDDate(parser.DDate(v)), nil
case ColumnType_TIMESTAMP:
v, err := value.GetTime()
if err != nil {
return nil, err
}
return a.NewDTimestamp(parser.DTimestamp{Time: v}), nil
case ColumnType_TIMESTAMPTZ:
v, err := value.GetTime()
if err != nil {
return nil, err
}
return a.NewDTimestampTZ(parser.DTimestampTZ{Time: v}), nil
case ColumnType_INTERVAL:
d, err := value.GetDuration()
if err != nil {
return nil, err
}
return a.NewDInterval(parser.DInterval{Duration: d}), nil
default:
return nil, errors.Errorf("unsupported column type: %s", kind)
}
}
示例9: decodeOidDatum
// decodeOidDatum decodes bytes with specified Oid and format code into
// a datum.
func decodeOidDatum(id oid.Oid, code formatCode, b []byte) (parser.Datum, error) {
var d parser.Datum
switch id {
case oid.T_bool:
switch code {
case formatText:
v, err := strconv.ParseBool(string(b))
if err != nil {
return d, err
}
d = parser.MakeDBool(parser.DBool(v))
case formatBinary:
switch b[0] {
case 0:
d = parser.MakeDBool(false)
case 1:
d = parser.MakeDBool(true)
default:
return d, errors.Errorf("unsupported binary bool: %q", b)
}
default:
return d, errors.Errorf("unsupported bool format code: %d", code)
}
case oid.T_int2:
switch code {
case formatText:
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return d, err
}
d = parser.NewDInt(parser.DInt(i))
case formatBinary:
if len(b) < 2 {
return d, errors.Errorf("int2 requires 2 bytes for binary format")
}
i := int16(binary.BigEndian.Uint16(b))
d = parser.NewDInt(parser.DInt(i))
default:
return d, errors.Errorf("unsupported int2 format code: %d", code)
}
case oid.T_int4:
switch code {
case formatText:
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return d, err
}
d = parser.NewDInt(parser.DInt(i))
case formatBinary:
if len(b) < 4 {
return d, errors.Errorf("int4 requires 4 bytes for binary format")
}
i := int32(binary.BigEndian.Uint32(b))
d = parser.NewDInt(parser.DInt(i))
default:
return d, errors.Errorf("unsupported int4 format code: %d", code)
}
case oid.T_int8:
switch code {
case formatText:
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return d, err
}
d = parser.NewDInt(parser.DInt(i))
case formatBinary:
if len(b) < 8 {
return d, errors.Errorf("int8 requires 8 bytes for binary format")
}
i := int64(binary.BigEndian.Uint64(b))
d = parser.NewDInt(parser.DInt(i))
default:
return d, errors.Errorf("unsupported int8 format code: %d", code)
}
case oid.T_float4:
switch code {
case formatText:
f, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return d, err
}
d = parser.NewDFloat(parser.DFloat(f))
case formatBinary:
if len(b) < 4 {
return d, errors.Errorf("float4 requires 4 bytes for binary format")
}
f := math.Float32frombits(binary.BigEndian.Uint32(b))
d = parser.NewDFloat(parser.DFloat(f))
default:
return d, errors.Errorf("unsupported float4 format code: %d", code)
}
case oid.T_float8:
switch code {
case formatText:
f, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return d, err
}
//.........这里部分代码省略.........
示例10: typByVal
func typByVal(typ parser.Type) parser.Datum {
_, variable := typ.Size()
return parser.MakeDBool(parser.DBool(!variable))
}
示例11:
addColumn := func(column *sqlbase.ColumnDescriptor, attRelID parser.Datum, colNum int) error {
colTyp := column.Type.ToDatumType()
return addRow(
attRelID, // attrelid
parser.NewDString(column.Name), // attname
typOid(colTyp), // atttypid
zeroVal, // attstattarget
typLen(colTyp), // attlen
parser.NewDInt(parser.DInt(colNum)), // attnum
zeroVal, // attndims
negOneVal, // attcacheoff
negOneVal, // atttypmod
parser.DNull, // attbyval (see pg_type.typbyval)
parser.DNull, // attstorage
parser.DNull, // attalign
parser.MakeDBool(parser.DBool(!column.Nullable)), // attnotnull
parser.MakeDBool(parser.DBool(column.DefaultExpr != nil)), // atthasdef
parser.MakeDBool(false), // attisdropped
parser.MakeDBool(true), // attislocal
zeroVal, // attinhcount
parser.DNull, // attacl
parser.DNull, // attoptions
parser.DNull, // attfdwoptions
)
}
// Columns for table.
colNum := 0
if err := forEachColumnInTable(table, func(column *sqlbase.ColumnDescriptor) error {
colNum++
tableID := h.TableOid(db, table)
示例12:
COLUMN_NAME STRING NOT NULL DEFAULT '',
"COLLATION" STRING NOT NULL DEFAULT '',
CARDINALITY INT NOT NULL DEFAULT 0,
DIRECTION STRING NOT NULL DEFAULT '',
STORING BOOL NOT NULL DEFAULT FALSE
);`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
appendRow := func(index *sqlbase.IndexDescriptor, colName string, sequence int,
direction string, isStored bool) error {
return addRow(
defString, // table_catalog
parser.NewDString(db.GetName()), // table_schema
parser.NewDString(table.GetName()), // table_name
parser.MakeDBool(parser.DBool(index.Unique)), // non_unique
parser.NewDString(db.GetName()), // index_schema
parser.NewDString(index.Name), // index_name
parser.NewDInt(parser.DInt(sequence)), // seq_in_index
parser.NewDString(colName), // column_name
parser.DNull, // collation
parser.DNull, // cardinality
parser.NewDString(direction), // direction
parser.MakeDBool(parser.DBool(isStored)), // storing
)
}
return forEachIndexInTable(table, func(index *sqlbase.IndexDescriptor) error {
sequence := 1
for i, col := range index.ColumnNames {
// We add a row for each column of index.