Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#358] Add Count, Pluck, DoesntExist, Distinct, FirstOr, Decrement, Increment, InsertGetId, When methods #931

Merged
merged 20 commits into from
Mar 3, 2025
Merged
77 changes: 61 additions & 16 deletions contracts/database/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,80 +14,125 @@ type DB interface {
}

type Query interface {
// Avg(column string) (any, error)
// commit
// Count(dest *int64) error
// Chunk(size int, callback func(rows []any) error) error
// Count Retrieve the "count" result of the query.
Count() (int64, error)
// Chunk Execute a callback over a given chunk size.
// Chunk(size int, callback func(dest []any) error) error
// CrossJoin(table string, on any, args ...any) Query
// DoesntExist() (bool, error)
// Distinct() Query
// DoesntExist Determine if no rows exist for the current query.
DoesntExist() (bool, error)
// Distinct Force the query to only return distinct results.
Distinct() Query
// dump
// dumpRawSql
// Delete records from the database.
Delete() (*Result, error)
// Each(callback func(rows []any) error) error
// Exists Determine if any rows exist for the current query.
Exists() (bool, error)
// Find Execute a query for a single record by ID.
Find(dest any, conds ...any) error
// First finds record that match given conditions.
First(dest any) error
// FirstOr
// FirstOr finds the first record that matches the given conditions or execute the callback and return its result if no record is found.
FirstOr(dest any, callback func() error) error
// FirstOrFail finds the first record that matches the given conditions or throws an error.
FirstOrFail(dest any) error
// decrement
// Decrement the given column's values by the given amounts.
Decrement(column string, value ...uint64) error
// Get Retrieve all rows from the database.
Get(dest any) error
// GroupBy(column string) Query
// GroupByRaw(query string, args ...any) Query
// having
// HavingRaw(query any, args ...any) Query
// increment
// Increment a column's value by a given amount.
Increment(column string, value ...uint64) error
// inRandomOrder
// Insert a new record into the database.
Insert(data any) (*Result, error)
// incrementEach
// insertGetId
// InsertGetId returns the ID of the inserted row, only supported by MySQL and Sqlite
InsertGetId(data any) (int64, error)
// Join(table string, on any, args ...any) Query
// latest
// Latest Retrieve the latest record from the database.
Latest(dest any, column ...string) error
// LeftJoin(table string, on any, args ...any) Query
// limit
// Limit(limit uint64) Query
// lockForUpdate
// Max(column string) (any, error)
// offset
// OrderBy Add an "order by" clause to the query.
OrderBy(column string) Query
// OrderByDesc Add a descending "order by" clause to the query.
OrderByDesc(column string) Query
// OrderByRaw Add a raw "order by" clause to the query.
OrderByRaw(raw string) Query
// OrWhere add an "or where" clause to the query.
OrWhere(query any, args ...any) Query
// OrWhereBetween adds an "or where column between x and y" clause to the query.
OrWhereBetween(column string, x, y any) Query
// OrWhereColumn adds an "or where column" clause to the query.
OrWhereColumn(column1 string, column2 ...string) Query
// OrWhereIn adds an "or where column in" clause to the query.
OrWhereIn(column string, args []any) Query
// OrWhereLike adds an "or where column like" clause to the query.
OrWhereLike(column string, value string) Query
// OrWhereNot adds an "or where not" clause to the query.
OrWhereNot(query any, args ...any) Query
// OrWhereNotBetween adds an "or where column not between x and y" clause to the query.
OrWhereNotBetween(column string, x, y any) Query
// OrWhereNotIn adds an "or where column not in" clause to the query.
OrWhereNotIn(column string, args []any) Query
// OrWhereNotLike adds an "or where column not like" clause to the query.
OrWhereNotLike(column string, value string) Query
// OrWhereNotNull adds an "or where column is not null" clause to the query.
OrWhereNotNull(column string) Query
// OrWhereNull adds an "or where column is null" clause to the query.
OrWhereNull(column string) Query
// OrWhereRaw adds a raw "or where" clause to the query.
OrWhereRaw(raw string, args []any) Query
// Pluck(column string, dest any) error
// Pluck Get a collection instance containing the values of a given column.
Pluck(column string, dest any) error
// rollBack
// RightJoin(table string, on any, args ...any) Query
// Select Set the columns to be selected.
Select(columns ...string) Query
// sharedLock
// skip
// take
// ToSql
// ToRawSql
// Update records in the database.
Update(column any, value ...any) (*Result, error)
// updateOrInsert
// Value(column string, dest any) error
// when
// When executes the callback if the condition is true.
When(condition bool, callback func(query Query) Query) Query
// Where Add a basic where clause to the query.
Where(query any, args ...any) Query
// WhereBetween Add a where between statement to the query.
WhereBetween(column string, x, y any) Query
// WhereColumn Add a "where" clause comparing two columns to the query.
WhereColumn(column1 string, column2 ...string) Query
// WhereExists Add an exists clause to the query.
WhereExists(func() Query) Query
// WhereIn Add a "where in" clause to the query.
WhereIn(column string, args []any) Query
// WhereLike Add a "where like" clause to the query.
WhereLike(column string, value string) Query
// WhereNot Add a basic "where not" clause to the query.
WhereNot(query any, args ...any) Query
// WhereNotBetween Add a where not between statement to the query.
WhereNotBetween(column string, x, y any) Query
// WhereNotIn Add a "where not in" clause to the query.
WhereNotIn(column string, args []any) Query
// WhereNotLike Add a "where not like" clause to the query.
WhereNotLike(column string, value string) Query
// WhereNotNull Add a "where not null" clause to the query.
WhereNotNull(column string) Query
// WhereNull Add a "where null" clause to the query.
WhereNull(column string) Query
// WhereRaw Add a raw where clause to the query.
WhereRaw(raw string, args []any) Query
}

Expand All @@ -98,6 +143,6 @@ type Result struct {
type Builder interface {
Exec(query string, args ...any) (sql.Result, error)
Get(dest any, query string, args ...any) error
// Query(query string, args ...any) (*sql.Rows, error)
Query(query string, args ...any) (*sql.Rows, error)
Select(dest any, query string, args ...any) error
}
10 changes: 6 additions & 4 deletions database/db/conditions.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package db

type Conditions struct {
table string
where []Where
orderBy []string
selects []string
Distinct *bool
Limit *uint64
OrderBy []string
Selects []string
Table string
Where []Where
}

type Where struct {
Expand Down
Loading