sqlthon 0.0.6__tar.gz → 0.0.7__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.6
3
+ Version: 0.0.7
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.6'
7
+ version = '0.0.7'
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.6'
8
+ __version__ = '0.0.7'
9
9
  __author__ = "SAUMS"
10
10
  __email__ = "saums1391@gmail.com"
11
11
  __description__ = "Operate With SQLite3 Buy Simple"
@@ -20,27 +20,40 @@ class _Expression:
20
20
  return _Expression(self, "AND", other)
21
21
  def __or__(self, other):
22
22
  return _Expression(self, "OR", other)
23
-
24
-
23
+
24
+
25
+ class _Aggregate:
26
+ def __init__(self, func, column="*"):
27
+ self.func = func
28
+ self.column = column
29
+
30
+ def __gt__(self, value): return _Expression(self, ">", value)
31
+ def __lt__(self, value): return _Expression(self, "<", value)
32
+ def __ge__(self, value): return _Expression(self, ">=", value)
33
+ def __le__(self, value): return _Expression(self, "<=", value)
34
+ def __eq__(self, value): return _Expression(self, "=", value)
35
+ def __ne__(self, value): return _Expression(self, "!=", value)
36
+
37
+ def __str__(self):
38
+ return f"{self.func}({self.column})"
39
+
40
+
25
41
  def _compile(expr):
26
42
  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)
43
+ left_sql, left_params = _compile(expr.left)
44
+ right_sql, right_params = _compile(expr.right)
29
45
  sql = f"{left_sql} {expr.op} {right_sql}"
30
46
  params = left_params + right_params
31
47
  return sql, params
32
-
48
+ elif isinstance(expr.left, _Aggregate):
49
+ sql = f"{expr.left} {expr.op} ?"
50
+ params = (expr.right,)
51
+ return sql, params
33
52
  else:
34
- field_name = expr.left.name
35
- sql = f'"{field_name}" {expr.op} ?'
53
+ sql = f'"{expr.left.name}" {expr.op} ?'
36
54
  params = (expr.right,)
37
55
  return sql, params
38
-
39
-
40
-
41
-
42
-
43
-
56
+
44
57
 
45
58
  class Connect:
46
59
  __slots__ = ("_con", "_cur", "_changes")
@@ -48,7 +61,7 @@ class Connect:
48
61
  from sqlite3 import connect as _cn
49
62
  self._con = _cn(database_path)
50
63
  self._cur = self._con.cursor()
51
- self._changes = []
64
+ self._changes: list = []
52
65
  del _cn
53
66
 
54
67
  # ____________| TABLE |____________
@@ -103,6 +116,39 @@ class Connect:
103
116
  return [i[1] for i in self.run_code(f"PRAGMA table_info({table_name})")]
104
117
 
105
118
  # ____________| RECORD |____________
119
+ 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):
120
+ query = "SELECT "
121
+ param = ()
122
+ if unique:
123
+ query += "DISTINCT "
124
+ query += f"{', '.join(columns) if columns else '*'} FROM {table_name}"
125
+ if compound:
126
+ query += f" {compound[0]} JOIN \"{compound[1]}\" ON {compound[2]} = {compound[3]}"
127
+ if condition:
128
+ _: tuple = _compile(condition)
129
+ query += f" WHERE {_[0]}"
130
+ param += _[1]
131
+ del _
132
+ if bundle:
133
+ query += f" GROUP BY {", ".join(bundle)}"
134
+ if filter_bundle:
135
+ _: tuple = _compile(filter_bundle)
136
+ query += f" HAVING {_[0]}"
137
+ param += _[1]
138
+ del _
139
+
140
+ if sort:
141
+ query += " ORDER BY "
142
+ if sort[-1].upper() == "ASC" or sort[-1].upper() == "DESC":
143
+ query += f"{", ".join(sort[:-1])} {sort[-1]}"
144
+ else:
145
+ query += ", ".join(sort)
146
+ if several is not None:
147
+ query += f" LIMIT {several}"
148
+ if jump:
149
+ query += f" OFFSET {jump}"
150
+ self._changes.append((query, param))
151
+
106
152
  def add_record(self, table_name: str, record_info: tuple) -> None:
107
153
  self._changes.append((f"INSERT INTO {table_name} VALUES({', '.join(['?']*len(record_info))})", record_info))
108
154
 
@@ -134,7 +180,22 @@ class Connect:
134
180
  def count_record(self, table_name: str) -> int:
135
181
  return self.run_code(f"SELECT COUNT(*) FROM {table_name}")[0][0]
136
182
 
183
+ # ____________| INDEX |_________
184
+ def add_index(self, table_name: str, column_name: str, unique: bool=False) -> None:
185
+ self._changes.append((f"CREATE INDEX{" UNIQUE" if unique else ""} {table_name}_{column_name} ON {table_name} ({column_name})",))
186
+
187
+ def delete_index(self, table_name: str, column_name: str) -> None:
188
+ self._changes.append((f"DROP INDEX {table_name}_{column_name}",))
189
+
190
+ # ____________| VIEW |_________
191
+ def add_view(self, view_name: str, query: str) -> None:
192
+ self._changes.append((f"CREATE VIEW {view_name} AS {query}",))
193
+
194
+ def delete_view(self, view_name: str) -> None:
195
+ self._changes.append((f"DROP VIEW {view_name}",))
137
196
 
197
+ def exist_view(self, view_name: str) -> bool:
198
+ return view_name in [i[0] for i in self.run_code("SELECT name FROM sqlite_master WHERE type='view'")]
138
199
  # ____________| OTHER |_________
139
200
 
140
201
  def run_code(self, code: str, parameters: tuple=()) -> list | None:
@@ -143,7 +204,7 @@ class Connect:
143
204
  try:
144
205
  return self._cur.fetchall()
145
206
  except:
146
- pass
207
+ return None
147
208
 
148
209
  def close(self):
149
210
  self._con.close()
@@ -11,6 +11,8 @@ NOT_NULL = "NOT NULL"
11
11
  PRIMARY_KEY = "PRIMARY KEY"
12
12
  AUTO_INCREMENT = "AUTOINCREMENT"
13
13
 
14
+ LEFT = "LEFT"
15
+ INNER = "INNER"
14
16
 
15
17
 
16
18
  def DEFAULT(value):
@@ -35,16 +37,16 @@ def DEFAULT_NOW():
35
37
 
36
38
 
37
39
  def COUNT(query="*"):
38
- return f"COUNT({query})"
40
+ return _Aggregate("COUNT", query)
39
41
 
40
42
  def SUM(query):
41
- return f"SUM({query})"
43
+ return _Aggregate("SUM", query)
42
44
 
43
45
  def AVG(query):
44
- return f"AVG({query})"
46
+ return _Aggregate("AVG", query)
45
47
 
46
48
  def MIN(query):
47
- return f"MIN({query})"
49
+ return _Aggregate("MIN", query)
48
50
 
49
51
  def MAX(query):
50
- return f"MAX({query})"
52
+ return _Aggregate("MAX", query)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlthon
3
- Version: 0.0.6
3
+ Version: 0.0.7
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