@@ -25,6 +25,10 @@ class Query:
2525 Query handles all the querying done by the Database Library.
2626 """
2727
28+ def __init__ (self , log_query_results , log_query_results_head ):
29+ self .LOG_QUERY_RESULTS = log_query_results
30+ self .LOG_QUERY_RESULTS_HEAD = log_query_results_head
31+
2832 def query (
2933 self ,
3034 selectStatement : str ,
@@ -86,7 +90,7 @@ def query(
8690 self ._execute_sql (cur , selectStatement , parameters = parameters )
8791 all_rows = cur .fetchall ()
8892 col_names = [c [0 ] for c in cur .description ]
89- self ._log_query_result (col_names , all_rows )
93+ self ._log_query_results (col_names , all_rows )
9094 if returnAsDict :
9195 return [dict (zip (col_names , row )) for row in all_rows ]
9296 return all_rows
@@ -132,7 +136,7 @@ def row_count(
132136 else :
133137 current_row_count = cur .rowcount
134138 logger .info (f"Retrieved { current_row_count } rows" )
135- self ._log_query_result (col_names , data )
139+ self ._log_query_results (col_names , data )
136140 return current_row_count
137141 finally :
138142 if cur and not sansTran :
@@ -552,6 +556,24 @@ def call_stored_procedure(
552556 if cur and not sansTran :
553557 db_connection .client .rollback ()
554558
559+ def set_logging_query_results (self , enabled : Optional [bool ] = None , log_head : Optional [int ] = None ):
560+ """
561+ Allows to enable/disable logging of query results and to adjust the log head value.
562+ - Overrides the values, which were set during the library import.
563+ - See `Logging query results` for details.
564+
565+ Examples:
566+ | Set Logging Query Results | enabled=False |
567+ | Set Logging Query Results | enabled=True | log_head=0 |
568+ | Set Logging Query Results | log_head=10 |
569+ """
570+ if enabled is not None :
571+ self .LOG_QUERY_RESULTS = enabled
572+ if log_head is not None :
573+ if log_head < 0 :
574+ raise ValueError (f"Wrong log head value provided: { log_head } . The value can't be negative!" )
575+ self .LOG_QUERY_RESULTS_HEAD = log_head
576+
555577 def _execute_sql (
556578 self ,
557579 cur ,
@@ -577,12 +599,17 @@ def _execute_sql(
577599 logger .debug (f"Executing sql '{ sql_statement } ' with parameters: { parameters } " )
578600 return cur .execute (sql_statement , parameters )
579601
580- def _log_query_result (self , col_names , result_rows , log_head = 50 ):
602+ def _log_query_results (self , col_names , result_rows , log_head : Optional [ int ] = None ):
581603 """
582604 Logs the `result_rows` of a query in RF log as a HTML table.
583605 The `col_names` are needed for the table header.
584606 Max. `log_head` rows are logged (`0` disables the limit).
585607 """
608+ if not self .LOG_QUERY_RESULTS :
609+ return
610+
611+ if log_head is None :
612+ log_head = self .LOG_QUERY_RESULTS_HEAD
586613 cell_border_and_align = "border: 1px solid rgb(160 160 160);padding: 8px 10px;text-align: center;"
587614 table_border = "2px solid rgb(140 140 140)"
588615 row_index_color = "#d6ecd4"
0 commit comments