Skip to content

Commit

Permalink
Merge pull request #53 from olachat/yinloo/fix-query-interface
Browse files Browse the repository at this point in the history
deprecate methods with ctx
  • Loading branch information
yinloo-ola authored Nov 6, 2023
2 parents 5a07679 + 98c5ae5 commit 44053ba
Show file tree
Hide file tree
Showing 15 changed files with 520 additions and 0 deletions.
13 changes: 13 additions & 0 deletions coredb/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func getDB(dbname string, mode DBMode) *sql.DB {
}

// FetchByPK returns a row of T type with given primary key value
// Deprecated: use the function with context
func FetchByPK[T any](dbname string, tableName string, pkName []string, val ...any) *T {
sql := "WHERE `" + pkName[0] + "` = ?"
for _, name := range pkName[1:] {
Expand All @@ -53,6 +54,7 @@ func FetchByPK[T any](dbname string, tableName string, pkName []string, val ...a
}

// FetchByPKs returns rows of T type with given primary key values
// Deprecated: use the function with context
func FetchByPKs[T any](dbname string, tableName string, pkName string, vals []any) []*T {
if len(vals) == 0 {
return make([]*T, 0)
Expand All @@ -69,6 +71,7 @@ func FetchByPKs[T any](dbname string, tableName string, pkName string, vals []an
}

// FetchByPKFromMaster returns a row of T type with given primary key value
// Deprecated: use the function with context
func FetchByPKFromMaster[T any](dbname string, tableName string, pkName []string, val ...any) *T {
sql := "WHERE `" + pkName[0] + "` = ?"
for _, name := range pkName[1:] {
Expand All @@ -79,6 +82,7 @@ func FetchByPKFromMaster[T any](dbname string, tableName string, pkName []string
}

// FetchByPKsFromMaster returns rows of T type with given primary key values
// Deprecated: use the function with context
func FetchByPKsFromMaster[T any](dbname string, tableName string, pkName string, vals []any) []*T {
if len(vals) == 0 {
return make([]*T, 0)
Expand All @@ -95,12 +99,14 @@ func FetchByPKsFromMaster[T any](dbname string, tableName string, pkName string,
}

// Exec given query with given db info & params
// Deprecated: use the function with context
func Exec(dbname string, query string, params ...any) (sql.Result, error) {
mydb := getDB(dbname, DBModeWrite)
return mydb.Exec(query, params...)
}

// FindOne returns a row from given table type with where query
// Deprecated: use the function with context
func FindOne[T any](dbname string, tableName string, where WhereQuery) *T {
u := new(T)
columnsNames := GetColumnsNames[T]()
Expand All @@ -125,6 +131,7 @@ func FindOne[T any](dbname string, tableName string, where WhereQuery) *T {
}

// Find returns rows from given table type with where query
// Deprecated: use the function with context
func Find[T any](dbname string, tableName string, where WhereQuery) ([]*T, error) {
columnsNames := GetColumnsNames[T]()
whereSQL, params := where.GetWhere()
Expand All @@ -135,6 +142,7 @@ func Find[T any](dbname string, tableName string, where WhereQuery) ([]*T, error
}

// FindOneFromMaster using master DB returns a row from given table type with where query
// Deprecated: use the function with context
func FindOneFromMaster[T any](dbname string, tableName string, where WhereQuery) *T {
u := new(T)
columnsNames := GetColumnsNames[T]()
Expand All @@ -159,6 +167,7 @@ func FindOneFromMaster[T any](dbname string, tableName string, where WhereQuery)
}

// FindFromMaster using master DB returns rows from given table type with where query
// Deprecated: use the function with context
func FindFromMaster[T any](dbname string, tableName string, where WhereQuery) ([]*T, error) {
columnsNames := GetColumnsNames[T]()
whereSQL, params := where.GetWhere()
Expand All @@ -169,20 +178,23 @@ func FindFromMaster[T any](dbname string, tableName string, where WhereQuery) ([
}

// QueryInt single int result by query, handy for count(*) querys
// Deprecated: use the function with context
func QueryInt(dbname string, query string, params ...any) (result int, err error) {
mydb := getDB(dbname, DBModeRead)
mydb.QueryRow(query, params...).Scan(&result)
return
}

// QueryIntFromMaster single int result by query, handy for count(*) querys
// Deprecated: use the function with context
func QueryIntFromMaster(dbname string, query string, params ...any) (result int, err error) {
mydb := getDB(dbname, DBModeReadFromWrite)
mydb.QueryRow(query, params...).Scan(&result)
return
}

// Query rows from given table type with where query & params
// Deprecated: use the function with context
func Query[T any](dbname string, query string, params ...any) (result []*T, err error) {
mydb := getDB(dbname, DBModeRead)
rows, err := mydb.Query(query, params...)
Expand All @@ -205,6 +217,7 @@ func Query[T any](dbname string, query string, params ...any) (result []*T, err
}

// Query rows from master DB from given table type with where query & params
// Deprecated: use the function with context
func QueryFromMaster[T any](dbname string, query string, params ...any) (result []*T, err error) {
mydb := getDB(dbname, DBModeReadFromWrite)
rows, err := mydb.Query(query, params...)
Expand Down
11 changes: 11 additions & 0 deletions coredb/query_type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package coredb

import "context"

// WhereQuery defines interface which support forming query query
type WhereQuery interface {
GetWhere() (whereSQL string, params []any)
Expand All @@ -26,10 +28,19 @@ func NewWhere(whereSQL string, params ...any) WhereQuery {

// ReadQuery defines interface which support reading multiple objects
type ReadQuery[T any] interface {
// Deprecated: use the function with context
All() []*T
// Deprecated: use the function with context
Limit(offset, limit int) []*T
// Deprecated: use the function with context
AllFromMaster() []*T
// Deprecated: use the function with context
LimitFromMaster(offset, limit int) []*T

AllCtx(context.Context) ([]*T, error)
LimitCtx(ctx context.Context, offset, limit int) ([]*T, error)
AllFromMasterCtx(context.Context) ([]*T, error)
LimitFromMasterCtx(ctx context.Context, offset, limit int) ([]*T, error)
}

// ReadOneQuery defines interface which support reading one object
Expand Down
32 changes: 32 additions & 0 deletions golalib/testdata/account/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 44053ba

Please sign in to comment.