diff --git a/api.go b/api.go index e4b45b6..0e5a6c0 100644 --- a/api.go +++ b/api.go @@ -401,6 +401,17 @@ func APIViewDefinition(c *gin.Context) { c.JSON(200, res) } +func APISearch(c *gin.Context) { + res, err := dbClient.Search(c.Params.ByName("query")) + + if err != nil { + c.JSON(400, NewError(err)) + return + } + + c.JSON(200, res) +} + //APIHandleQuery handles thq query and return the resultset as JSON func APIHandleQuery(query string, c *gin.Context) { result, err := dbClient.Query(query) diff --git a/client.go b/client.go index 4d5aab1..06ff83f 100644 --- a/client.go +++ b/client.go @@ -312,6 +312,36 @@ func (client *Client) ViewDefinition(database string, name string) (*Result, err return res, err } +func (client *Client) Search(query string) (*Result, error) { + //Search in table list + resTbl, err := client.Query(fmt.Sprintf(MySQLSearchTable, query)) + + if err != nil { + return nil, err + } + + resProc, err := client.Query(fmt.Sprintf(MySQLSearchProcedure, query)) + + if err != nil { + return nil, err + } + + resFunc, err := client.Query(fmt.Sprintf(MySQLSearchFunction, query)) + + if err != nil { + return nil, err + } + + resMerge := Result{ + Columns: resTbl.Columns, + Rows: append(resTbl.Rows, resProc.Rows...), + } + + resMerge.Rows = append(resMerge.Rows, resFunc.Rows...) + + return &resMerge, err +} + //Query will execute the sql query passed as parameter, and return the resultset func (client *Client) Query(query string) (*Result, error) { rows, err := client.db.Queryx(query) diff --git a/main.go b/main.go index 92789d9..d722cd7 100644 --- a/main.go +++ b/main.go @@ -174,6 +174,7 @@ func startServer() { router.POST("/databases/:database/functions/:function", APICreateFunction) router.DELETE("/databases/:database/procedures/:procedure/actions/drop", APIDropProcedure) router.GET("/databases/:database/views/:view", APIViewDefinition) + router.GET("/search/:query", APISearch) fmt.Println("Starting server...") go router.Run(fmt.Sprintf("%v:%v", options.HttpHost, options.HttpPort)) diff --git a/statements.go b/statements.go index a3bb8c7..23defaa 100644 --- a/statements.go +++ b/statements.go @@ -19,4 +19,7 @@ const ( MySQLProcedureDefinition = "SHOW CREATE %s %s.%s" MySQLProcedureDrop = "DROP %s IF EXISTS %s.%s" MySQLViewDefinition = "SHOW CREATE VIEW %s.%s" + MySQLSearchTable = "SELECT TABLE_NAME, TABLE_SCHEMA, 'TBL' AS type FROM information_schema.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME LIKE '%%%s%%' ORDER BY TABLE_NAME;" + MySQLSearchProcedure = "SELECT ROUTINE_NAME, ROUTINE_SCHEMA, 'PROC' AS type FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE= 'PROCEDURE' AND ROUTINE_NAME LIKE '%%%s%%' ORDER BY ROUTINE_NAME;" + MySQLSearchFunction = "SELECT ROUTINE_NAME, ROUTINE_SCHEMA, 'FUNC' AS type FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE= 'FUNCTION' AND ROUTINE_NAME LIKE '%%%s%%' ORDER BY ROUTINE_NAME;" )