generated from allenai/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add query manager * simplify sql_job context manager * update type hints * update changelog
- Loading branch information
Showing
7 changed files
with
221 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Any, Dict, Optional, Union | ||
|
||
from ..types import QueryOptions | ||
from .query import Query | ||
from .sql_job import SQLJob | ||
|
||
|
||
class QueryManager: | ||
def __init__(self, job: SQLJob) -> None: | ||
self.job = job | ||
|
||
def get_query_options( | ||
self, opts: Optional[Union[Dict[str, Any], QueryOptions]] = None | ||
) -> QueryOptions: | ||
query_options = ( | ||
opts | ||
if isinstance(opts, QueryOptions) | ||
else ( | ||
QueryOptions(**opts) | ||
if isinstance(opts, dict) | ||
else QueryOptions(isClCommand=False, parameters=None, autoClose=False) | ||
) | ||
) | ||
|
||
return query_options | ||
|
||
def create_query( | ||
self, | ||
query: str, | ||
opts: Optional[Union[Dict[str, Any], QueryOptions]] = None, | ||
) -> Query: | ||
|
||
if opts and not isinstance(opts, (dict, QueryOptions)): | ||
raise Exception("opts must be a dictionary, a QueryOptions object, or None") | ||
|
||
query_options = self.get_query_options(opts) | ||
|
||
return Query(self.job, query, opts=query_options) | ||
|
||
def run_query(self, query: Query, rows_to_fetch: Optional[int] = None) -> Dict[str, Any]: | ||
return query.run(rows_to_fetch=rows_to_fetch) | ||
|
||
def query_and_run( | ||
self, query: str, opts: Optional[Union[Dict[str, Any], QueryOptions]] = None, **kwargs | ||
) -> Dict[str, Any]: | ||
with self.create_query(query, opts) as query: | ||
return query.run(**kwargs) |
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,32 @@ | ||
import base64 | ||
import ssl | ||
from typing import Any, Dict | ||
|
||
from websocket import WebSocket, create_connection | ||
|
||
from ..types import DaemonServer | ||
|
||
|
||
class WebsocketConnection: | ||
def __init__(self, db2_server: DaemonServer) -> None: | ||
self.uri = f"wss://{db2_server.host}:{db2_server.port}/db/" | ||
self.headers = { | ||
"Authorization": "Basic " | ||
+ base64.b64encode(f"{db2_server.user}:{db2_server.password}".encode()).decode("ascii") | ||
} | ||
|
||
self.ssl_opts = self._build_ssl_options(db2_server) | ||
|
||
def _build_ssl_options(self, db2_server: DaemonServer) -> Dict[str, Any]: | ||
ssl_opts: Dict[str, Any] = {} | ||
if db2_server.ignoreUnauthorized: | ||
ssl_opts["cert_reqs"] = ssl.CERT_NONE | ||
if db2_server.ca: | ||
ssl_context = ssl.create_default_context(cadata=db2_server.ca) | ||
ssl_context.check_hostname = False | ||
ssl_opts["ssl_context"] = ssl_context | ||
ssl_opts["cert_reqs"] = ssl.CERT_NONE | ||
return ssl_opts | ||
|
||
def connect(self) -> WebSocket: | ||
return create_connection(self.uri, header=self.headers, sslopt=self.ssl_opts) |
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 @@ | ||
import os | ||
import re | ||
|
||
import pytest | ||
|
||
from mapepire_python.client.query_manager import QueryManager | ||
from mapepire_python.client.sql_job import SQLJob | ||
from mapepire_python.types import DaemonServer, QueryOptions | ||
|
||
# Fetch environment variables | ||
server = os.getenv('VITE_SERVER') | ||
user = os.getenv('VITE_DB_USER') | ||
password = os.getenv('VITE_DB_PASS') | ||
port = os.getenv('VITE_DB_PORT') | ||
|
||
# Check if environment variables are set | ||
if not server or not user or not password: | ||
raise ValueError('One or more environment variables are missing.') | ||
|
||
|
||
creds = DaemonServer( | ||
host=server, | ||
port=port, | ||
user=user, | ||
password=password, | ||
ignoreUnauthorized=True, | ||
) | ||
|
||
def test_query_manager(): | ||
# connection logic | ||
job = SQLJob() | ||
job.connect(creds) | ||
|
||
# Query Manager | ||
query_manager = QueryManager(job) | ||
|
||
# create a unique query | ||
query = query_manager.create_query("select * from sample.employee") | ||
|
||
# run query | ||
result = query_manager.run_query(query) | ||
|
||
assert result['success'] | ||
job.close() | ||
|
||
|
||
|
||
def test_context_manager(): | ||
with SQLJob() as job: | ||
job.connect(creds) | ||
|
||
query_manager = QueryManager(job) | ||
query = query_manager.create_query("select * from sample.department") | ||
result = query_manager.run_query(query) | ||
assert result['success'] | ||
|
||
def test_simple_v2(): | ||
with SQLJob(creds) as job: | ||
query_manager = QueryManager(job) | ||
query = query_manager.create_query('select * from sample.employee') | ||
result = query_manager.run_query(query, rows_to_fetch=5) | ||
assert result['success'] == True | ||
assert result['is_done'] == False | ||
assert result['has_results'] == True | ||
query.close() | ||
|
||
def test_query_large_dataset(): | ||
job = SQLJob() | ||
_ = job.connect(creds) | ||
query_manager = QueryManager(job) | ||
query = query_manager.create_query('select * from sample.employee') | ||
|
||
result = query_manager.run_query(query, rows_to_fetch=30) | ||
query.close() | ||
job.close() | ||
|
||
assert result['success'] == True | ||
assert result['is_done'] == False | ||
assert result['has_results'] == True | ||
assert len(result['data']) == 30 | ||
|
||
def test_query_and_run(): | ||
with SQLJob(creds) as job: | ||
query_manager = QueryManager(job) | ||
result = query_manager.query_and_run('select * from sample.employee', rows_to_fetch=5) | ||
assert result['success'] == True | ||
assert result['is_done'] == False | ||
assert result['has_results'] == True |