drivers

package
v0.0.0-...-03e849d Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: GPL-2.0 Imports: 28 Imported by: 2

Documentation

Index

Constants

View Source
const MARIADB_DRIVER_NAME = "mariadb"
View Source
const MYSQL_DRIVER_NAME = "mysql"
View Source
const POSTGRES_DRIVER_NAME = "postgres"
View Source
const SQLITE3_DRIVER_NAME = "sqlite3"

Variables

View Source
var (
	LOG_SQL_QUERIES   = true
	LOG_SQL_NAMESPACE = "SQL"
)

Functions

func Change

func Change(name string, fn func(driver *Driver))

Change allows you to change a driver's properties within a function.

This is only really useful for testing purposes, where you might want to change the behavior of a driver during tests.

func ContextQueryExec

func ContextQueryExec[T any](ctx context.Context, driver string, query string, args []any, flags QueryFlag, fn func(ctx context.Context, query string, args ...any) (T, error)) (T, error)

func DBToDefaultGoType

func DBToDefaultGoType(dbType dbtype.Type) reflect.Type

DBToDefaultGoType converts a database type to its default Go representation.

This should only be used as a last resort, as it does not take into account the actual type that the value was first defined as.

I.E, if a field is defined as uuid.UUID, it will return a type of dbtype.UUID. This means that the value will be interpreted as UUID, and not as uuid.UUID.

Whenever possible, use TypeFromString to convert a type string to a Go type.

func DBType

func DBType(field attrs.FieldDefinition) (dbType dbtype.Type, ok bool)

DBType returns the database type of the field definition.

It checks if the field implements [CanDBType] and calls its [CanDBType.DBType] method. If it does not implement [CanDBType], it will check the [TYPES] registry to find the database type based on the field's reflect.Type.

func DatabaseError

func DatabaseError(driverOrName any, err error) (errors.DatabaseError, error, bool)

DatabaseError converts a driver error to a errors.DatabaseError and a boolean indicating whether the driver was able to convert the error.

func FieldType

func FieldType(field attrs.FieldDefinition) reflect.Type

FieldType returns the reflect.Type of the field definition.

It does so by calling attrs.FieldDefinition.Type on the field.

If the field is a relation, it will return the related models' primary field reflect.Type

func LogSQL

func LogSQL(ctx context.Context, from string, err error, query string, args ...any) (logged bool)

LogSQL logs the SQL query and its arguments if logging is enabled in the context.

It logs the query with the source of the query (from) and the error if it exists.

If the error is not nil, it logs an error message; otherwise, it logs a debug message.

func LogSQLScope

func LogSQLScope(ctx context.Context, log bool, fn func(context.Context) error) (context.Context, error, bool)

LogSQLScope takes a context, a bool and a function, and returns a new context with SQL logging enabled or disabled based on the bool value.

It returns three values: - A new context with the SQL logging flag set. - A possible error returned by the function. - A boolean indicating whether the SQL query was logged or not.

func Register

func Register(name string, driver Driver)

Register registers a driver with the given database name.

This is used to: - determine the proper returning support for the driver - open a database connection using the registered opener

If your driver is not one of: - github.com/go-django-queries/src/drivers.DriverMariaDB - github.com/go-sql-driver/mysql.MySQLDriver - github.com/mattn/go-sqlite3.SQLiteDriver - github.com/jackc/pgx/v5/stdlib.Driver

Then it explicitly needs to be registered here.

func RegisterGoType

func RegisterGoType(typeName string, typ any)

RegisterGoType registers a Go type with a string identifier.

It stores the full package path and type name of the Go type, including pointer levels, in the registeredTypeConversions map.

After registering, the type can be retrieved using the full type name. This is useful, for example in engine_table.go where we need to convert default values from JSON to Go types.

This function is exposed in case you want to register custom Go types OR for types which have been migrated to another package, but the old type name is still used in migration files.

func SetLogSQLContext

func SetLogSQLContext(ctx context.Context, log bool) context.Context

