|
|
|
|
@ -3,13 +3,16 @@ package features_admin |
|
|
|
|
import ( |
|
|
|
|
"reflect" |
|
|
|
|
"fmt" |
|
|
|
|
"errors" |
|
|
|
|
"MY/webapp/data" |
|
|
|
|
_ "github.com/mattn/go-sqlite3" |
|
|
|
|
sq "github.com/Masterminds/squirrel" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func Schema(table string) []string { |
|
|
|
|
the_type := data.Models()[table] |
|
|
|
|
func Schema(table string) ([]string, error) { |
|
|
|
|
the_type, ok := data.Models()[table] |
|
|
|
|
if !ok { return nil, errors.New("Invalid table") } |
|
|
|
|
|
|
|
|
|
field_num := the_type.NumField() |
|
|
|
|
|
|
|
|
|
fields := make([]string, 0, field_num) |
|
|
|
|
@ -19,7 +22,7 @@ func Schema(table string) []string { |
|
|
|
|
fields = append(fields, tag) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fields |
|
|
|
|
return fields, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func SearchTable(search string, table string, limit uint64, page uint64) ([]any, error) { |
|
|
|
|
@ -140,8 +143,9 @@ func Insert(table string, value reflect.Value) (int64, int64, error) { |
|
|
|
|
return id, count, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Update(table string, value reflect.Value) error { |
|
|
|
|
func Update(table string, value reflect.Value) (int64, error) { |
|
|
|
|
builder := sq.Update(table) |
|
|
|
|
orig_id := value.FieldByName("Id").Int() |
|
|
|
|
|
|
|
|
|
type_of := value.Type() |
|
|
|
|
field_num := value.NumField() |
|
|
|
|
@ -156,13 +160,15 @@ func Update(table string, value reflect.Value) error { |
|
|
|
|
builder = builder.Set(tag, field.Interface()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
builder = builder.Where(sq.Eq{"id": value.FieldByName("Id").Interface()}) |
|
|
|
|
builder = builder.Where(sq.Eq{"id": orig_id}) |
|
|
|
|
sql_query, args, err := builder.ToSql() |
|
|
|
|
if err != nil { return err } |
|
|
|
|
if err != nil { return -1, err } |
|
|
|
|
|
|
|
|
|
fmt.Println("UPDATE QUERY", sql_query, args) |
|
|
|
|
if err != nil { return err} |
|
|
|
|
if err != nil { return -1, err} |
|
|
|
|
|
|
|
|
|
_, err = data.DB.Exec(sql_query, args...) |
|
|
|
|
return err |
|
|
|
|
if err != nil { return -1, err } |
|
|
|
|
|
|
|
|
|
return orig_id, err |
|
|
|
|
} |
|
|
|
|
|