Skip to content

Commit

Permalink
只有注释的逻辑,没有真正的代码逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wln32 committed Nov 27, 2024
1 parent 5521d76 commit af5c21c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
27 changes: 27 additions & 0 deletions database/gdb/gdb_core_underlying.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,33 @@ func (c *Core) RowsToResult(ctx context.Context, rows *sql.Rows) (Result, error)
return nil, err
}

typ := ctx.Value("scan_typ")
if typ != nil {
values := make([]interface{}, 1)
result := make(Result, 1)
scanArgs := make([]interface{}, 1)
// 只取第一列返回
scanArgs[0] = &values[0]
if err = rows.Scan(scanArgs...); err != nil {
return result, err
}
record := Record{}
value := values[0]
if value == nil {
// DO NOT use `gvar.New(nil)` here as it creates an initialized object
// which will cause struct converting issue.
record[columnTypes[0].Name()] = nil
} else {
var convertedValue interface{}
if convertedValue, err = c.columnValueToLocalValue(ctx, value, columnTypes[i]); err != nil {

Check failure on line 451 in database/gdb/gdb_core_underlying.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

undefined: i (typecheck)

Check failure on line 451 in database/gdb/gdb_core_underlying.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

undefined: i) (typecheck)

Check failure on line 451 in database/gdb/gdb_core_underlying.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

undefined: i) (typecheck)
return nil, err
}
record[columnTypes[0].Name()] = gvar.New(convertedValue)
}
result[0] = record
return result, nil
}

if len(columnTypes) > 0 {
if internalData := c.getInternalColumnFromCtx(ctx); internalData != nil {
internalData.FirstResultColumn = columnTypes[0].Name()
Expand Down
18 changes: 18 additions & 0 deletions database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (m *Model) One(where ...interface{}) (Record, error) {
if len(where) > 0 {
return m.Where(where[0], where[1:]...).One()
}

all, err := m.doGetAll(ctx, true)
if err != nil {
return nil, err
Expand Down Expand Up @@ -185,10 +186,12 @@ func (m *Model) doStruct(pointer interface{}, where ...interface{}) error {
model = m.Fields(pointer)
}
}

one, err := model.One(where...)
if err != nil {
return err
}

if err = one.Struct(pointer); err != nil {
return err
}
Expand Down Expand Up @@ -275,6 +278,21 @@ func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
case reflect.Struct, reflect.Invalid:
return m.doStruct(pointer, where...)

case reflect.Int:
// 假设为*int
// 通过ctx往下传递此次需要基础类型的数据
// ctx = context.WithValue(ctx,"scan_typ","int")
// 假设这里就是像上面调用doStruct方法一样
// 通过生成sql,然后调用底层的(*Core).RowsToResult
// 调用完成返回到这里来
panic("")

// 返回后为取出第一列的数据即可
// val:=result[0]["这里第一列的列名"]
// 也可以使用for range语句取出第一列的数据,由于只有一个数据
// v:=gconv.Int(val)
// reflect.ValueOf(pointer).Elem.SetInt(v)
// return
default:
return gerror.NewCode(
gcode.CodeInvalidParameter,
Expand Down

0 comments on commit af5c21c

Please sign in to comment.