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: facades.Orm supports cursor #243

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions contracts/database/orm/mocks/Cursor.go

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

26 changes: 26 additions & 0 deletions contracts/database/orm/mocks/Query.go

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

6 changes: 6 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Query interface {
Driver() Driver
Count(count *int64) error
Create(value any) error
Cursor() (chan Cursor, error)
Delete(value any, conds ...any) (*Result, error)
Distinct(args ...any) Query
Exec(sql string, values ...any) (*Result, error)
Expand Down Expand Up @@ -83,6 +84,11 @@ type Association interface {
Count() int64
}

//go:generate mockery --name=Cursor
type Cursor interface {
Scan(value any) error
}

type Result struct {
RowsAffected int64
}
21 changes: 21 additions & 0 deletions database/gorm/cursor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gorm

import "github.com/mitchellh/mapstructure"

type CursorImpl struct {
row map[string]any
}

func (c *CursorImpl) Scan(value any) error {
msConfig := &mapstructure.DecoderConfig{
Squash: true,
Result: value,
}

decoder, err := mapstructure.NewDecoder(msConfig)
if err != nil {
return err
}

return decoder.Decode(c.row)
}
29 changes: 29 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"database/sql"
"errors"
"reflect"

Expand Down Expand Up @@ -81,6 +82,34 @@
return r.create(value)
}

func (r *QueryImpl) Cursor() (chan ormcontract.Cursor, error) {
var err error
cursorChan := make(chan ormcontract.Cursor)
go func() {
rows, err := r.instance.Rows()
if err != nil {
return
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {

Check failure on line 95 in database/gorm/query.go

View workflow job for this annotation

GitHub Actions / lint

SA9003: empty branch (staticcheck)

}
}(rows)

kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
for rows.Next() {
val := make(map[string]any)
err := r.instance.ScanRows(rows, val)
if err != nil {
return
}
cursorChan <- &CursorImpl{row: val}
}
close(cursorChan)
}()
return cursorChan, err
}

func (r *QueryImpl) Delete(dest any, conds ...any) (*ormcontract.Result, error) {
if err := r.deleting(dest); err != nil {
return nil, err
Expand Down
23 changes: 23 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2458,6 +2458,29 @@ func (s *QueryTestSuite) TestSum() {
}
}

func (s *QueryTestSuite) TestCursor() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "cursor_user", Avatar: "cursor_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "cursor_user", Avatar: "cursor_avatar1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

users, err := query.Model(&User{}).Where("name = ?", "cursor_user").Cursor()
s.Nil(err)
for row := range users {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
var user User
s.Nil(row.Scan(&user))
s.True(user.ID > 0)
s.True(len(user.Name) > 0)
}
})
}
}

func (s *QueryTestSuite) TestTransactionSuccess() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
Expand Down
Loading