Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions contracts/database/driver/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
sq "github.com/Masterminds/squirrel"
"github.com/goravel/framework/contracts/database/orm"
"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -159,11 +160,19 @@ type CompileLimitGrammar interface {
}

type Schema interface {
// GetColumns Get the columns for a given table.
GetColumns(table string) ([]Column, error)
// GetIndexes Get the indexes for a given table.
GetIndexes(table string) ([]Index, error)
// Orm Get the orm instance.
Orm() orm.Orm
}

type Blueprint interface {
// GetAddedColumns Get the added columns.
GetAddedColumns() []ColumnDefinition
// GetCommands Get the commands.
GetCommands() []*Command
// GetTableName Get the table name with prefix.
GetTableName() string
// HasCommand Determine if the blueprint has a specific command.
Expand Down
7 changes: 1 addition & 6 deletions database/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ func (r *Query) Chunk(size uint64, callback func(rows []db.Row) error) error {

var destSlice []db.Row
for row := range rows {
var dest map[string]any
if err := row.Scan(&dest); err != nil {
return err
}

destSlice = append(destSlice, NewRow(dest))
destSlice = append(destSlice, row)
}

if len(destSlice) == 0 {
Expand Down
21 changes: 19 additions & 2 deletions database/db/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/mitchellh/mapstructure"
"github.com/go-viper/mapstructure/v2"
"gorm.io/gorm"

"github.com/goravel/framework/support/carbon"
Expand All @@ -23,7 +23,7 @@ func NewRow(row map[string]any) *Row {
func (r *Row) Scan(value any) error {
msConfig := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
ToTimeHookFunc(), ToCarbonHookFunc(), ToDeletedAtHookFunc(),
ToStringHookFunc(), ToTimeHookFunc(), ToCarbonHookFunc(), ToDeletedAtHookFunc(),
),
Squash: true,
Result: value,
Expand All @@ -40,6 +40,23 @@ func (r *Row) Scan(value any) error {
return decoder.Decode(r.row)
}

// ToStringHookFunc is a hook function that converts []uint8 to string.
// Mysql returns []uint8 for String type when scanning the rows.
func ToStringHookFunc() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, data any) (any, error) {
if t != reflect.TypeOf("") {
return data, nil
}

dataSlice, ok := data.([]uint8)
if ok {
return string(dataSlice), nil
}

return data, nil
}
}

func ToTimeHookFunc() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, data any) (any, error) {
if t != reflect.TypeOf(time.Time{}) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ require (
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
Expand Down
47 changes: 47 additions & 0 deletions mocks/database/driver/Blueprint.go

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

170 changes: 169 additions & 1 deletion mocks/database/driver/Schema.go

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

Loading