dal

package
v0.15.0-alpha16 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const FromClause string = "From"
View Source
const GroupbyClause string = "GroupBy"
View Source
const HavingClause string = "Having"
View Source
const JoinClause string = "Join"
View Source
const LimitClause string = "Limit"
View Source
const LockClause string = "Lock"
View Source
const OffsetClause string = "Offset"
View Source
const OrderbyClause string = "OrderBy"
View Source
const SelectClause string = "Select"
View Source
const WhereClause string = "Where"

Variables

This section is empty.

Functions

func GetColumnNames

func GetColumnNames(d Dal, dst Tabler, filter func(columnMeta ColumnMeta) bool) (names []string, err errors.Error)

GetColumnNames returns table Column Names in database

func GetPrimarykeyColumnNames

func GetPrimarykeyColumnNames(d Dal, dst Tabler) (names []string, err errors.Error)

GetPrimarykeyColumnNames get returns PrimaryKey Column Names in database

Types

type Clause

type Clause struct {
	Type string
	Data interface{}
}

Clause represents SQL Clause

func From

func From(table interface{}, params ...interface{}) Clause

From creates a new TableClause

func Groupby

func Groupby(expr string) Clause

Groupby creates a new Groupby clause

func Having

func Having(clause string, params ...interface{}) Clause

Having creates a new Having clause

func Join

func Join(clause string, params ...interface{}) Clause

Join creates a new JoinClause

func Limit

func Limit(limit int) Clause

Limit creates a new LimitClause

func Lock added in v0.15.0

func Lock(write bool, nowait bool) Clause

Having creates a new Having clause

func Offset

func Offset(offset int) Clause

Offset creates a new OffsetClause

func Orderby

func Orderby(expr string) Clause

Orderby creates a new Orderby clause

func Select

func Select(clause string, params ...interface{}) Clause

Select creates a new TableClause

func Where

func Where(clause string, params ...interface{}) Clause

Where creates a new WhereClause

type ClauseColumn added in v0.15.0

type ClauseColumn struct {
	Table string
	Name  string
	Alias string
	Raw   bool
}

ClauseColumn quote with name

type ClauseTable added in v0.15.0

type ClauseTable struct {
	Name  string
	Alias string
	Raw   bool
}

ClauseTable quote with name

type ColumnMeta

type ColumnMeta interface {
	Name() string
	DatabaseTypeName() string                 // varchar
	ColumnType() (columnType string, ok bool) // varchar(64)
	PrimaryKey() (isPrimaryKey bool, ok bool)
	AutoIncrement() (isAutoIncrement bool, ok bool)
	Length() (length int64, ok bool)
	DecimalSize() (precision int64, scale int64, ok bool)
	Nullable() (nullable bool, ok bool)
	Unique() (unique bool, ok bool)
	ScanType() reflect.Type
	Comment() (value string, ok bool)
	DefaultValue() (value string, ok bool)
}

ColumnMeta column type interface

func GetPrimarykeyColumns

func GetPrimarykeyColumns(d Dal, dst Tabler) ([]ColumnMeta, errors.Error)

GetPrimarykeyColumns get returns PrimaryKey table Meta in database

type Dal

