|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from tarantool.error import InterfaceError |
| 3 | +from .connection import Connection as BaseConnection |
| 4 | + |
| 5 | + |
| 6 | +class Cursor: |
| 7 | + _lastrowid = 0 |
| 8 | + _rowcount = 0 |
| 9 | + description = None |
| 10 | + arraysize = 1 |
| 11 | + autocommit = True |
| 12 | + closed = False |
| 13 | + rows = None |
| 14 | + |
| 15 | + def __init__(self, conn): |
| 16 | + self._c = conn |
| 17 | + |
| 18 | + def callproc(self, procname, *params): # TODO |
| 19 | + """ |
| 20 | + Call a stored database procedure with the given name. The sequence of |
| 21 | + parameters must contain one entry for each argument that the |
| 22 | + procedure expects. The result of the call is returned as modified |
| 23 | + copy of the input sequence. Input parameters are left untouched, |
| 24 | + output and input/output parameters replaced with possibly new values. |
| 25 | +
|
| 26 | + The procedure may also provide a result set as output. This must then |
| 27 | + be made available through the standard .fetch*() methods. |
| 28 | + """ |
| 29 | + |
| 30 | + def close(self): |
| 31 | + """ |
| 32 | + Close the cursor now (rather than whenever __del__ is called). |
| 33 | +
|
| 34 | + The cursor will be unusable from this point forward; an Error (or |
| 35 | + subclass) exception will be raised if any operation is attempted with |
| 36 | + the cursor. |
| 37 | + """ |
| 38 | + self._c = None |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def _extract_last_row_id(body): |
| 42 | + try: |
| 43 | + val = next(iter(body.values())).get(1)[-1] |
| 44 | + except TypeError: |
| 45 | + val = -1 |
| 46 | + return val |
| 47 | + |
| 48 | + def execute(self, query, params=None): |
| 49 | + """ |
| 50 | + Prepare and execute a database operation (query or command). |
| 51 | +
|
| 52 | + Parameters may be provided as sequence or mapping and will be bound |
| 53 | + to variables in the operation. Variables are specified in a |
| 54 | + database-specific notation (see the module's paramstyle attribute for |
| 55 | + details). |
| 56 | +
|
| 57 | + A reference to the operation will be retained by the cursor. If the |
| 58 | + same operation object is passed in again, then the cursor can |
| 59 | + optimize its behavior. This is most effective for algorithms where |
| 60 | + the same operation is used, but different parameters are bound to it |
| 61 | + (many times). |
| 62 | +
|
| 63 | + For maximum efficiency when reusing an operation, it is best to use |
| 64 | + the .setinputsizes() method to specify the parameter types and sizes |
| 65 | + ahead of time. It is legal for a parameter to not match the |
| 66 | + predefined information; the implementation should compensate, |
| 67 | + possibly with a loss of efficiency. |
| 68 | +
|
| 69 | + The parameters may also be specified as list of tuples to e.g. insert |
| 70 | + multiple rows in a single operation, but this kind of usage is |
| 71 | + deprecated: .executemany() should be used instead. |
| 72 | +
|
| 73 | + Return values are not defined. |
| 74 | + """ |
| 75 | + if self.closed: |
| 76 | + raise self._c.ProgrammingError |
| 77 | + |
| 78 | + response = self._c.execute(query, params) |
| 79 | + self.rows = tuple(response.data) if len( |
| 80 | + response.body) > 1 else None |
| 81 | + |
| 82 | + self._rowcount = response.rowcount |
| 83 | + self._lastrowid = response.lastrowid |
| 84 | + |
| 85 | + def executemany(self, query, param_sets): |
| 86 | + rowcounts = [] |
| 87 | + for params in param_sets: |
| 88 | + self.execute(query, params) |
| 89 | + rowcounts.append(self.rowcount) |
| 90 | + |
| 91 | + self._rowcount = -1 if -1 in rowcounts else sum(rowcounts) |
| 92 | + |
| 93 | + @property |
| 94 | + def lastrowid(self): |
| 95 | + """ |
| 96 | + This read-only attribute provides the rowid of the last modified row |
| 97 | + (most databases return a rowid only when a single INSERT operation is |
| 98 | + performed). If the operation does not set a rowid or if the database |
| 99 | + does not support rowids, this attribute should be set to None. |
| 100 | +
|
| 101 | + The semantics of .lastrowid are undefined in case the last executed |
| 102 | + statement modified more than one row, e.g. when using INSERT with |
| 103 | + .executemany(). |
| 104 | +
|
| 105 | + Warning Message: "DB-API extension cursor.lastrowid used" |
| 106 | + """ |
| 107 | + return self._lastrowid |
| 108 | + |
| 109 | + @property |
| 110 | + def rowcount(self): |
| 111 | + """ |
| 112 | + This read-only attribute specifies the number of rows that the last |
| 113 | + .execute*() produced (for DQL statements like SELECT) or affected ( |
| 114 | + for DML statements like UPDATE or INSERT). |
| 115 | +
|
| 116 | + The attribute is -1 in case no .execute*() has been performed on the |
| 117 | + cursor or the rowcount of the last operation is cannot be determined |
| 118 | + by the interface. |
| 119 | +
|
| 120 | + Note: |
| 121 | + Future versions of the DB API specification could redefine the latter |
| 122 | + case to have the object return None instead of -1. |
| 123 | + """ |
| 124 | + return self._rowcount |
| 125 | + |
| 126 | + def fetchone(self): |
| 127 | + """ |
| 128 | + Fetch the next row of a query result set, returning a single |
| 129 | + sequence, or None when no more data is available. |
| 130 | +
|
| 131 | + An Error (or subclass) exception is raised if the previous call to |
| 132 | + .execute*() did not produce any result set or no call was issued yet. |
| 133 | + """ |
| 134 | + if self.rows is None: |
| 135 | + raise self._c.ProgrammingError('Nothing to fetch') |
| 136 | + return self.fetchmany(1)[0] if len(self.rows) else None |
| 137 | + |
| 138 | + def fetchmany(self, size=None): |
| 139 | + """ |
| 140 | + Fetch the next set of rows of a query result, returning a sequence of |
| 141 | + sequences (e.g. a list of tuples). An empty sequence is returned when |
| 142 | + no more rows are available. |
| 143 | +
|
| 144 | + The number of rows to fetch per call is specified by the parameter. |
| 145 | + If it is not given, the cursor's arraysize determines the number of |
| 146 | + rows to be fetched. The method should try to fetch as many rows as |
| 147 | + indicated by the size parameter. If this is not possible due to the |
| 148 | + specified number of rows not being available, fewer rows may be |
| 149 | + returned. |
| 150 | +
|
| 151 | + An Error (or subclass) exception is raised if the previous call to |
| 152 | + .execute*() did not produce any result set or no call was issued yet. |
| 153 | +
|
| 154 | + Note there are performance considerations involved with the size |
| 155 | + parameter. For optimal performance, it is usually best to use the |
| 156 | + .arraysize attribute. If the size parameter is used, then it is best |
| 157 | + for it to retain the same value from one .fetchmany() call to the next. |
| 158 | + """ |
| 159 | + size = size or self.arraysize |
| 160 | + |
| 161 | + if self.rows is None: |
| 162 | + raise self._c.ProgrammingError('Nothing to fetch') |
| 163 | + |
| 164 | + if len(self.rows) < size: |
| 165 | + items = self.rows |
| 166 | + self.rows = [] |
| 167 | + else: |
| 168 | + items, self.rows = self.rows[:size], self.rows[size:] |
| 169 | + |
| 170 | + return items if len(items) else [] |
| 171 | + |
| 172 | + def fetchall(self): |
| 173 | + """Fetch all (remaining) rows of a query result, returning them as a |
| 174 | + sequence of sequences (e.g. a list of tuples). Note that the cursor's |
| 175 | + arraysize attribute can affect the performance of this operation. |
| 176 | +
|
| 177 | + An Error (or subclass) exception is raised if the previous call to |
| 178 | + .execute*() did not produce any result set or no call was issued yet. |
| 179 | + """ |
| 180 | + if self.rows is None: |
| 181 | + raise self._c.ProgrammingError('Nothing to fetch') |
| 182 | + |
| 183 | + items = self.rows[:] |
| 184 | + self.rows = [] |
| 185 | + return items |
| 186 | + |
| 187 | + def setinputsizes(self, sizes): |
| 188 | + """This can be used before a call to .execute*() to predefine memory |
| 189 | + areas for the operation's parameters. |
| 190 | + sizes is specified as a sequence - one item for each input parameter. |
| 191 | + The item should be a Type Object that corresponds to the input that |
| 192 | + will be used, or it should be an integer specifying the maximum |
| 193 | + length of a string parameter. If the item is None, then no predefined |
| 194 | + memory area will be reserved for that column (this is useful to avoid |
| 195 | + predefined areas for large inputs). |
| 196 | +
|
| 197 | + This method would be used before the .execute*() method is invoked. |
| 198 | +
|
| 199 | + Implementations are free to have this method do nothing and users are |
| 200 | + free to not use it.""" |
| 201 | + |
| 202 | + def setoutputsize(self, size, column=None): |
| 203 | + """Set a column buffer size for fetches of large columns (e.g. LONGs, |
| 204 | + BLOBs, etc.). The column is specified as an index into the result |
| 205 | + sequence. Not specifying the column will set the default size for all |
| 206 | + large columns in the cursor. |
| 207 | + This method would be used before the .execute*() method is invoked. |
| 208 | + Implementations are free to have this method do nothing and users are |
| 209 | + free to not use it.""" |
| 210 | + |
| 211 | + |
| 212 | +class Connection(BaseConnection): |
| 213 | + _cursor = None |
| 214 | + paramstyle = 'qmark' |
| 215 | + apilevel = "2.0" |
| 216 | + threadsafety = 0 |
| 217 | + |
| 218 | + server_version = 2 |
| 219 | + |
| 220 | + def connect(self): |
| 221 | + super(Connection, self).connect() |
| 222 | + return self |
| 223 | + |
| 224 | + def commit(self): |
| 225 | + """ |
| 226 | + Commit any pending transaction to the database. |
| 227 | +
|
| 228 | + Note that if the database supports an auto-commit feature, this must |
| 229 | + be initially off. An interface method may be provided to turn it back |
| 230 | + on. |
| 231 | +
|
| 232 | + Database modules that do not support transactions should implement |
| 233 | + this method with void functionality. |
| 234 | + """ |
| 235 | + if self._socket is None: |
| 236 | + raise self.ProgrammingError |
| 237 | + |
| 238 | + def rollback(self): |
| 239 | + """ |
| 240 | + In case a database does provide transactions this method causes the |
| 241 | + database to roll back to the start of any pending transaction. |
| 242 | + Closing a connection without committing the changes first will cause |
| 243 | + an implicit rollback to be performed. |
| 244 | + """ |
| 245 | + if self._socket is None: |
| 246 | + raise self.ProgrammingError |
| 247 | + |
| 248 | + def execute(self, query, params=None): |
| 249 | + if self._socket is None: |
| 250 | + raise self.ProgrammingError('Can not execute on closed connection') |
| 251 | + return super(Connection, self).execute(query, params) |
| 252 | + |
| 253 | + def close(self): |
| 254 | + """ |
| 255 | + Close the connection now (rather than whenever .__del__() is called). |
| 256 | +
|
| 257 | + The connection will be unusable from this point forward; an Error (or |
| 258 | + subclass) exception will be raised if any operation is attempted with |
| 259 | + the connection. The same applies to all cursor objects trying to use |
| 260 | + the connection. Note that closing a connection without committing the |
| 261 | + changes first will cause an implicit rollback to be performed. |
| 262 | + """ |
| 263 | + if self._socket: |
| 264 | + self._socket.close() |
| 265 | + self._socket = None |
| 266 | + else: |
| 267 | + raise self.ProgrammingError('Connection already closed') |
| 268 | + |
| 269 | + def cursor(self, params=None): |
| 270 | + """ |
| 271 | + Return a new Cursor Object using the connection. |
| 272 | +
|
| 273 | + If the database does not provide a direct cursor concept, the module |
| 274 | + will have to emulate cursors using other means to the extent needed |
| 275 | + by this specification. |
| 276 | + """ |
| 277 | + return Cursor(self) |
0 commit comments