db_client

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2024 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DriverPostgres = "pgx"
	DriverMySQL    = "mysql"
	DriverDuckDB   = "duckdb"
	DriverSQLite   = "sqlite3"
	DriverUnknown  = "_unknown_"
)

Variables

This section is empty.

Functions

func ExecuteQuery

func ExecuteQuery(ctx context.Context, client DbClient, queryString string, args ...any) (*queryresult.ResultStreamer, error)

ExecuteQuery executes a single query. If shutdownAfterCompletion is true, shutdown the client after completion

func GetCloudWorkspaceConnectionString added in v1.0.0

func GetCloudWorkspaceConnectionString(workspace string) (string, error)

func GetDatabaseConfigForResource

func GetDatabaseConfigForResource(resource modconfig.ModTreeItem, workspaceMod *modconfig.Mod, defaultDatabase string, defaultSearchPathConfig backend.SearchPathConfig) (string, backend.SearchPathConfig, error)

func GetDefaultDatabaseConfig

func GetDefaultDatabaseConfig(opts ...backend.ConnectOption) (string, backend.SearchPathConfig, error)

GetDefaultDatabaseConfig builds the default database and searchPathConfig NOTE: if the dashboardUI has overridden the search path, opts wil be passed in to set the overridden value

func MaxDbConnections

func MaxDbConnections() int

func PgEscapeName

func PgEscapeName(name string) string

PgEscapeName escapes strings which will be usaed for Podsdtgres object identifiers (table names, column names, schema names)

func PgEscapeSearchPath

func PgEscapeSearchPath(searchPath []string) []string

PgEscapeSearchPath applies postgres escaping to search path and remove whitespace

func PgEscapeString

func PgEscapeString(str string) string

PgEscapeString escapes strings which are to be inserted use a custom escape tag to avoid chance of clash with the escaped text https://medium.com/@lnishada/postgres-dollar-quoting-6d23e4f186ec

Types

type ClientMap

type ClientMap struct {
	// contains filtered or unexported fields
}

func NewClientMap

func NewClientMap() *ClientMap

func (*ClientMap) Add

func (e *ClientMap) Add(client *DbClient, searchPathConfig backend.SearchPathConfig) *ClientMap

func (*ClientMap) Close

func (e *ClientMap) Close(ctx context.Context) error

func (*ClientMap) Get

func (e *ClientMap) Get(connectionString string, searchPathConfig backend.SearchPathConfig) *DbClient

Get returns an existing db client for the given connection string if no client is stored for the string, it returns null

func (*ClientMap) GetOrCreate

func (e *ClientMap) GetOrCreate(ctx context.Context, connectionString string, searchPathConfig backend.SearchPathConfig) (*DbClient, error)

GetOrCreate returns a db client for the given connection string if clients map already contains a client for this connection string, use that otherwise create a new client and add to the map

type ClientMapOption

type ClientMapOption func(*ClientMap)

type DbClient

type DbClient struct {

	// the Backend
	Backend backend.Backend
	// contains filtered or unexported fields
}

DbClient wraps over `sql.DB` and gives an interface to the database

func NewDbClient

func NewDbClient(ctx context.Context, connectionString string, opts ...backend.ConnectOption) (_ *DbClient, err error)

func (*DbClient) Close

func (c *DbClient) Close(context.Context) error

Close closes the connection to the database and shuts down the Backend

func (*DbClient) Execute

func (c *DbClient) Execute(ctx context.Context, query string, args ...any) (*localqueryresult.Result, error)

Execute executes the query in the given Context NOTE: The returned Result MUST be fully read - otherwise the connection will block and will prevent further communication

func (*DbClient) ExecuteSync

func (c *DbClient) ExecuteSync(ctx context.Context, query string, args ...any) (*queryresult.SyncQueryResult, error)

ExecuteSync executes a query against this client and wait for the result

func (*DbClient) GetConnectionString

func (c *DbClient) GetConnectionString() string

func (*DbClient) StartQuery

func (c *DbClient) StartQuery(ctx context.Context, dbConn *sql.Conn, query string, args ...any) (rows *sql.Rows, err error)

StartQuery runs query in a goroutine, so we can check for cancellation in case the client becomes unresponsive and does not respect context cancellation

type PgxConnector

type PgxConnector struct {
	driver.Connector
	AfterConnectFunc func(context.Context, driver.Conn) error
}

func NewPgxConnector

func NewPgxConnector(dataSourceName string, afterConnectFunc func(context.Context, driver.Conn) error) (*PgxConnector, error)

func (*PgxConnector) Connect

func (c *PgxConnector) Connect(ctx context.Context) (driver.Conn, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL