package database

Import Path
	golang.org/x/pkgsite/internal/database (on go.dev)

Dependency Relation
	imports 20 packages, and imported by 2 packages

Involved Source Files copy.go Package database adds some useful functionality to a sql.DB. It is independent of the database driver and the DB schema. driver.go logging.go reflect.go
Package-Level Type Names (total 10, in which 2 are exported)
/* sort exporteds by: | */
DB wraps a sql.DB. The methods it exports correspond closely to those of sql.DB. They enhance the original by requiring a context argument, and by logging the query and any resulting errors. A DB may represent a transaction. If so, its execution and query methods operate within the transaction. BulkInsert constructs and executes a multi-value insert statement. The query is constructed using the format: INSERT INTO <table> (<columns>) VALUES (<placeholders-for-each-item-in-values>) If conflictAction is not empty, it is appended to the statement. The query is executed using a PREPARE statement with the provided values. BulkInsertReturning is like BulkInsert, but supports returning values from the INSERT statement. In addition to the arguments of BulkInsert, it takes a list of columns to return and a function to scan those columns. To get the returned values, provide a function that scans them as if they were the selected columns of a query. See TestBulkInsert for an example. BulkUpdate executes multiple UPDATE statements in a transaction. Columns must contain the names of some of table's columns. The first is treated as a key; that is, the values to update are matched with existing rows by comparing the values of the first column. Types holds the database type of each column. For example, []string{"INT", "TEXT"} Values contains one slice of values per column. (Note that this is unlike BulkInsert, which takes a single slice of interleaved values.) BulkUpsert is like BulkInsert, but instead of a conflict action, a list of conflicting columns is provided. An "ON CONFLICT (conflict_columns) DO UPDATE" clause is added to the statement, with assignments "c=excluded.c" for every column c. BulkUpsertReturning is like BulkInsertReturning, but performs an upsert like BulkUpsert. Close closes the database connection. CollectStructs scans the the rows from the query into structs and appends them to pslice, which must be a pointer to a slice of structs. Example: type Player struct { Name string; Score int } var players []Player err := db.CollectStructs(ctx, &players, "SELECT name, score FROM players") CopyUpsert upserts rows into table using the pgx driver's CopyFrom method. It returns an error if the underlying driver is not pgx. columns is the list of columns to upsert. src is the source of the rows to upsert. conflictColumns are the columns that might conflict (i.e. that have a UNIQUE constraint). If dropColumn is non-empty, that column will be dropped from the temporary table before copying. Use dropColumn for generated ID columns. CopyUpsert works by first creating a temporary table, populating it with CopyFrom, and then running an INSERT...SELECT...ON CONFLICT to upsert its rows into the original table. Exec executes a SQL statement and returns the number of rows it affected. (*T) InTransaction() bool (*T) IsRetryable() bool MaxRetries returns the maximum number of times thata serializable transaction was retried. (*T) Ping() error (*T) Prepare(ctx context.Context, query string) (*sql.Stmt, error) Query runs the DB query. QueryRow runs the query and returns a single row. RunQuery executes query, then calls f on each row. Transact executes the given function in the context of a SQL transaction at the given isolation level, rolling back the transaction if the function panics or returns an error. The given function is called with a DB that is associated with a transaction. The DB should be used only inside the function; if it is used to access the database after the function returns, the calls will return errors. If the isolation level requires it, Transact will retry the transaction upon serialization failure, so txFunc may be called more than once. (*T) WithPGXConn(f func(conn *pgx.Conn) error) error *T : io.Closer func New(db *sql.DB, instanceID string) *DB func Open(driverName, dbinfo, instanceID string) (_ *DB, err error) func golang.org/x/pkgsite/internal/postgres.(*DB).Underlying() *DB func golang.org/x/pkgsite/internal/postgres.GetPathID(ctx context.Context, ddb *DB, path string) (id int, err error) func golang.org/x/pkgsite/internal/postgres.GetSymbolHistoryForBuildContext(ctx context.Context, ddb *DB, pathID int, modulePath string, bc internal.BuildContext) (_ map[string]string, err error) func golang.org/x/pkgsite/internal/postgres.GetSymbolHistoryFromTable(ctx context.Context, ddb *DB, packagePath, modulePath string) (_ *internal.SymbolHistory, err error) func golang.org/x/pkgsite/internal/postgres.GetSymbolHistoryWithPackageSymbols(ctx context.Context, ddb *DB, packagePath, modulePath string) (_ *internal.SymbolHistory, err error) func golang.org/x/pkgsite/internal/postgres.New(db *DB) *postgres.DB func golang.org/x/pkgsite/internal/postgres.NewBypassingLicenseCheck(db *DB) *postgres.DB func golang.org/x/pkgsite/internal/postgres.UpsertSearchDocument(ctx context.Context, ddb *DB, args postgres.UpsertSearchDocumentArgs) (err error)
A RowItem is a row of values or an error. Err error Values []interface{} func CopyFromChan(c <-chan RowItem) pgx.CopyFromSource
Package-Level Functions (total 17, in which 7 are exported)
CopyFromChan returns a CopyFromSource that gets its rows from a channel.
New creates a new DB from a sql.DB.
NullIsEmpty returns a sql.Scanner that writes the empty string to s if the sql.Value is NULL.
NullPtr is for scanning nullable database columns into pointer variables or fields. When given a pointer to to a pointer to some type T, it returns a value that can be passed to a Scan function. If the corresponding column is nil, the variable will be set to nil. Otherwise, it will be set to a newly allocated pointer to the column value.
Open creates a new DB for the given connection string.
RegisterOCWrapper registers a driver that wraps the OpenCensus driver, which in turn wraps the driver named as the first argument.
StructScanner takes a struct and returns a function that, when called on a struct pointer of that type, returns a slice of arguments suitable for Row.Scan or Rows.Scan. The call to either Scan will populate the exported fields of the struct in the order they appear in the type definition. StructScanner panics if p is not a struct or a pointer to a struct. The function it returns will panic if its argument is not a pointer to a struct. Example: type Player struct { Name string; Score int } playerScanArgs := database.StructScanner(Player{}) err := db.RunQuery(ctx, "SELECT name, score FROM players", func(rows *sql.Rows) error { var p Player if err := rows.Scan(playerScanArgs(&p)...); err != nil { return err } // use p return nil })
Package-Level Variables (total 4, in which 1 are exported)
QueryLoggingDisabled stops logging of queries when true. For use in tests only: not concurrency-safe.
Package-Level Constants (total 2, in which 1 are exported)
const OnConflictDoNothing = "ON CONFLICT DO NOTHING"