@@ -34,6 +34,39 @@ func GetQueryFromRequest(req string) (string, error) {
3434 return string (requestDecoded [MinPgSQLMessageLength :size ]), nil
3535}
3636
37+ // isMulti checks if the query is a union, intersect, or except.
38+ func isMulti (stmt * pgQuery.SelectStmt ) bool {
39+ return stmt .GetOp () == pgQuery .SetOperation_SETOP_UNION ||
40+ stmt .GetOp () == pgQuery .SetOperation_SETOP_INTERSECT ||
41+ stmt .GetOp () == pgQuery .SetOperation_SETOP_EXCEPT
42+ }
43+
44+ // getSingleTable returns the tables used in a query.
45+ func getSingleTable (stmt * pgQuery.SelectStmt ) []string {
46+ tables := []string {}
47+ for _ , from := range stmt .FromClause {
48+ rangeVar := from .GetRangeVar ()
49+ if rangeVar != nil {
50+ tables = append (tables , rangeVar .Relname )
51+ }
52+ }
53+
54+ return tables
55+ }
56+
57+ // getMultiTable returns the tables used in a union, intersect, or except query.
58+ func getMultiTable (stmt * pgQuery.SelectStmt ) []string {
59+ tables := []string {}
60+ // Get the tables from the left side.
61+ left := stmt .GetLarg ()
62+ tables = append (tables , getSingleTable (left )... )
63+ // Get the tables from the right side.
64+ right := stmt .GetRarg ()
65+ tables = append (tables , getSingleTable (right )... )
66+
67+ return tables
68+ }
69+
3770// GetTablesFromQuery returns the tables used in a query.
3871func GetTablesFromQuery (query string ) ([]string , error ) {
3972 stmt , err := pgQuery .Parse (query )
@@ -47,59 +80,27 @@ func GetTablesFromQuery(query string) ([]string, error) {
4780
4881 tables := []string {}
4982
50- isMulti := func (stmt * pgQuery.SelectStmt ) bool {
51- return stmt .GetOp () == pgQuery .SetOperation_SETOP_UNION ||
52- stmt .GetOp () == pgQuery .SetOperation_SETOP_INTERSECT ||
53- stmt .GetOp () == pgQuery .SetOperation_SETOP_EXCEPT
54- }
55-
56- // If the query is a union, we need to get the tables from both sides.
57- getMultiTable := func (stmt * pgQuery.SelectStmt ) {
58- // Get the tables from the left side.
59- for _ , fromClause := range stmt .GetLarg ().FromClause {
60- rangeVar := fromClause .GetRangeVar ()
61- if rangeVar != nil {
62- tables = append (tables , rangeVar .Relname )
63- }
64- }
65-
66- // Get the tables from the right side.
67- for _ , fromClause := range stmt .GetRarg ().FromClause {
68- rangeVar := fromClause .GetRangeVar ()
69- if rangeVar != nil {
70- tables = append (tables , rangeVar .Relname )
71- }
72- }
73- }
74-
75- getSingleTable := func (fromClause []* pgQuery.Node ) {
76- for _ , from := range fromClause {
77- rangeVar := from .GetRangeVar ()
78- if rangeVar != nil {
79- tables = append (tables , rangeVar .Relname )
80- }
81- }
82- }
83-
8483 for _ , stmt := range stmt .Stmts {
85- if isMulti (stmt .Stmt .GetSelectStmt ()) {
86- getMultiTable (stmt .Stmt .GetSelectStmt ())
84+ // Get the tables from the left and right side of the complex query.
85+ selectStatement := stmt .Stmt .GetSelectStmt ()
86+ if isMulti (selectStatement ) {
87+ tables = append (tables , getMultiTable (selectStatement )... )
8788 }
8889
8990 // Get the table from the WITH clause.
9091 if withClause := stmt .Stmt .GetSelectStmt ().GetWithClause (); withClause != nil {
9192 for _ , cte := range withClause .Ctes {
9293 selectStmt := cte .GetCommonTableExpr ().Ctequery .GetSelectStmt ()
9394 if isMulti (selectStmt ) {
94- getMultiTable (selectStmt )
95+ tables = append ( tables , getMultiTable (selectStmt ) ... )
9596 } else {
96- getSingleTable (selectStmt . FromClause )
97+ tables = append ( tables , getSingleTable (selectStmt ) ... )
9798 }
9899 }
99100 } else {
100101 // Get the table from the FROM clause.
101- if selectQuery := stmt .Stmt .GetSelectStmt (); selectQuery != nil {
102- getSingleTable (selectQuery . FromClause )
102+ if selectStatement := stmt .Stmt .GetSelectStmt (); selectStatement != nil {
103+ tables = append ( tables , getSingleTable (selectStatement ) ... )
103104 }
104105 }
105106
@@ -114,6 +115,24 @@ func GetTablesFromQuery(query string) ([]string, error) {
114115 if deleteQuery := stmt .Stmt .GetDeleteStmt (); deleteQuery != nil {
115116 tables = append (tables , deleteQuery .Relation .Relname )
116117 }
118+
119+ if truncateQuery := stmt .Stmt .GetTruncateStmt (); truncateQuery != nil {
120+ for _ , relation := range truncateQuery .Relations {
121+ tables = append (tables , relation .GetRangeVar ().Relname )
122+ }
123+ }
124+
125+ if dropTable := stmt .Stmt .GetDropStmt (); dropTable != nil {
126+ for _ , object := range dropTable .GetObjects () {
127+ for _ , table := range object .GetList ().GetItems () {
128+ tables = append (tables , table .GetString_ ().Str )
129+ }
130+ }
131+ }
132+
133+ if alterTable := stmt .Stmt .GetAlterTableStmt (); alterTable != nil {
134+ tables = append (tables , alterTable .Relation .Relname )
135+ }
117136 }
118137
119138 return tables , nil
0 commit comments