Skip to content

Commit

Permalink
feat: [#358] Implement Value, Offset, Limit methods (#941)
Browse files Browse the repository at this point in the history
* feat: [#358] Implement Value, Offset, Limit methods

* chore: update mocks

* optimize

---------

Co-authored-by: hwbrzzl <[email protected]>
  • Loading branch information
hwbrzzl and hwbrzzl authored Mar 5, 2025
1 parent dfecd6c commit 66b7b71
Show file tree
Hide file tree
Showing 15 changed files with 825 additions and 219 deletions.
18 changes: 13 additions & 5 deletions contracts/database/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import (
)

type DB interface {
// BeginTransaction Begin a transaction.
BeginTransaction() (DB, error)
// Commit Commit the transaction.
Commit() error
// Connection Get a database connection by name.
Connection(name string) DB
// Rollback Rollback the transaction.
Rollback() error
// Table Get a table instance.
Table(name string) Query
// Transaction Execute a transaction.
Transaction(txFunc func(tx DB) error) error
// WithContext Set the context for the query.
WithContext(ctx context.Context) DB
}

Expand Down Expand Up @@ -60,9 +67,11 @@ type Query interface {
Latest(dest any, column ...string) error
// LeftJoin specifying LEFT JOIN conditions for the query.
LeftJoin(query string, args ...any) Query
// Limit(limit uint64) Query
// Limit Add a limit to the query.
Limit(limit uint64) Query
// lockForUpdate
// offset
// Offset Add an "offset" clause to the query.
Offset(offset uint64) Query
// OrderBy Add an "order by" clause to the query.
OrderBy(column string) Query
// OrderByDesc Add a descending "order by" clause to the query.
Expand Down Expand Up @@ -100,15 +109,14 @@ type Query interface {
// Select Set the columns to be selected.
Select(columns ...string) Query
// sharedLock
// skip
// take
// ToSql Get the SQL representation of the query.
ToSql() ToSql
// ToRawSql Get the raw SQL representation of the query with embedded bindings.
ToRawSql() ToSql
// Update records in the database.
Update(column any, value ...any) (*Result, error)
// Value(column string, dest any) error
// Value Get a single column's value from the first result of a query.
Value(column string, dest any) error
// 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package driver

type Conditions struct {
CrossJoin []Join
Expand All @@ -8,6 +8,7 @@ type Conditions struct {
Join []Join
LeftJoin []Join
Limit *uint64
Offset *uint64
OrderBy []string
RightJoin []Join
Selects []string
Expand All @@ -16,17 +17,17 @@ type Conditions struct {
}

type Having struct {
query any
args []any
Query any
Args []any
}

type Join struct {
query string
args []any
Query string
Args []any
}

type Where struct {
query any
args []any
or bool
Query any
Args []any
Or bool
}
8 changes: 7 additions & 1 deletion contracts/database/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import (
)

type Driver interface {
// Config returns the database configuration.
Config() database.Config
// DB returns the database connection.
DB() (*sql.DB, error)
// Docker returns the database driver for Docker.
Docker() (docker.DatabaseDriver, error)
// Explain generate SQL string with given parameters
// Explain generates an SQL string with given parameters.
Explain(sql string, args ...any) string
// Gorm returns the Gorm database connection.
Gorm() (*gorm.DB, GormQuery, error)
// Grammar returns the database grammar.
Grammar() Grammar
// Processor returns the database processor.
Processor() Processor
}

Expand Down
16 changes: 16 additions & 0 deletions contracts/database/driver/grammar.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package driver

import (
sq "github.com/Masterminds/squirrel"
)

type Grammar interface {
// CompileAdd Compile an add column command.
CompileAdd(blueprint Blueprint, command *Command) string
Expand Down Expand Up @@ -117,6 +121,18 @@ type Grammar interface {
TypeString(column ColumnDefinition) string
}

type CompileOffsetGrammar interface {
CompileOffset(builder sq.SelectBuilder, conditions Conditions) sq.SelectBuilder
}

type CompileOrderByGrammar interface {
CompileOrderBy(builder sq.SelectBuilder, conditions Conditions) sq.SelectBuilder
}

type CompileLimitGrammar interface {
CompileLimit(builder sq.SelectBuilder, conditions Conditions) sq.SelectBuilder
}

type Schema interface {
}

Expand Down
Loading

0 comments on commit 66b7b71

Please sign in to comment.