本文整理匯總了Golang中github.com/cockroachdb/cockroach/pkg/sql/parser.Name函數的典型用法代碼示例。如果您正苦於以下問題:Golang Name函數的具體用法?Golang Name怎麽用?Golang Name使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Name函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getTableNames
// getTableNames implements the SchemaAccessor interface.
func (p *planner) getTableNames(dbDesc *sqlbase.DatabaseDescriptor) (parser.TableNames, error) {
if e, ok := p.session.virtualSchemas.getVirtualSchemaEntry(dbDesc.Name); ok {
return e.tableNames(), nil
}
prefix := sqlbase.MakeNameMetadataKey(dbDesc.ID, "")
sr, err := p.txn.Scan(prefix, prefix.PrefixEnd(), 0)
if err != nil {
return nil, err
}
var tableNames parser.TableNames
for _, row := range sr {
_, tableName, err := encoding.DecodeUnsafeStringAscending(
bytes.TrimPrefix(row.Key, prefix), nil)
if err != nil {
return nil, err
}
tn := parser.TableName{
DatabaseName: parser.Name(dbDesc.Name),
TableName: parser.Name(tableName),
}
tableNames = append(tableNames, tn)
}
return tableNames, nil
}
示例2: userTablesAndDBsMatchingName
func userTablesAndDBsMatchingName(
descs []sqlbase.Descriptor, name parser.TableName,
) ([]sqlbase.Descriptor, error) {
tableName := name.TableName.Normalize()
dbName := name.DatabaseName.Normalize()
matches := make([]sqlbase.Descriptor, 0, len(descs))
dbIDsToName := make(map[sqlbase.ID]string)
for _, desc := range descs {
if db := desc.GetDatabase(); db != nil {
if db.ID == keys.SystemDatabaseID {
continue // Not a user database.
}
if n := parser.Name(db.Name).Normalize(); dbName == "*" || n == dbName {
matches = append(matches, desc)
dbIDsToName[db.ID] = n
}
continue
}
}
for _, desc := range descs {
if table := desc.GetTable(); table != nil {
if _, ok := dbIDsToName[table.ParentID]; !ok {
continue
}
if tableName == "*" || parser.Name(table.Name).Normalize() == tableName {
matches = append(matches, desc)
}
}
}
return matches, nil
}
示例3: runRestore
func runRestore(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("input basepath argument is required")
}
base := args[0]
ctx := context.Background()
kvDB, stopper, err := makeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
tableName := parser.TableName{
DatabaseName: parser.Name(backupCtx.database),
TableName: parser.Name(backupCtx.table),
}
restored, err := sql.Restore(ctx, *kvDB, base, tableName)
if err != nil {
return err
}
for _, table := range restored {
fmt.Printf("Restored table %q\n", table.Name)
}
fmt.Printf("Restored from %s\n", base)
return nil
}
示例4: upsertExprsAndIndex
// upsertExprsAndIndex returns the upsert conflict index and the (possibly
// synthetic) SET expressions used when a row conflicts.
func upsertExprsAndIndex(
tableDesc *sqlbase.TableDescriptor,
onConflict parser.OnConflict,
insertCols []sqlbase.ColumnDescriptor,
) (parser.UpdateExprs, *sqlbase.IndexDescriptor, error) {
if onConflict.IsUpsertAlias() {
// The UPSERT syntactic sugar is the same as the longhand specifying the
// primary index as the conflict index and SET expressions for the columns
// in insertCols minus any columns in the conflict index. Example:
// `UPSERT INTO abc VALUES (1, 2, 3)` is syntactic sugar for
// `INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT a DO UPDATE SET b = 2, c = 3`.
conflictIndex := &tableDesc.PrimaryIndex
indexColSet := make(map[sqlbase.ColumnID]struct{}, len(conflictIndex.ColumnIDs))
for _, colID := range conflictIndex.ColumnIDs {
indexColSet[colID] = struct{}{}
}
updateExprs := make(parser.UpdateExprs, 0, len(insertCols))
for _, c := range insertCols {
if _, ok := indexColSet[c.ID]; !ok {
names := parser.UnresolvedNames{
parser.UnresolvedName{parser.Name(c.Name)},
}
expr := &parser.ColumnItem{
TableName: upsertExcludedTable,
ColumnName: parser.Name(c.Name),
}
updateExprs = append(updateExprs, &parser.UpdateExpr{Names: names, Expr: expr})
}
}
return updateExprs, conflictIndex, nil
}
indexMatch := func(index sqlbase.IndexDescriptor) bool {
if !index.Unique {
return false
}
if len(index.ColumnNames) != len(onConflict.Columns) {
return false
}
for i, colName := range index.ColumnNames {
if parser.ReNormalizeName(colName) != onConflict.Columns[i].Normalize() {
return false
}
}
return true
}
if indexMatch(tableDesc.PrimaryIndex) {
return onConflict.Exprs, &tableDesc.PrimaryIndex, nil
}
for _, index := range tableDesc.Indexes {
if indexMatch(index) {
return onConflict.Exprs, &index, nil
}
}
return nil, nil, fmt.Errorf("there is no unique or exclusion constraint matching the ON CONFLICT specification")
}
示例5: tableNames
func (e virtualSchemaEntry) tableNames() parser.TableNames {
var res parser.TableNames
for _, tableName := range e.orderedTableNames {
tn := parser.TableName{
DatabaseName: parser.Name(e.desc.Name),
TableName: parser.Name(tableName),
}
res = append(res, tn)
}
return res
}
示例6: getQualifiedTableName
// getQualifiedTableName returns the database-qualified name of the table
// or view represented by the provided descriptor.
func (p *planner) getQualifiedTableName(desc *sqlbase.TableDescriptor) (string, error) {
dbDesc, err := sqlbase.GetDatabaseDescFromID(p.txn, desc.ParentID)
if err != nil {
return "", err
}
tbName := parser.TableName{
DatabaseName: parser.Name(dbDesc.Name),
TableName: parser.Name(desc.Name),
}
return tbName.String(), nil
}
示例7: queryNamespace
func queryNamespace(conn *sqlConn, parentID sqlbase.ID, name string) (sqlbase.ID, error) {
rows, err := makeQuery(
`SELECT id FROM system.namespace WHERE parentID = $1 AND name = $2`,
parentID, parser.Name(name).Normalize())(conn)
if err != nil {
return 0, err
}
defer func() { _ = rows.Close() }()
if err != nil {
return 0, fmt.Errorf("%s not found: %v", name, err)
}
if len(rows.Columns()) != 1 {
return 0, fmt.Errorf("unexpected result columns: %d", len(rows.Columns()))
}
vals := make([]driver.Value, 1)
if err := rows.Next(vals); err != nil {
return 0, err
}
switch t := vals[0].(type) {
case int64:
return sqlbase.ID(t), nil
default:
return 0, fmt.Errorf("unexpected result type: %T", vals[0])
}
}
示例8: GetUserHashedPassword
// GetUserHashedPassword returns the hashedPassword for the given username if
// found in system.users.
func GetUserHashedPassword(
ctx context.Context, executor *Executor, metrics *MemoryMetrics, username string,
) ([]byte, error) {
normalizedUsername := parser.Name(username).Normalize()
// The root user is not in system.users.
if normalizedUsername == security.RootUser {
return nil, nil
}
var hashedPassword []byte
if err := executor.cfg.DB.Txn(ctx, func(txn *client.Txn) error {
p := makeInternalPlanner("get-pwd", txn, security.RootUser, metrics)
defer finishInternalPlanner(p)
const getHashedPassword = `SELECT hashedPassword FROM system.users ` +
`WHERE username=$1`
values, err := p.queryRow(getHashedPassword, normalizedUsername)
if err != nil {
return errors.Errorf("error looking up user %s", normalizedUsername)
}
if len(values) == 0 {
return errors.Errorf("user %s does not exist", normalizedUsername)
}
hashedPassword = []byte(*(values[0].(*parser.DBytes)))
return nil
}); err != nil {
return nil, err
}
return hashedPassword, nil
}
示例9: quoteNames
// quoteName quotes based on Traditional syntax and adds commas between names.
func quoteNames(names ...string) string {
nameList := make(parser.NameList, len(names))
for i, n := range names {
nameList[i] = parser.Name(n)
}
return parser.AsString(nameList)
}
示例10: extractFunc
// extractFunc returns an aggregateFuncHolder for a given SelectExpr specifying
// an aggregation function.
func (ag *aggregator) extractFunc(
expr AggregatorSpec_Expr, eh *exprHelper,
) (*aggregateFuncHolder, error) {
if expr.Func == AggregatorSpec_IDENT {
fn := ag.newAggregateFuncHolder(parser.NewIdentAggregate)
return fn, nil
}
// In order to reuse the aggregate functions as defined in the parser
// package we are relying on the fact the each name defined in the Func enum
// within the AggregatorSpec matches a SQL function name known to the parser.
// See pkg/sql/parser/aggregate_builtins.go for the aggregate builtins we
// are repurposing.
p := &parser.FuncExpr{
Func: parser.ResolvableFunctionReference{
FunctionReference: parser.UnresolvedName{parser.Name(expr.Func.String())},
},
Exprs: []parser.Expr{eh.vars.IndexedVar(int(expr.ColIdx))},
}
_, err := p.TypeCheck(nil, parser.NoTypePreference)
if err != nil {
return nil, err
}
if agg := p.GetAggregateConstructor(); agg != nil {
fn := ag.newAggregateFuncHolder(agg)
if expr.Distinct {
fn.seen = make(map[string]struct{})
}
return fn, nil
}
return nil, errors.Errorf("unable to get aggregate constructor for %s",
AggregatorSpec_Func_name[int32(expr.Func)])
}
示例11: newReturningHelper
// newReturningHelper creates a new returningHelper for use by an
// insert/update node.
func (p *planner) newReturningHelper(
r parser.ReturningExprs,
desiredTypes []parser.Type,
alias string,
tablecols []sqlbase.ColumnDescriptor,
) (*returningHelper, error) {
rh := &returningHelper{
p: p,
}
if len(r) == 0 {
return rh, nil
}
for _, e := range r {
if err := p.parser.AssertNoAggregationOrWindowing(
e.Expr, "RETURNING", p.session.SearchPath,
); err != nil {
return nil, err
}
}
rh.columns = make(ResultColumns, 0, len(r))
aliasTableName := parser.TableName{TableName: parser.Name(alias)}
rh.source = newSourceInfoForSingleTable(aliasTableName, makeResultColumns(tablecols))
rh.exprs = make([]parser.TypedExpr, 0, len(r))
ivarHelper := parser.MakeIndexedVarHelper(rh, len(tablecols))
for i, target := range r {
// Pre-normalize VarNames at the top level so that checkRenderStar can see stars.
if err := target.NormalizeTopLevelVarName(); err != nil {
return nil, err
}
if isStar, cols, typedExprs, err := checkRenderStar(target, rh.source, ivarHelper); err != nil {
return nil, err
} else if isStar {
rh.exprs = append(rh.exprs, typedExprs...)
rh.columns = append(rh.columns, cols...)
continue
}
// When generating an output column name it should exactly match the original
// expression, so determine the output column name before we perform any
// manipulations to the expression.
outputName := getRenderColName(target)
desired := parser.TypeAny
if len(desiredTypes) > i {
desired = desiredTypes[i]
}
typedExpr, err := rh.p.analyzeExpr(target.Expr, multiSourceInfo{rh.source}, ivarHelper, desired, false, "")
if err != nil {
return nil, err
}
rh.exprs = append(rh.exprs, typedExpr)
rh.columns = append(rh.columns, ResultColumn{Name: outputName, Typ: typedExpr.ResolvedType()})
}
return rh, nil
}
示例12: Format
// Format pretty-prints the orderingInfo to a stream.
// If columns is not nil, column names are printed instead of column indexes.
func (ord orderingInfo) Format(buf *bytes.Buffer, columns ResultColumns) {
sep := ""
// Print the exact match columns. We sort them to ensure
// a deterministic output order.
cols := make([]int, 0, len(ord.exactMatchCols))
for i := range ord.exactMatchCols {
cols = append(cols, i)
}
sort.Ints(cols)
for _, i := range cols {
buf.WriteString(sep)
sep = ","
buf.WriteByte('=')
if columns == nil || i >= len(columns) {
fmt.Fprintf(buf, "%d", i)
} else {
parser.Name(columns[i].Name).Format(buf, parser.FmtSimple)
}
}
// Print the ordering columns and for each their sort order.
for _, o := range ord.ordering {
buf.WriteString(sep)
sep = ","
prefix := byte('+')
if o.Direction == encoding.Descending {
prefix = '-'
}
buf.WriteByte(prefix)
if columns == nil || o.ColIdx >= len(columns) {
fmt.Fprintf(buf, "%d", o.ColIdx)
} else {
parser.Name(columns[o.ColIdx].Name).Format(buf, parser.FmtSimple)
}
}
if ord.unique {
buf.WriteString(sep)
buf.WriteString("unique")
}
}
示例13: indexDefFromDescriptor
// indexDefFromDescriptor creates an index definition (`CREATE INDEX ... ON (...)`) from
// and index descriptor by reconstructing a CreateIndex parser node and calling its
// String method.
func indexDefFromDescriptor(
p *planner,
db *sqlbase.DatabaseDescriptor,
table *sqlbase.TableDescriptor,
index *sqlbase.IndexDescriptor,
) (string, error) {
indexDef := parser.CreateIndex{
Name: parser.Name(index.Name),
Table: parser.NormalizableTableName{
TableNameReference: &parser.TableName{
DatabaseName: parser.Name(db.Name),
TableName: parser.Name(table.Name),
},
},
Unique: index.Unique,
Columns: make(parser.IndexElemList, len(index.ColumnNames)),
Storing: make(parser.NameList, len(index.StoreColumnNames)),
}
for i, name := range index.ColumnNames {
elem := parser.IndexElem{
Column: parser.Name(name),
Direction: parser.Ascending,
}
if index.ColumnDirections[i] == sqlbase.IndexDescriptor_DESC {
elem.Direction = parser.Descending
}
indexDef.Columns[i] = elem
}
for i, name := range index.StoreColumnNames {
indexDef.Storing[i] = parser.Name(name)
}
if len(index.Interleave.Ancestors) > 0 {
intl := index.Interleave
parentTable, err := sqlbase.GetTableDescFromID(p.txn, intl.Ancestors[len(intl.Ancestors)-1].TableID)
if err != nil {
return "", err
}
var sharedPrefixLen int
for _, ancestor := range intl.Ancestors {
sharedPrefixLen += int(ancestor.SharedPrefixLen)
}
fields := index.ColumnNames[:sharedPrefixLen]
intlDef := &parser.InterleaveDef{
Parent: parser.NormalizableTableName{
TableNameReference: &parser.TableName{
TableName: parser.Name(parentTable.Name),
},
},
Fields: make(parser.NameList, len(fields)),
}
for i, field := range fields {
intlDef.Fields[i] = parser.Name(field)
}
indexDef.Interleave = intlDef
}
return indexDef.String(), nil
}
示例14: GetTableSpan
// GetTableSpan gets the key span for a SQL table, including any indices.
func (ie InternalExecutor) GetTableSpan(
user string, txn *client.Txn, dbName, tableName string,
) (roachpb.Span, error) {
// Lookup the table ID.
p := makeInternalPlanner("get-table-span", txn, user, ie.LeaseManager.memMetrics)
defer finishInternalPlanner(p)
p.leaseMgr = ie.LeaseManager
tn := parser.TableName{DatabaseName: parser.Name(dbName), TableName: parser.Name(tableName)}
tableID, err := getTableID(p, &tn)
if err != nil {
return roachpb.Span{}, err
}
// Determine table data span.
tablePrefix := keys.MakeTablePrefix(uint32(tableID))
tableStartKey := roachpb.Key(tablePrefix)
tableEndKey := tableStartKey.PrefixEnd()
return roachpb.Span{Key: tableStartKey, EndKey: tableEndKey}, nil
}
示例15: getGeneratorPlan
func (p *planner) getGeneratorPlan(t *parser.FuncExpr) (planDataSource, error) {
plan, name, err := p.makeGenerator(t)
if err != nil {
return planDataSource{}, err
}
tn := parser.TableName{TableName: parser.Name(name)}
return planDataSource{
info: newSourceInfoForSingleTable(tn, plan.Columns()),
plan: plan,
}, nil
}