def solutions(): """Asks for a n problem""" context = {} n = request.args.get('n') context['n'] = n if not n: return redirect(url_for('index')) n = int(n) session = session_factory() query = session.query(SolutionQuery).filter_by(n=n) if query.count() == 0: status = 0 create_solution_query(n, 1) Thread(target=save_solutions, args=[n]).start() else: status = query.first().status if status == 2: context['solutions'] = session.query(Solution).filter_by(n=n) context['status'] = status session.close() return render_template('solutions.html', **context)
def find_by_model(cls, model): ''' Returns the vehicle of the queried model :param model: :return Car: ''' session = session_factory() query = session.query(Car).filter_by(model=model).first() return query
def save_solutions(n): """Solution saver""" for solution in solve_puzzle(n): create_solution(n, solution) session = session_factory() solution = session.query(SolutionQuery).filter_by(n=n).first() solution.status = 2 session.commit() session.close()
def handle(): # 普通查询 with session_factory() as session: res = session.query(Factory).all() data = to_dict(res) # 基于一对多关系的连表查询 # with session_factory() as session: # res = session.query(Factory.name, Product.name).join(Product, Factory.factory_id == Product.factory_id).all() # res = session.query(Factory.name, Product.name).filter(Factory.factory_id == Product.factory_id).all() # 基于多对多关系的连表查询 # 1. 查这张订单 23ff2f2a-bc09-4444-b415-121bd79df5df 包含的商品名称 # with session_factory() as session: # res = session.query(OrdersProduct.product_id, Product.name)\ # .join(Product, OrdersProduct.product_id == Product.product_id)\ # .filter(OrdersProduct.order_id == "23ff2f2a-bc09-4444-b415-121bd79df5df")\ # .all() # 2. 查这个商品 a473f8af-bdbd-418d-a986-50e10bd9673c 对应订单的价格 # with session_factory() as session: # res = session.query(OrdersProduct.order_id, Orders.price)\ # .join(Orders, OrdersProduct.order_id == Orders.order_id)\ # .filter(Product.product_id == "a473f8af-bdbd-418d-a986-50e10bd9673c")\ # .all() # 原生 SQL # sql = text("select * from factory where name=:name;") # res = dbengine.execute(sql, {"name": "工厂1号"}) # for row in res: # for k, v in row.items(): # print("{}={}".format(k, v)) data = {"code": 0, "message": "success", "data": data} result = json.dumps(data, ensure_ascii=False) response = Response(result, content_type="application/json; charset=utf-8") return response
def find_by_number_of_passanger_seats(cls, passenger_seats): session = session_factory() return session.query(Car).filter_by(passenger_seats=passenger_seats).all()
def find_by_type(cls, type): session = session_factory() return session.query(Car).filter_by(type=type).all()
def save_to_db(self): session = session_factory() session.add(self) session.commit()
def find_by_email(cls, email): session = session_factory() query = session.query(User).filter_by(email=email).first() return query