Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 08:39:06

0001 class SQLQueryManager:
0002     def __init__(self, sql_query):
0003         self.sql_query = sql_query
0004 
0005     def format_query(self):
0006         """Format the SQL query by removing line breaks and redundant spaces."""
0007         # Replace newline characters with a single space
0008         formatted_query = self.sql_query.replace('\n', ' ')
0009         # Remove redundant spaces (more than one space in a row)
0010         formatted_query = ' '.join(formatted_query.split())
0011         return formatted_query
0012 
0013     def set_query(self, sql_query):
0014         """Set a new SQL query."""
0015         self.sql_query = sql_query
0016 
0017     def get_query(self):
0018         """Get the current SQL query."""
0019         return self.sql_query
0020 
0021     def log_query(self):
0022         """Log the formatted SQL query."""
0023         formatted_query = self.format_query()
0024         print(f"SQL Query: {formatted_query}")
0025 
0026     def append(self, sql_text):
0027         """Append more text to the existing SQL query."""
0028         self.sql_query += ' ' + sql_text
0029 
0030     def __str__(self):
0031         """Custom string representation of the SQLQueryManager instance."""
0032         return self.format_query()
0033 
0034 
0035 # Example usage:
0036 if __name__ == "__main__":
0037     # Initialize the SQLQueryManager with an SQL query
0038     sql_manager = SQLQueryManager("""
0039         SELECT *
0040         FROM your_table
0041         WHERE condition = 'something'
0042         ORDER BY column_name
0043     """)
0044 
0045     # Log the SQL query
0046     print(sql_manager)  # This will print the formatted SQL query
0047 
0048     # Change the SQL query
0049     new_query = """
0050         SELECT id, name
0051         FROM another_table
0052         WHERE condition = 'another condition'
0053     """
0054     sql_manager.set_query(new_query)
0055     sql_manager.append("""
0056     AND condition2 = 'more conditions'
0057     """)
0058 
0059     # Log the updated SQL query
0060     print(sql_manager)  # This will print the updated formatted SQL query