sqlthon 0.0.2__tar.gz → 0.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sqlthon-0.0.2 → sqlthon-0.0.5}/PKG-INFO +1 -1
- {sqlthon-0.0.2 → sqlthon-0.0.5}/pyproject.toml +1 -1
- sqlthon-0.0.5/src/sqlthon/__init__.py +11 -0
- sqlthon-0.0.5/src/sqlthon/core.py +156 -0
- {sqlthon-0.0.2 → sqlthon-0.0.5}/src/sqlthon.egg-info/PKG-INFO +1 -1
- sqlthon-0.0.2/src/sqlthon/__init__.py +0 -11
- sqlthon-0.0.2/src/sqlthon/core.py +0 -106
- {sqlthon-0.0.2 → sqlthon-0.0.5}/README.md +0 -0
- {sqlthon-0.0.2 → sqlthon-0.0.5}/setup.cfg +0 -0
- {sqlthon-0.0.2 → sqlthon-0.0.5}/src/sqlthon.egg-info/SOURCES.txt +0 -0
- {sqlthon-0.0.2 → sqlthon-0.0.5}/src/sqlthon.egg-info/dependency_links.txt +0 -0
- {sqlthon-0.0.2 → sqlthon-0.0.5}/src/sqlthon.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Sqlthon - A Simple Package For SQLite3 Operations.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .core import Connect, Field
|
|
6
|
+
|
|
7
|
+
__all__ = ['Connect', 'Field']
|
|
8
|
+
__version__ = '0.0.5'
|
|
9
|
+
__author__ = "SAUMS"
|
|
10
|
+
__email__ = "saums1391@gmail.com"
|
|
11
|
+
__description__ = "Operate With SQLite3 Buy Simple"
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
class Field:
|
|
2
|
+
def __init__(self, name):
|
|
3
|
+
self.name = name
|
|
4
|
+
|
|
5
|
+
def __gt__(self, value): return _Expression(self, ">", value)
|
|
6
|
+
def __lt__(self, value): return _Expression(self, "<", value)
|
|
7
|
+
def __ge__(self, value): return _Expression(self, ">=", value)
|
|
8
|
+
def __le__(self, value): return _Expression(self, "<=", value)
|
|
9
|
+
def __eq__(self, value): return _Expression(self, "=", value)
|
|
10
|
+
def __ne__(self, value): return _Expression(self, "!=", value)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _Expression:
|
|
14
|
+
def __init__(self, left, op, right):
|
|
15
|
+
self.left = left
|
|
16
|
+
self.op = op
|
|
17
|
+
self.right = right
|
|
18
|
+
|
|
19
|
+
def __and__(self, other):
|
|
20
|
+
return _Expression(self, "AND", other)
|
|
21
|
+
def __or__(self, other):
|
|
22
|
+
return _Expression(self, "OR", other)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _compile(expr):
|
|
26
|
+
if isinstance(expr.left, _Expression) or isinstance(expr.right, _Expression):
|
|
27
|
+
left_sql, left_params = compile(expr.left)
|
|
28
|
+
right_sql, right_params = compile(expr.right)
|
|
29
|
+
sql = f"{left_sql} {expr.op} {right_sql}"
|
|
30
|
+
params = left_params + right_params
|
|
31
|
+
return sql, params
|
|
32
|
+
|
|
33
|
+
else:
|
|
34
|
+
field_name = expr.left.name
|
|
35
|
+
sql = f'"{field_name}" {expr.op} ?'
|
|
36
|
+
params = (expr.right,)
|
|
37
|
+
return sql, params
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Connect:
|
|
46
|
+
__slots__ = ("_con", "_cur", "_changes")
|
|
47
|
+
def __init__(self, database_path: str) -> None:
|
|
48
|
+
from sqlite3 import connect as _cn
|
|
49
|
+
self._con = _cn(database_path)
|
|
50
|
+
self._cur = self._con.cursor()
|
|
51
|
+
self._changes = []
|
|
52
|
+
del _cn
|
|
53
|
+
|
|
54
|
+
# ____________| TABLE |____________
|
|
55
|
+
|
|
56
|
+
def add_table(self, table_name: str, *columns: tuple, if_not_exists=False) -> None:
|
|
57
|
+
query = "CREATE TABLE "
|
|
58
|
+
if if_not_exists:
|
|
59
|
+
query += "IF NOT EXISTS "
|
|
60
|
+
query += f"{table_name} ("
|
|
61
|
+
for column in columns:
|
|
62
|
+
name, type = column[0], column[1]
|
|
63
|
+
try:
|
|
64
|
+
limits = column[2]
|
|
65
|
+
query += f"{name} {type} {" ".join(limits)}"
|
|
66
|
+
except IndexError:
|
|
67
|
+
query += f"{name} {type}"
|
|
68
|
+
|
|
69
|
+
query += ", "
|
|
70
|
+
query = query[:-2]+")"
|
|
71
|
+
self._changes.append((query,))
|
|
72
|
+
|
|
73
|
+
def rename_table(self, old_name_table: str, new_name_table: str) -> None:
|
|
74
|
+
self._changes.append((f"ALTER TABLE {old_name_table} RENAME TO {new_name_table}",))
|
|
75
|
+
|
|
76
|
+
def drop_table(self, table_name: str) -> None:
|
|
77
|
+
self._changes.append((f"DROP TABLE {table_name}",))
|
|
78
|
+
|
|
79
|
+
def exist_table(self, table_name: str) -> bool:
|
|
80
|
+
return table_name in [i[0] for i in self.run_code("SELECT name FROM sqlite_master WHERE type='table'")]
|
|
81
|
+
|
|
82
|
+
def copy_table(self, table_name: str, new_name_table: str) -> None:
|
|
83
|
+
self._changes.append((f"CREATE TABLE {new_name_table} AS SELECT * FROM {table_name}"),)
|
|
84
|
+
|
|
85
|
+
# ____________| COLUMN |____________
|
|
86
|
+
def add_column(self, table_name: str, column_name: str, column_type: str, limits: list=[]) -> None:
|
|
87
|
+
if limits:
|
|
88
|
+
limits = " ".join(limits)
|
|
89
|
+
else:
|
|
90
|
+
limits = ""
|
|
91
|
+
self._changes.append((f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type} {limits}",))
|
|
92
|
+
|
|
93
|
+
def drop_column(self, table_name: str, column_name: str) -> None:
|
|
94
|
+
self._changes.append((f"ALTER TABLE {table_name} DROP COLUMN {column_name}",))
|
|
95
|
+
|
|
96
|
+
def rename_column(self, table_name: str, old_name_column: str, new_name_column: str) -> None:
|
|
97
|
+
self._changes.append((f"ALTER TABLE {table_name} RENAME COLUMN {old_name_column} TO {new_name_column}",))
|
|
98
|
+
|
|
99
|
+
def exist_column(self, table_name: str, column_name: str) -> bool:
|
|
100
|
+
return column_name in [i[1] for i in self.run_code(f"PRAGMA table_info({table_name})")]
|
|
101
|
+
|
|
102
|
+
def get_columns(self, table_name: str) -> list:
|
|
103
|
+
return [i[1] for i in self.run_code(f"PRAGMA table_info({table_name})")]
|
|
104
|
+
|
|
105
|
+
# ____________| RECORD |____________
|
|
106
|
+
def add_record(self, table_name: str, record_info: tuple) -> None:
|
|
107
|
+
self._changes.append((f"INSERT INTO {table_name} VALUES({', '.join(['?']*len(record_info))})", record_info))
|
|
108
|
+
|
|
109
|
+
def delete_record(self, table_name, condition=None):
|
|
110
|
+
query = f"DELETE FROM {table_name}"
|
|
111
|
+
params = ()
|
|
112
|
+
if condition:
|
|
113
|
+
sql_where, params = _compile(condition)
|
|
114
|
+
query += " WHERE " + sql_where
|
|
115
|
+
self._changes.append((query, params))
|
|
116
|
+
|
|
117
|
+
def edit_record(self, table_name, new_info_record, condition=None):
|
|
118
|
+
query = f"UPDATE {table_name} SET "
|
|
119
|
+
params = ()
|
|
120
|
+
|
|
121
|
+
set_parts = []
|
|
122
|
+
for key, value in new_info_record.items():
|
|
123
|
+
set_parts.append(f'"{key}" = ?')
|
|
124
|
+
params += (value,)
|
|
125
|
+
query += ", ".join(set_parts)
|
|
126
|
+
|
|
127
|
+
if condition:
|
|
128
|
+
sql_where, where_params = _compile(condition)
|
|
129
|
+
query += " WHERE " + sql_where
|
|
130
|
+
params += where_params
|
|
131
|
+
|
|
132
|
+
self._changes.append((query, params))
|
|
133
|
+
|
|
134
|
+
def count_record(self, table_name: str) -> int:
|
|
135
|
+
return self.run_code(f"SELECT COUNT(*) FROM {table_name}")[0][0]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
# ____________| OTHER |_________
|
|
139
|
+
|
|
140
|
+
def run_code(self, code: str, parameters: tuple=()) -> list | None:
|
|
141
|
+
self._cur.execute(code, parameters)
|
|
142
|
+
self._con.commit()
|
|
143
|
+
try:
|
|
144
|
+
return self._cur.fetchall()
|
|
145
|
+
except:
|
|
146
|
+
pass
|
|
147
|
+
|
|
148
|
+
def close(self):
|
|
149
|
+
self._con.close()
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def save_to_data_base(self) -> None:
|
|
153
|
+
with self._con:
|
|
154
|
+
for sql_code in self._changes:
|
|
155
|
+
self._cur.execute(*sql_code)
|
|
156
|
+
self._changes.clear()
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import sqlite3 as sq
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class Connect:
|
|
5
|
-
__slots__ = ("_con", "_cur", "_changes")
|
|
6
|
-
def __init__(self, path: str) -> None:
|
|
7
|
-
self._con = sq.connect(path)
|
|
8
|
-
self._cur = self._con.cursor()
|
|
9
|
-
self._changes = []
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class Column:
|
|
13
|
-
__slots__ = ("_name", "_type", "_limits")
|
|
14
|
-
def __init__(self, name_column: str, type: str="", limits: list=[]):
|
|
15
|
-
self._name = name_column
|
|
16
|
-
self._type = " "+type if type else ""
|
|
17
|
-
self._limits = limits
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def add_table(self, name_table: str, *columns: Column, if_not_exist: bool=False) -> None:
|
|
21
|
-
text = "CREATE TABLE "
|
|
22
|
-
if if_not_exist:
|
|
23
|
-
text += "IF NOT EXISTS "
|
|
24
|
-
text += f"{name_table} ("
|
|
25
|
-
for column in columns:
|
|
26
|
-
text += column._name + column._type
|
|
27
|
-
if column._limits:
|
|
28
|
-
text += " " + str(column._limits).replace("[", "").replace("'", "").replace("]", "").replace(",", "")
|
|
29
|
-
text += ", "
|
|
30
|
-
text = f"{text[:-2]})"
|
|
31
|
-
|
|
32
|
-
self._changes.append((text,))
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def add_record(self, name_table: str, *record: list) -> None:
|
|
36
|
-
self._changes.append((f"INSERT INTO {name_table} VALUES ({"?, "*(len(record)-1)}?)", record))
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def find_table(self, word: str="") -> list:
|
|
40
|
-
self._cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
|
41
|
-
tables = [tup[0] for tup in self._cur.fetchall()]
|
|
42
|
-
return [table for table in tables if word in table]
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def find_record(self, name_table: str, columns: list|str="*", where: dict|str="") -> list:
|
|
46
|
-
text = "SELECT "
|
|
47
|
-
if type(columns) == list:
|
|
48
|
-
columns = str(columns).replace("[", "").replace("]", "").replace("'", "")
|
|
49
|
-
text += columns+" FROM "+name_table
|
|
50
|
-
if where:
|
|
51
|
-
equal = " WHERE " + str(list(where.keys())).replace("]", ", ").replace("[", "").replace("'", "").replace(", ", "=? and ")[:-5]
|
|
52
|
-
where = tuple(where.values())
|
|
53
|
-
try:
|
|
54
|
-
text += equal
|
|
55
|
-
except:
|
|
56
|
-
pass
|
|
57
|
-
|
|
58
|
-
self._cur.execute(text, where)
|
|
59
|
-
return self._cur.fetchall()
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def delete_table(self, name_table: str):
|
|
63
|
-
self._changes.append((f"DROP TABLE {name_table}",))
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
def delete_record(self, name_table: str, info_record: dict={}) -> None:
|
|
67
|
-
text = f"DELETE FROM {name_table}"
|
|
68
|
-
if info_record:
|
|
69
|
-
text += " WHERE "
|
|
70
|
-
equal = tuple(info_record.values())
|
|
71
|
-
info_record = str(list(info_record.keys())).replace("[", "").replace("'", "").replace("]", ", ").replace(", ", "=? ").replace(" ", " AND ")[:-5]
|
|
72
|
-
try:
|
|
73
|
-
equal
|
|
74
|
-
self._changes.append((text+info_record, equal))
|
|
75
|
-
except:
|
|
76
|
-
self._changes.append((text,))
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
def edit_record(self, name_table: str, new_record: dict, old_record: dict) -> None:
|
|
80
|
-
text = "UPDATE "+name_table+" SET "
|
|
81
|
-
equal = tuple(old_record.values())+tuple(new_record.values())
|
|
82
|
-
old_record = str(list(old_record.keys())).replace("[", "").replace("'", "").replace("]", ", ").replace(", ", "=? ").replace(" ", ", ")[:-2]
|
|
83
|
-
new_record = str(list(new_record.keys())).replace("[", "").replace("'", "").replace("]", ", ").replace(", ", "=? ").replace(" ", " AND ")[:-4]
|
|
84
|
-
|
|
85
|
-
text += old_record+" WHERE "+new_record
|
|
86
|
-
self._changes.append((text, equal))
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def run_code(self, code: str, parameters: tuple=()) -> list | None:
|
|
90
|
-
self._cur.execute(code, parameters)
|
|
91
|
-
self._con.commit()
|
|
92
|
-
try:
|
|
93
|
-
return self._cur.fetchall()
|
|
94
|
-
except:
|
|
95
|
-
pass
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def close(self):
|
|
99
|
-
self._con.close()
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def save_to_data_base(self) -> None:
|
|
103
|
-
with self._con:
|
|
104
|
-
for sql_code in self._changes:
|
|
105
|
-
self._cur.execute(*sql_code)
|
|
106
|
-
self._changes.clear()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|