async def select_by_id(cls, id_: str): return await db.fetch_one(query=Select([cls]).where(cls.id == id_))
def _insertToDb(dispIds, gridCompiledQueueItems, gridKeyIndexesByDispId, locationCompiledQueueItems, locationIndexByDispId, queueIds): """ Insert to DB This method provides the DB inserts and deletes after the data has been calculated. """ startTime = datetime.now(pytz.utc) dispBaseTable = DispBase.__table__ dispQueueTable = DispIndexerQueue.__table__ gridKeyIndexTable = GridKeyIndex.__table__ gridQueueTable = GridKeyCompilerQueue.__table__ locationIndexTable = LocationIndex.__table__ locationIndexCompilerQueueTable = LocationIndexCompilerQueue.__table__ engine = CeleryDbConn.getDbEngine() conn = engine.connect() transaction = conn.begin() try: lockedDispIds = conn.execute( Select(whereclause=dispBaseTable.c.id.in_(dispIds), columns=[dispBaseTable.c.id], for_update=True)) lockedDispIds = [o[0] for o in lockedDispIds] # Ensure that the Disps exist, otherwise we get an integrity error. gridKeyIndexes = [] locationIndexes = [] for dispId in lockedDispIds: gridKeyIndexes.extend(gridKeyIndexesByDispId[dispId]) if dispId in locationIndexByDispId: locationIndexes.append(locationIndexByDispId[dispId]) # Delete existing items in the location and grid index # grid index conn.execute( gridKeyIndexTable.delete(gridKeyIndexTable.c.dispId.in_(dispIds))) # location index conn.execute( locationIndexTable.delete( locationIndexTable.c.dispId.in_(dispIds))) # --------------- # Insert the Grid Key indexes if gridKeyIndexes: conn.execute(gridKeyIndexTable.insert(), gridKeyIndexes) # Directly insert into the Grid compiler queue. if gridCompiledQueueItems: conn.execute(gridQueueTable.insert(), [ dict(coordSetId=i.coordSetId, gridKey=i.gridKey) for i in gridCompiledQueueItems ]) # --------------- # Insert the Location indexes if locationIndexes: conn.execute(locationIndexTable.insert(), locationIndexes) # Directly insert into the Location compiler queue. if locationCompiledQueueItems: conn.execute(locationIndexCompilerQueueTable.insert(), [ dict(modelSetId=i.modelSetId, indexBucket=i.indexBucket) for i in locationCompiledQueueItems ]) # --------------- # Finally, delete the disp queue items conn.execute(dispQueueTable.delete(dispQueueTable.c.id.in_(queueIds))) transaction.commit() logger.debug("Committed %s GridKeyIndex in %s", len(gridKeyIndexes), (datetime.now(pytz.utc) - startTime)) except Exception as e: raise finally: conn.close()
async def select_by_passport_number(cls, number: str): return await db.fetch_one(query=Select([cls]).where(cls.passport_number == number))