sqlthon 0.0.9__tar.gz → 0.0.11__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.9 → sqlthon-0.0.11}/PKG-INFO +1 -1
- {sqlthon-0.0.9 → sqlthon-0.0.11}/pyproject.toml +1 -1
- {sqlthon-0.0.9 → sqlthon-0.0.11}/src/sqlthon/__init__.py +1 -1
- {sqlthon-0.0.9 → sqlthon-0.0.11}/src/sqlthon/core.py +82 -17
- {sqlthon-0.0.9 → sqlthon-0.0.11}/src/sqlthon.egg-info/PKG-INFO +1 -1
- {sqlthon-0.0.9 → sqlthon-0.0.11}/README.md +0 -0
- {sqlthon-0.0.9 → sqlthon-0.0.11}/setup.cfg +0 -0
- {sqlthon-0.0.9 → sqlthon-0.0.11}/src/sqlthon/keywords.py +0 -0
- {sqlthon-0.0.9 → sqlthon-0.0.11}/src/sqlthon.egg-info/SOURCES.txt +0 -0
- {sqlthon-0.0.9 → sqlthon-0.0.11}/src/sqlthon.egg-info/dependency_links.txt +0 -0
- {sqlthon-0.0.9 → sqlthon-0.0.11}/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.11'
|
|
9
9
|
__author__ = "SAUMS"
|
|
10
10
|
__email__ = "saums1391@gmail.com"
|
|
11
11
|
__description__ = "Operate With SQLite3 Buy Simple"
|
|
@@ -59,16 +59,16 @@ def _compile(expr):
|
|
|
59
59
|
|
|
60
60
|
|
|
61
61
|
class Connect:
|
|
62
|
-
__slots__ = ("_con", "_cur", "_changes")
|
|
62
|
+
__slots__ = ("_con", "_cur", "_changes", "_path")
|
|
63
63
|
def __init__(self, database_path: str) -> None:
|
|
64
64
|
from sqlite3 import connect as _cn
|
|
65
|
+
self._path = database_path
|
|
65
66
|
self._con = _cn(database_path)
|
|
66
67
|
self._cur = self._con.cursor()
|
|
67
68
|
self._changes: list = []
|
|
68
69
|
del _cn
|
|
69
70
|
|
|
70
71
|
# ____________| TABLE |____________
|
|
71
|
-
|
|
72
72
|
def add_table(self, table_name: str, *columns: tuple, if_not_exists=False) -> None:
|
|
73
73
|
query = "CREATE TABLE "
|
|
74
74
|
if if_not_exists:
|
|
@@ -98,8 +98,11 @@ class Connect:
|
|
|
98
98
|
def copy_table(self, table_name: str, new_name_table: str) -> None:
|
|
99
99
|
self._changes.append((f"CREATE TABLE {new_name_table} AS SELECT * FROM {table_name}"),)
|
|
100
100
|
|
|
101
|
+
def get_tables(self) -> list:
|
|
102
|
+
return [i[0] for i in self.run_query("SELECT name FROM sqlite_master WHERE type='table'")]
|
|
103
|
+
|
|
101
104
|
# ____________| COLUMN |____________
|
|
102
|
-
def add_column(self, table_name: str, column_name: str, column_type: str, limits: list=[]) -> None:
|
|
105
|
+
def add_column(self, table_name: str, column_name: str, column_type: str, limits: list | str=[]) -> None:
|
|
103
106
|
if limits:
|
|
104
107
|
limits = " ".join(limits)
|
|
105
108
|
else:
|
|
@@ -135,10 +138,10 @@ class Connect:
|
|
|
135
138
|
if bundle:
|
|
136
139
|
query += f" GROUP BY {", ".join(bundle)}"
|
|
137
140
|
if filter_bundle:
|
|
138
|
-
|
|
141
|
+
__: tuple = _compile(filter_bundle)
|
|
139
142
|
query += f" HAVING {_[0]}"
|
|
140
143
|
param += _[1]
|
|
141
|
-
del
|
|
144
|
+
del __
|
|
142
145
|
|
|
143
146
|
if sort:
|
|
144
147
|
query += " ORDER BY "
|
|
@@ -150,7 +153,7 @@ class Connect:
|
|
|
150
153
|
query += f" LIMIT {several}"
|
|
151
154
|
if jump:
|
|
152
155
|
query += f" OFFSET {jump}"
|
|
153
|
-
self.
|
|
156
|
+
return self.run_query(query, param)
|
|
154
157
|
|
|
155
158
|
def add_record(self, table_name: str, record_info: tuple) -> None:
|
|
156
159
|
self._changes.append((f"INSERT INTO {table_name} VALUES({', '.join(['?']*len(record_info))})", record_info))
|
|
@@ -159,14 +162,15 @@ class Connect:
|
|
|
159
162
|
self._cur.executemany(f"INSERT INTO {table_name} VALUES ({", ".join(["?"]*len(info_records[0]))})", info_records)
|
|
160
163
|
self._con.commit()
|
|
161
164
|
|
|
162
|
-
def delete_record(self, table_name, condition
|
|
165
|
+
def delete_record(self, table_name, condition):
|
|
163
166
|
query = f"DELETE FROM {table_name}"
|
|
164
|
-
params = ()
|
|
165
|
-
|
|
166
|
-
sql_where, params = _compile(condition)
|
|
167
|
-
query += " WHERE " + sql_where
|
|
167
|
+
sql_where, params = _compile(condition)
|
|
168
|
+
query += " WHERE " + sql_where
|
|
168
169
|
self._changes.append((query, params))
|
|
169
170
|
|
|
171
|
+
def delete_all_record(self, table_name: str) -> None:
|
|
172
|
+
self._changes.append((f"DELETE FROM {table_name}",))
|
|
173
|
+
|
|
170
174
|
def edit_record(self, table_name, new_info_record, condition=None):
|
|
171
175
|
query = f"UPDATE {table_name} SET "
|
|
172
176
|
params = ()
|
|
@@ -187,6 +191,18 @@ class Connect:
|
|
|
187
191
|
def count_record(self, table_name: str) -> int:
|
|
188
192
|
return self.run_query(f"SELECT COUNT(*) FROM {table_name}")[0][0]
|
|
189
193
|
|
|
194
|
+
def upsert_record(self, table_name: str, info_record: tuple, conflict: tuple) -> None:
|
|
195
|
+
query = f"INSERT INTO {table_name} VALUES ({", ".join(["?"]*len(info_record))}) ON CONFLICT(\"{'", "'.join(conflict)}\") DO UPDATE SET "
|
|
196
|
+
param = info_record
|
|
197
|
+
colue = dict(zip(self.get_columns(table_name), info_record)) # columns value
|
|
198
|
+
for i in conflict:
|
|
199
|
+
del colue[i]
|
|
200
|
+
for key, value in colue.items():
|
|
201
|
+
query += f'"{key}"=?, '
|
|
202
|
+
param += (value,)
|
|
203
|
+
query = query[:-2]
|
|
204
|
+
self._changes.append((query, param))
|
|
205
|
+
|
|
190
206
|
# ____________| INDEX |_________
|
|
191
207
|
def add_index(self, table_name: str, column_name: str, unique: bool=False) -> None:
|
|
192
208
|
self._changes.append((f"CREATE INDEX{" UNIQUE" if unique else ""} {table_name}_{column_name} ON {table_name} ({column_name})",))
|
|
@@ -204,19 +220,68 @@ class Connect:
|
|
|
204
220
|
def exist_view(self, view_name: str) -> bool:
|
|
205
221
|
return view_name in [i[0] for i in self.run_query("SELECT name FROM sqlite_master WHERE type='view'")]
|
|
206
222
|
|
|
223
|
+
# ____________| BACKUP |_________
|
|
224
|
+
def backup_database(self, backup_path: str) -> None:
|
|
225
|
+
from shutil import copy2
|
|
226
|
+
self._con.commit()
|
|
227
|
+
copy2(self._path, backup_path)
|
|
228
|
+
|
|
229
|
+
# ____________| PANDAS |_________
|
|
230
|
+
def to_dataframe(self, table_name: str) -> "pd.DataFrame":
|
|
231
|
+
import pandas as pd
|
|
232
|
+
columns = self.get_columns(table_name)
|
|
233
|
+
result = self.run_query(f"SELECT * FROM {table_name}")
|
|
234
|
+
return pd.DataFrame(result, columns=columns)
|
|
235
|
+
|
|
236
|
+
# ____________| CSV |_________
|
|
237
|
+
def load_csv(self, table_name: str, csv_path: str) -> None:
|
|
238
|
+
import csv as _csv
|
|
239
|
+
|
|
240
|
+
with open(csv_path) as file:
|
|
241
|
+
reader = _csv.reader(file)
|
|
242
|
+
header = next(reader)
|
|
243
|
+
|
|
244
|
+
self.add_table(table_name, (header[0], "TEXT"))
|
|
245
|
+
for column in header[1:]:
|
|
246
|
+
self.add_column(table_name, column, "TEXT")
|
|
247
|
+
|
|
248
|
+
self.save_to_database()
|
|
249
|
+
|
|
250
|
+
records = []
|
|
251
|
+
for row in reader:
|
|
252
|
+
records.append(tuple(row))
|
|
253
|
+
|
|
254
|
+
self.add_records(table_name, tuple(records))
|
|
255
|
+
|
|
256
|
+
|
|
207
257
|
# ____________| OTHER |_________
|
|
208
|
-
|
|
258
|
+
def last_id(self) -> int:
|
|
259
|
+
return self.run_query("SELECT last_insert_rowid()")[0][0]
|
|
260
|
+
|
|
261
|
+
def database_info(self) -> dict:
|
|
262
|
+
from os.path import basename, exists, getsize
|
|
263
|
+
return {
|
|
264
|
+
"name": basename(self._path),
|
|
265
|
+
"size": getsize(self._path) if exists(self._path) else 0,
|
|
266
|
+
"count_tables": len(self.get_tables())
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
# ____________| BASIC |_________
|
|
209
270
|
def run_query(self, code: str, parameters: tuple=()) -> list:
|
|
210
271
|
self._cur.execute(code, parameters)
|
|
211
272
|
self._con.commit()
|
|
212
273
|
return self._cur.fetchall()
|
|
213
|
-
|
|
214
|
-
def
|
|
215
|
-
self.
|
|
216
|
-
|
|
274
|
+
|
|
275
|
+
def optimize(self) -> None:
|
|
276
|
+
self.save_to_database()
|
|
277
|
+
self._cur.execute("VACUUM")
|
|
278
|
+
self._con.commit()
|
|
217
279
|
|
|
218
280
|
def save_to_database(self) -> None:
|
|
219
281
|
with self._con:
|
|
220
282
|
for sql_code in self._changes:
|
|
221
283
|
self._cur.execute(*sql_code)
|
|
222
|
-
self._changes.clear()
|
|
284
|
+
self._changes.clear()
|
|
285
|
+
|
|
286
|
+
def close(self):
|
|
287
|
+
self._con.close()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|