On 2022-02-12 20:12:02 -0500, Mladen Gogala wrote:
> On 2/12/22 19:11, Andreas 'ads' Scherbaum wrote:
>
> The complaint is not about complex queries, or CTEs, or Joins. This is
> about simple queries where a user wants to discover - surf - the database
> and look into specific tables, but exclude certain columns. More
> specifically,
> this is when the user types in interactive queries.
>
> There is already something very similar to what you are describing:
>
> https://www.psycopg.org/docs/cursor.html
I'm not sure whether the PEP 249 notion of a cursor is relevant here.
That's quite Python specific and at least one step removed from the SQL
concept of a cursor.
> Each cursor has its description, which consists of the column descriptions.
Not really. While description is a property of the cursor object in
Python, it always describes the last query executed within that cursor:
% python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> db = psycopg2.connect("")
>>> csr = db.cursor()
>>> csr.description
(no output)
>>> csr.execute("select * from t1 natural join t2")
>>> csr.description
(Column(name='a', type_code=23), Column(name='b', type_code=23), Column(name='c', type_code=23))
>>> csr.fetchall()
[(1, 2, 11), (1, 2, 10)]
>>> csr.description
(Column(name='a', type_code=23), Column(name='b', type_code=23), Column(name='c', type_code=23))
(we can still refer to the description even after fetching all the data)
>>> csr.execute("select x from t1 natural join t2")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.errors.UndefinedColumn: column "x" does not exist
LINE 1: select x from t1 natural join t2
>>> csr.description
(no output again after a failed query)
> Basically, it's like doing \d on a cursor. Unfortunately, it's not interactive,
> one has to do some pythong programming in order do to that. Unfortunately, it
> is not possible to just "describe the cursor", the description becomes
> available after the "execute" call.
Yup, as demonstrated above. Which means that you have to actually
execute the query. Which is something that a should not happen as a side
effect of editing the query.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"