diff --git a/dist/pysqltools-0.2.3-py3-none-any.whl b/dist/pysqltools-0.2.3-py3-none-any.whl new file mode 100644 index 0000000..d6853d3 Binary files /dev/null and b/dist/pysqltools-0.2.3-py3-none-any.whl differ diff --git a/dist/pysqltools-0.2.3.tar.gz b/dist/pysqltools-0.2.3.tar.gz new file mode 100644 index 0000000..507529c Binary files /dev/null and b/dist/pysqltools-0.2.3.tar.gz differ diff --git a/dist/pysqltools-0.2.4-py3-none-any.whl b/dist/pysqltools-0.2.4-py3-none-any.whl new file mode 100644 index 0000000..1b01c42 Binary files /dev/null and b/dist/pysqltools-0.2.4-py3-none-any.whl differ diff --git a/dist/pysqltools-0.2.4.tar.gz b/dist/pysqltools-0.2.4.tar.gz new file mode 100644 index 0000000..b15fc31 Binary files /dev/null and b/dist/pysqltools-0.2.4.tar.gz differ diff --git a/pyproject.toml b/pyproject.toml index 782891e..92368c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pysqltools" -version = "0.2.2" +version = "0.2.4" description = "PySQLTools" authors = ["Pablo Minué"] license = "None" diff --git a/pysqltools/__init__.py b/pysqltools/__init__.py index be1a3bb..d080443 100644 --- a/pysqltools/__init__.py +++ b/pysqltools/__init__.py @@ -1,6 +1,7 @@ from .src.connection import SQLConnection -from .src.queries.insert import generate_insert_query, insert_pandas -from .src.queries.query import Query, SQLString +from .src.SQL.insert import generate_insert_query, insert_pandas +from .src.SQL.query import Query, SQLString +from .src.SQL.table import Table def format_sql(sql: str, **kwargs) -> str: diff --git a/pysqltools/src/queries/__init__.py b/pysqltools/src/SQL/__init__.py similarity index 54% rename from pysqltools/src/queries/__init__.py rename to pysqltools/src/SQL/__init__.py index d06ae53..8f69434 100644 --- a/pysqltools/src/queries/__init__.py +++ b/pysqltools/src/SQL/__init__.py @@ -2,4 +2,4 @@ Queries Package. Contains everything SQL-Text query related """ -from pysqltools.src.queries.query import Query, SQLString +from pysqltools.src.SQL.query import Query, SQLString diff --git a/pysqltools/src/SQL/constants.py b/pysqltools/src/SQL/constants.py new file mode 100644 index 0000000..7e5409d --- /dev/null +++ b/pysqltools/src/SQL/constants.py @@ -0,0 +1,13 @@ +""" +Constant values for the queries package +""" + +KEYWORDS = ["select", "from", "where", "group", "having", "order", "limit"] + +TYPE_MAPPING = { + "object": "varchar", + "int64": "int", + "float64": "double", + "bool": "bool", + "datetime64": "timestamp", +} diff --git a/pysqltools/src/queries/exceptions.py b/pysqltools/src/SQL/exceptions.py similarity index 100% rename from pysqltools/src/queries/exceptions.py rename to pysqltools/src/SQL/exceptions.py diff --git a/pysqltools/src/queries/insert.py b/pysqltools/src/SQL/insert.py similarity index 98% rename from pysqltools/src/queries/insert.py rename to pysqltools/src/SQL/insert.py index 24a9a77..2e4b330 100644 --- a/pysqltools/src/queries/insert.py +++ b/pysqltools/src/SQL/insert.py @@ -8,7 +8,7 @@ from pysqltools.src.connection import SQLConnection from pysqltools.src.log import PabLog -from pysqltools.src.queries.query import Query, assign_parameter +from pysqltools.src.SQL.query import Query, assign_parameter lg = PabLog("Insert") diff --git a/pysqltools/src/queries/query.py b/pysqltools/src/SQL/query.py similarity index 98% rename from pysqltools/src/queries/query.py rename to pysqltools/src/SQL/query.py index 63d8a2d..8686587 100644 --- a/pysqltools/src/queries/query.py +++ b/pysqltools/src/SQL/query.py @@ -10,7 +10,7 @@ import sqlparse from multimethod import multimethod -from pysqltools.src.queries.exceptions import QueryFormattingError +from pysqltools.src.SQL.exceptions import QueryFormattingError class SQLString(str): diff --git a/pysqltools/src/SQL/table.py b/pysqltools/src/SQL/table.py new file mode 100644 index 0000000..3339f87 --- /dev/null +++ b/pysqltools/src/SQL/table.py @@ -0,0 +1,33 @@ +""" +This module allows to interact with tables in SQL (Create, Alter, etc) +""" + +from typing import Any, Union + +import pandas as pd +import sqlparse + +from .constants import TYPE_MAPPING + + +class Table: + def __init__(self, table: str, schema: Union[str, None] = None) -> None: + self.table = table + if schema: + self.table = f"{schema}.{table}" + + def create_from_df(self, df: pd.DataFrame) -> str: + columns = dict( + zip( + df.dtypes.index.to_list(), + map( + lambda x: TYPE_MAPPING[x], + map(lambda x: str(x), df.dtypes.to_list()), + ), + ) + ) + sql = f"CREATE TABLE {self.table} ( " + for k, v in columns.items(): + sql += f"{k} {v}, " + sql = sql[:-2] + " )" + return sqlparse.format(sql, encoding="utf-8") diff --git a/pysqltools/src/__init__.py b/pysqltools/src/__init__.py index 6f78802..4ce04d7 100644 --- a/pysqltools/src/__init__.py +++ b/pysqltools/src/__init__.py @@ -2,5 +2,6 @@ Source code for the pysqltools package """ -from pysqltools.src.queries.insert import generate_insert_query -from pysqltools.src.queries.query import Query, SQLString +from pysqltools.src.SQL.insert import generate_insert_query +from pysqltools.src.SQL.query import Query, SQLString +from pysqltools.src.SQL.table import Table diff --git a/pysqltools/src/connection/connection.py b/pysqltools/src/connection/connection.py index def3384..3cfb2e4 100644 --- a/pysqltools/src/connection/connection.py +++ b/pysqltools/src/connection/connection.py @@ -13,7 +13,7 @@ from pysqltools.src.connection.exceptions import ConnectionException from pysqltools.src.log import PabLog -from pysqltools.src.queries.query import Query +from pysqltools.src.SQL.query import Query lg = PabLog("Connections") diff --git a/pysqltools/src/queries/constants.py b/pysqltools/src/queries/constants.py deleted file mode 100644 index 30bf3f0..0000000 --- a/pysqltools/src/queries/constants.py +++ /dev/null @@ -1,5 +0,0 @@ -""" -Constant values for the queries package -""" - -KEYWORDS = ["select", "from", "where", "group", "having", "order", "limit"] diff --git a/tests/test_connections.py b/tests/test_connections.py index c1382e1..8b45fdf 100644 --- a/tests/test_connections.py +++ b/tests/test_connections.py @@ -7,7 +7,7 @@ import pandas as pd from pysqltools.src.connection.connection import SQLConnection -from pysqltools.src.queries.insert import insert_pandas +from pysqltools.src.SQL.insert import insert_pandas df = pd.DataFrame( { diff --git a/tests/test_queries.py b/tests/test_queries.py index 3c74cec..b9a101d 100644 --- a/tests/test_queries.py +++ b/tests/test_queries.py @@ -1,8 +1,10 @@ from datetime import datetime import pandas as pd +import sqlparse from pysqltools.src import Query, generate_insert_query +from pysqltools.src.SQL.table import Table def test_ctes(): @@ -78,3 +80,26 @@ def test_cte_replacement(): """ q.replace_cte("test_2_cte", new_cte_content=new_cte) assert new_cte in q.sql + + +def test_create_table_string(): + expected = ( + "CREATE TABLE myTable ( col1 int, col11 double, col2 bool, col3 varchar )" + ) + + with open("tests/queries/test_cte.sql", "r", encoding="utf-8") as f: + sql = f.read() + q = Query(sql=sql) + + df = pd.DataFrame( + { + "col1": [1, 2, 3, 4, 5], + "col11": [float(i) for i in range(5)], + "col2": [True, True, False, True, False], + "col3": ["a", "b", "a", "a", "a"], + } + ) + table = Table("myTable") + table_sql = table.create_from_df(df) + + assert table_sql == expected