SetLogSQL sets the logging flag for SQL queries in the context.

It allows you to enable or disable SQL query logging for the current context.

func StringForType

func StringForType(i any) string

func TypeFromString

func TypeFromString(typeString string) (reflect.Type, bool)

Types

type BLOB

type BLOB []byte

func (BLOB) String

func (t BLOB) String() string

type Bool

type Bool bool

func (Bool) String

func (t Bool) String() string

type Bytes

type Bytes []byte

func (Bytes) String

func (t Bytes) String() string

type Char

type Char string

type DB

type DB interface {
	Unwrap() any
	QueryContext(ctx context.Context, query string, args ...any) (SQLRows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) SQLRow
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

This interface is the base for all database interactions.

It is used to specify a unified way to interact with the database, regardless of the underlying driver or database type.

If a transaction was started, the queryset should return the transaction instead of the database connection when calling github.com/Nigel2392/go-django/queries/src.QuerySet.DB.

type Database

type Database interface {
	DB
	Ping(context.Context) error
	Driver() driver.Driver
	Begin(ctx context.Context) (Transaction, error)
	Close() error
}

Database interface represents a database connection.

It is used to interact with the database, execute queries, and manage transactions.

An implementation of this interface can be found in `db_impl_sql.go` for SQL databases, or for Postgress it is in `db_impl_pgx.go`.

func Open

func Open(ctx context.Context, driverName, dsn string, opts ...OpenOption) (Database, error)

Open opens a database connection using the registered opener for the given driver name.

This should always be used instead of directly using sql.Open or pgx.Connect.

func OpenPGX

func OpenPGX(ctx context.Context, drv *Driver, dsn string, opts ...OpenOption) (Database, error)

func OpenSQL

func OpenSQL(driverName string, drv *Driver, dsn string, opts ...OpenOption) (Database, error)

type DateTime

type DateTime time.Time

func CurrentDateTime

func CurrentDateTime() DateTime

func (DateTime) Add

func (t DateTime) Add(d time.Duration) DateTime

func (DateTime) IsZero

func (t DateTime) IsZero() bool

func (DateTime) MarshalJSON

func (t DateTime) MarshalJSON() ([]byte, error)

func (*DateTime) Scan

func (t *DateTime) Scan(value any) error

func (DateTime) String

func (t DateTime) String() string

func (DateTime) Time

func (t DateTime) Time() time.Time

func (*DateTime) UnmarshalJSON

func (t *DateTime) UnmarshalJSON(data []byte) error

func (DateTime) Value

func (t DateTime) Value() (driver.Value, error)

type Driver

type Driver struct {
	Name               string
	SupportsReturning  SupportsReturningType
	Driver             driver.Driver
	Open               func(ctx context.Context, drv *Driver, dsn string, opts ...OpenOption) (Database, error)
	BuildDatabaseError func(err error) errors.DatabaseError
	ExplainQuery       func(ctx context.Context, q DB, query string, args []any) (string, error)
}

func Retrieve

func Retrieve(nameOrType any) (*Driver, bool)

Retrieve retrieves the driver by name or type.

type DriverMariaDB

type DriverMariaDB struct {
	mysql.MySQLDriver
}

func (DriverMariaDB) Open

func (d DriverMariaDB) Open(dsn string) (driver.Conn, error)

func (DriverMariaDB) OpenConnector

func (d DriverMariaDB) OpenConnector(dsn string) (driver.Connector, error)

type DriverMySQL

type DriverMySQL = mysql.MySQLDriver

type DriverPostgres

type DriverPostgres = pg_stdlib.Driver

type DriverSQLite

type DriverSQLite = sqlite3.SQLiteDriver

type Email

type Email mail.Address

func MustParseEmail

func MustParseEmail(addr string) *Email

func (*Email) Scan

func (e *Email) Scan(src interface{}) error

func (Email) String

func (e Email) String() string

func (Email) Value

func (e Email) Value() (driver.Value, error)

type Float

type Float float64

func (Float) String

func (t Float) String() string

type Int

type Int int64

func (Int) String

func (t Int) String() string

type JSON

type JSON[T any] struct {
	Data T
	Null bool
}

func (JSON[T]) DBType

func (t JSON[T]) DBType() dbtype.Type

func (JSON[T]) IsZero

func (t JSON[T]) IsZero() bool

func (JSON[T]) MarshalJSON

func (t JSON[T]) MarshalJSON() ([]byte, error)

func (*JSON[T]) Scan

func (j *JSON[T]) Scan(value any) error

func (*JSON[T]) UnmarshalJSON

func (t *JSON[T]) UnmarshalJSON(data []byte) error

func (JSON[T]) Value

func (t JSON[T]) Value() (driver.Value, error)

type LocalTime

type LocalTime time.Time

func CurrentLocalTime

func CurrentLocalTime() LocalTime

func (LocalTime) Add

func (t LocalTime) Add(d time.Duration) LocalTime

func (LocalTime) IsZero

func (t LocalTime) IsZero() bool

func (LocalTime) MarshalJSON

func (t LocalTime) MarshalJSON() ([]byte, error)

func (*LocalTime) Scan

func (t *LocalTime) Scan(value any) error

func (LocalTime) String

func (t LocalTime) String() string

func (LocalTime) Time

func (t LocalTime) Time() time.Time

func (*LocalTime) UnmarshalJSON

func (t *LocalTime) UnmarshalJSON(data []byte) error

func (LocalTime) Value

func (t LocalTime) Value() (driver.Value, error)

type OpenOption

type OpenOption func(driverName string, db any) error

func PGXOption

func PGXOption(opt func(driverName string, db *pgxpool.Pool) error) OpenOption

func SQLDBOption

func SQLDBOption(opt func(driverName string, db *sql.DB) error) OpenOption

type Query

type Query struct {
	Context   *QueryInformation
	Driver    string
	Flags     QueryFlag
	Query     string
	Args      []any
	Error     error
	Start     time.Time
	TimeTaken time.Duration
}

func (*Query) Explain

func (q *Query) Explain(c context.Context, db DB) (string, error)

type QueryFlag

type QueryFlag int
const (
	Q_UNKNOWN QueryFlag = 0
	Q_QUERY   QueryFlag = 1 << iota
	Q_QUERYROW
	Q_EXEC
	Q_PING
	Q_TSTART
	Q_TCOMMIT
	Q_TROLLBACK
	Q_MULTIPLE
)

func (QueryFlag) String

func (f QueryFlag) String() string

type QueryInformation

type QueryInformation struct {
	Queries []*Query
	Start   time.Time
}

func ContextQueryInfo

func ContextQueryInfo(ctx context.Context) (*QueryInformation, bool)

func ContextWithQueryInfo

func ContextWithQueryInfo(ctx context.Context) (context.Context, *QueryInformation)

func (*QueryInformation) AverageTime

func (q *QueryInformation) AverageTime() time.Duration

func (*QueryInformation) Slowest

func (q *QueryInformation) Slowest() *Query

func (*QueryInformation) TotalExecutionTime

func (q *QueryInformation) TotalExecutionTime() time.Duration

func (*QueryInformation) TotalTime

func (q *QueryInformation) TotalTime() time.Duration

type SQLRow

type SQLRow interface {
	Err() error
	Scan(dest ...any) error
}

SQLRow interface represents a single row result from a database query.

It provides methods to check for errors and scan the row's data into destination variables.

SQLRow is typically returned by methods like QueryRowContext, allowing you to retrieve a single row of data.

type SQLRows

type SQLRows interface {
	SQLRow
	Close() error
	Next() bool
	Columns() ([]string, error)
	NextResultSet() bool
}

SQLRows interface represents a set of rows returned by a database query.

It provides methods to iterate over the rows, retrieve column names, and manage the result set.

It extends the SQLRow interface to include methods for iterating through multiple rows and managing the result set.

SQLRows is typically returned by methods like QueryContext, allowing you to process multiple rows of data.

type String

type String string

type SupportsReturningType

type SupportsReturningType string
const (
	SupportsReturningNone         SupportsReturningType = ""
	SupportsReturningLastInsertId SupportsReturningType = "last_insert_id"
	SupportsReturningColumns      SupportsReturningType = "columns"
)

func SupportsReturning

func SupportsReturning(db interface{ Driver() driver.Driver }) SupportsReturningType

SupportsReturning returns the type of returning supported by the database. It can be one of the following:

- SupportsReturningNone: no returning supported - SupportsReturningLastInsertId: last insert id supported - SupportsReturningColumns: returning columns supported

type Text

type Text string

type Timestamp

type Timestamp time.Time

func CurrentTimestamp

func CurrentTimestamp() Timestamp

func (Timestamp) Add

func (t Timestamp) Add(d time.Duration) Timestamp

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) Scan

