我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sqlalchemy.exc.InvalidRequestError()。
def create(cls, message, object, stage=u'Fetch', line=None): ''' Helper function to create an error object and save it. ''' err = cls(message=message, object=object, stage=stage, line=line) try: err.save() except InvalidRequestError, e: # Clear any in-progress sqlalchemy transactions try: Session.rollback() except: pass try: Session.remove() except: pass err.save() finally: log_message = '{0}, line {1}'.format(message, line) \ if line else message log.debug(log_message)
def connectionLost(self, reason): self.logger.info("Client disconnected") self.spirit.players.remove(self) try: self.session.commit() if hasattr(self, "room") and self.room is not None: self.room.remove(self) if hasattr(self, "user"): self.session.expunge(self.user) except InvalidRequestError: self.logger.info("There aren't any transactions in progress") finally: self.session.close() self.event.emit("disconnected", self, reason)
def note_file_created(self, file_obj): """Tell M&C about a new Librarian file. M&C file records can also have null obsids, so we don't need to do anything special for maintenance files. """ try: self.mc_session.add_lib_file(file_obj.name, file_obj.obsid, file_obj.create_time_astropy, file_obj.size / 1024**3) except InvalidRequestError as e: # This could happen if the file's obsid were not registered in the # M&C database. Which shouldn't happen, but ... self.error(SEVERE, 'couldn\'t register file %s (obsid %s) with M&C: %s', file_obj.name, file_obj.obsid, e) try: self.mc_session.commit() except SQLAlchemyError as e: self.mc_session.rollback() self.error(SEVERE, 'could not commit file creation note to the M&C system: %s', e)
def get_model(cls): """ Returns the model class """ model = None with suppress(AttributeError, InvalidRequestError): model = cls.queryset._only_entity_zero().class_ if model: return model with suppress(AttributeError): model = cls.serializer_class.Meta.model assert model is not None, ( "Couldn't figure out the model for {viewset} attribute, either provide a" 'queryset or a serializer with a Meta.model'.format(viewset=cls.__name__) ) return model
def test_group_by_query(self): table = self.create_table() query = session.query(table.c.x).group_by(table.c.x) self.assertEqual( self.compile(query), 'SELECT x AS t1_x FROM t1 GROUP BY x' ) query = session.query(table.c.x).group_by(table.c.x).with_totals() self.assertEqual( self.compile(query), 'SELECT x AS t1_x FROM t1 GROUP BY x WITH TOTALS' ) with self.assertRaises(exc.InvalidRequestError) as ex: session.query(table.c.x).with_totals() self.assertIn('with_totals', str(ex.exception))
def tearDown(self): """ Tear down this test case by closing any existing database connection and deleting all of the objects that were marked for deletion. :return: None """ if len(self.to_delete) > 0: for cur_delete in self.to_delete: try: self.db_session.delete(cur_delete) except InvalidRequestError: continue self.db_session.commit() if self._db_session is not None: self._db_session.close() if self._transaction is not None: self._transaction.rollback() if self._connection is not None: self._connection.close() super(BaseSqlalchemyTestCase, self).tearDown() # Protected Methods # Private Methods
def pre_exec(self): if self.isinsert: tbl = self.compiled.statement.table seq_column = tbl._autoincrement_column insert_has_sequence = seq_column is not None if insert_has_sequence: self._enable_identity_insert = \ seq_column.key in self.compiled_parameters[0] else: self._enable_identity_insert = False if self._enable_identity_insert: self.cursor.execute( "SET IDENTITY_INSERT %s ON" % self.dialect.identifier_preparer.format_table(tbl)) if self.isddl: # TODO: to enhance this, we can detect "ddl in tran" on the # database settings. this error message should be improved to # include a note about that. if not self.should_autocommit: raise exc.InvalidRequestError( "The Sybase dialect only supports " "DDL in 'autocommit' mode at this time.") self.root_connection.engine.logger.info( "AUTOCOMMIT (Assuming no Sybase 'ddl in tran')") self.set_ddl_autocommit( self.root_connection.connection.connection, True)
def add(cls, id, shortname, fullname, city_id, finance_stage=0, process_rate=0, features='', introduce='', address='', advantage='', size=0): company = cls(id=id, shortname=shortname, fullname=fullname, finance_stage=int(finance_stage), city_id=int(city_id), process_rate=process_rate, features=features, introduce=introduce, address=address, advantage=advantage, size=int(size)) try: cls.session.merge(company) cls.session.flush() except InvalidRequestError as e: cls.session.rollback() raise e
def add(cls, id, company_id, city_id, title, work_year=0, department='', salary='', education=0, description='', advantage='', job_nature=0, created_at=0): job = cls(id=id, title=title, city_id=city_id, company_id=company_id, work_year=int(work_year), department=department, salary=salary, education=int(education), description=description, advantage=advantage, job_nature=int(job_nature), created_at=created_at) try: cls.session.merge(job) cls.session.flush() except InvalidRequestError as e: cls.session.rollback() raise e
def unregister(cls): try: db.event.remove(db.session, 'before_commit', cls._unexpectedly_closed) db.event.remove(db.session, 'after_soft_rollback', cls._unexpectedly_closed) except InvalidRequestError: # ok, it is not registered pass
def delete(self, user, force=False): """Release all user's resources and mark user as deleted. :param user: user id, username or kuberdock.users.models.User object :param force: if True, will not raise ResourceReleaseError :raises ResourceReleaseError: if couldn't release some resources :raises APIError: if user was not found """ user = self._convert_user(user) self._is_deletable(user, raise_=True) uid = user.id user.logout(commit=False) pod_collection = PodCollection(user) for pod in pod_collection.get(as_json=False): pod_collection.delete(pod['id']) # Now, when we have deleted all pods, events will rape db session a # little bit. # Get new, clean user instance to prevent a lot of various SA errors user = User.get(uid) # Add some delay for deleting drives to allow kubernetes unmap drives # after a pod was deleted. Also in some cases this delay is not enough, # for example DBMS in container may save data to PD for a long time. # So there is a regular procedure to clean such undeleted drives # tasks.clean_drives_for_deleted_users. for pd in user.persistent_disks: PersistentStorage.end_stat(pd.name, user.id) delete_persistent_drives_task.apply_async( ([pd.id for pd in user.persistent_disks],), countdown=10 ) prefix = '__' + generate() user.username += prefix user.email += prefix user.deleted = True try: db.session.commit() except (IntegrityError, InvalidRequestError), e: db.session.rollback() raise APIError('Cannot delete a user: {0}'.format(str(e)), 500)
def visit_select_precolumns(self, select): """Access puts TOP, it's version of LIMIT here """ s = select.distinct and "DISTINCT " or "" if select.limit: s += "TOP %s " % (select.limit) if select.offset: raise exc.InvalidRequestError( 'Access does not support LIMIT with an offset') return s
def create(cls, message, job): ''' Helper function to create an error object and save it. ''' err = cls(message=message, job=job) try: err.save() except InvalidRequestError: Session.rollback() err.save() finally: # No need to alert administrator so don't log as an error log.info(message)
def clean_harvest_log(condition): Session.query(HarvestLog).filter(HarvestLog.created <= condition)\ .delete(synchronize_session=False) try: Session.commit() except InvalidRequestError: Session.rollback() log.error('An error occurred while trying to clean-up the harvest log table') log.info('Harvest log table clean-up finished successfully')
def make_transient_to_detached(instance): ''' Moved from sqlalchemy newer version ''' state = attributes.instance_state(instance) if state.session_id or state.key: raise sa_exc.InvalidRequestError( "Given object must be transient") state.key = state.mapper._identity_key_from_state(state) if state.deleted: del state.deleted state._commit_all(state.dict) state._expire_attributes(state.dict, state.unloaded)
def pre_exec(self): if self.isinsert: tbl = self.compiled.statement.table seq_column = tbl._autoincrement_column insert_has_sequence = seq_column is not None if insert_has_sequence: self._enable_identity_insert = \ seq_column.key in self.compiled_parameters[0] else: self._enable_identity_insert = False if self._enable_identity_insert: self.cursor.execute("SET IDENTITY_INSERT %s ON" % self.dialect.identifier_preparer.format_table(tbl)) if self.isddl: # TODO: to enhance this, we can detect "ddl in tran" on the # database settings. this error message should be improved to # include a note about that. if not self.should_autocommit: raise exc.InvalidRequestError( "The Sybase dialect only supports " "DDL in 'autocommit' mode at this time.") self.root_connection.engine.logger.info( "AUTOCOMMIT (Assuming no Sybase 'ddl in tran')") self.set_ddl_autocommit( self.root_connection.connection.connection, True)
def _do_account_dir(self, acct_id, path): """ Handle all OFX statements in a per-account directory. :param acct_id: account database ID :type acct_id: int :param path: absolute path to per-account directory :type path: str """ logger.debug('Doing account %d directory (%s)', acct_id, path) files = {} for f in os.listdir(path): p = os.path.join(path, f) if not os.path.isfile(p): continue extension = p.split('.')[-1].lower() if extension not in ['ofx', 'qfx']: continue files[p] = os.path.getmtime(p) logger.debug('Found %d files for account %d', len(files), acct_id) # run through the files, oldest to newest success = 0 already = 0 for p in sorted(files, key=files.get): try: self._do_one_file(acct_id, p) success += 1 except DuplicateFileException: already += 1 logger.warning('OFX is already parsed for account; skipping') except (InvalidRequestError, IntegrityError, TypeError): raise except Exception: logger.error('Exception parsing and inserting file %s', p, exc_info=True) logger.info('Successfully parsed and inserted %d of %d files for ' 'account %d; %d files already in DB', success, len(files), acct_id, already)
def instrument_declarative_classes(self, all_classes): registry = {} for cls in all_classes: try: if not hasattr(cls, '__mapper__'): instrument_declarative(cls, registry, metadata) logging.getLogger(__file__).info( 'Instrumented %s: __tablename__=%s [polymorphic_identity=%s]' % \ (cls, cls.table, cls.mapper.polymorphic_identity) ) except InvalidRequestError: logging.info('skipping declarative instrumentation of %s' % cls)
def test_add_clock_column_verification(): with pytest.raises(exc.InvalidRequestError): @temporal.add_clock('prop_a', 'prop_b', 'prop_c') class TempFail(temporal.Clocked, models.ExpectedFailBase): __tablename__ = 'temp_fail' __table_args__ = {'schema': models.SCHEMA} id = models.auto_uuid() prop_a = sa.Column(sa.Integer) prop_b = sa.Column(sap.TEXT)
def get_by_name(cls, name): try: return cls.query.filter_by(name=name).one() except exc.InvalidRequestError: return None
def get_by_email(cls, email): try: return cls.query.filter_by(email=email).one() except exc.InvalidRequestError: return None
def get_by_nick(cls, nick): try: return cls.query.filter_by(nick=nick).one() except exc.InvalidRequestError: return None
def login_user(cls, email, password): try: user = cls.query.filter_by(email=email).one() except exc.InvalidRequestError: return None if pbkdf2.crypt(password, user.pwhash) == user.pwhash: if flask.has_request_context(): user.last_login_ip = flask.request.remote_addr db.session.commit() return user return None
def with_totals(self): if not self._group_by: raise exc.InvalidRequestError( "Query.with_totals() can be used only with specified " "GROUP BY, call group_by()" ) self._with_totals = True return self
def add_all(cls, datas): try: db_session.add_all(datas) db_session.commit() except (SqlalchemyIntegrityError, PymysqlIntegrityError, InvalidRequestError): for data in datas: cls.add_one(data)
def decorate_query( self, query, owner_alias, coll_alias, parent_instance, ctx): # This will decorate a query with a join on the relation. inv = self.back_relation if inv: query = query.join(owner_alias, getattr(coll_alias, inv.key)) else: # hope for the best try: query = query.join(owner_alias) except InvalidRequestError: log.error("Could not join %s to %s" % (owner_alias, query)) # This is very likely to fail downstream return query found_key = False if inv and not uses_list(inv): # Try to constrain on coll_alias's key vs owner_alias. # Difficult cases happen when tombstone is part of the # reln's columns for column in inv.local_columns: for fk in column.foreign_keys: if fk.column.table == parent_instance.__class__.__table__: query = query.filter( getattr(coll_alias, column.name) == parent_instance.id) found_key = True if not found_key: query = query.filter(owner_alias.id == parent_instance.id) return query
def pre_exec(self): quoted_bind_names = \ getattr(self.compiled, '_quoted_bind_names', None) if quoted_bind_names: if not self.dialect.supports_unicode_statements: # if DBAPI doesn't accept unicode statements, # keys in self.parameters would have been encoded # here. so convert names in quoted_bind_names # to encoded as well. quoted_bind_names = \ dict( (fromname.encode(self.dialect.encoding), toname.encode(self.dialect.encoding)) for fromname, toname in quoted_bind_names.items() ) for param in self.parameters: for fromname, toname in quoted_bind_names.items(): param[toname] = param[fromname] del param[fromname] if self.dialect.auto_setinputsizes: # cx_oracle really has issues when you setinputsizes # on String, including that outparams/RETURNING # breaks for varchars self.set_input_sizes( quoted_bind_names, exclude_types=self.dialect.exclude_setinputsizes ) # if a single execute, check for outparams if len(self.compiled_parameters) == 1: for bindparam in self.compiled.binds.values(): if bindparam.isoutparam: dbtype = bindparam.type.dialect_impl(self.dialect).\ get_dbapi_type(self.dialect.dbapi) if not hasattr(self, 'out_parameters'): self.out_parameters = {} if dbtype is None: raise exc.InvalidRequestError( "Cannot create out parameter for parameter " "%r - its type %r is not supported by" " cx_oracle" % (bindparam.key, bindparam.type) ) name = self.compiled.bind_names[bindparam] self.out_parameters[name] = self.cursor.var(dbtype) self.parameters[0][quoted_bind_names.get(name, name)] = \ self.out_parameters[name]