type Dal interface {
	// AutoMigrate runs auto migration for given entity
	AutoMigrate(entity interface{}, clauses ...Clause) errors.Error
	// AddColumn add column for the table
	AddColumn(table, columnName, columnType string) errors.Error
	// DropColumns drop column from the table
	DropColumns(table string, columnName ...string) errors.Error
	// Exec executes raw sql query
	Exec(query string, params ...interface{}) errors.Error
	// Cursor returns a database cursor, cursor is especially useful when handling big amount of rows of data
	Cursor(clauses ...Clause) (Rows, errors.Error)
	// Fetch loads row data from `cursor` into `dst`
	Fetch(cursor Rows, dst interface{}) errors.Error
	// All loads matched rows from database to `dst`, USE IT WITH CAUTIOUS!!
	All(dst interface{}, clauses ...Clause) errors.Error
	// First loads first matched row from database to `dst`, error will be returned if no records were found
	First(dst interface{}, clauses ...Clause) errors.Error
	// Count matched rows from database
	Count(clauses ...Clause) (int64, errors.Error)
	// Pluck used to query single column
	Pluck(column string, dest interface{}, clauses ...Clause) errors.Error
	// Create insert record to database
	Create(entity interface{}, clauses ...Clause) errors.Error
	// Update updates record
	Update(entity interface{}, clauses ...Clause) errors.Error
	// UpdateColumn allows you to update multiple records
	UpdateColumn(entity interface{}, columnName string, value interface{}, clauses ...Clause) errors.Error
	// UpdateColumns allows you to update multiple columns of multiple records
	UpdateColumns(entity interface{}, set []DalSet, clauses ...Clause) errors.Error
	// UpdateAllColumn updated all Columns of entity
	UpdateAllColumn(entity interface{}, clauses ...Clause) errors.Error
	// CreateOrUpdate tries to create the record, or fallback to update all if failed
	CreateOrUpdate(entity interface{}, clauses ...Clause) errors.Error
	// CreateIfNotExist tries to create the record if not exist
	CreateIfNotExist(entity interface{}, clauses ...Clause) errors.Error
	// Delete records from database
	Delete(entity interface{}, clauses ...Clause) errors.Error
	// AllTables returns all tables in database
	AllTables() ([]string, errors.Error)
	// DropTables drops all specified tables
	DropTables(dst ...interface{}) errors.Error
	// RenameTable renames table name
	RenameTable(oldName, newName string) errors.Error
	// GetColumns returns table columns in database
	GetColumns(dst Tabler, filter func(columnMeta ColumnMeta) bool) (cms []ColumnMeta, err errors.Error)
	// GetPrimaryKeyFields get the PrimaryKey from `gorm` tag
	GetPrimaryKeyFields(t reflect.Type) []reflect.StructField
	// RenameColumn renames column name for specified table
	RenameColumn(table, oldColumnName, newColumnName string) errors.Error
	// DropIndexes drops all specified tables
	DropIndexes(table string, indexes ...string) errors.Error
	// Dialect returns the dialect of current database
	Dialect() string
	// Session creates a new manual session for special scenarios
	Session(config SessionConfig) Dal
	// Begin create a new transaction
	Begin() Transaction
	// IsErrorNotFound returns true if error is record-not-found
	IsErrorNotFound(err errors.Error) bool
	// IsDuplicationError returns true if error is duplicate-error
	IsDuplicationError(err errors.Error) bool
}

Dal aims to facilitate an isolation between DBS and our System by defining a set of operations should a DBS provide

type DalClause

type DalClause struct {
	Expr   string
	Params []interface{}
}

func Expr added in v0.15.0

func Expr(expr string, params ...interface{}) DalClause

type DalSet added in v0.15.0

type DalSet struct {
	ColumnName string
	Value      interface{}
}

type DefaultTabler added in v0.15.0

type DefaultTabler struct {
	Name string
}

Default Table is working for the Tabler interface witch only need TableName

func (DefaultTabler) TableName added in v0.15.0

func (d DefaultTabler) TableName() string

type Rows added in v0.15.0

type Rows interface {
	// Next prepares the next result row for reading with the Scan method. It
	// returns true on success, or false if there is no next result row or an error
	// happened while preparing it. Err should be consulted to distinguish between
	// the two cases.
	//
	// Every call to Scan, even the first one, must be preceded by a call to Next.
	Next() bool

	// Close closes the Rows, preventing further enumeration. If Next is called
	// and returns false and there are no further result sets,
	// the Rows are closed automatically and it will suffice to check the
	// result of Err. Close is idempotent and does not affect the result of Err.
	Close() error

	// Scan copies the columns in the current row into the values pointed at by dest.
	// The number of values in dest must be the same as the number of columns in Rows.
	Scan(dest ...any) error

	// Columns returns the column names.
	// Columns returns an error if the rows are closed.
	Columns() ([]string, error)

	// ColumnTypes returns column information such as column type, length,
	// and nullable. Some information may not be available from some drivers.
	ColumnTypes() ([]*sql.ColumnType, error)
}

type SessionConfig added in v0.15.0

type SessionConfig struct {
	PrepareStmt            bool
	SkipDefaultTransaction bool
}

SessionConfig specify options for the session

type Tabler added in v0.15.0

type Tabler interface {
	TableName() string
}

type Transaction added in v0.15.0

type Transaction interface {
	Dal
	Rollback() errors.Error
	Commit() errors.Error
}

Jump to

Keyboard shortcuts

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