database

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: AGPL-3.0 Imports: 9 Imported by: 14

Documentation

Overview

Package database contains functions to interact with the database.

The package is imported like this:

import "github.com/gouniverse/base/database"

Index

Constants

View Source
const DATABASE_TYPE_MSSQL = "mssql"
View Source
const DATABASE_TYPE_MYSQL = "mysql"
View Source
const DATABASE_TYPE_POSTGRES = "postgres"
View Source
const DATABASE_TYPE_SQLITE = "sqlite"

Variables

This section is empty.

Functions

func DatabaseType added in v0.0.2

func DatabaseType(q QueryableInterface) string

DatabaseType finds the driver name from database

It returns the type of the database in the following way:

  • "mysql" for MySQL
  • "postgres" for PostgreSQL
  • "sqlite" for SQLite
  • "mssql" for Microsoft SQL Server
  • the full name of the driver otherwise

The function is useful when you want to find the type of the database, without knowing it during compilation.

Parameters: - db *sql.DB: the database connection

Returns: - string: the type of the database

#nosec G103 - we use unsafe deliberately to get private fields of sql.Tx and sql.Conn

func Execute added in v0.0.6

func Execute(ctx QueryableContext, sqlStr string, args ...any) (sql.Result, error)

func IsQueryableContext added in v0.0.6

func IsQueryableContext(ctx context.Context) bool

IsQueryableContext checks if the given context is a QueryableContext.

Parameters: - ctx: The context to check.

Returns: - bool: True if the context is a QueryableContext, false otherwise.

func Open

func Open(options openOptionsInterface) (*sql.DB, error)

Open opens the database

Note:

  • drivers are not included to this package to prevent size bloat
  • you must add only the required database driver

Drivers: - sqlite add the following includes: ``` _ "modernc.org/sqlite" ``` - mysql add the following includes: ``` _ "github.com/go-sql-driver/mysql" ``` - postgres add the following includes: ``` _ "github.com/lib/pq" ```

Business logic:

  • opens the database based on the driver name
  • each driver has its own set of parameters

Parameters: - options openOptionsInterface

Returns: - *sql.DB: the database connection - error: the error if any

func Options

func Options() openOptionsInterface

func Query added in v0.0.6

func Query(ctx QueryableContext, sqlStr string, args ...any) (*sql.Rows, error)

func SelectToMapAny added in v0.0.3

func SelectToMapAny(ctx QueryableContext, sqlStr string, args ...any) ([]map[string]any, error)

func SelectToMapString added in v0.0.3

func SelectToMapString(ctx QueryableContext, sqlStr string, args ...any) ([]map[string]string, error)

Types

type QueryableContext added in v0.0.6

type QueryableContext struct {
	context.Context
	// contains filtered or unexported fields
}

QueryableContext extends the context.Context interface with a queryable field. The queryable field may be of type *sql.DB, *sql.Conn, or *sql.Tx.

func Context added in v0.0.6

func Context(ctx context.Context, queryable QueryableInterface) QueryableContext

Context returns a new context with the given QueryableInterface. It is a shortcut for NewQueryableContext.

Example:

ctx := database.Context(context.Background(), tx)

Parameters: - ctx: The parent context. - queryable: The QueryableInterface to be associated with the context.

Returns: - QueryableContext: A new context with the given QueryableInterface.

func NewQueryableContext added in v0.0.6

func NewQueryableContext(ctx context.Context, queryable QueryableInterface) QueryableContext

NewQueryableContext returns a new context with the given QueryableInterface.

func (QueryableContext) IsConn added in v0.0.7

func (ctx QueryableContext) IsConn() bool

func (QueryableContext) IsDB added in v0.0.7

func (ctx QueryableContext) IsDB() bool

func (QueryableContext) IsTx added in v0.0.7

func (ctx QueryableContext) IsTx() bool

func (QueryableContext) Queryable added in v0.0.7

func (ctx QueryableContext) Queryable() QueryableInterface

type QueryableInterface added in v0.0.6

type QueryableInterface interface {
	// ExecContext executes a SQL query in the given context.
	// It returns a sql.Result object containing information about the execution,
	// or an error if the query failed.
	//
	// The context is used to control the execution of the query, allowing for
	// cancellation and timeout control.
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	// PrepareContext creates a prepared statement for use within a transaction.
	//
	// The returned statement operates within the transaction and will be closed
	// when the transaction has been committed or rolled back.
	//
	// To use an existing prepared statement on this transaction, see [Tx.Stmt].
	//
	// The provided context will be used for the preparation of the context, not
	// for the execution of the returned statement. The returned statement
	// will run in the transaction context.
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

	// QueryContext executes a SQL query in the given context and returns a
	// *sql.Rows object containing the query results.
	//
	// The context is used to control the execution of the query, allowing for
	// cancellation and timeout control.
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

	// QueryRowContext executes a SQL query in the given context and returns a
	// *sql.Row object containing a single row of results.
	//
	// The context is used to control the execution of the query, allowing for
	// cancellation and timeout control.
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

Queryable is an interface that defines a set of methods for executing SQL queries on a database or data source.

It can be one of the following: - *sql.DB - *sql.Conn - *sql.Tx

Implementations of this interface provide a way to execute queries in a context, allowing for cancellation and timeout control.

Jump to

Keyboard shortcuts

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