[docs]deffirst_row(client:Client,query:Union[str,SQL],parameters=()):""" Execute a query and return the first matching row, if any. Parameters: cursor: Cursor to use query: Query to execute parameters: Optional parameters """withclient.cursor()ascursor:cursor.execute(query,parameters)returncursor.fetchone()
[docs]deffirst_value(client:Client,query:Union[str,SQL],parameters=()):""" Execute a query and return the first value of the first matching row, if any. Parameters: cursor: Cursor to use query: Query to execute parameters: Optional parameters """withclient:row=first_row(client,query,parameters)ifrowisNone:returnNonereturnrow[0]
[docs]deffirst_values(client:Client,query:Union[str,SQL],parameters=()):""" Execute a query and return the first value of each matching row. Parameters: cursor: Cursor to use query: Query to execute parameters: Optional parameters """withclient.cursor()ascursor:cursor.execute(query,parameters)return[row[0]forrowincursor]
defquery(client:Client,query:Union[str,SQL],parameters=())->list:""" Run a query and return the result as a Pandas DataFrame. Parameters: cursor: The psycopg Cursor to use query: The query to retrieve data parameters: Optional set of parameters passed to the cursor """withclient.cursor()ascursor:cursor.execute(query,parameters)row=namedtuple('Row',[col.nameforcolincursor.description])return[row(*r)forrincursor]