Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 6937f39

Browse files
committed
fyCursor to self
1 parent 8fdb8b3 commit 6937f39

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

fyCursor/core/core.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# pyright: reportPrivateUsage=false
2-
import sqlite3
32
import logging
43

4+
from sqlite3 import Cursor, Connection, ProgrammingError
5+
from sqlite3.dbapi2 import _Parameters
6+
57
from .fields import Field
6-
from typing import Union, Any, Optional
8+
from typing import Union, Any, Optional, Self
79

810
NULL = None # python None is same as "NULL" here
911

@@ -106,7 +108,7 @@ def create(self, if_not_exist: bool = False) -> "Table":
106108
return self.cursor.create_table(self, if_not_exist)
107109

108110

109-
class fyCursor(sqlite3.Cursor):
111+
class fyCursor(Cursor):
110112
"""
111113
Custom `sqlite3.Cursor` that can be used without string query. \n
112114
I just hate query because it does not have any highlighting in IDE, yeah.
@@ -115,7 +117,7 @@ class fyCursor(sqlite3.Cursor):
115117
"""
116118
def __init__(
117119
self,
118-
__cursor: sqlite3.Connection,
120+
__cursor: Connection,
119121
logger: Any = None
120122
) -> None:
121123
"""
@@ -133,7 +135,7 @@ def __init__(
133135
"fyCursor"
134136
) if logger is None else logger
135137

136-
def update(self, table: str) -> 'fyCursor':
138+
def update(self, table: str) -> Self:
137139
"""
138140
Use this as SQL `UPDATE {table}` method
139141
@@ -143,7 +145,7 @@ def update(self, table: str) -> 'fyCursor':
143145
self._query = f"UPDATE {table}"
144146
return self
145147

146-
def add(self, **kwargs: Any) -> 'fyCursor':
148+
def add(self, **kwargs: Any) -> Self:
147149
"""
148150
Use this as SQL `SET {kwargs.keys}={kwargs.keys}+{kwargs.values}`
149151
@@ -155,15 +157,15 @@ def add(self, **kwargs: Any) -> 'fyCursor':
155157
`fyCursor.update()` or something similar before this statement
156158
"""
157159
if not self._query:
158-
raise sqlite3.ProgrammingError(
160+
raise ProgrammingError(
159161
"You should use something before `add`"
160162
)
161163
column = list(kwargs.keys())[0]
162164
value = list(kwargs.values())[0]
163165
self._query += f" SET {column} = {column} + {value}"
164166
return self
165167

166-
def set(self, fix: bool = True, **kwargs: Any) -> 'fyCursor':
168+
def set(self, fix: bool = True, **kwargs: Any) -> Self:
167169
"""
168170
Use this as SQL`SET {kwargs.keys} = {kwargs.values}`
169171
@@ -173,7 +175,7 @@ def set(self, fix: bool = True, **kwargs: Any) -> 'fyCursor':
173175
`fyCursor.update()` or something similar before this statement
174176
"""
175177
if not self._query:
176-
raise sqlite3.ProgrammingError(
178+
raise ProgrammingError(
177179
"You should use something before `set`"
178180
)
179181

@@ -187,7 +189,7 @@ def set(self, fix: bool = True, **kwargs: Any) -> 'fyCursor':
187189
self._query += f" SET {column} = {value}"
188190
return self
189191

190-
def select(self, value: str, from_: Optional[str] = None) -> 'fyCursor':
192+
def select(self, value: str, from_: Optional[str] = None) -> Self:
191193
"""
192194
Use this as SQL `SELECT {value} FROM {from_}`
193195
@@ -201,13 +203,13 @@ def select(self, value: str, from_: Optional[str] = None) -> 'fyCursor':
201203
self._from(from_)
202204
return self
203205

204-
def _from(self, table: str) -> 'fyCursor':
206+
def _from(self, table: str) -> Self:
205207
self._query += f" FROM {table}" # type: ignore
206208
return self
207209

208-
def where(self, **kwargs: Any) -> 'fyCursor':
210+
def where(self, **kwargs: Any) -> Self:
209211
if not self._query:
210-
raise sqlite3.ProgrammingError(
212+
raise ProgrammingError(
211213
"You should use something before `where`"
212214
)
213215
self._query += (
@@ -216,6 +218,13 @@ def where(self, **kwargs: Any) -> 'fyCursor':
216218
)
217219
return self
218220

221+
def execute(self, __sql: str, __parameters: _Parameters = ...) -> Self:
222+
self._query = __sql
223+
if __parameters:
224+
for param in __parameters:
225+
self._query.replace("?", param, 1) # type: ignore
226+
return super().execute(__sql, __parameters)
227+
219228
def fetch(
220229
self, one: bool = False
221230
) -> Optional[Union[tuple[Any], list[Any]] | Any]:
@@ -226,7 +235,7 @@ def fetch(
226235
function will be used
227236
"""
228237
if not self._query:
229-
raise sqlite3.ProgrammingError("Nothing to fetch")
238+
raise ProgrammingError("Nothing to fetch")
230239
super().execute(self._query)
231240
super().connection.commit()
232241
return super().fetchone() if one else super().fetchall()
@@ -243,7 +252,7 @@ def one(self) -> Any:
243252
return fetching[0]
244253
return fetching
245254

246-
def commit(self) -> 'fyCursor':
255+
def commit(self) -> Self:
247256
if self._query:
248257
super().execute(self._query)
249258
super().connection.commit()

0 commit comments

Comments
 (0)