Guest User

Untitled

a guest
Oct 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // Extract table names from SQL statement
  2.  
  3. package main
  4.  
  5. import (
  6. "fmt"
  7. "regexp"
  8. )
  9.  
  10. // extractTableNames from sql statement
  11. func extractTableNames(fromSQL string) (rets []string) {
  12. reg := "(\\s+from\\s+(?P<table1>\\w+)\\s+((\\w+)\\s+)?(where|left|join|inner))|(\\s+join\\s+(?P<table2>\\w+)\\s+((\\w+)\\s+)?on)"
  13. r, _ := regexp.Compile(reg)
  14. n1 := r.SubexpNames()
  15. finds := r.FindAllStringSubmatch(fromSQL, -1)
  16. for _, v := range finds {
  17. for ii, vv := range v {
  18. if n1[ii] != "" && vv != "" {
  19. // fmt.Println("=>", n1[ii], vv)
  20. rets = append(rets, string(vv))
  21. }
  22.  
  23. }
  24. }
  25. return
  26. }
  27.  
  28. func main() {
  29.  
  30. sql := `select * from Outvisit l
  31. left join patient p on l.patid=p.patientid
  32. join patstatic c on l.patid=c.patid inner join patphone ph on l.patid=ph.patid
  33. from pharmacywestpas p where p.outvisitid=l.outvisitid)
  34. unit all
  35. select * from invisit where`
  36.  
  37. fmt.Println(extractTableNames(sql))
  38. }
Add Comment
Please, Sign In to add comment