def close(self): """Closes the connection. No further operations are allowed, either on the connection or any of its cursors, once the connection is closed. If the connection is used in a ``with`` statement, this method will be automatically called at the end of the ``with`` block. """ if self._closed: raise ProgrammingError('the connection is already closed') for cursor_ref in self._cursors: cursor = cursor_ref() if cursor is not None and not cursor._closed: cursor.close() self._client.closeConnection(self._id) self._client.close() self._closed = True
def executemany(self, operation, seq_of_parameters): if self._closed: raise ProgrammingError('the cursor is already closed') self._updatecount = -1 self._set_frame(None) statement = self._connection._client.prepare(self._connection._id, operation, max_rows_total=0) self._set_id(statement.id) self._set_signature(statement.signature) for parameters in seq_of_parameters: self._connection._client.execute( self._connection._id, self._id, statement.signature, self._transform_parameters(parameters), first_frame_max_size=0)
def close(self): """Closes the cursor. No further operations are allowed once the cursor is closed. If the cursor is used in a ``with`` statement, this method will be automatically called at the end of the ``with`` block. """ if self._closed: raise ProgrammingError('the cursor is already closed') if self._id is not None: self._connection._client.close_statement(self._connection._id, self._id) self._id = None self._signature = None self._column_data_types = [] self._frame = None self._pos = None self._closed = True
def cursor(self, cursor_factory=None): """Creates a new cursor. :param cursor_factory: This argument can be used to create non-standard cursors. The class returned must be a subclass of :class:`~phoenixdb.cursor.Cursor` (for example :class:`~phoenixdb.cursor.DictCursor`). A default factory for the connection can also be specified using the :attr:`cursor_factory` attribute. :returns: A :class:`~phoenixdb.cursor.Cursor` object. """ if self._closed: raise ProgrammingError('the connection is already closed') cursor = (cursor_factory or self.cursor_factory)(self) self._cursors.append(weakref.ref(cursor, self._cursors.remove)) return cursor
def get_primary_keys(self, catalog=None, schema=None, table=None): if self._connection._closed: raise ProgrammingError('The cursor is already closed.') state = common_pb2.QueryState() state.type = common_pb2.StateType.METADATA state.op = common_pb2.MetaDataOperation.GET_PRIMARY_KEYS state.has_args = True state.has_op = True catalog_arg = self._moa_string_arg_factory(catalog) schema_arg = self._moa_string_arg_factory(schema) table_arg = self._moa_string_arg_factory(table) state.args.extend([catalog_arg, schema_arg, table_arg]) with DictCursor(self._connection) as cursor: syncResultResponse = cursor.get_sync_results(state) if not syncResultResponse.more_results: return [] signature = common_pb2.Signature() signature.columns.append(self._column_meta_data_factory(1, 'TABLE_CAT', 12)) signature.columns.append(self._column_meta_data_factory(2, 'TABLE_SCHEM', 12)) signature.columns.append(self._column_meta_data_factory(3, 'TABLE_NAME', 12)) signature.columns.append(self._column_meta_data_factory(4, 'COLUMN_NAME', 12)) signature.columns.append(self._column_meta_data_factory(5, 'KEY_SEQ', 5)) signature.columns.append(self._column_meta_data_factory(6, 'PK_NAME', 12)) # The following are non-standard Phoenix extensions # This returns '\x00\x00\x00A' or '\x00\x00\x00D' , but that's consistent with Java signature.columns.append(self._column_meta_data_factory(7, 'ASC_OR_DESC', 12)) signature.columns.append(self._column_meta_data_factory(8, 'DATA_TYPE', 5)) signature.columns.append(self._column_meta_data_factory(9, 'TYPE_NAME', 12)) signature.columns.append(self._column_meta_data_factory(10, 'COLUMN_SIZE', 5)) signature.columns.append(self._column_meta_data_factory(11, 'TYPE_ID', 5)) signature.columns.append(self._column_meta_data_factory(12, 'VIEW_CONSTANT', 12)) cursor.fetch(signature) return cursor.fetchall()
def execute(self, operation, parameters=None): if self._closed: raise ProgrammingError('The cursor is already closed.') self._updatecount = -1 self._set_frame(None) if parameters is None: if self._id is None: self._set_id(self._connection._client.create_statement(self._connection._id)) results = self._connection._client.prepare_and_execute( self._connection._id, self._id, operation, first_frame_max_size=self.itersize) self._process_results(results) else: statement = self._connection._client.prepare( self._connection._id, operation) self._set_id(statement.id) self._set_signature(statement.signature) results = self._connection._client.execute( self._connection._id, self._id, statement.signature, self._transform_parameters(parameters), first_frame_max_size=self.itersize) self._process_results(results)
def _transform_parameters(self, parameters): typed_parameters = [] for value, data_type in zip(parameters, self._parameter_data_types): field_name, rep, mutate_to, cast_from, is_array = data_type typed_value = common_pb2.TypedValue() if value is None: typed_value.null = True typed_value.type = common_pb2.NULL else: typed_value.null = False if is_array: if type(value) in [list, tuple]: for element in value: if mutate_to is not None: element = mutate_to(element) typed_element = common_pb2.TypedValue() if element is None: typed_element.null = True else: typed_element.type = rep setattr(typed_element, field_name, element) typed_value.array_value.append(typed_element) typed_value.type = common_pb2.ARRAY typed_value.component_type = rep else: raise ProgrammingError( 'scalar value specified for array parameter') else: if mutate_to is not None: value = mutate_to(value) typed_value.type = rep setattr(typed_value, field_name, value) typed_parameters.append(typed_value) return typed_parameters
def get_sync_results(self, state): if self._closed: raise ProgrammingError('The cursor is already closed.') if self._id is None: self._set_id(self._connection._client.create_statement(self._connection._id)) return self._connection._client.get_sync_results(self._connection._id, self._id, state)
def transactionisolation(self, value): if self._closed: raise ProgrammingError('the connection is already closed') props = self._client.connection_sync( self._id, {'transactionIsolation': bool(value)}) self._transactionisolation = props.transaction_isolation
def readonly(self, value): if self._closed: raise ProgrammingError('the connection is already closed') props = self._client.connection_sync(self._id, {'readOnly': bool(value)}) self._readonly = props.read_only
def autocommit(self, value): if self._closed: raise ProgrammingError('the connection is already closed') props = self._client.connection_sync(self._id, {'autoCommit': bool(value)}) self._autocommit = props.auto_commit
def commit(self): if self._closed: raise ProgrammingError('the connection is already closed') self._client.commit(self._id)
def rollback(self): if self._closed: raise ProgrammingError('the connection is already closed') self._client.rollback(self._id)
def transactionisolation(self, value): if self._closed: raise ProgrammingError('The connection is already closed.') self._avatica_props = self._client.connection_sync_dict( self._id, {'transactionIsolation': bool(value)})
def readonly(self, value): if self._closed: raise ProgrammingError('The connection is already closed.') self._avatica_props = self._client.connection_sync_dict( self._id, {'readOnly': bool(value)})
def autocommit(self, value): if self._closed: raise ProgrammingError('The connection is already closed.') self._avatica_props = self._client.connection_sync_dict( self._id, {'autoCommit': bool(value)})