Parse a single (...) row from an INSERT VALUES clause into a tuple.
(line, col_names)
| 120 | |
| 121 | |
| 122 | def parse_insert_row(line, col_names): |
| 123 | """Parse a single (...) row from an INSERT VALUES clause into a tuple.""" |
| 124 | # Remove leading ( and trailing ), then split by , while handling quotes |
| 125 | line = line.strip() |
| 126 | if line.startswith('('): |
| 127 | line = line[1:] |
| 128 | if line.endswith(');') or line.endswith(');\n'): |
| 129 | line = line[:-2].strip() |
| 130 | elif line.endswith(')'): |
| 131 | line = line[:-1] |
| 132 | |
| 133 | values = [] |
| 134 | current = '' |
| 135 | in_quotes = False |
| 136 | quote_char = None |
| 137 | escape_next = False |
| 138 | |
| 139 | for ch in line: |
| 140 | if escape_next: |
| 141 | current += ch |
| 142 | escape_next = False |
| 143 | continue |
| 144 | if ch == '\\': |
| 145 | current += ch |
| 146 | escape_next = True |
| 147 | continue |
| 148 | if in_quotes: |
| 149 | current += ch |
| 150 | if ch == quote_char: |
| 151 | in_quotes = False |
| 152 | else: |
| 153 | if ch in ("'", '"'): |
| 154 | in_quotes = True |
| 155 | quote_char = ch |
| 156 | current += ch |
| 157 | elif ch == ',': |
| 158 | values.append(parse_sql_value(current)) |
| 159 | current = '' |
| 160 | else: |
| 161 | current += ch |
| 162 | |
| 163 | if current.strip(): |
| 164 | values.append(parse_sql_value(current.strip())) |
| 165 | |
| 166 | return tuple(values) |
| 167 | |
| 168 | |
| 169 | def column_map(col_str): |
no test coverage detected