本文整理匯總了Golang中github.com/cockroachdb/cockroach/structured.TableDescriptor.PrimaryIndex方法的典型用法代碼示例。如果您正苦於以下問題:Golang TableDescriptor.PrimaryIndex方法的具體用法?Golang TableDescriptor.PrimaryIndex怎麽用?Golang TableDescriptor.PrimaryIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cockroachdb/cockroach/structured.TableDescriptor
的用法示例。
在下文中一共展示了TableDescriptor.PrimaryIndex方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: makeTableDesc
func makeTableDesc(p *parser.CreateTable) (structured.TableDescriptor, error) {
desc := structured.TableDescriptor{}
desc.Name = p.Table.String()
for _, def := range p.Defs {
switch d := def.(type) {
case *parser.ColumnTableDef:
col := structured.ColumnDescriptor{
Name: string(d.Name),
Nullable: (d.Nullable != parser.NotNull),
}
switch t := d.Type.(type) {
case *parser.BitType:
col.Type.Kind = structured.ColumnType_BIT
col.Type.Width = int32(t.N)
case *parser.IntType:
col.Type.Kind = structured.ColumnType_INT
col.Type.Width = int32(t.N)
case *parser.FloatType:
col.Type.Kind = structured.ColumnType_FLOAT
col.Type.Precision = int32(t.Prec)
case *parser.DecimalType:
col.Type.Kind = structured.ColumnType_DECIMAL
col.Type.Width = int32(t.Scale)
col.Type.Precision = int32(t.Prec)
case *parser.DateType:
col.Type.Kind = structured.ColumnType_DATE
case *parser.TimeType:
col.Type.Kind = structured.ColumnType_TIME
case *parser.TimestampType:
col.Type.Kind = structured.ColumnType_TIMESTAMP
case *parser.CharType:
col.Type.Kind = structured.ColumnType_CHAR
col.Type.Width = int32(t.N)
case *parser.TextType:
col.Type.Kind = structured.ColumnType_TEXT
case *parser.BlobType:
col.Type.Kind = structured.ColumnType_BLOB
}
desc.Columns = append(desc.Columns, col)
// Create any associated index.
if d.PrimaryKey || d.Unique {
index := structured.IndexDescriptor{
Unique: true,
ColumnNames: []string{string(d.Name)},
}
if d.PrimaryKey {
index.Name = structured.PrimaryKeyIndexName
desc.PrimaryIndex = index
} else {
desc.Indexes = append(desc.Indexes, index)
}
}
case *parser.IndexTableDef:
index := structured.IndexDescriptor{
Name: string(d.Name),
Unique: d.Unique,
ColumnNames: d.Columns,
}
if d.PrimaryKey {
// Only override the index name if it hasn't been set by the user.
if index.Name == "" {
index.Name = structured.PrimaryKeyIndexName
}
desc.PrimaryIndex = index
} else {
desc.Indexes = append(desc.Indexes, index)
}
default:
return desc, fmt.Errorf("unsupported table def: %T", def)
}
}
return desc, nil
}