orm

package
v0.0.0-...-26d55d5 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

README

database/orm

Example

// declare a model
type User struct {
  ID uint `orm:"id,pk"`
  Email string `orm:"email"`
  Active bool `orm:"is_active"`
}

// open a connection
db, _ := orm.Open("sqlite3", "app.db")

// create a row
_ = db.Create(&User {
  Email: "matt@example.com"
})

// find a row
user := User{}

q := db.NewQuery().
  Where("email", "matt@example.com").
  Where("active", true).
  Limit(1)

_ = db.Query(q, &user)

// update a row
user.Active = false

_ = db.Update(&user, "is_active")

// delete a row
_ = db.Delete(&user)

TODO

  • relationships
  • raw SQL support
  • docs
  • examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ORM

type ORM struct {
	// contains filtered or unexported fields
}

ORM is a handle representing an object relational mapper. The ORM wraps an underlying SQL database connection and maps database rows to structs.

func Open

func Open(driverName, dataSourceName string) (*ORM, error)

Open opens a new database connection with the given configuration and returns a new ORM.

func (*ORM) Close

func (orm *ORM) Close() error

Close closes the ORM and the underlying database connection.

func (*ORM) Create

func (orm *ORM) Create(ctx context.Context, v any) error

Create adds records for v to the database.

func (*ORM) DB

func (orm *ORM) DB() *sql.DB

DB returns the underlying sql.DB for the database connection.

func (*ORM) Delete

func (orm *ORM) Delete(ctx context.Context, v any) error

Delete deletes the database row corresponding to the value v. The value should be a pointer to a struct.

func (*ORM) Exec

func (orm *ORM) Exec(ctx context.Context, q *dml.Query, v any) error

Exec executes a query without returning any results. If the final argument v is not nil, v must be a pointer to a struct. If v is specified the last inserted ID will be set after executing the query.

func (*ORM) Find

func (orm *ORM) Find(ctx context.Context, v any) error

Find finds the database row corresponding to the value v. The value should be a pointer to a struct with any primary key fields populated. The struct will be filled if a row is found.

func (*ORM) NewQuery

func (orm *ORM) NewQuery() *dml.Query

NewQuery creates a new dml.Query pre-configured for the ORM's database dialect.

func (*ORM) Query

func (orm *ORM) Query(ctx context.Context, q *dml.Query, v any) error

Query executes the query q and scans the results into v. The value v must be a pointer to a struct, slice, or array. If a struct is provided but the query returns more than 1 row, only the first row will be scanned. If a slice is provided rows will be appended.

func (*ORM) Update

func (orm *ORM) Update(ctx context.Context, v any, columns ...string) error

Update updates the database row corresponding to the value v. The value should be a pointer to a struct. The struct's primary key values are used to constrain the update statement. The remaining arguments may be used to specify a subset of columns that should be updated. If no column names are specified, all columns are updated.

Jump to

Keyboard shortcuts

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