some work done on the running of the model
This commit is contained in:
@@ -190,18 +190,29 @@ type Generic struct{ reflect.Type }
|
||||
|
||||
var NotFoundError = errors.New("Not found")
|
||||
|
||||
func GetDbMultitple[T interface{}](c *Context, tablename string, args ...any) ([]*T, error) {
|
||||
t := reflect.TypeFor[T]()
|
||||
nargs := t.NumField()
|
||||
|
||||
query := ""
|
||||
func generateQuery(t reflect.Type) (query string, nargs int) {
|
||||
nargs = t.NumField()
|
||||
query = ""
|
||||
|
||||
for i := 0; i < nargs; i += 1 {
|
||||
query += strings.ToLower(t.Field(i).Name) + ","
|
||||
field := t.Field(i)
|
||||
name, ok := field.Tag.Lookup("db")
|
||||
if !ok {
|
||||
name = field.Name;
|
||||
}
|
||||
query += strings.ToLower(name) + ","
|
||||
}
|
||||
|
||||
// Remove the last comma
|
||||
query = query[0 : len(query)-1]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetDbMultitple[T interface{}](c *Context, tablename string, args ...any) ([]*T, error) {
|
||||
t := reflect.TypeFor[T]()
|
||||
|
||||
query, nargs := generateQuery(t)
|
||||
|
||||
rows, err := c.Db.Query(fmt.Sprintf("select %s from %s", query, tablename), args...)
|
||||
if err != nil {
|
||||
@@ -242,16 +253,8 @@ func mapRow(store interface{}, rows *sql.Rows, nargs int) (err error) {
|
||||
|
||||
func GetDBOnce(c *Context, store interface{}, tablename string, args ...any) error {
|
||||
t := reflect.TypeOf(store).Elem()
|
||||
nargs := t.NumField()
|
||||
|
||||
query := ""
|
||||
|
||||
for i := 0; i < nargs; i += 1 {
|
||||
query += strings.ToLower(t.Field(i).Name) + ","
|
||||
}
|
||||
|
||||
// Remove the last comma
|
||||
query = query[0 : len(query)-1]
|
||||
query, nargs := generateQuery(t)
|
||||
|
||||
rows, err := c.Db.Query(fmt.Sprintf("select %s from %s", query, tablename), args...)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user