db

package module
v0.0.0-...-98dd6c2 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
const DEFAULT_POSTGRES_PORT int = 5432
View Source
const SQL_DIALECT = "postgres"

Variables

View Source
var ErrDatabaseAlreadyInitialized = fmt.Errorf("Database connection is already initialized")

Functions

func ConstructInsertQuery

func ConstructInsertQuery(ctx context.Context, dialectStr string, req InsertBuilderRequest) (string, []interface{}, error)

func ConstructSelectByIDQuery

func ConstructSelectByIDQuery(ctx context.Context, dialectStr string, req SelectByIDBuilderRequest) (string, []interface{}, error)

ConstructSelectQuery creates a string SQL query with args

func ConstructUpdateQuery

func ConstructUpdateQuery(ctx context.Context, dialectStr string, req UpdateBuilderRequest) (string, []interface{}, error)

ConstructSelectQuery creates a string SQL query with args

func ConstructUpsertQuery

func ConstructUpsertQuery(ctx context.Context, req ConstructUpsertQueryRequest) (string, []interface{}, error)

func GetTestDatabaseNameForService

func GetTestDatabaseNameForService(serviceName string) string

func NewSqlConnection

func NewSqlConnection(ctx context.Context, o Options) (*sql.DB, error)

func PrettyPrint

func PrettyPrint(i interface{}) string

func SqlRowsToUUIDs

func SqlRowsToUUIDs(ctx context.Context, rows *sql.Rows) ([]scalars.ID, error)

func StringsToInterfaces

func StringsToInterfaces(strs []string) []interface{}

func UUIDsToInterfaces

func UUIDsToInterfaces(ids []scalars.ID) []interface{}

Types

type Connection

type Connection struct {
	Dialect string
	DB      *sql.DB
	Tx      *sql.Tx
	NumTxs  int
	DbName  string
}

func NewMockConnection

func NewMockConnection(ctx context.Context, serviceName string) (*Connection, error)

func (*Connection) Begin

func (c *Connection) Begin(ctx context.Context) error

func (*Connection) Close

func (c *Connection) Close(ctx context.Context) error

func (*Connection) Commit

func (c *Connection) Commit(ctx context.Context) error

func (Connection) ExecuteQuery

func (c Connection) ExecuteQuery(ctx context.Context, query string, args ...interface{}) (int64, error)

ExecuteQuery executes an insert or update query, returning the number of rows affected.

func (Connection) GetQueryableConnection

func (c Connection) GetQueryableConnection(ctx context.Context) (QueryableConnection, error)

GetSQLConnection returns a direct sql.DB or sql.Tx object that can be used to run queries

func (*Connection) IsInTransaction

func (c *Connection) IsInTransaction(ctx context.Context) bool

func (*Connection) MustClose

func (c *Connection) MustClose(ctx context.Context)

MustClose is usefull for defer function calls where we can't easily handle err returns

func (*Connection) MustCommit

func (c *Connection) MustCommit(ctx context.Context)

func (*Connection) MustRollback

func (c *Connection) MustRollback(ctx context.Context)

func (Connection) QueryRow

func (c Connection) QueryRow(ctx context.Context, v interface{}, query string, args ...interface{}) error

func (Connection) QueryRows

func (c Connection) QueryRows(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryRows executes a query that returns rows e.g. Select Query

func (*Connection) Rollback

func (c *Connection) Rollback(ctx context.Context) error

type ConnectionProvider

type ConnectionProvider struct {
	Dialect string
	DbName  string
	// DB      *sql.DB
	// Tx      *sql.Tx
	// NumTxs  int
	// UseTransaction bool    // Do we need this extra bool?
	InitOptions Options // Options used to initialize the connection
}

Connection (DB Connection) is a struct that will hold database connection details

func GetExistingConnectionProviderByDatabase

func GetExistingConnectionProviderByDatabase(ctx context.Context, dbname string) (*ConnectionProvider, error)

func NewConnectionProvider

func NewConnectionProvider(ctx context.Context, opt Options) (ConnectionProvider, error)

NewConnectionProvider initializes a totally new database connection provider. It does not check if the connection is already initialized. This should ideally not be used directly, but through NewOrExistingConnectionProvider (which reuses existing providers).

func NewOrExistingConnectionProvider

func NewOrExistingConnectionProvider(ctx context.Context, o Options) (*ConnectionProvider, error)

NewOrExistingConnectionProvider initializes a new database connection, and also saves it in the global map of connections.

func (*ConnectionProvider) GetConnection

func (c *ConnectionProvider) GetConnection(ctx context.Context) (*Connection, error)

type ConstructUpsertQueryRequest

type ConstructUpsertQueryRequest struct {
	Dialect      string
	TableName    string
	UpsertColumn string // The column that is the primary key, used to check if the row exists
	ColumnNames  []string
	Values       []interface{}
}

type InsertBuilderRequest

type InsertBuilderRequest struct {
	TableName   string
	ColumnNames []string
	Values      [][]interface{}
}

type InsertTypeParams

type InsertTypeParams struct {
	TableName string
}

type ListTypeByIDsParams

type ListTypeByIDsParams struct {
	TableName string
	IDColumn  string
	IDs       []scalars.ID
}

type Options

type Options struct {
	Database string
	ServerOptions
}

type QueryableConnection

type QueryableConnection interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

QueryableConnection groups together a sql.Connection, sq.DB (with pool handling) and a Transaction

type SelectByIDBuilderRequest

type SelectByIDBuilderRequest struct {
	TableName string
	Columns   []string
	// For where condition, so we only update the required row
	IDColumn string
	IDs      []scalars.ID
	OrderBy  []SelectOrderBy
}

type SelectOrderBy

type SelectOrderBy struct {
	Column string
	Order  string // ASC or DESC
}

type SelectQueryBuilder

type SelectQueryBuilder struct {
	Withs  []With
	Select *goqu.SelectDataset
}

func (SelectQueryBuilder) ToGoquDataset

func (b SelectQueryBuilder) ToGoquDataset() *goqu.SelectDataset

func (SelectQueryBuilder) ToSQL

func (b SelectQueryBuilder) ToSQL() (string, []interface{}, error)

type ServerOptions

type ServerOptions struct {
	Host     string
	Port     int
	User     string
	Password string
	SSLMode  string
}

ServerOptions are the options required to initialize a connection for a service. The database name is not required since it's the same as the service name. This is used for the generated service code to initialize the database connection for a service.

type UniqueIDsQueryBuilderParams

type UniqueIDsQueryBuilderParams struct {
	TableName     string
	SelectColumns []string
}

type UpdateBuilderRequest

type UpdateBuilderRequest struct {
	TableName string
	Columns   []string
	Values    []interface{}
	// For where condition, so we only update the required row
	IdentifierColumn string
	IdentifierValue  interface{}
}

type With

type With struct {
	Alias  string
	Select *goqu.SelectDataset
}

Jump to

Keyboard shortcuts

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