|
|
|
|
@ -9,6 +9,12 @@ import ( |
|
|
|
|
sq "github.com/Masterminds/squirrel" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func GetDbTag(type_of reflect.Type, i int) (string, bool) { |
|
|
|
|
db_tag, ok := type_of.Field(i).Tag.Lookup("db") |
|
|
|
|
|
|
|
|
|
return db_tag, ok && db_tag != "-" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Schema(table string) ([]string, error) { |
|
|
|
|
the_type, ok := data.Models()[table] |
|
|
|
|
if !ok { return nil, errors.New("Invalid table") } |
|
|
|
|
@ -18,8 +24,12 @@ func Schema(table string) ([]string, error) { |
|
|
|
|
fields := make([]string, 0, field_num) |
|
|
|
|
|
|
|
|
|
for i := 0; i < field_num; i++ { |
|
|
|
|
tag := the_type.Field(i).Name |
|
|
|
|
fields = append(fields, tag) |
|
|
|
|
_, ok := GetDbTag(the_type, i) |
|
|
|
|
|
|
|
|
|
if ok { |
|
|
|
|
field_name := the_type.Field(i).Name |
|
|
|
|
fields = append(fields, field_name) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fields, nil |
|
|
|
|
@ -40,9 +50,12 @@ func SearchTable(search string, table string, limit uint64, page uint64) ([]any, |
|
|
|
|
var or_clause sq.Or |
|
|
|
|
|
|
|
|
|
for i := 0; i < field_num; i++ { |
|
|
|
|
tag := the_type.Field(i).Tag.Get("db") |
|
|
|
|
tag, ok := GetDbTag(the_type, i) |
|
|
|
|
|
|
|
|
|
if ok { |
|
|
|
|
or_clause = append(or_clause, sq.Like{tag: like}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
builder = builder.Where(or_clause) |
|
|
|
|
sql_query, args, err := builder.ToSql() |
|
|
|
|
@ -119,10 +132,12 @@ func Insert(table string, value reflect.Value) (int64, int64, error) { |
|
|
|
|
var columns []string |
|
|
|
|
var values []any |
|
|
|
|
|
|
|
|
|
// TODO: I think I don't need this, look if squirrel can just insert
|
|
|
|
|
for i := 0; i < field_num; i++ { |
|
|
|
|
field := value.Field(i) |
|
|
|
|
tag := type_of.Field(i).Tag.Get("db") |
|
|
|
|
if tag == "id" { continue } |
|
|
|
|
tag, ok := GetDbTag(type_of, i) |
|
|
|
|
|
|
|
|
|
if !ok || tag == "id" { continue } |
|
|
|
|
columns = append(columns, tag) |
|
|
|
|
values = append(values, field.Interface()) |
|
|
|
|
} |
|
|
|
|
@ -152,10 +167,10 @@ func Update(table string, value reflect.Value) (int64, error) { |
|
|
|
|
|
|
|
|
|
for i := 0; i < field_num; i++ { |
|
|
|
|
field := value.Field(i) |
|
|
|
|
tag := type_of.Field(i).Tag.Get("db") |
|
|
|
|
tag, ok := GetDbTag(type_of, i) |
|
|
|
|
|
|
|
|
|
// skip update of id to avoid replacing it
|
|
|
|
|
if tag == "id" { continue } |
|
|
|
|
if !ok || tag == "id" { continue } |
|
|
|
|
|
|
|
|
|
builder = builder.Set(tag, field.Interface()) |
|
|
|
|
} |
|
|
|
|
|