Documentation
¶
Index ¶
- Variables
- func AutoMigrate(dbTarget any) error
- func AutoMigrateContext(ctx context.Context, dbTarget any) error
- func Close(dbTarget any) error
- func Create[T any](table *T) *create[T]
- func CreateContext[T any](ctx context.Context, table *T) *create[T]
- func Delete[T any](table *T) *stateDelete
- func DeleteContext[T any](ctx context.Context, table *T) *stateDelete
- func DropColumn(dbTarget any, table, column string) error
- func DropTable(dbTarget any, table string) error
- func Find[T any](table *T) *find[T]
- func FindContext[T any](ctx context.Context, table *T) *find[T]
- func Insert[T any](table *T) *stateInsert[T]
- func InsertContext[T any](ctx context.Context, table *T) *stateInsert[T]
- func List[T any](table *T) *list[T]
- func ListContext[T any](ctx context.Context, table *T) *list[T]
- func Open[T any](driver Driver) (*T, error)
- func Remove[T any](table *T) *remove[T]
- func RemoveContext[T any](ctx context.Context, table *T) *remove[T]
- func RenameColumn(dbTarget any, table, oldColumn, newColumn string) error
- func Save[T any](table *T) *save[T]
- func SaveContext[T any](ctx context.Context, table *T) *save[T]
- func Select[T any](table *T) *stateSelect[T]
- func SelectContext[T any](ctx context.Context, table *T) *stateSelect[T]
- func Update[T any](table *T) *stateUpdate[T]
- func UpdateContext[T any](ctx context.Context, table *T) *stateUpdate[T]
- type AttributeMigrate
- type Config
- type Connection
- type DB
- func (db *DB) Log(b bool)
- func (db *DB) Name() string
- func (db *DB) NewTransaction() (Transaction, error)
- func (db *DB) NewTransactionContext(ctx context.Context, isolation sql.IsolationLevel) (Transaction, error)
- func (db *DB) RawExecContext(ctx context.Context, query string, args ...any) error
- func (db *DB) RawQueryContext(ctx context.Context, query string, args ...any) (Rows, error)
- func (db *DB) Stats() sql.DBStats
- type Driver
- type IndexMigrate
- type ManyToOneMigrate
- type Migrator
- type OneToOneMigrate
- type Pagination
- type PrimaryKeyMigrate
- type Row
- type Rows
- type TableMigrate
- type Transaction
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("goe: not found any element on result set")
Functions ¶
func AutoMigrate ¶
func Create ¶
func Create[T any](table *T) *create[T]
Create is a wrapper over Insert for simple insert one record and return the inserted record with the new id.
Create uses context.Background internally; to specify the context, use CreateContext.
Examples ¶
// create animal
insertedAnimal, err = goe.Create(db.Animal).ByValue(Animal{})
func CreateContext ¶
func Delete ¶
func Delete[T any](table *T) *stateDelete
Delete remove records in the given table
Delete uses context.Background internally; to specify the context, use DeleteContext.
Examples ¶
// delete all records err = goe.Delete(db.UserRole).Wheres() // delete one record err = goe.Delete(db.Animal).Wheres(where.Equals(&db.Animal.Id, 2))
func DropColumn ¶
func Find ¶
func Find[T any](table *T) *find[T]
Find returns a matched record, if non record is found returns a ErrNotFound.
Find uses context.Background internally; to specify the context, use FindContext.
Example ¶
goe.Find(db.Animal).ById(Animal{Id: 2})
func FindContext ¶
FindContext returns a matched, if non record is found returns a ErrNotFound.
See Find for examples
func Insert ¶
func Insert[T any](table *T) *stateInsert[T]
Insert inserts a new record into the given table.
Insert uses context.Background internally; to specify the context, use InsertContext.
Examples ¶
// insert one record
err = goe.Insert(db.Person).One(&Person{Name: "Jhon"})
// insert a list of records
persons := []Person{{Name: "Jhon"}, {Name: "Mary"}}
err = goe.Insert(db.Person).All(persons)
func InsertContext ¶
InsertContext inserts a new record into the given table.
See Insert for examples.
func List ¶
func List[T any](table *T) *list[T]
List is a wrapper over Select for more simple queries using filters, pagination and ordering.
List uses context.Background internally; to specify the context, use ListContext
Example ¶
// where animals.name LIKE $1
// on LIKE Filter goe uses ToUpper to match all results
goe.List(db.Animal).OrderByDesc(&db.Animal.Name).Filter(Animal{Name: "%Cat%"}).AsSlice()
// where animals.name equals $1 AND animal.id = $2 AND animals.idhabitat = $3
goe.List(db.Animal).OrderByAsc(&db.Animal.Name).Filter(Animal{Name: "Cat", Id: animals[0].Id, IdHabitat: &habitats[0].Id}).AsSlice()
// pagination list
var p *goe.Pagination[Animal]
p, err = goe.List(db.Animal).AsPagination(1, 10)
func ListContext ¶
ListContext is a wrapper over Select for more simple queries using filters, pagination and ordering.
See List for examples.
func Open ¶
Open opens a database connection
Example ¶
goe.Open[Database](postgres.Open("user=postgres password=postgres host=localhost port=5432 database=postgres", postgres.Config{}))
func Remove ¶
func Remove[T any](table *T) *remove[T]
Remove is a wrapper over Delete for more simple deletes, uses the value for create a where matching the primary keys. If the record don't exists returns a ErrNotFound.
Remove uses context.Background internally; to specify the context, use RemoveContext.
Examples ¶
// remove animal of id 2
err = goe.Remove(db.Animal).ById(Animal{Id: 2})
func RemoveContext ¶
func RenameColumn ¶
func Save ¶
func Save[T any](table *T) *save[T]
Save is a wrapper over Update for more simple updates, uses the value for create a where matching the primary keys and includes for update all non-zero values excluding the primary keys. If the record don't exists returns a ErrNotFound.
Save uses context.Background internally; to specify the context, use SaveContext.
Examples ¶
// updates animal name on record id 1
err = goe.Save(db.Animal).ByValue(Animal{Id: 1, Name: "Cat"})
func SaveContext ¶
SaveContext is a wrapper over Update for more simple updates, uses the value for create a where matching the primary keys and includes for update all non-zero values excluding the primary keys.
See Save for examples.
func Select ¶
func Select[T any](table *T) *stateSelect[T]
Select retrieves rows from tables.
Select uses context.Background internally; to specify the context, use SelectContext
Example ¶
// simple select
goe.Select(db.Animal).From(db.Animal).AsSlice()
// iterator select
for row, err := range goe.Select(db.Animal).From(db.Animal).Rows() { ... }
// pagination select
var p *goe.Pagination[Animal]
p, err = goe.Select(db.Animal).From(db.Animal).AsPagination(1, 10)
// select with where, joins and order by
goe.Select(db.Food).From(db.Food).
Joins(
join.Join[uuid.UUID](&db.Food.Id, &db.AnimalFood.IdFood),
join.Join[int](&db.AnimalFood.IdAnimal, &db.Animal.Id),
join.Join[uuid.UUID](&db.Animal.IdHabitat, &db.Habitat.Id),
join.Join[int](&db.Habitat.IdWeather, &db.Weather.Id),
).
Wheres(
where.Equals(&db.Food.Id, foods[0].Id),
where.And(),
where.Equals(&db.Food.Name, foods[0].Name),
).OrderByAsc(&db.Food.Name).AsSlice()
// select any argument
goe.Select(&struct {
User *string
Role *string
EndTime **time.Time
}{
User: &db.User.Name,
Role: &db.Role.Name,
EndTime: &db.UserRole.EndDate,
}).From(db.User).AsSlice()
func Update ¶
func Update[T any](table *T) *stateUpdate[T]
Update updates records in the given table
Update uses context.Background internally; to specify the context, use UpdateContext.
Examples ¶
// update only the attribute IdJobTitle from PersonJobTitle with the value 3 err = goe.Update(db.PersonJobTitle). Sets(update.Set(&db.PersonJobTitle.IdJobTitle, 3)). Wheres( where.Equals(&db.PersonJobTitle.PersonId, 2), where.And(), where.Equals(&db.PersonJobTitle.IdJobTitle, 1), ) // update all animals name to Cat goe.Update(db.Animal).Sets(update.Set(&db.Animal.Name, "Cat")).Wheres()
Types ¶
type AttributeMigrate ¶
type Connection ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func (*DB) NewTransaction ¶
func (db *DB) NewTransaction() (Transaction, error)
NewTransaction creates a new Transaction using the specified database target. It sets the isolation level to sql.LevelSerializable by default. The dbTarget parameter should be a valid database connection or instance. If successful, it returns the created Transaction; otherwise, it returns an error.
NewTransaction uses context.Background internally; to specify the context, use goe.NewTransactionContext
func (*DB) NewTransactionContext ¶
func (db *DB) NewTransactionContext(ctx context.Context, isolation sql.IsolationLevel) (Transaction, error)
func (*DB) RawExecContext ¶
func (*DB) RawQueryContext ¶
type Driver ¶
type Driver interface {
MigrateContext(context.Context, *Migrator) error
DropTable(string) error
DropColumn(table, column string) error
RenameColumn(table, oldColumn, newColumn string) error
Init() error
KeywordHandler(string) string
NewConnection() Connection
NewTransaction(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
Stats() sql.DBStats
Close() error
Config
}
type IndexMigrate ¶
type IndexMigrate struct {
Name string
EscapingName string
Unique bool
Attributes []AttributeMigrate
}
type ManyToOneMigrate ¶
type ManyToOneMigrate struct {
AttributeMigrate
TargetTable string
TargetColumn string
EscapingTargetTable string
EscapingTargetColumn string
}
type Migrator ¶
type Migrator struct {
Tables map[string]*TableMigrate
Error error
}
type OneToOneMigrate ¶
type OneToOneMigrate struct {
AttributeMigrate
TargetTable string
TargetColumn string
EscapingTargetTable string
EscapingTargetColumn string
}
type Pagination ¶
type Pagination[T any] struct { TotalValues int64 `json:"total_values"` TotalPages int `json:"total_pages"` PageValues int `json:"page_values"` PageSize int `json:"page_size"` CurrentPage int `json:"current_page"` HasPreviousPage bool `json:"has_previous_page"` PreviousPage int `json:"previous_page"` HasNextPage bool `json:"has_next_page"` NextPage int `json:"next_page"` StartIndex int `json:"start_index"` EndIndex int `json:"end_index"` Values []T `json:"values"` }
type PrimaryKeyMigrate ¶
type TableMigrate ¶
type TableMigrate struct {
Name string
EscapingName string
Migrated bool
PrimaryKeys []PrimaryKeyMigrate
Attributes []AttributeMigrate
ManyToOnes []ManyToOneMigrate
OneToOnes []OneToOneMigrate
Indexes []IndexMigrate
}
type Transaction ¶
type Transaction interface {
Connection
Commit() error
Rollback() error
}
GOE logo by