github.com/goravel/framework@v1.13.9/contracts/database/orm/orm.go (about)

     1  package orm
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  )
     7  
     8  //go:generate mockery --name=Orm
     9  type Orm interface {
    10  	// Connection gets an Orm instance from the connection pool.
    11  	Connection(name string) Orm
    12  	// DB gets the underlying database connection.
    13  	DB() (*sql.DB, error)
    14  	// Query gets a new query builder instance.
    15  	Query() Query
    16  	// Factory gets a new factory instance for the given model name.
    17  	Factory() Factory
    18  	// Observe registers an observer with the Orm.
    19  	Observe(model any, observer Observer)
    20  	// Transaction runs a callback wrapped in a database transaction.
    21  	Transaction(txFunc func(tx Transaction) error) error
    22  	// WithContext sets the context to be used by the Orm.
    23  	WithContext(ctx context.Context) Orm
    24  }
    25  
    26  //go:generate mockery --name=Transaction
    27  type Transaction interface {
    28  	Query
    29  	// Commit commits the changes in a transaction.
    30  	Commit() error
    31  	// Rollback rolls back the changes in a transaction.
    32  	Rollback() error
    33  }
    34  
    35  //go:generate mockery --name=Query
    36  type Query interface {
    37  	// Association gets an association instance by name.
    38  	Association(association string) Association
    39  	// Begin begins a new transaction
    40  	Begin() (Transaction, error)
    41  	// Driver gets the driver for the query.
    42  	Driver() Driver
    43  	// Count retrieve the "count" result of the query.
    44  	Count(count *int64) error
    45  	// Create inserts new record into the database.
    46  	Create(value any) error
    47  	// Cursor returns a cursor, use scan to iterate over the returned rows.
    48  	Cursor() (chan Cursor, error)
    49  	// Delete deletes records matching given conditions, if the conditions are empty will delete all records.
    50  	Delete(value any, conds ...any) (*Result, error)
    51  	// Distinct specifies distinct fields to query.
    52  	Distinct(args ...any) Query
    53  	// Exec executes raw sql
    54  	Exec(sql string, values ...any) (*Result, error)
    55  	// Find finds records that match given conditions.
    56  	Find(dest any, conds ...any) error
    57  	// FindOrFail finds records that match given conditions or throws an error.
    58  	FindOrFail(dest any, conds ...any) error
    59  	// First finds record that match given conditions.
    60  	First(dest any) error
    61  	// FirstOrCreate finds the first record that matches the given attributes
    62  	// or create a new one with those attributes if none was found.
    63  	FirstOrCreate(dest any, conds ...any) error
    64  	// FirstOr finds the first record that matches the given conditions or
    65  	// execute the callback and return its result if no record is found.
    66  	FirstOr(dest any, callback func() error) error
    67  	// FirstOrFail finds the first record that matches the given conditions or throws an error.
    68  	FirstOrFail(dest any) error
    69  	// FirstOrNew finds the first record that matches the given conditions or
    70  	// return a new instance of the model initialized with those attributes.
    71  	FirstOrNew(dest any, attributes any, values ...any) error
    72  	// ForceDelete forces delete records matching given conditions.
    73  	ForceDelete(value any, conds ...any) (*Result, error)
    74  	// Get retrieves all rows from the database.
    75  	Get(dest any) error
    76  	// Group specifies the group method on the query.
    77  	Group(name string) Query
    78  	// Having specifying HAVING conditions for the query.
    79  	Having(query any, args ...any) Query
    80  	// Join specifying JOIN conditions for the query.
    81  	Join(query string, args ...any) Query
    82  	// Limit the number of records returned.
    83  	Limit(limit int) Query
    84  	// Load loads a relationship for the model.
    85  	Load(dest any, relation string, args ...any) error
    86  	// LoadMissing loads a relationship for the model that is not already loaded.
    87  	LoadMissing(dest any, relation string, args ...any) error
    88  	// LockForUpdate locks the selected rows in the table for updating.
    89  	LockForUpdate() Query
    90  	// Model sets the model instance to be queried.
    91  	Model(value any) Query
    92  	// Offset specifies the number of records to skip before starting to return the records.
    93  	Offset(offset int) Query
    94  	// Omit specifies columns that should be omitted from the query.
    95  	Omit(columns ...string) Query
    96  	// Order specifies the order in which the results should be returned.
    97  	Order(value any) Query
    98  	// OrWhere add an "or where" clause to the query.
    99  	OrWhere(query any, args ...any) Query
   100  	// Paginate the given query into a simple paginator.
   101  	Paginate(page, limit int, dest any, total *int64) error
   102  	// Pluck retrieves a single column from the database.
   103  	Pluck(column string, dest any) error
   104  	// Raw creates a raw query.
   105  	Raw(sql string, values ...any) Query
   106  	// Save updates value in a database
   107  	Save(value any) error
   108  	// SaveQuietly updates value in a database without firing events
   109  	SaveQuietly(value any) error
   110  	// Scan scans the query result and populates the destination object.
   111  	Scan(dest any) error
   112  	// Scopes applies one or more query scopes.
   113  	Scopes(funcs ...func(Query) Query) Query
   114  	// Select specifies fields that should be retrieved from the database.
   115  	Select(query any, args ...any) Query
   116  	// SharedLock locks the selected rows in the table.
   117  	SharedLock() Query
   118  	// Sum calculates the sum of a column's values and populates the destination object.
   119  	Sum(column string, dest any) error
   120  	// Table specifies the table for the query.
   121  	Table(name string, args ...any) Query
   122  	// Update updates records with the given column and values
   123  	Update(column any, value ...any) (*Result, error)
   124  	// UpdateOrCreate finds the first record that matches the given attributes
   125  	// or create a new one with those attributes if none was found.
   126  	UpdateOrCreate(dest any, attributes any, values any) error
   127  	// Where add a "where" clause to the query.
   128  	Where(query any, args ...any) Query
   129  	// WithoutEvents disables event firing for the query.
   130  	WithoutEvents() Query
   131  	// WithTrashed allows soft deleted models to be included in the results.
   132  	WithTrashed() Query
   133  	// With returns a new query instance with the given relationships eager loaded.
   134  	With(query string, args ...any) Query
   135  }
   136  
   137  //go:generate mockery --name=Association
   138  type Association interface {
   139  	// Find finds records that match given conditions.
   140  	Find(out any, conds ...any) error
   141  	// Append appending a model to the association.
   142  	Append(values ...any) error
   143  	// Replace replaces the association with the given value.
   144  	Replace(values ...any) error
   145  	// Delete deletes the given value from the association.
   146  	Delete(values ...any) error
   147  	// Clear clears the association.
   148  	Clear() error
   149  	// Count returns the number of records in the association.
   150  	Count() int64
   151  }
   152  
   153  type ConnectionModel interface {
   154  	// Connection gets the connection name for the model.
   155  	Connection() string
   156  }
   157  
   158  //go:generate mockery --name=Cursor
   159  type Cursor interface {
   160  	// Scan scans the current row into the given destination.
   161  	Scan(value any) error
   162  }
   163  
   164  type Result struct {
   165  	RowsAffected int64
   166  }