sqlthon 0.0.10__tar.gz → 0.0.12__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.12
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.12'
8
8
  description = "A simple package for SQLite3 operations"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -0,0 +1,21 @@
1
+ """
2
+ A Simple Package For SQLite3 Operations.
3
+
4
+ ||||| |||| || |||||| || || |||||| || ||
5
+ || || || || || || || || || ||| ||
6
+ |||| || || || || |||||| || || ||||||
7
+ || |||| || || || || || || || |||
8
+ ||||| || |||||| || || || |||||| || ||
9
+
10
+ Github: https://Github.com/saums-1391/sqlthon
11
+ Channel: https://ble.ir/saums_channel
12
+
13
+ """
14
+
15
+ from .core import Connect, Field
16
+
17
+ __all__ = ['Connect', 'Field']
18
+ __version__ = '0.0.12'
19
+ __author__ = "SAUMS"
20
+ __email__ = "saums1391@gmail.com"
21
+ __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:
@@ -80,7 +79,7 @@ class Connect:
80
79
  try:
81
80
  limits = column[2]
82
81
  query += f"{name} {type} {" ".join(limits)}"
83
- except IndexError:
82
+ except:
84
83
  query += f"{name} {type}"
85
84
 
86
85
  query += ", "
@@ -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)
143
- query += f" HAVING {_[0]}"
144
- param += _[1]
145
- del _
141
+ __: tuple = _compile(filter_bundle)
142
+ query += f" HAVING {__[0]}"
143
+ param += __[1]
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,69 @@ 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
+ if records:
254
+ self.add_records(table_name, tuple(records))
255
+
256
+ def to_csv(self, table_name: str, csv_path: str) -> None:
257
+ import csv as _csv
258
+ with open(csv_path, "w", newline="", encoding="utf-8") as f:
259
+ writer = _csv.writer(f)
260
+ writer.writerow(self.get_columns(table_name))
261
+ for row in self.find_record(table_name):
262
+ writer.writerow(row)
224
263
  # ____________| OTHER |_________
225
-
264
+ def last_id(self) -> int:
265
+ return self.run_query("SELECT last_insert_rowid()")[0][0]
266
+
267
+ def database_info(self) -> dict:
268
+ from os.path import basename, exists, getsize
269
+ return {
270
+ "name": basename(self._path),
271
+ "size": getsize(self._path) if exists(self._path) else 0,
272
+ "count_tables": len(self.get_tables())
273
+ }
274
+
275
+ # ____________| BASIC |_________
226
276
  def run_query(self, code: str, parameters: tuple=()) -> list:
227
277
  self._cur.execute(code, parameters)
228
278
  self._con.commit()
229
279
  return self._cur.fetchall()
230
-
231
- def close(self):
232
- self._con.close()
233
-
280
+
281
+ def optimize(self) -> None:
282
+ self.save_to_database()
283
+ self._cur.execute("VACUUM")
284
+ self._con.commit()
234
285
 
235
286
  def save_to_database(self) -> None:
236
287
  with self._con:
237
288
  for sql_code in self._changes:
238
289
  self._cur.execute(*sql_code)
239
- self._changes.clear()
290
+ self._changes.clear()
291
+
292
+ def close(self):
293
+ self._con.close()
294
+
295
+ # ____________| MAGICALS |_________
296
+ def __enter__(self) -> "Connect":
297
+ return self
298
+
299
+ def __exit__(self, exc_type, exc_value, exc_tb) -> None:
300
+ self.save_to_database()
301
+ self.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlthon
3
- Version: 0.0.10
3
+ Version: 0.0.12
4
4
  Summary: A simple package for SQLite3 operations
5
5
  Author-email: SAUMS <mohamnown@gmail.com>
6
6
  License: MIT
@@ -1,11 +0,0 @@
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.10'
9
- __author__ = "SAUMS"
10
- __email__ = "saums1391@gmail.com"
11
- __description__ = "Operate With SQLite3 Buy Simple"
File without changes
File without changes