Skip to content

Commit

Permalink
support join table
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhg5 committed Oct 6, 2019
1 parent 1e2aee5 commit 9f7f771
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 33 deletions.
7 changes: 4 additions & 3 deletions examples/gin/main.go
Expand Up @@ -26,13 +26,13 @@ func main() {
Databases: config.DatabaseList{
"default": {
Host: "127.0.0.1",
Port: "5433",
User: "postgres",
Port: "3306",
User: "root",
Pwd: "root",
Name: "godmin",
MaxIdleCon: 50,
MaxOpenCon: 150,
Driver: config.DriverPostgresql,
Driver: config.DriverMysql,

//Driver: config.DriverSqlite,
//File: "../datamodel/admin.db",
Expand All @@ -47,6 +47,7 @@ func main() {
IndexUrl: "/",
Debug: true,
ColorScheme: adminlte.COLORSCHEME_SKIN_BLACK,
SqlLog: true,
}

adminPlugin := admin.NewAdmin(datamodel.Generators)
Expand Down
15 changes: 13 additions & 2 deletions plugins/admin/modules/parameter/parameter.go
Expand Up @@ -128,15 +128,26 @@ func (param Parameters) GetFixedParamStrWithoutPageSize() string {
for key, value := range param.Fields {
str += key + "=" + value + "&"
}
return "&__sort=" + param.SortField + "&__sort_type=" + param.SortType + str[:len(str)-1]
if len(param.Columns) > 0 {
return "&__columns=" + strings.Join(param.Columns, ",") + "&__sort=" + param.SortField +
"&__sort_type=" + param.SortType + str[:len(str)-1]
} else {
return "&__sort=" + param.SortField + "&__sort_type=" + param.SortType + str[:len(str)-1]
}
}

func (param Parameters) GetFixedParamStr() string {
str := "&"
for key, value := range param.Fields {
str += key + "=" + value + "&"
}
return "&__pageSize=" + param.PageSize + "&__sort=" + param.SortField + "&__sort_type=" + param.SortType + str[:len(str)-1]
if len(param.Columns) > 0 {
return "&__columns=" + strings.Join(param.Columns, ",") + "&__pageSize=" + param.PageSize + "&__sort=" +
param.SortField + "&__sort_type=" + param.SortType + str[:len(str)-1]
} else {
return "&__pageSize=" + param.PageSize + "&__sort=" +
param.SortField + "&__sort_type=" + param.SortType + str[:len(str)-1]
}
}

func GetDefault(values url.Values, key, def string) string {
Expand Down
99 changes: 77 additions & 22 deletions plugins/admin/modules/table/table.go
Expand Up @@ -249,7 +249,7 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet

var (
queryStatement = "select %s from " + connection.GetDelimiter() + "%s" + connection.GetDelimiter() +
"%s order by " + connection.GetDelimiter() + "%s" + connection.GetDelimiter() + " %s LIMIT ? OFFSET ?"
"%s %s order by " + connection.GetDelimiter() + "%s" + connection.GetDelimiter() + " %s LIMIT ? OFFSET ?"
countStatement = "select count(*) from " + connection.GetDelimiter() + "%s" + connection.GetDelimiter() + "%s"
)

Expand All @@ -260,11 +260,27 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet

columns := getColumns(columnsModel, tb.connectionDriver)

var sortable string
var (
sortable string
joins string
headField string
)
for i := 0; i < len(tb.info.FieldList); i++ {
if tb.info.FieldList[i].Field != tb.primaryKey.Name && checkInTable(columns, tb.info.FieldList[i].Field) {
fields += filterFiled(tb.info.FieldList[i].Field, connection.GetDelimiter()) + ","
if tb.info.FieldList[i].Field != tb.primaryKey.Name && checkInTable(columns, tb.info.FieldList[i].Field) &&
!tb.info.FieldList[i].Join.Valid() {
fields += tb.info.Table + "." + filterFiled(tb.info.FieldList[i].Field, connection.GetDelimiter()) + ","
}

headField = tb.info.FieldList[i].Field

if tb.info.FieldList[i].Join.Valid() {
headField = tb.info.FieldList[i].Join.Table + "_" + tb.info.FieldList[i].Field
fields += tb.info.FieldList[i].Join.Table + "." + filterFiled(tb.info.FieldList[i].Field, connection.GetDelimiter()) + " as " + headField + ","
joins += " left join " + filterFiled(tb.info.FieldList[i].Join.Table, connection.GetDelimiter()) + " on " +
tb.info.FieldList[i].Join.Table + "." + filterFiled(tb.info.FieldList[i].Join.JoinField, connection.GetDelimiter()) + " = " +
tb.info.Table + "." + filterFiled(tb.info.FieldList[i].Join.Field, connection.GetDelimiter())
}

if tb.info.FieldList[i].Hide {
continue
}
Expand All @@ -273,18 +289,18 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet
sortable = "1"
}
hide := "0"
if !modules.InArrayWithoutEmpty(params.Columns, tb.info.FieldList[i].Field) {
if !modules.InArrayWithoutEmpty(params.Columns, headField) {
hide = "1"
}
thead = append(thead, map[string]string{
"head": tb.info.FieldList[i].Head,
"sortable": sortable,
"field": tb.info.FieldList[i].Field,
"field": headField,
"hide": hide,
})
}

fields += filterFiled(tb.primaryKey.Name, connection.GetDelimiter())
fields += tb.info.Table + "." + filterFiled(tb.primaryKey.Name, connection.GetDelimiter())

if !checkInTable(columns, params.SortField) {
params.SortField = tb.primaryKey.Name
Expand All @@ -307,7 +323,7 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet

// TODO: add left join table relations, FilterFn is inefficient.

queryCmd := fmt.Sprintf(queryStatement, fields, tb.info.Table, wheres, params.SortField, params.SortType)
queryCmd := fmt.Sprintf(queryStatement, fields, tb.info.Table, joins, wheres, params.SortField, params.SortType)

logger.LogSql(queryCmd, args)

Expand All @@ -325,17 +341,24 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet
primaryKeyValue := db.GetValueFromDatabaseType(tb.primaryKey.Type, res[i][tb.primaryKey.Name])

for j := 0; j < len(tb.info.FieldList); j++ {

headField = tb.info.FieldList[j].Field

if tb.info.FieldList[j].Join.Valid() {
headField = tb.info.FieldList[j].Join.Table + "_" + tb.info.FieldList[j].Field
}

if tb.info.FieldList[j].Hide {
continue
}
if !modules.InArrayWithoutEmpty(params.Columns, tb.info.FieldList[j].Field) {
if !modules.InArrayWithoutEmpty(params.Columns, headField) {
continue
}
var value interface{}
if checkInTable(columns, tb.info.FieldList[j].Field) {
if checkInTable(columns, headField) || tb.info.FieldList[j].Join.Valid() {
value = tb.info.FieldList[j].FilterFn(types.RowModel{
ID: primaryKeyValue.String(),
Value: db.GetValueFromDatabaseType(tb.info.FieldList[j].TypeName, row[tb.info.FieldList[j].Field]).String(),
Value: db.GetValueFromDatabaseType(tb.info.FieldList[j].TypeName, row[headField]).String(),
Row: row,
})
} else {
Expand Down Expand Up @@ -392,7 +415,7 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
connection := tb.db()

var (
queryStatement = "select %s from %s where " + tb.primaryKey.Name + " in (%s) order by " + connection.GetDelimiter() +
queryStatement = "select %s from %s %s where " + tb.primaryKey.Name + " in (%s) order by " + connection.GetDelimiter() +
"%s" + connection.GetDelimiter() + " %s"
countStatement = "select count(*) from " + connection.GetDelimiter() + "%s" + connection.GetDelimiter() +
" where " + tb.primaryKey.Name + " in (%s)"
Expand All @@ -405,11 +428,27 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.

columns := getColumns(columnsModel, tb.connectionDriver)

var sortable string
var (
sortable string
joins string
headField string
)
for i := 0; i < len(tb.info.FieldList); i++ {
if tb.info.FieldList[i].Field != tb.primaryKey.Name && checkInTable(columns, tb.info.FieldList[i].Field) {
fields += tb.info.FieldList[i].Field + ","
if tb.info.FieldList[i].Field != tb.primaryKey.Name && checkInTable(columns, tb.info.FieldList[i].Field) &&
!tb.info.FieldList[i].Join.Valid() {
fields += tb.info.Table + "." + filterFiled(tb.info.FieldList[i].Field, connection.GetDelimiter()) + ","
}

headField = tb.info.FieldList[i].Field

if tb.info.FieldList[i].Join.Valid() {
headField = tb.info.FieldList[i].Join.Table + "_" + tb.info.FieldList[i].Field
fields += tb.info.FieldList[i].Join.Table + "." + filterFiled(tb.info.FieldList[i].Field, connection.GetDelimiter()) + " as " + headField + ","
joins += " left join " + filterFiled(tb.info.FieldList[i].Join.Table, connection.GetDelimiter()) + " on " +
tb.info.FieldList[i].Join.Table + "." + filterFiled(tb.info.FieldList[i].Join.JoinField, connection.GetDelimiter()) + " = " +
tb.info.Table + "." + filterFiled(tb.info.FieldList[i].Join.Field, connection.GetDelimiter())
}

if tb.info.FieldList[i].Hide {
continue
}
Expand All @@ -418,18 +457,27 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
sortable = "1"
}
hide := "0"
if !modules.InArrayWithoutEmpty(params.Columns, tb.info.FieldList[i].Field) {
if !modules.InArrayWithoutEmpty(params.Columns, headField) {
hide = "1"
}
thead = append(thead, map[string]string{
"head": tb.info.FieldList[i].Head,
"sortable": sortable,
"field": tb.info.FieldList[i].Field,
"field": headField,
"hide": hide,
})
if tb.info.FieldList[i].Join.Table != "" &&
tb.info.FieldList[i].Join.Field != "" &&
tb.info.FieldList[i].Join.JoinField != "" {
joins += " left join " + filterFiled(tb.info.FieldList[i].Join.Table, connection.GetDelimiter()) + " on " +
filterFiled(tb.info.FieldList[i].Join.Table, connection.GetDelimiter()) + "." +
filterFiled(tb.info.FieldList[i].Join.JoinField, connection.GetDelimiter()) + "=" +
filterFiled(tb.info.Table, connection.GetDelimiter()) + "." +
filterFiled(tb.info.FieldList[i].Join.Field, connection.GetDelimiter())
}
}

fields += tb.primaryKey.Name
fields += tb.info.Table + "." + filterFiled(tb.primaryKey.Name, connection.GetDelimiter())

if !checkInTable(columns, params.SortField) {
params.SortField = tb.primaryKey.Name
Expand All @@ -446,7 +494,7 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.

// TODO: add left join table relations

queryCmd := fmt.Sprintf(queryStatement, fields, tb.info.Table, whereIds, params.SortField, params.SortType)
queryCmd := fmt.Sprintf(queryStatement, fields, tb.info.Table, joins, whereIds, params.SortField, params.SortType)

res, _ := connection.QueryWithConnection(tb.connection, queryCmd)

Expand All @@ -464,17 +512,24 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
primaryKeyValue := db.GetValueFromDatabaseType(tb.primaryKey.Type, res[i][tb.primaryKey.Name])

for j := 0; j < len(tb.info.FieldList); j++ {

headField = tb.info.FieldList[i].Field

if tb.info.FieldList[j].Join.Valid() {
headField = tb.info.FieldList[j].Join.Table + "_" + tb.info.FieldList[j].Field
}

if tb.info.FieldList[j].Hide {
continue
}
if !modules.InArrayWithoutEmpty(params.Columns, tb.info.FieldList[j].Field) {
if !modules.InArrayWithoutEmpty(params.Columns, headField) {
continue
}
var value interface{}
if checkInTable(columns, tb.info.FieldList[j].Field) {
if checkInTable(columns, headField) {
value = tb.info.FieldList[j].FilterFn(types.RowModel{
ID: primaryKeyValue.String(),
Value: db.GetValueFromDatabaseType(tb.info.FieldList[j].TypeName, row[tb.info.FieldList[j].Field]).String(),
Value: db.GetValueFromDatabaseType(tb.info.FieldList[j].TypeName, row[headField]).String(),
Row: row,
})
} else {
Expand Down
16 changes: 10 additions & 6 deletions template/types/types.go
Expand Up @@ -121,18 +121,22 @@ type Field struct {
Field string
TypeName db.DatabaseType
Head string
JoinTable []Join
Join Join
Sortable bool
Filterable bool
Hide bool
}

type Join struct {
Table string
Field string
TableField string
HasChild bool
JoinTable *Join
Table string
Field string
JoinField string
HasChild bool
JoinTable *Join
}

func (j Join) Valid() bool {
return j.Table != "" && j.Field != "" && j.JoinField != ""
}

// InfoPanel
Expand Down

0 comments on commit 9f7f771

Please sign in to comment.