Skip to content

New feature. You don't have to import sql types #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions sqllex/core/entities/abc/sql_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ def content_gen(parameters, column=None) -> str:
'col': ["INTEGER", "NOT NULL"],
}
"""
parameters = sorted(parameters, key=lambda par: sort.column_types(par))
parameters = sorted(parameters,
key=lambda par: sort.column_types(par))
return script_gen.column(name=column, params=tuple(parameters))

elif isinstance(parameters, tuple):
Expand All @@ -697,7 +698,8 @@ def content_gen(parameters, column=None) -> str:
'col': ("INTEGER", "NOT NULL"),
}
"""
parameters = sorted(list(parameters), key=lambda par: sort.column_types(par))
parameters = sorted(list(parameters),
key=lambda par: sort.column_types(par))
return script_gen.column(name=column, params=tuple(parameters))

elif isinstance(parameters, dict):
Expand All @@ -709,20 +711,46 @@ def content_gen(parameters, column=None) -> str:
}
"""
if column != FOREIGN_KEY:
raise TypeError(f'Incorrect column "{column}" initialisation: {parameters}')
raise TypeError(f'Incorrect column "{column}" '
f'initialisation: {parameters}')

res = ""
for (key, refs) in parameters.items():
if isinstance(refs, (list, tuple)):
res += script_gen.column_with_foreign_key(key=key, table=refs[0], column=refs[1])
res += script_gen.column_with_foreign_key(
key=key, table=refs[0], column=refs[1])
if isinstance(refs, AbstractColumn):
res += script_gen.column_with_foreign_key(key=key, table=refs.table, column=refs.name)
res += script_gen.column_with_foreign_key(
key=key, table=refs.table, column=refs.name)

return res[:-1]

else:
raise TypeError(f'Incorrect column "{column}" initialisation, parameters type {type(parameters)}, '
f'expected tuple, list or str')
raise TypeError(f'Incorrect column "{column}" initialisation, '
f'parameters type {type(parameters)}, '
'expected tuple, list or str')

def translate_param(param: Union[type, str]) -> str:
dictionary = {int: "INTEGER",
str: "TEXT",
float: "REAL",
None: "NULL"}

translation = dictionary.get(param)
return translation if translation else param

def translate_params(parameters: Union[type, str]) -> str:
if isinstance(parameters, (str, dict)):
return parameters

if isinstance(parameters, list):
parameters = [translate_param(param) for param in parameters]
elif isinstance(parameters, list):
parameters = (translate_param(param) for param in parameters)
else:
parameters = translate_param(parameters)

return parameters

if not columns:
raise ValueError("Zero-column tables aren't supported in SQLite")
Expand All @@ -731,7 +759,7 @@ def content_gen(parameters, column=None) -> str:
values = ()

for (col, params) in columns.items():
content += content_gen(params, column=col)
content += content_gen(translate_params(params), column=col)

script = script_gen.create(
temp=temp,
Expand Down
2 changes: 1 addition & 1 deletion sqllex/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
ConstantType,
str
]
ColumnsType = Mapping[str, ColumnType]
ColumnsType = Mapping[Union[str, type], ColumnType]

# Type for databases template
DBTemplateType = Mapping[
Expand Down
15 changes: 7 additions & 8 deletions tests/temp_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@
db.create_table(
'suggestions',
{
# 'sid': [sx.TEXT, sx.PRIMARY_KEY, sx.AUTOINCREMENT],
'sid': [sx.INTEGER, sx.PRIMARY_KEY, sx.AUTOINCREMENT],
'uid': [sx.INTEGER, sx.NOT_NULL],
'category': [sx.TEXT, sx.NOT_NULL], # header|article|idea
'status': [sx.TEXT, sx.NOT_NULL, sx.DEFAULT, "sent"],
'sid': [int, sx.PRIMARY_KEY, sx.AUTOINCREMENT],
'uid': [int, sx.NOT_NULL],
'category': [str, sx.NOT_NULL], # header|article|idea
'status': [str, sx.NOT_NULL, sx.DEFAULT, "sent"],
# sent|rejected|in_work|posted
'comment': [sx.TEXT],
'date': [sx.TEXT, sx.NOT_NULL]
'comment': [str],
'date': [str, sx.NOT_NULL]
}, IF_NOT_EXIST=True
)

db.insert(
'suggestions',
{
"uid": 1,
"category": 'category',
"category": 'header',
"date": datetime.now().strftime("%Y-%m-%d %H:%M")
}
)
Expand Down
18 changes: 9 additions & 9 deletions tests/test_postgresqlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_create_table_basic(self):
"""
Testing table creating
"""
columns = {'id': INTEGER}
columns = {'id': int}

self.db.create_table(
'test_table_1',
Expand All @@ -241,10 +241,10 @@ def test_create_table_all_columns(self):
self.db.create_table(
name='test_table',
columns={
'id': [INTEGER, PRIMARY_KEY],
'user': [TEXT, UNIQUE, NOT_NULL],
'about': [TEXT, DEFAULT, NULL],
'status': [TEXT, DEFAULT, "'offline'"]
'id': [int, PRIMARY_KEY],
'user': [str, UNIQUE, NOT_NULL],
'about': [str, DEFAULT, NULL],
'status': [str, DEFAULT, "'offline'"]
}
)

Expand All @@ -254,9 +254,9 @@ def test_create_table_all_columns(self):
name='test_table_1',
columns={
'id': [SERIAL, PRIMARY_KEY],
'user': [TEXT, UNIQUE, NOT_NULL],
'about': [TEXT, DEFAULT, NULL],
'status': [TEXT, DEFAULT, "'offline'"]
'user': [str, UNIQUE, NOT_NULL],
'about': [str, DEFAULT, NULL],
'status': [str, DEFAULT, "'offline'"]
}
)
self.assertEqual(self.raw_sql_get_tables_names(), ('test_table', 'test_table_1'))
Expand All @@ -265,7 +265,7 @@ def test_create_table_inx(self):
"""
Testing if not exist kwarg
"""
columns = {'id': INTEGER}
columns = {'id': int}

self.db.create_table('test_table_1', columns, IF_NOT_EXIST=True)
self.db.create_table('test_table_2', columns, IF_NOT_EXIST=True)
Expand Down