sqlthon 0.0.8__tar.gz → 0.0.10__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.8 → sqlthon-0.0.10}/PKG-INFO +1 -1
- {sqlthon-0.0.8 → sqlthon-0.0.10}/pyproject.toml +1 -1
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon/__init__.py +1 -1
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon/core.py +34 -12
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon.egg-info/PKG-INFO +1 -1
- {sqlthon-0.0.8 → sqlthon-0.0.10}/README.md +0 -0
- {sqlthon-0.0.8 → sqlthon-0.0.10}/setup.cfg +0 -0
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon/keywords.py +0 -0
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon.egg-info/SOURCES.txt +0 -0
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon.egg-info/dependency_links.txt +0 -0
- {sqlthon-0.0.8 → sqlthon-0.0.10}/src/sqlthon.egg-info/top_level.txt +0 -0
|
@@ -5,7 +5,7 @@ Sqlthon - A Simple Package For SQLite3 Operations.
|
|
|
5
5
|
from .core import Connect, Field
|
|
6
6
|
|
|
7
7
|
__all__ = ['Connect', 'Field']
|
|
8
|
-
__version__ = '0.0.
|
|
8
|
+
__version__ = '0.0.10'
|
|
9
9
|
__author__ = "SAUMS"
|
|
10
10
|
__email__ = "saums1391@gmail.com"
|
|
11
11
|
__description__ = "Operate With SQLite3 Buy Simple"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
class Field:
|
|
2
|
+
__slots__ = ("name",)
|
|
2
3
|
def __init__(self, name):
|
|
3
4
|
self.name = name
|
|
4
5
|
|
|
@@ -11,6 +12,7 @@ class Field:
|
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class _Expression:
|
|
15
|
+
__slots__ = ("left", "op", "right")
|
|
14
16
|
def __init__(self, left, op, right):
|
|
15
17
|
self.left = left
|
|
16
18
|
self.op = op
|
|
@@ -23,6 +25,7 @@ class _Expression:
|
|
|
23
25
|
|
|
24
26
|
|
|
25
27
|
class _Aggregate:
|
|
28
|
+
__slots__ = ("func", "column")
|
|
26
29
|
def __init__(self, func, column="*"):
|
|
27
30
|
self.func = func
|
|
28
31
|
self.column = column
|
|
@@ -56,9 +59,10 @@ def _compile(expr):
|
|
|
56
59
|
|
|
57
60
|
|
|
58
61
|
class Connect:
|
|
59
|
-
__slots__ = ("_con", "_cur", "_changes")
|
|
62
|
+
__slots__ = ("_con", "_cur", "_changes", "_path")
|
|
60
63
|
def __init__(self, database_path: str) -> None:
|
|
61
64
|
from sqlite3 import connect as _cn
|
|
65
|
+
self._path = database_path
|
|
62
66
|
self._con = _cn(database_path)
|
|
63
67
|
self._cur = self._con.cursor()
|
|
64
68
|
self._changes: list = []
|
|
@@ -90,11 +94,14 @@ class Connect:
|
|
|
90
94
|
self._changes.append((f"DROP TABLE {table_name}",))
|
|
91
95
|
|
|
92
96
|
def exist_table(self, table_name: str) -> bool:
|
|
93
|
-
return table_name in [i[0] for i in self.
|
|
97
|
+
return table_name in [i[0] for i in self.run_query("SELECT name FROM sqlite_master WHERE type='table'")]
|
|
94
98
|
|
|
95
99
|
def copy_table(self, table_name: str, new_name_table: str) -> None:
|
|
96
100
|
self._changes.append((f"CREATE TABLE {new_name_table} AS SELECT * FROM {table_name}"),)
|
|
97
101
|
|
|
102
|
+
def get_tables(self) -> list:
|
|
103
|
+
return [i[0] for i in self.run_query("SELECT name FROM sqlite_master WHERE type='table'")]
|
|
104
|
+
|
|
98
105
|
# ____________| COLUMN |____________
|
|
99
106
|
def add_column(self, table_name: str, column_name: str, column_type: str, limits: list=[]) -> None:
|
|
100
107
|
if limits:
|
|
@@ -110,10 +117,10 @@ class Connect:
|
|
|
110
117
|
self._changes.append((f"ALTER TABLE {table_name} RENAME COLUMN {old_name_column} TO {new_name_column}",))
|
|
111
118
|
|
|
112
119
|
def exist_column(self, table_name: str, column_name: str) -> bool:
|
|
113
|
-
return column_name in [i[1] for i in self.
|
|
120
|
+
return column_name in [i[1] for i in self.run_query(f"PRAGMA table_info({table_name})")]
|
|
114
121
|
|
|
115
122
|
def get_columns(self, table_name: str) -> list:
|
|
116
|
-
return [i[1] for i in self.
|
|
123
|
+
return [i[1] for i in self.run_query(f"PRAGMA table_info({table_name})")]
|
|
117
124
|
|
|
118
125
|
# ____________| RECORD |____________
|
|
119
126
|
def find_record(self, table_name: str, columns: None | tuple=None, compound: None | tuple=None, condition: None | _Expression=None, bundle: None | tuple=None, filter_bundle: None | tuple=None, sort: None | tuple=None, several: None | int=None, jump: int=0, unique: bool=False):
|
|
@@ -151,6 +158,10 @@ class Connect:
|
|
|
151
158
|
|
|
152
159
|
def add_record(self, table_name: str, record_info: tuple) -> None:
|
|
153
160
|
self._changes.append((f"INSERT INTO {table_name} VALUES({', '.join(['?']*len(record_info))})", record_info))
|
|
161
|
+
|
|
162
|
+
def add_records(self, table_name: str, info_records: tuple) -> None:
|
|
163
|
+
self._cur.executemany(f"INSERT INTO {table_name} VALUES ({", ".join(["?"]*len(info_records[0]))})", info_records)
|
|
164
|
+
self._con.commit()
|
|
154
165
|
|
|
155
166
|
def delete_record(self, table_name, condition=None):
|
|
156
167
|
query = f"DELETE FROM {table_name}"
|
|
@@ -178,7 +189,7 @@ class Connect:
|
|
|
178
189
|
self._changes.append((query, params))
|
|
179
190
|
|
|
180
191
|
def count_record(self, table_name: str) -> int:
|
|
181
|
-
return self.
|
|
192
|
+
return self.run_query(f"SELECT COUNT(*) FROM {table_name}")[0][0]
|
|
182
193
|
|
|
183
194
|
# ____________| INDEX |_________
|
|
184
195
|
def add_index(self, table_name: str, column_name: str, unique: bool=False) -> None:
|
|
@@ -195,22 +206,33 @@ class Connect:
|
|
|
195
206
|
self._changes.append((f"DROP VIEW {view_name}",))
|
|
196
207
|
|
|
197
208
|
def exist_view(self, view_name: str) -> bool:
|
|
198
|
-
return view_name in [i[0] for i in self.
|
|
209
|
+
return view_name in [i[0] for i in self.run_query("SELECT name FROM sqlite_master WHERE type='view'")]
|
|
210
|
+
|
|
211
|
+
# ____________| BACKUP |_________
|
|
212
|
+
def backup_database(self, backup_path: str) -> None:
|
|
213
|
+
from shutil import copy2
|
|
214
|
+
self._con.commit()
|
|
215
|
+
copy2(self._path, backup_path)
|
|
216
|
+
|
|
217
|
+
# ____________| PANDAS |_________
|
|
218
|
+
def to_dataframe(self, table_name: str) -> "pd.DataFrame":
|
|
219
|
+
import pandas as pd
|
|
220
|
+
columns = self.get_columns(table_name)
|
|
221
|
+
result = self.run_query(f"SELECT * FROM {table_name}")
|
|
222
|
+
return pd.DataFrame(result, columns=columns)
|
|
223
|
+
|
|
199
224
|
# ____________| OTHER |_________
|
|
200
225
|
|
|
201
|
-
def
|
|
226
|
+
def run_query(self, code: str, parameters: tuple=()) -> list:
|
|
202
227
|
self._cur.execute(code, parameters)
|
|
203
228
|
self._con.commit()
|
|
204
|
-
|
|
205
|
-
return self._cur.fetchall()
|
|
206
|
-
except:
|
|
207
|
-
return None
|
|
229
|
+
return self._cur.fetchall()
|
|
208
230
|
|
|
209
231
|
def close(self):
|
|
210
232
|
self._con.close()
|
|
211
233
|
|
|
212
234
|
|
|
213
|
-
def
|
|
235
|
+
def save_to_database(self) -> None:
|
|
214
236
|
with self._con:
|
|
215
237
|
for sql_code in self._changes:
|
|
216
238
|
self._cur.execute(*sql_code)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|