Skip to content

Commit

Permalink
Change name of function cli to table2sql and add example to...
Browse files Browse the repository at this point in the history
...docstring of `convert_table_file_to_insert_statement` function
  • Loading branch information
piotrgredowski committed Apr 12, 2022
1 parent 3ae04f6 commit ddbf4a0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,venv
max-complexity = 10
per-file-ignores =
test_*.py:E501
# line too long
table2sql/main.py: E501,
10 changes: 5 additions & 5 deletions table2sql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
@click.option(
"--has-types-row",
is_flag=True,
help=f"""
If file contains row with types as row 1 (second row in file).
Available types: {', '.join(TYPES_MAP.keys())}.
""",
help=(
"If file contains row with types as row 1 (second row in file). "
f"Available types: {', '.join(TYPES_MAP.keys())}."
),
)
@click.option("--output-file", default=None, help="Name of file to write SQL insert to.")
def cli(
def table2sql(
path_to_file: str,
output_table: str,
delimiter: Optional[str],
Expand Down
24 changes: 23 additions & 1 deletion table2sql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def convert_table_file_to_insert_statement(
has_types_row: Optional[bool] = False,
sheet_name: Optional[str] = None,
):
"""Converts CSV file to SQL insert statements.
# noqa: E501
"""Converts table file (CSV or Excel) to SQL insert statements.
If file doesn't contain row with types as second row - every value is treated as string and
will be available in insert statement with single quotes.
Expand All @@ -89,6 +90,27 @@ def convert_table_file_to_insert_statement(
Returns:
str: SQL insert statement
Example:
```python
from table2sql import convert_table_file_to_insert_statement
## some.csv
# a,b,c,d
# int,str,float,sql
# 1,2,3,(SELECT id FROM another.table WHERE name = 'Paul')
# 5,6,7,(SELECT id FROM another.table WHERE name = 'Paul')
inserts = convert_table_file_to_insert_statement(
path_to_file="some.csv",
output_table="test.table",
has_types_row=True,
)
print(inserts)
# INSERT INTO some.table (a, b, c, d)
# VALUES (1, '2', 3.0, (SELECT id FROM another.table WHERE name = 'Paul')), (5, '6', 7.0, (SELECT id FROM another.table WHERE name = 'Paul'));
```
"""

file_extension = _get_file_extension(path_to_file)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from click.testing import CliRunner

from table2sql.cli import cli
from table2sql.cli import table2sql
from tests import TEST_TABLE_NAME
from tests.helpers import save_to_csv

Expand Down Expand Up @@ -40,7 +40,7 @@ def test_cli(test_data, delimiter, types_row, table_name, expected):
test_csv_filename = save_to_csv(test_data)

result = runner.invoke(
cli,
table2sql,
[
test_csv_filename,
"--output-table",
Expand All @@ -67,7 +67,7 @@ def test_cli_saving_file(test_data, delimiter, types_row, table_name, expected):

with tempfile.NamedTemporaryFile() as f:
result = runner.invoke(
cli,
table2sql,
[
test_csv_filename,
"--output-table",
Expand All @@ -93,7 +93,7 @@ def test_cli_not_supported_file_extension():
wrong_file = "file" + file_extension

result = runner.invoke(
cli,
table2sql,
[
wrong_file,
"--output-table",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from table2sql.main import convert_table_file_to_insert_statement
from table2sql import convert_table_file_to_insert_statement
from tests import TEST_TABLE_NAME
from tests.helpers import save_to_csv, save_to_excel

Expand Down

0 comments on commit ddbf4a0

Please sign in to comment.