Я использую psqlparse
для разбора таблиц из psql операторов. Эта библиотека не совсем полная. Для операторов select он возвращает объект psqlparse.nodes.parsenodes.Statement
, где я могу просто вызвать parsenode.tables()
, чтобы получить таблицы. Для некоторых других операторов он возвращает словарь, который библиотека фактически анализирует для создания объекта Statement
.
Я не могу получить таблицы, используемые для одного запроса, потому что я даже не могу сказать, какой тип объекта psqlparse.parse(tagged_no_table_query)[0]
возвращается.
print("psqlparse.parse(tagged_no_table_query) = {!r}".format(psqlparse.parse(tagged_no_table_query)))
print("psqlparse.parse(tagged_no_table_query)[0] = {!r}".format(psqlparse.parse(tagged_no_table_query)[0]))
print("type(psqlparse.parse(tagged_no_table_query)[0]) = {!r}".format(type(psqlparse.parse(tagged_no_table_query)[0])))
выходы
psqlparse.parse(tagged_no_table_query) = [<psqlparse.nodes.parsenodes.SelectStmt object at 0x7f9a8f80a5f8>]
psqlparse.parse(tagged_no_table_query)[0] = <psqlparse.nodes.parsenodes.SelectStmt object at 0x7f9a8f751320>
type(psqlparse.parse(tagged_no_table_query)[0]) = <class 'psqlparse.nodes.parsenodes.SelectStmt'>
print("psqlparse.parse(tagged_no_table_query)[0].keys() = {}".format(psqlparse.parse(tagged_no_table_query)[0].keys()))
выходы
AttributeError: 'SelectStmt' object has no attribute 'keys'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<command-127687> in <module>
----> 1 print("psqlparse.parse(tagged_no_table_query)[0].keys() = {}".format(psqlparse.parse(tagged_no_table_query)[0].keys()))
AttributeError: 'SelectStmt' object has no attribute 'keys'
"psqlparse.parse(tagged_no_table_query)[0].tables() = {}".format(psqlparse.parse(tagged_no_table_query)[0].tables())
выходы
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<command-127688> in <module>
----> 1 "psqlparse.parse(tagged_no_table_query)[0].tables() = {}".format(psqlparse.parse(tagged_no_table_query)[0].tables())
/databricks/python/lib/python3.7/site-packages/psqlparse/nodes/parsenodes.py in tables(self)
47 if self.from_clause:
48 for item in self.from_clause:
---> 49 _tables |= item.tables()
50 if self.where_clause:
51 _tables |= self.where_clause.tables()
/databricks/python/lib/python3.7/site-packages/psqlparse/nodes/parsenodes.py in tables(self)
185
186 def tables(self):
--> 187 return self.subquery.tables()
188
189
/databricks/python/lib/python3.7/site-packages/psqlparse/nodes/parsenodes.py in tables(self)
44 if self.target_list:
45 for item in self.target_list:
---> 46 _tables |= item.tables()
47 if self.from_clause:
48 for item in self.from_clause:
/databricks/python/lib/python3.7/site-packages/psqlparse/nodes/parsenodes.py in tables(self)
217 _tables |= item.tables()
218 elif isinstance(self.val, Node):
--> 219 _tables |= self.val.tables()
220
221 return _tables
/databricks/python/lib/python3.7/site-packages/psqlparse/nodes/parsenodes.py in tables(self)
250 if self.args:
251 for item in self.args:
--> 252 _tables |= item.tables()
253 return _tables
254
AttributeError: 'dict' object has no attribute 'tables'