Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tupleCursor and type dictCursor #2086

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/snowflake/connector/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from threading import Lock
from time import strptime
from types import TracebackType
from typing import Any, Callable, Generator, Iterable, Iterator, NamedTuple, Sequence
from typing import Any, Callable, Generator, Iterable, Iterator, NamedTuple, Sequence, TypeVar
from uuid import UUID

from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -113,6 +113,7 @@
DEFAULT_CLIENT_PREFETCH_THREADS = 4
MAX_CLIENT_PREFETCH_THREADS = 10
DEFAULT_BACKOFF_POLICY = exponential_backoff()
T = TypeVar('T', bound=SnowflakeCursor)


def DefaultConverterClass() -> type:
Expand Down Expand Up @@ -855,8 +856,8 @@ def rollback(self) -> None:
self.cursor().execute("ROLLBACK")

def cursor(
self, cursor_class: type[SnowflakeCursor] = SnowflakeCursor
) -> SnowflakeCursor:
self, cursor_class: type[T] = SnowflakeCursor
) -> T:
"""Creates a cursor object. Each statement will be executed in a new cursor object."""
logger.debug("cursor")
if not self.rest:
Expand Down
27 changes: 26 additions & 1 deletion src/snowflake/connector/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,13 +1724,38 @@ def get_result_batches(self) -> list[ResultBatch] | None:

class DictCursor(SnowflakeCursor):
"""Cursor returning results in a dictionary."""

def __init__(self, connection) -> None:
super().__init__(
connection,
use_dict_result=True,
)

def fetchone(self) -> dict | None:
return super().fetchone()

def fetchmany(self, size: int | None = None) -> list[dict]:
return super().fetchmany()

def fetchall(self) -> list[dict]:
return super().fetchall()

class TupleCursor(SnowflakeCursor):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need TupleCursor? this is effectively the default SnowflakeCursor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SnowflakeCursor has to return Union[None|dict|tuple] because could be instanciated as
SnowflakeCursor(use_dict_result=True).

TupleCursor is just a SnowflakeCursor that return Union[None|tuple] on fetch

On DictCursor the better types will not require nothing for the user, on TupleCursor it will require that they start to instanciate as TupleDict, so maybe its not useful. I let you to decide, let me know if you want and i delete the TupleCursor from PR and issue.

"""Cursor returning results in a dictionary."""

def __init__(self, connection) -> None:
super().__init__(
connection,
use_dict_result=False,
)

def fetchone(self) -> tuple | None:
return super().fetchone()

def fetchmany(self, size: int | None = None) -> list[tuple]:
return super().fetchmany()

def fetchall(self) -> list[tuple]:
return super().fetchall()

def __getattr__(name):
if name == "NanoarrowUsage":
Expand Down