pingcap / parser

A MySQL Compatible SQL Parser
Apache License 2.0
1.41k stars 489 forks source link

Is it possible to extract table and its related columns from sql query? #1349

Open vasudevla opened 3 years ago

vasudevla commented 3 years ago

Question

For example, If query is

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

I need something like below,

{
   "tableName":"Orders",
   "columns":[
      "OrderID",
      "OrderData",
      "CustomerID"
   ]
}

And

{
   "tableName":"Customers",
   "columns":[
      "CustomerName",
      "CustomerID"
   ]
}

I can extract table names and column names separately, but is there anyway to extract table and its related columns? or Is it possible to extract table and its related columns in a single struct?

cjphaha commented 3 years ago

I think this is a good idea, but there seems no mehod, you can get table name from *ast.ColumnName.Table.String()

penwyp commented 2 years ago

you need a visitor to do that