model

package
v0.8.4-b1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	Table string
	Name  string
}

Attribute represents a column in a table.

type AttributeMigrate

type AttributeMigrate struct {
	Nullable     bool
	FieldName    string
	Name         string
	EscapingName string
	DataType     string
	Default      string
}

AttributeMigrate represents a column to be created during migration.

type Config

type Config interface {
	Name() string
	GetDatabaseConfig() *DatabaseConfig
}

Config represents a database configuration.

type Connection

type Connection interface {
	ExecContext(ctx context.Context, query *Query) error
	QueryRowContext(ctx context.Context, query *Query) Row
	QueryContext(ctx context.Context, query *Query) (Rows, error)
}

Connection represents a database connection.

type DatabaseConfig

type DatabaseConfig struct {
	Logger           Logger
	IncludeArguments bool          // include all arguments used on query
	QueryThreshold   time.Duration // query threshold to warning on slow queries
	// contains filtered or unexported fields
}

DatabaseConfig contains database configuration including logging and error handling settings.

func (*DatabaseConfig) AddSchema

func (c *DatabaseConfig) AddSchema(s string)

AddSchema adds a schema to the list of schemas for the database.

func (*DatabaseConfig) ErrorHandler

func (c *DatabaseConfig) ErrorHandler(ctx context.Context, err error) error

ErrorHandler logs the database error using the configured logger.

func (*DatabaseConfig) ErrorQueryHandler

func (c *DatabaseConfig) ErrorQueryHandler(ctx context.Context, query Query) error

ErrorQueryHandler logs the query error using the configured logger.

func (*DatabaseConfig) InfoHandler

func (c *DatabaseConfig) InfoHandler(ctx context.Context, query Query)

InfoHandler logs the query information using the configured logger.

func (*DatabaseConfig) Init

func (c *DatabaseConfig) Init(driverName string, errorTranslator func(err error) error)

Init initializes the database configuration with the given driver name and error translator.

func (*DatabaseConfig) InitCallback

func (c *DatabaseConfig) InitCallback() func() error

InitCallback returns the initialization callback function for the database.

func (*DatabaseConfig) Schemas

func (c *DatabaseConfig) Schemas() []string

Schemas returns the list of schemas configured for the database.

func (*DatabaseConfig) SetInitCallback

func (c *DatabaseConfig) SetInitCallback(f func() error)

SetInitCallback sets the initialization callback function for the database.

func (*DatabaseConfig) SetSchemas

func (c *DatabaseConfig) SetSchemas(s []string)

SetSchemas sets the list of schemas for the database.

type Driver

type Driver interface {
	MigrateContext(context.Context, *Migrator) error // MigrateContext migrates the database schema.

	DropTable(schema, table string) error                        // DropTable drops a table from the database schema.
	DropColumn(schema, table, column string) error               // DropColumn drops a column from a table in the database schema.
	RenameColumn(schema, table, oldColumn, newName string) error // RenameColumn renames a column in a table in the database schema.
	RenameTable(schema, table, newName string) error             // RenameTable renames a table in the database schema.

	AddLogger(Logger, error) error               // AddLogger adds a logger to the database driver.
	ErrorTranslator() func(err error) error      // ErrorTranslator translates database errors.
	KeywordHandler(string) string                // KeywordHandler handles SQL keywords in the database driver.
	FormatTableName(schema, table string) string // FormatTableName formats the table name in the database driver.
	SupportsReturning() bool                     // SupportsReturning checks if the database driver supports RETURNING clause.

	NewConnection() Connection                                                    // NewConnection creates a new database connection.
	NewTransaction(ctx context.Context, opts *sql.TxOptions) (Transaction, error) // NewTransaction creates a new database transaction.

	Init() error        // Init initializes the database schema.
	Close() error       // Close closes the database driver.
	Stats() sql.DBStats // Stats returns the database statistics.
	Config              // Config represents a database configuration.
}

Driver represents a database driver.

type IndexMigrate

type IndexMigrate struct {
	Name         string
	EscapingName string
	Unique       bool
	Func         string
	Attributes   []AttributeMigrate
}

IndexMigrate represents an index to be created during migration.

type JoinType

type JoinType string

JoinType represents the type of a JOIN clause in a SQL query.

