I am formatting a python sql string, and it goes outside the declare block which should…
- User
I am formatting a python sql string, and it goes outside the declare block which should only be modified inside this method and nothing else. what could be wrong in my code that it is modifying the string outside of the declare scope?
sql_query = """ use db_name go declare @cursor_table_name cursor, @var1 tinyint , @var2 smallint , @var3 varchar(10) , @var4 int , @var5 varchar(64) , @var6 int , @var7 varchar(64) , @var8 varchar(1) declare cursor_table_name insensitive cursor for select var1 , var2 , ... var8 from db_name..table_name for read only open cursor_table_name fetch cursor_table_name into @var1 , @var2 , @var3 , @var4 , @var5 , @var6 , @var7 , @var8 while @@sqlstatus = 0 begin fetch cursor_table_name into @var1 , @var2 , @var3 , @var4 , @var5 , @var6 , @var7 , @var8 end close cursor_table_name deallocate cursor_table_name """ def format_declare_block(sql): lines = sql.strip().split('\n') formatted_lines = [] variable_lines = [] for line in lines: if line.strip().lower() == 'declare': formatted_lines.append(line) elif line.strip().startswith('@'): variable_lines.append(line) else: if variable_lines: # If we have collected variable lines, process them formatted_lines.extend(format_variable_lines(variable_lines)) variable_lines = [] formatted_lines.append(line) # Append any remaining variable lines if variable_lines: formatted_lines.extend(format_variable_lines(variable_lines)) return '\n'.join(formatted_lines) def format_variable_lines(variable_lines): max_length = 0 variable_parts = [] for var_line in variable_lines: # Get current leading spaces leading_spaces = var_line[:len(var_line) - len(var_line.lstrip())] # Ensure the line starts with two spaces if var_line.strip(): if not leading_spaces.endswith(' '): # Add two leading spaces if not present leading_spaces += ' ' parts = [part.strip() for part in var_line.split(',') if part.strip()] variable_parts.extend(parts) for part in parts: name = part.split()[0] max_length = max(max_length, len(name)) max_length += 1 # Add space for alignment # Format the lines aligned_parts = [] for part in variable_parts: name_type = part.split() if len(name_type) > 1: aligned_parts.append(f'{leading_spaces}{name_type[0]:<{max_length}} {name_type[1]}') else: aligned_parts.append(part.strip()) # This case shouldn't normally happen return [',\n'.join(aligned_parts)] - Assistant
- User
wow it works perfectly! thanks!!!! it is perfect! Now, I want to add a new function that receive this sql string, and format it based on indentation rules, so this formatting should be related to the indentation of the variables. At most, a select or any other methods should have. I tried using sqlparse library but I think we need a custom formatted for this complex task.
The rules for the indentation are like this:
- Queries may consist of one or more lines and SQL statements must be separated by a blank line.
- Each nesting level must have three spaces, do not use tabs.
- Items of a similar nature should be placed in the same column.
Example of correct indented sql strings:
exec @w_error = sp_something @i_var1 = @w_var1 , @i_longer_var2 = @w_longer_var2 , @i_var3 = @w_var3 , @o_var_out1 = @w_var_out1 out , @o_var_longer_out2 = @w_var_longer_out2 out - Assistant
- User
AttributeError Traceback (most recent call last) Cell In[115], line 245 1 query = '''
sql 2 use cob_reca 3 go (...) 241 deallocate cursor_re_convenio 242''' 244 formatted_query = format_query(query) --> 245 formatted_query = format_sql_query(query) 246 print(formatted_query)Cell In[114], line 27 25 # Process assignments and align them 26 if '=' in stripped_line or 'out' in stripped_line: ---> 27 formatted_lines.append(format_assignment_line(stripped_line, indent_level)) 28 else: 29 formatted_lines.append(' ' * (indent_level * 3) + stripped_line)
Cell In[114], line 55 52 aligned_parts.append(part.strip()) 54 # Join the assignments, using the required indent spaces ---> 55 return ' ' * (indent_level * 3) + ',\n' + ' ' * (indent_level * 3).join(aligned_parts)
AttributeError: 'int' object has no attribute 'join'
- Assistant
- User
it is giving me the same error:
AttributeError Traceback (most recent call last) Cell In[121], line 245 1 query = '''
sql 2 use cob_reca 3 go (...) 241 deallocate cursor_re_convenio 242''' 244 formatted_query = format_query(query) --> 245 formatted_query = format_sql_query(formatted_query) 246 print(formatted_query)Cell In[120], line 27 25 # Process assignments and align them 26 if '=' in stripped_line or 'out' in stripped_line: ---> 27 formatted_lines.append(format_assignment_line(stripped_line, indent_level)) 28 else: 29 formatted_lines.append(' ' * (indent_level * 3) + stripped_line)
Cell In[120], line 56 53 aligned_parts.append(part.strip()) 55 # Join the assignments, using the required indent spaces ---> 56 return ' ' * (indent_level * 3) + ',\n' + ' ' * (indent_level * 3).join(aligned_parts)
AttributeError: 'int' object has no attribute 'join'
- Assistant