|
| 1 | +# Copyright 2020 The SQLFlow Authors. All rights reserved. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License |
| 13 | + |
| 14 | +from abc import ABC, abstractmethod |
| 15 | + |
| 16 | + |
| 17 | +class ResultSet(ABC): |
| 18 | + """Base class for DB query result, caller can iteratable this object |
| 19 | + to get all result rows""" |
| 20 | + def __init__(self): |
| 21 | + self._generator = None |
| 22 | + |
| 23 | + def __iter__(self): |
| 24 | + return self |
| 25 | + |
| 26 | + def _gen(self): |
| 27 | + fetch_size = 128 |
| 28 | + while True: |
| 29 | + rows = self._fetch(fetch_size) or [] |
| 30 | + for r in rows: |
| 31 | + yield r |
| 32 | + if len(rows) < fetch_size: |
| 33 | + break |
| 34 | + |
| 35 | + def __next__(self): |
| 36 | + if self._generator is None: |
| 37 | + self._generator = self._gen() |
| 38 | + return next(self._generator) |
| 39 | + |
| 40 | + @abstractmethod |
| 41 | + def _fetch(self, fetch_size): |
| 42 | + """Fetch given count of records in the result set |
| 43 | +
|
| 44 | + Args: |
| 45 | + fetch_size: max record to retrive |
| 46 | +
|
| 47 | + Returns: |
| 48 | + A list of records, each record is a list |
| 49 | + represent a row in the result set |
| 50 | + """ |
| 51 | + pass |
| 52 | + |
| 53 | + @abstractmethod |
| 54 | + def column_info(self): |
| 55 | + """Get the result column meta, type in the meta maybe DB-specific |
| 56 | +
|
| 57 | + Returns: |
| 58 | + A list of column metas, like [(field_a, INT), (field_b, STRING)] |
| 59 | + """ |
| 60 | + pass |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def success(self): |
| 64 | + """Return True if the query is success""" |
| 65 | + return False |
| 66 | + |
| 67 | + @abstractmethod |
| 68 | + def close(self): |
| 69 | + """Close the ResultSet explicitly, release any resource incurred by this query""" |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +class Connection(ABC): |
| 74 | + """Base class for DB connection |
| 75 | +
|
| 76 | + Args: |
| 77 | + conn_uri: a connection uri in the schema://name:passwd@host/path?params format |
| 78 | +
|
| 79 | + """ |
| 80 | + def __init__(self, conn_uri): |
| 81 | + self.conn_uri = conn_uri |
| 82 | + |
| 83 | + @abstractmethod |
| 84 | + def _get_result_set(self, statement): |
| 85 | + """Get the ResultSet for given statement |
| 86 | +
|
| 87 | + Args: |
| 88 | + statement: the statement to execute |
| 89 | +
|
| 90 | + Returns: |
| 91 | + A ResultSet object |
| 92 | + """ |
| 93 | + pass |
| 94 | + |
| 95 | + def query(self, statement): |
| 96 | + """Execute given statement and return a ResultSet |
| 97 | + Typical usage will be: |
| 98 | +
|
| 99 | + rs = conn.query("SELECT * FROM a;") |
| 100 | + result_rows = [r for r in rs] |
| 101 | + rs.close() |
| 102 | +
|
| 103 | + Args: |
| 104 | + statement: the statement to execute |
| 105 | +
|
| 106 | + Returns: |
| 107 | + A ResultSet object which is iteratable, each generated |
| 108 | + record in the iterator is a result-row wrapped by list |
| 109 | + """ |
| 110 | + return self._get_result_set(statement) |
| 111 | + |
| 112 | + def exec(self, statement): |
| 113 | + """Execute given statement and return True on success""" |
| 114 | + try: |
| 115 | + rs = self._get_result_set(statement) |
| 116 | + return rs.success() |
| 117 | + except: |
| 118 | + return False |
| 119 | + finally: |
| 120 | + rs.close() |
0 commit comments