我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用sqlalchemy.sql.exists()。
def getProjects(self, subscribed=False, active=False, topicless=False): """Retrieve projects. :param subscribed: If True limit to only subscribed projects. :param active: If True limit to only projects with active stories. :param topicless: If True limit to only projects without topics. """ query = self.session().query(Project) if subscribed: query = query.filter_by(subscribed=subscribed) if active: query = query.filter(exists().where(Project.active_stories)) if topicless: query = query.filter_by(topics=None) return query.order_by(Project.name).all()
def __init__(self, source, highlight, expiration, title, password): expiration = int(expiration) if not source: raise ValueError() self.source = source if title: self.title = title if password: self.password = password if expiration > 0: self.expire_at = datetime.now() + timedelta(minutes=expiration) if highlight == 'auto': self.lexer = guess_lexer(source).aliases[0] else: self.lexer = highlight for _ in range(5): slug = self._generate_random_slug() if not db.session.query(exists().where(Paste.slug == slug)).scalar(): self.slug = slug break else: raise RuntimeError()
def is_exists_feed(self, url): return self.session.query(exists().where(Feed.url == url)).scalar()
def is_exists_article(self, item): q = self.session.query( Article ).filter( (Article.domain == item['domain']) & (Article.title == item['title']) & (Article.pub_date >= item['pub_date'])).exists() return self.session.query(q).scalar()
def _get_unscheduled_bgp_speakers(self, context): """BGP speakers that needs to be scheduled. """ no_agent_binding = ~sql.exists().where( bgp_db.BgpSpeaker.id == bgp_dras_db.BgpSpeakerDrAgentBinding.bgp_speaker_id) query = context.session.query(bgp_db.BgpSpeaker.id).filter( no_agent_binding) return [bgp_speaker_id_[0] for bgp_speaker_id_ in query]
def toolbar_icon_clicked(self, widget, movie): # # remove unused posters # session = self.db.Session() delete_posters = delete(posters_table) delete_posters = delete_posters.where(not_(exists([movies_table.c.movie_id], and_(posters_table.c.md5sum==movies_table.c.poster_md5)).correlate(posters_table))) log.debug(delete_posters) session.execute(delete_posters) session.commit() # # compressing sqlite databases # if self.app.config.get('type', 'sqlite', section='database') == 'sqlite': databasefilename = "%s.db" % os.path.join(self.app.locations['home'], self.app.config.get('name', section='database')) pagesize = gutils.get_filesystem_pagesize(databasefilename) # works since sqlite 3.5.8 # python 2.5 doesn't include 3.x but perhaps in future versions # another way is the installation of pysqlite2 with 2.5.6/2.6.0 or higher try: from pysqlite2 import dbapi2 as sqlite3 con = sqlite3.connect(databasefilename) try: con.isolation_level = None cur = con.cursor() cur.execute('PRAGMA page_size=' + str(pagesize)) cur.execute('VACUUM;') finally: con.close() except: log.error('fallback to default driver') self.app.db.engine.execute('PRAGMA page_size=' + str(pagesize)) self.app.db.engine.execute('VACUUM;') gutils.info(_("Finished"))
def __init__(self, db, store, read=True, write=True, read_through_write=True, delete=True, create_db=False, schema=None, create_schema=True): upgrade_db=False super(PostgresRepo, self).__init__(read=read, write=write, read_through_write=read_through_write, delete=delete) if not isinstance(db, string_type) and schema is not None: raise ValueError("You can only provide a schema with a DB url.") init_db = False if create_db and isinstance(db, string_type): _create_db_if_needed(db) init_db = True upgrade_db = False self._run = None if isinstance(db, string_type): if create_db: init_db = True self._db_engine = _db_engine(db, schema) self._sessionmaker = sqlalchemy.orm.sessionmaker(bind=self._db_engine) else: self._session = db if create_schema and schema is not None: with self.session() as session: q = sa.exists(sa.select([("schema_name")]).select_from(sa.text("information_schema.schemata")) .where(sa.text("schema_name = :schema") .bindparams(schema=schema))) if not session.query(q).scalar(): session.execute(CreateSchema(schema)) session.commit() init_db = True upgrade_db = False if init_db: self._db_init() if upgrade_db: self._db_upgrade() self.blobstore = store