const (
	Join      JoinType = "JOIN"       // JOIN or OUTER JOIN clause
	LeftJoin  JoinType = "LEFT JOIN"  // LEFT JOIN clause
	RightJoin JoinType = "RIGHT JOIN" // RIGHT JOIN clause
	InnerJoin JoinType = "INNER JOIN" // INNER JOIN clause
)

type Logger

type Logger interface {
	InfoContext(ctx context.Context, msg string, kv ...any)
	WarnContext(ctx context.Context, msg string, kv ...any)
	ErrorContext(ctx context.Context, msg string, kv ...any)
}

Logger represents a logger for database operations.

type ManyToSomeMigrate

type ManyToSomeMigrate struct {
	TargetTable          string
	TargetColumn         string
	EscapingTargetTable  string
	EscapingTargetColumn string
	TargetSchema         *string
	AttributeMigrate
}

ManyToSomeMigrate represents a many-to-one or many-to-many relationship for migration.

func (ManyToSomeMigrate) EscapingTargetTableName

func (m ManyToSomeMigrate) EscapingTargetTableName() string

EscapingTargetTableName returns the target table and the schema.

type Migrator

type Migrator struct {
	Tables  map[string]*TableMigrate
	Schemas []string
	Error   error
}

Migrator represents a database migrator with its tables and schemas.

type OneToSomeMigrate

type OneToSomeMigrate struct {
	IsOneToMany          bool
	TargetTable          string
	TargetColumn         string
	EscapingTargetTable  string
	EscapingTargetColumn string
	TargetSchema         *string
	AttributeMigrate
}

OneToSomeMigrate represents a one-to-one or one-to-many relationship for migration.

func (OneToSomeMigrate) EscapingTargetTableName

func (o OneToSomeMigrate) EscapingTargetTableName() string

EscapingTargetTableName returns the target table and the schema.

type PrimaryKeyMigrate

type PrimaryKeyMigrate struct {
	AutoIncrement bool
	AttributeMigrate
}

PrimaryKeyMigrate represents a primary key column with auto-increment flag.

type Query

type Query struct {
	RawSql        string
	Arguments     []any
	QueryDuration time.Duration
	Err           error
}

Query represents a SQL query with its arguments and duration.

func CreateQuery

func CreateQuery(rawSql string, args []any) Query

CreateQuery creates a new Query with the given raw SQL and arguments.

type QueryType

type QueryType uint

QueryType represents the type of a SQL query.

const (
	SelectQuery     QueryType // SELECT query
	SelectJoinQuery           // SELECT query with JOIN clause
	InsertQuery               // INSERT query
	InsertAllQuery            // INSERT ALL query
	UpdateQuery               // UPDATE query
	UpdateJoinQuery           // UPDATE query with JOIN clause
	DeleteQuery               // DELETE query
	RawQuery                  // Raw SQL query
)

type Row

type Row interface {
	Scan(dest ...any) error
}

Row represents a single row of a SQL query result.

type Rows

type Rows interface {
	Close() error
	Next() bool
	Row
}

Rows represents a result set of a SQL query.

type SavePoint

type SavePoint interface {
	Commit() error
	Rollback() error
}

SavePoint represents a savepoint in a database transaction.

type Table

type Table struct {
	Schema *string
	Name   string
}

Table represents a database table with its schema and name.

func (Table) String

func (t Table) String() string

String returns the table name with schema if available.

type TableMigrate

type TableMigrate struct {
	Name         string
	EscapingName string
	Schema       *string
	Migrated     bool
	PrimaryKeys  []PrimaryKeyMigrate
	Indexes      []IndexMigrate
	Attributes   []AttributeMigrate
	ManyToSomes  []ManyToSomeMigrate
	OneToSomes   []OneToSomeMigrate
}

TableMigrate represents a table to be migrated with its columns, indexes, and relationships.

func (TableMigrate) EscapingTableName

func (t TableMigrate) EscapingTableName() string

EscapingTableName returns the table and the schema.

type Transaction

type Transaction interface {
	Connection
	Commit() error
	Rollback() error
	SavePoint() (SavePoint, error)
}

Transaction represents a database transaction.

Jump to

Keyboard shortcuts

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