sqlthon 0.0.10__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlthon
3
- Version: 0.0.10
3
+ Version: 0.0.11
4
4
  Summary: A simple package for SQLite3 operations
5
5
  Author-email: SAUMS <mohamnown@gmail.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sqlthon"
7
- version = '0.0.10'
7
+ version = '0.0.11'
8
8
  description = "A simple package for SQLite3 operations"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -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.10'
8
+ __version__ = '0.0.11'
9
9
  __author__ = "SAUMS"
10
10
  __email__ = "saums1391@gmail.com"
11
11
  __description__ = "Operate With SQLite3 Buy Simple"
@@ -69,7 +69,6 @@ class Connect:
69
69
  del _cn
70
70
 
71
71
  # ____________| TABLE |____________
72
-
73
72
  def add_table(self, table_name: str, *columns: tuple, if_not_exists=False) -> None:
74
73
  query = "CREATE TABLE "
75
74
  if if_not_exists:
@@ -103,7 +102,7 @@ class Connect:
103
102
  return [i[0] for i in self.run_query("SELECT name FROM sqlite_master WHERE type='table'")]
104
103
 
105
104
  # ____________| COLUMN |____________
106
- 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:
107
106
  if limits:
108
107
  limits = " ".join(limits)
109
108
  else:
@@ -139,10 +138,10 @@ class Connect:
139
138
  if bundle:
140
139
  query += f" GROUP BY {", ".join(bundle)}"
141
140
  if filter_bundle:
142
- _: tuple = _compile(filter_bundle)
141
+ __: tuple = _compile(filter_bundle)
143
142
  query += f" HAVING {_[0]}"
144
143
  param += _[1]
145
- del _
144
+ del __
146
145
 
147
146
  if sort:
148
147
  query += " ORDER BY "
@@ -154,7 +153,7 @@ class Connect:
154
153
  query += f" LIMIT {several}"
155
154
  if jump:
156
155
  query += f" OFFSET {jump}"
157
- self._changes.append((query, param))
156
+ return self.run_query(query, param)
158
157
 
159
158
  def add_record(self, table_name: str, record_info: tuple) -> None:
160
159
  self._changes.append((f"INSERT INTO {table_name} VALUES({', '.join(['?']*len(record_info))})", record_info))
@@ -163,14 +162,15 @@ class Connect:
163
162
  self._cur.executemany(f"INSERT INTO {table_name} VALUES ({", ".join(["?"]*len(info_records[0]))})", info_records)
164
163
  self._con.commit()
165
164
 
166
- def delete_record(self, table_name, condition=None):
165
+ def delete_record(self, table_name, condition):
167
166
  query = f"DELETE FROM {table_name}"
168
- params = ()
169
- if condition:
170
- sql_where, params = _compile(condition)
171
- query += " WHERE " + sql_where
167
+ sql_where, params = _compile(condition)
168
+ query += " WHERE " + sql_where
172
169
  self._changes.append((query, params))
173
170
 
171
+ def delete_all_record(self, table_name: str) -> None:
172
+ self._changes.append((f"DELETE FROM {table_name}",))
173
+
174
174
  def edit_record(self, table_name, new_info_record, condition=None):
175
175
  query = f"UPDATE {table_name} SET "
176
176
  params = ()
@@ -191,6 +191,18 @@ class Connect:
191
191
  def count_record(self, table_name: str) -> int:
192
192
  return self.run_query(f"SELECT COUNT(*) FROM {table_name}")[0][0]
193
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
+
194
206
  # ____________| INDEX |_________
195
207
  def add_index(self, table_name: str, column_name: str, unique: bool=False) -> None:
196
208
  self._changes.append((f"CREATE INDEX{" UNIQUE" if unique else ""} {table_name}_{column_name} ON {table_name} ({column_name})",))
@@ -221,19 +233,55 @@ class Connect:
221
233
  result = self.run_query(f"SELECT * FROM {table_name}")
222
234
  return pd.DataFrame(result, columns=columns)
223
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
+
224
257
  # ____________| OTHER |_________
225
-
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 |_________
226
270
  def run_query(self, code: str, parameters: tuple=()) -> list:
227
271
  self._cur.execute(code, parameters)
228
272
  self._con.commit()
229
273
  return self._cur.fetchall()
230
-
231
- def close(self):
232
- self._con.close()
233
-
274
+
275
+ def optimize(self) -> None:
276
+ self.save_to_database()
277
+ self._cur.execute("VACUUM")
278
+ self._con.commit()
234
279
 
235
280
  def save_to_database(self) -> None:
236
281
  with self._con:
237
282
  for sql_code in self._changes:
238
283
  self._cur.execute(*sql_code)
239
- self._changes.clear()
284
+ self._changes.clear()
285
+
286
+ def close(self):
287
+ self._con.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlthon
3
- Version: 0.0.10
3
+ Version: 0.0.11
4
4
  Summary: A simple package for SQLite3 operations
5
5
  Author-email: SAUMS <mohamnown@gmail.com>
6
6
  License: MIT
File without changes
File without changes