Skip to content

Commit

Permalink
Backend: Search into table, procedure & function
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfpandey committed Mar 11, 2015
1 parent 2cbfb34 commit f75b941
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
)

0 comments on commit f75b941

Please sign in to comment.