Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
v1a0 committed May 30, 2021
2 parents 56822c3 + 163d498 commit e8230c1
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 103 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ pip install sqllex

If you need most stable version install **sqllex==0.1.8.12**

| version | status |
| :--------: | :---------------------------- |
| `0.1.8.12` | ✔️ (most) stable |
| `0.1.9.0` | ⚠️ unstable (experimental) |


## About
Use databases without thinking about SQL. Let me show you how sqllex makes
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
packages=[
'sqllex', 'sqllex.classes', 'sqllex.constants', 'sqllex.exceptions', 'sqllex.types', 'sqllex.debug',
],
version='0.1.8.12',
version='0.1.9.0',
license='gpl-3.0',
description='Better than sqlite3',
author='v1a0',
url='https://github.com/v1a0/sqllex',
download_url='https://github.com/V1A0/sqllex/archive/refs/tags/v0.1.8.12.tar.gz',
download_url='https://github.com/V1A0/sqllex/archive/refs/tags/v0.1.9.0.tar.gz',
keywords=['sql', 'sql3', 'sqlite', 'sqlite3', 'sqllex', 'db', 'database', 'easy'],
install_requires=[
'colorama==0.4.4',
Expand Down
54 changes: 35 additions & 19 deletions sqllex/classes/sqlite3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def col_types_sort(val: Union[DataType, AnyStr]) -> int:
:return: index of priority, if unknown returns 1
"""

prior = CONST_PRIORITY.get(val)
prior = CONST_PRIORITY.get(val) # How about set dict.setdefault(1) ?

if prior is None:
return 1
Expand Down Expand Up @@ -60,7 +60,7 @@ def with_wrapper(*args, **kwargs):
with_dict: None = None

if with_dict:
script = f"WITH "
script = f"WITH RECURSIVE "
values = []

for (var, statement) in with_dict.items():
Expand Down Expand Up @@ -112,6 +112,7 @@ def __where__(func: callable) -> callable:
def where_wrapper(*args, **kwargs):
if "WHERE" in kwargs.keys():
where_: WhereType = kwargs.pop("WHERE")

else:
where_: None = None

Expand All @@ -134,6 +135,7 @@ def where_wrapper(*args, **kwargs):
for wh in where_:

# List[List] -> Dict[val[0], val[1]]

if isinstance(wh[0], str) and len(wh) > 1:
new_where.update({wh[0]: wh[1:]})
else:
Expand All @@ -143,6 +145,7 @@ def where_wrapper(*args, **kwargs):

if isinstance(where_, dict):
for (key, values) in where_.items():

if not isinstance(values, list):
values = [values]

Expand All @@ -159,6 +162,7 @@ def where_wrapper(*args, **kwargs):
"<>",
]:
operator = values.pop(0)

if len(values) == 1 and isinstance(
values[0], list
):
Expand Down Expand Up @@ -531,25 +535,32 @@ def wrap(self, *args, **kwargs):
return wrap


def lister(value: Any):
def lister(value: Any, remove_one_len: bool = False):
"""
Function converting input value from
Tuple[Any] or List[Tuple] with any deepness to List[List]
:param value: Any value contains tuples
:param remove_one_len: Convert
:return: Decorated method with update after it was run
"""

if isinstance(value, tuple):
value = list(value)

if isinstance(value, list):
if len(value) == 1:
return lister(value[0])
if remove_one_len and (len(value) == 1):
return lister(
value[0],
remove_one_len
)

for r in range(len(value)):
if isinstance(value[r], (list, tuple)):
value[r] = lister(value[r])
value[r] = lister(
value[r],
remove_one_len
)

return value

Expand All @@ -563,9 +574,11 @@ def tuples_to_lists(func: callable) -> callable:
"""

def t2l_wrapper(*args, **kwargs):
ret = lister(func(*args, **kwargs))
ret = func(*args, **kwargs)

if not issubclass(ret.__class__, SQLStatement):
ret = lister(ret)

if not isinstance(ret, list):
ret = [ret]

Expand Down Expand Up @@ -964,15 +977,17 @@ def _get_tables_(self) -> Generator[SQLite3xTable, None, None]:
# make it never end, because in the end of generation it'll be overridden
self.tables = self._get_tables_()

@tuples_to_lists
def _get_tables_names_(self) -> List[str]:
"""
Get list of tables names
:return: None
"""

return self.execute("SELECT name FROM sqlite_master WHERE type='table'")
return lister(
self.execute("SELECT name FROM sqlite_master WHERE type='table'"),
remove_one_len=True
)

@tuples_to_lists
@__execute__
Expand Down Expand Up @@ -1248,7 +1263,6 @@ def _select_stmt_(
"""
Parent method for all SELECT-like methods
"""

if not TABLE:
raise ArgumentError(TABLE="Argument unset and have not default value")

Expand Down Expand Up @@ -1345,7 +1359,7 @@ def connect(self):
"""
Create connection to database
:return: sqlite3.connection
:return: None
"""

if not self.connection:
Expand Down Expand Up @@ -1571,24 +1585,24 @@ def markup(
def get_columns(
self,
table: AnyStr
) -> Union[Tuple, List]:
) -> List[str]:
"""
Get list of table columns
:param table: schema-name.table-name or just table-name
:return: List[List] of columns
"""

columns = self.execute(f"SELECT name FROM PRAGMA_TABLE_INFO('{table}')")
columns_: List[List[str]] = self.execute(f"SELECT name FROM PRAGMA_TABLE_INFO('{table}')")

if not isinstance(columns, list):
columns = [columns]

if columns:
return columns
else:
if not columns_:
raise TableInfoError

# if not isinstance(columns, list):
# columns = [columns]

return list(map(lambda columns: columns[0], columns_))

def insert(
self,
TABLE: AnyStr,
Expand Down Expand Up @@ -1744,6 +1758,8 @@ def select(
WHERE = kwargs
kwargs = {}



return self._select_stmt_(
SELECT=SELECT,
TABLE=TABLE,
Expand Down
70 changes: 40 additions & 30 deletions sqllex/constants/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,44 @@


__all__ = [
"ALL",
"TEXT",
"NUMERIC",
"INTEGER",
"REAL",
"NONE",
"BLOB",
"NOT_NULL",
"DEFAULT",
"UNIQUE",
"AS",
"ON",

"PRIMARY_KEY",
"CHECK",
"AUTOINCREMENT",
"FOREIGN_KEY",
"REFERENCES",
"ABORT",
"FAIL",
"IGNORE",
"REPLACE",
"ROLLBACK",
"NULL",

"CONST_PRIORITY",

"INNER_JOIN",
"LEFT_JOIN",
"CROSS_JOIN",
'ABORT',
'ALL',
'AS',
'AUTOINCREMENT',

'BLOB',

'CHECK',
'CONST_PRIORITY',
'CROSS_JOIN',

'DEFAULT',

'FAIL',
'FOREIGN_KEY',

'IGNORE',
'INNER_JOIN',
'INTEGER',

'LEFT_JOIN',

'NONE',
'NOT_NULL',
'NULL',
'NUMERIC',

'ON',

'PRIMARY_KEY',

'REAL',
'REFERENCES',
'REPLACE',
'ROLLBACK',

'TEXT',

'UNIQUE'
]

Loading

0 comments on commit e8230c1

Please sign in to comment.