func (t *Timestamp) Scan(value any) error

func (Timestamp) String

func (t Timestamp) String() string

func (Timestamp) Time

func (t Timestamp) Time() time.Time

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

func (Timestamp) Value

func (t Timestamp) Value() (driver.Value, error)

type Transaction

type Transaction interface {
	DB

	// Finished returns true if the transaction has been committed or rolled back.
	Finished() bool
	Commit(context.Context) error
	Rollback(context.Context) error
}

Transaction interface represents a database transaction.

It extends the DB interface to include transaction management methods such as Commit and Rollback.

A transaction is a sequence of operations performed in a way that ensures data integrity and consistency. It allows multiple operations to be executed as a single unit of work, which can be committed or rolled back as a whole, depending on whether all operations succeed or if any operation fails.

type ULID

type ULID ulid.ULID

func NewULID

func NewULID() ULID

func (ULID) Bytes

func (id ULID) Bytes() []byte

func (ULID) Compare

func (id ULID) Compare(other ulid.ULID) int

func (ULID) Entropy

func (id ULID) Entropy() []byte

func (ULID) IsZero

func (id ULID) IsZero() bool

func (ULID) MarshalBinary

func (id ULID) MarshalBinary() ([]byte, error)

func (ULID) MarshalBinaryTo

func (id ULID) MarshalBinaryTo(dst []byte) error

