Skip to content

Commit

Permalink
Handle nulls correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgredowski committed Apr 12, 2022
1 parent e641a3a commit 16830a7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ homepage = "https://github.com/piotrgredowski/table2sql"
license = "MIT"
name = "table2sql"
readme = "README.md"
version = "1.0.0"
version = "1.0.1"

[tool.poetry.urls]
"Bug Tracker" = "https://github.com/piotrgredowski/table2sql/issues"
Expand Down
2 changes: 1 addition & 1 deletion table2sql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "1.0.0"
__version__ = "1.0.1"

from .main import convert_table_file_to_insert_statement # noqa: F401
10 changes: 7 additions & 3 deletions table2sql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"float": float,
"str": lambda value: f"'{value}'",
"sql": lambda sql: sql,
"null": lambda _: "NULL",
}


Expand All @@ -26,10 +27,13 @@ def _get_columns_formatted(column_names: Iterable[str]):
def _get_values_formatted(values: Iterable[str], types: Optional[List[Callable]]):
values_ = []
for row in values:

if types:
row = [str(type_(value)) for type_, value in zip(types, row)]
row = [
"NULL" if value is None else str(type_(value)) for type_, value in zip(types, row)
]
else:
row = [str(value) for value in row]
row = ["NULL" if value is None else str(value) for value in row]
values_.append(f'({", ".join(row)})')

return ", ".join(values_)
Expand All @@ -46,8 +50,8 @@ def _get_insert_statement_formatted(
def _get_types_functions(types_str: Tuple[str, ...]):
types_functions = []
for type_str in types_str:
type_str = type_str.lower()
try:

type_ = TYPES_MAP[type_str]
except KeyError:
type_ = str
Expand Down
Binary file added tests/excel/test_file_with_empty_columns.xlsx
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,17 @@ def test_convert_table_file_to_insert_statement_from_excel_with_multiple_sheets(
os.remove(test_filename)

assert result_insert_statement == expected


def test_file_with_empty_columns():
result = convert_table_file_to_insert_statement(
path_to_file="./tests/excel/test_file_with_empty_columns.xlsx",
output_table="test.table",
delimiter="not relevant",
has_types_row=True,
)
expected = """
INSERT INTO test.table (a, b, c, d)
VALUES (1, NULL, 3.0, (SELECT 1)), (5, '6', NULL, (SELECT 1));""".strip()

assert result == expected

0 comments on commit 16830a7

Please sign in to comment.