Skip to content

Commit

Permalink
Merge pull request #13 from pablominue/pablo-develop
Browse files Browse the repository at this point in the history
Version 0.2.3
  • Loading branch information
pablominue authored Jul 6, 2024
2 parents a73acd0 + 48dcdf6 commit 10764ae
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 15 deletions.
Binary file added dist/pysqltools-0.2.3-py3-none-any.whl
Binary file not shown.
Binary file added dist/pysqltools-0.2.3.tar.gz
Binary file not shown.
Binary file added dist/pysqltools-0.2.4-py3-none-any.whl
Binary file not shown.
Binary file added dist/pysqltools-0.2.4.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysqltools"
version = "0.2.2"
version = "0.2.4"
description = "PySQLTools"
authors = ["Pablo Minué"]
license = "None"
Expand Down
5 changes: 3 additions & 2 deletions pysqltools/__init__.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions pysqltools/src/SQL/constants.py
Original file line number Diff line number Diff line change
@@ -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",
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
33 changes: 33 additions & 0 deletions pysqltools/src/SQL/table.py
Original file line number Diff line number Diff line change
@@ -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")
5 changes: 3 additions & 2 deletions pysqltools/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion pysqltools/src/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 0 additions & 5 deletions pysqltools/src/queries/constants.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
25 changes: 25 additions & 0 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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

0 comments on commit 10764ae

Please sign in to comment.