|
| 1 | +# |
| 2 | +# Copyright 2021 GridGain Systems, Inc. and Contributors. |
| 3 | +# |
| 4 | +# Licensed under the GridGain Community Edition License (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +from ..cursors import SqlCursor |
| 18 | + |
| 19 | +class DBCursor: |
| 20 | + |
| 21 | + |
| 22 | + def __init__(self, connection): |
| 23 | + self.connection = connection |
| 24 | + self.cursor = None |
| 25 | + |
| 26 | + @property |
| 27 | + def description(self): |
| 28 | + if self._state == self._states.NONE: |
| 29 | + return None |
| 30 | + |
| 31 | + columns = self._columns or [] |
| 32 | + types = self._types or [] |
| 33 | + |
| 34 | + return [ |
| 35 | + Column(name, type_code, None, None, None, None, True) |
| 36 | + for name, type_code in zip(columns, types) |
| 37 | + ] |
| 38 | + |
| 39 | + @property |
| 40 | + def rowcount(self): |
| 41 | + """ |
| 42 | + :return: the number of rows that the last .execute*() produced. |
| 43 | + """ |
| 44 | + return self._rowcount |
| 45 | + |
| 46 | + def close(self): |
| 47 | + """ |
| 48 | + Close the cursor now. The cursor will be unusable from this point |
| 49 | + forward; an :data:`~clickhouse_driver.dbapi.Error` (or subclass) |
| 50 | + exception will be raised if any operation is attempted with the |
| 51 | + cursor. |
| 52 | + """ |
| 53 | + self._client.disconnect() |
| 54 | + self._state = self._states.CURSOR_CLOSED |
| 55 | + |
| 56 | + try: |
| 57 | + # cursor can be already closed |
| 58 | + self._connection.cursors.remove(self) |
| 59 | + except ValueError: |
| 60 | + pass |
| 61 | + |
| 62 | + def execute(self, operation, parameters=None): |
| 63 | + """ |
| 64 | + Prepare and execute a database operation (query or command). |
| 65 | +
|
| 66 | + :param operation: query or command to execute. |
| 67 | + :param parameters: sequence or mapping that will be bound to |
| 68 | + variables in the operation. |
| 69 | + :return: None |
| 70 | + """ |
| 71 | + self.cursor = self.connection.sql(operation, query_args=parameters) |
| 72 | + |
| 73 | + def executemany(self, operation, seq_of_parameters): |
| 74 | + """ |
| 75 | + Prepare a database operation (query or command) and then execute it |
| 76 | + against all parameter sequences found in the sequence |
| 77 | + `seq_of_parameters`. |
| 78 | +
|
| 79 | + :param operation: query or command to execute. |
| 80 | + :param seq_of_parameters: sequences or mappings for execution. |
| 81 | + :return: None |
| 82 | + """ |
| 83 | + pass |
| 84 | + |
| 85 | + def fetchone(self): |
| 86 | + """ |
| 87 | + Fetch the next row of a query result set, returning a single sequence, |
| 88 | + or None when no more data is available. |
| 89 | +
|
| 90 | + :return: the next row of a query result set or None. |
| 91 | + """ |
| 92 | + self._check_query_started() |
| 93 | + |
| 94 | + if self._stream_results: |
| 95 | + return next(self._rows, None) |
| 96 | + |
| 97 | + else: |
| 98 | + if not self._rows: |
| 99 | + return None |
| 100 | + |
| 101 | + return self._rows.pop(0) |
| 102 | + |
| 103 | + def fetchmany(self, size=None): |
| 104 | + """ |
| 105 | + Fetch the next set of rows of a query result, returning a sequence of |
| 106 | + sequences (e.g. a list of tuples). An empty sequence is returned when |
| 107 | + no more rows are available. |
| 108 | +
|
| 109 | + :param size: amount of rows to return. |
| 110 | + :return: list of fetched rows or empty list. |
| 111 | + """ |
| 112 | + self._check_query_started() |
| 113 | + |
| 114 | + if size is None: |
| 115 | + size = self.arraysize |
| 116 | + |
| 117 | + if self._stream_results: |
| 118 | + if size == -1: |
| 119 | + return list(self._rows) |
| 120 | + else: |
| 121 | + return list(islice(self._rows, size)) |
| 122 | + |
| 123 | + if size < 0: |
| 124 | + rv = self._rows |
| 125 | + self._rows = [] |
| 126 | + else: |
| 127 | + rv = self._rows[:size] |
| 128 | + self._rows = self._rows[size:] |
| 129 | + |
| 130 | + return rv |
| 131 | + |
| 132 | + def fetchall(self): |
| 133 | + """ |
| 134 | + Fetch all (remaining) rows of a query result, returning them as a |
| 135 | + sequence of sequences (e.g. a list of tuples). |
| 136 | +
|
| 137 | + :return: list of fetched rows. |
| 138 | + """ |
| 139 | + if self.cursor != None: |
| 140 | + rows = [] |
| 141 | + for row in self.cursor: |
| 142 | + rows.append(row) |
| 143 | + else: |
| 144 | + return None # error? |
| 145 | + |
| 146 | + return rows |
| 147 | + |
| 148 | + def setinputsizes(self, sizes): |
| 149 | + # Do nothing. |
| 150 | + pass |
| 151 | + |
| 152 | + def setoutputsize(self, size, column=None): |
| 153 | + # Do nothing. |
| 154 | + pass |
0 commit comments