func (ULID) MarshalText

func (id ULID) MarshalText() ([]byte, error)

func (ULID) MarshalTextTo

func (id ULID) MarshalTextTo(dst []byte) error

func (*ULID) Scan

func (id *ULID) Scan(src interface{}) error

func (*ULID) SetEntropy

func (id *ULID) SetEntropy(e []byte) error

func (*ULID) SetTime

func (id *ULID) SetTime(ms uint64) error

func (ULID) String

func (id ULID) String() string

func (ULID) Time

func (id ULID) Time() uint64

func (ULID) Timestamp

func (id ULID) Timestamp() time.Time

func (*ULID) UnmarshalBinary

func (id *ULID) UnmarshalBinary(data []byte) error

func (*ULID) UnmarshalText

func (id *ULID) UnmarshalText(v []byte) error

func (ULID) Value

func (id ULID) Value() (driver.Value, error)

type UUID

type UUID uuid.UUID

func NewUUID

func NewUUID() UUID

func (UUID) IsZero

func (t UUID) IsZero() bool

func (UUID) MarshalJSON

func (t UUID) MarshalJSON() ([]byte, error)

func (*UUID) Scan

func (t *UUID) Scan(value any) error

func (UUID) String

func (t UUID) String() string

func (*UUID) UnmarshalJSON

func (t *UUID) UnmarshalJSON(data []byte) error

func (UUID) Value

func (t UUID) Value() (driver.Value, error)

type Uint

type Uint uint64

func (Uint) String

func (t Uint) String() string

type Unwrapper

type Unwrapper interface {
	// Unwrap returns the underlying database connection.
	Unwrap() any
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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