Skip to content

Commit

Permalink
Rewrite sql generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ximenesuk committed Sep 14, 2023
1 parent 42b6eed commit dd4fbe3
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions etlhelper/etl.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,25 +575,20 @@ def generate_insert_sql(
}

# Namedtuples use a query with positional placeholders
if not hasattr(row, 'keys'):
if hasattr(row, '_asdict'):
paramstyle = helper.positional_paramstyle

# Convert namedtuple to dictionary to easily access keys
try:
row = row._asdict()
except AttributeError:
msg = f"Row is not a dictionary or namedtuple ({type(row)})"
raise ETLHelperInsertError(msg)

columns = row.keys()
row_dict = row._asdict()
columns = row_dict.keys()
if paramstyle == "numeric":
placeholders = [paramstyles[paramstyle].format(number=i + 1)
for i in range(len(columns))]
else:
placeholders = [paramstyles[paramstyle]] * len(columns)

# Dictionaries use a query with named placeholders
else:
elif hasattr(row, 'keys'):
paramstyle = helper.named_paramstyle
if not paramstyle:
msg = (f"Database connection ({str(conn.__class__)}) doesn't support named parameters. "
Expand All @@ -603,6 +598,10 @@ def generate_insert_sql(
columns = row.keys()
placeholders = [paramstyles[paramstyle].format(name=c) for c in columns]

else:
msg = f"Row is not a dictionary or namedtuple ({type(row)})"
raise ETLHelperInsertError(msg)

# Validate identifiers to prevent malicious code injection
for identifier in (table, *columns):
validate_identifier(identifier)
Expand Down

0 comments on commit dd4fbe3

Please sign in to comment.