-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from CrispenGari/query-builder
query builder and the doccumentation
- Loading branch information
Showing
12 changed files
with
415 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from typing import Callable, Any, Literal, Optional | ||
|
||
from dataloom.types import DIALECT_LITERAL | ||
|
||
|
||
class qb: | ||
def __repr__(self) -> str: | ||
return f"Loom QB<{self.dialect}>" | ||
|
||
def __str__(self) -> str: | ||
return f"Loom QB<{self.dialect}>" | ||
|
||
def __init__( | ||
self, _execute_sql: Callable[..., Any], dialect: DIALECT_LITERAL | ||
) -> None: | ||
self.__exc = _execute_sql | ||
self.dialect = dialect | ||
|
||
def run( | ||
self, | ||
sql: str, | ||
args: Any | None = None, | ||
fetchone: bool = False, | ||
fetchmany: bool = False, | ||
fetchall: bool = False, | ||
mutation: bool = True, | ||
bulk: bool = False, | ||
affected_rows: bool = False, | ||
operation: Optional[Literal["insert", "update", "delete", "read"]] = None, | ||
verbose: int = 1, | ||
is_script: bool = False, | ||
): | ||
""" | ||
run | ||
----------- | ||
Execute SQL query with optional parameters. | ||
Parameters | ||
---------- | ||
sql : str | ||
SQL query to execute. | ||
args : Any | None, optional | ||
Parameters for the SQL query. Defaults to None. | ||
fetchone : bool, optional | ||
Whether to fetch only one result. Defaults to False. | ||
fetchmany : bool, optional | ||
Whether to fetch multiple results. Defaults to False. | ||
fetchall : bool, optional | ||
Whether to fetch all results. Defaults to False. | ||
mutation : bool, optional | ||
Whether the query is a mutation (insert, update, delete). Defaults to True. | ||
bulk : bool, optional | ||
Whether the query is a bulk operation. Defaults to False. | ||
affected_rows : bool, optional | ||
Whether to return affected rows. Defaults to False. | ||
operation : Literal['insert', 'update', 'delete', 'read'] | None, optional | ||
Type of operation being performed. Defaults to None. | ||
verbose : int, optional | ||
Verbosity level for logging. Defaults to 1. | ||
is_script : bool, optional | ||
Whether the SQL is a script. Defaults to False. | ||
Returns | ||
------- | ||
Any | ||
Query result. | ||
Examples | ||
-------- | ||
>>> qb = loom.getQueryBuilder() | ||
... ids = qb.run("select id from posts;", fetchall=True) | ||
... print(ids) | ||
... | ||
""" | ||
return self.__exc( | ||
sql, | ||
args=args, | ||
fetchall=fetchall, | ||
fetchmany=fetchmany, | ||
fetchone=fetchone, | ||
mutation=mutation, | ||
bulk=bulk, | ||
affected_rows=affected_rows, | ||
operation=operation, | ||
_verbose=verbose, | ||
_is_script=is_script, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class TestQBMySQL: | ||
def test_qb(self): | ||
from dataloom import ( | ||
Loom, | ||
Model, | ||
Column, | ||
PrimaryKeyColumn, | ||
CreatedAtColumn, | ||
UpdatedAtColumn, | ||
TableColumn, | ||
ForeignKeyColumn, | ||
ColumnValue, | ||
) | ||
from dataloom.keys import MySQLConfig | ||
from typing import Optional | ||
|
||
mysql_loom = Loom( | ||
dialect="mysql", | ||
database=MySQLConfig.database, | ||
password=MySQLConfig.password, | ||
user=MySQLConfig.user, | ||
) | ||
|
||
class User(Model): | ||
__tablename__: Optional[TableColumn] = TableColumn(name="users") | ||
id = PrimaryKeyColumn(type="int", auto_increment=True) | ||
name = Column(type="text", nullable=False, default="Bob") | ||
username = Column(type="varchar", unique=True, length=255) | ||
|
||
class Post(Model): | ||
__tablename__: Optional[TableColumn] = TableColumn(name="posts") | ||
id = PrimaryKeyColumn( | ||
type="int", auto_increment=True, nullable=False, unique=True | ||
) | ||
completed = Column(type="boolean", default=False) | ||
title = Column(type="varchar", length=255, nullable=False) | ||
# timestamps | ||
createdAt = CreatedAtColumn() | ||
updatedAt = UpdatedAtColumn() | ||
# relations | ||
userId = ForeignKeyColumn( | ||
User, type="int", required=True, onDelete="CASCADE", onUpdate="CASCADE" | ||
) | ||
|
||
conn, _ = mysql_loom.connect_and_sync([Post, User], drop=True, force=True) | ||
userId = mysql_loom.insert_one( | ||
instance=User, | ||
values=ColumnValue(name="username", value="@miller"), | ||
) | ||
|
||
for title in ["Hey", "Hello", "What are you doing", "Coding"]: | ||
mysql_loom.insert_one( | ||
instance=Post, | ||
values=[ | ||
ColumnValue(name="userId", value=userId), | ||
ColumnValue(name="title", value=title), | ||
], | ||
) | ||
qb = mysql_loom.getQueryBuilder() | ||
res = qb.run("select id from posts;", fetchall=True) | ||
assert str(qb) == "Loom QB<mysql>" | ||
assert len(res) == 4 | ||
conn.close() |
Oops, something went wrong.