github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/testutils/sqlutils/table_id.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sqlutils
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  )
    17  
    18  // QueryDatabaseID returns the database ID of the specified database using the
    19  // system.namespace table.
    20  func QueryDatabaseID(t testing.TB, sqlDB DBHandle, dbName string) uint32 {
    21  	dbIDQuery := `
    22  		SELECT id FROM system.namespace
    23  		WHERE name = $1 AND "parentSchemaID" = 0 AND "parentID" = 0
    24  	`
    25  	var dbID uint32
    26  	result := sqlDB.QueryRowContext(context.Background(), dbIDQuery, dbName)
    27  	if err := result.Scan(&dbID); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	return dbID
    31  }
    32  
    33  // QueryTableID returns the table ID of the specified database.table
    34  // using the system.namespace table.
    35  func QueryTableID(
    36  	t testing.TB, sqlDB DBHandle, dbName, schemaName string, tableName string,
    37  ) uint32 {
    38  	tableIDQuery := `
    39   SELECT tables.id FROM system.namespace tables
    40     JOIN system.namespace dbs ON dbs.id = tables."parentID"
    41  	 JOIN system.namespace schemas ON schemas.id = tables."parentSchemaID"
    42     WHERE dbs.name = $1 AND schemas.name = $2 AND tables.name = $3
    43   `
    44  	var tableID uint32
    45  	result := sqlDB.QueryRowContext(
    46  		context.Background(),
    47  		tableIDQuery, dbName,
    48  		schemaName,
    49  		tableName,
    50  	)
    51  	if err := result.Scan(&tableID); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	return tableID
    55  }