-
Notifications
You must be signed in to change notification settings - Fork 705
DB api base class #2787
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
Merged
Merged
DB api base class #2787
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
| # Copyright 2020 The SQLFlow Authors. All rights reserved. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License |
This file contains hidden or 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,120 @@ | ||
| # Copyright 2020 The SQLFlow Authors. All rights reserved. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class ResultSet(ABC): | ||
| """Base class for DB query result, caller can iteratable this object | ||
| to get all result rows""" | ||
| def __init__(self): | ||
| self._generator = None | ||
|
|
||
| def __iter__(self): | ||
| return self | ||
|
|
||
| def _gen(self): | ||
| fetch_size = 128 | ||
| while True: | ||
| rows = self._fetch(fetch_size) or [] | ||
| for r in rows: | ||
| yield r | ||
| if len(rows) < fetch_size: | ||
| break | ||
|
|
||
| def __next__(self): | ||
| if self._generator is None: | ||
| self._generator = self._gen() | ||
| return next(self._generator) | ||
|
|
||
| @abstractmethod | ||
| def _fetch(self, fetch_size): | ||
| """Fetch given count of records in the result set | ||
|
|
||
| Args: | ||
| fetch_size: max record to retrive | ||
|
|
||
| Returns: | ||
| A list of records, each record is a list | ||
| represent a row in the result set | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def column_info(self): | ||
| """Get the result column meta, type in the meta maybe DB-specific | ||
|
|
||
| Returns: | ||
| A list of column metas, like [(field_a, INT), (field_b, STRING)] | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def success(self): | ||
| """Return True if the query is success""" | ||
| return False | ||
|
|
||
| @abstractmethod | ||
| def close(self): | ||
| """Close the ResultSet explicitly, release any resource incurred by this query""" | ||
| pass | ||
|
|
||
|
|
||
| class Connection(ABC): | ||
| """Base class for DB connection | ||
|
|
||
| Args: | ||
| conn_uri: a connection uri in the schema://name:passwd@host/path?params format | ||
|
|
||
| """ | ||
| def __init__(self, conn_uri): | ||
| self.conn_uri = conn_uri | ||
|
|
||
| @abstractmethod | ||
| def _get_result_set(self, statement): | ||
| """Get the ResultSet for given statement | ||
|
|
||
| Args: | ||
| statement: the statement to execute | ||
|
|
||
| Returns: | ||
| A ResultSet object | ||
| """ | ||
| pass | ||
|
|
||
| def query(self, statement): | ||
| """Execute given statement and return a ResultSet | ||
| Typical usage will be: | ||
|
|
||
| rs = conn.query("SELECT * FROM a;") | ||
| result_rows = [r for r in rs] | ||
| rs.close() | ||
|
|
||
| Args: | ||
| statement: the statement to execute | ||
|
|
||
| Returns: | ||
| A ResultSet object which is iteratable, each generated | ||
| record in the iterator is a result-row wrapped by list | ||
| """ | ||
| return self._get_result_set(statement) | ||
|
|
||
| def exec(self, statement): | ||
| """Execute given statement and return True on success""" | ||
| try: | ||
| rs = self._get_result_set(statement) | ||
| return rs.success() | ||
| except: | ||
| return False | ||
| finally: | ||
| rs.close() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also need the
buffered_db_writerfunctions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid not now, it seems the buffered_db_writer keep some internal state and do not follow the DB-API pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd better first implement the DB-API, then let the buffered_db_writer to call the DB-API if possible. We can do something to simplify the buffered_db_writer, for example, let Connection take the information of the URI, so we do not need to supply so much params when calling buffered_db_writer.