From 2ab689e87cd8e87cbc585b84323cfe11f41e3c2f Mon Sep 17 00:00:00 2001 From: assar Date: Sat, 3 Jun 2017 22:09:20 +0000 Subject: [PATCH] Implemented simple query with filter --- server.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index 386b540..560a8d4 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,7 @@ import json import sqlalchemy from sqlalchemy.sql import select +from sqlalchemy.sql import text from flask import Flask from flask import render_template @@ -14,9 +15,28 @@ parts = {} def index(): return render_template('partsearch.html') -@app.route('/query/') -def query(query): - pass +@app.route('/query//') +def query(filter, query): + query = '%' + query + '%' + s = 'select * from parts where ' + if filter & (1 << 0) != 0: + s += 'type like :name or ' + if filter & (1 << 1) != 0: + s += 'partno like :name or ' + if filter & (1 << 2) != 0: + s += 'partnoalt like :name or ' + if filter & (1 << 3) != 0: + s += 'partnoalt2 like :name or ' + if filter & (1 << 4) != 0: + s += 'description like :name or ' + s = s[:-4] + ';' + s = text(s) + r = db_engine.execute(s, name=query) + l = [] + for row in r: + l.append(dict(row)) + r.close() + return json.dumps(l) def connect(user, password, db, host='localhost', port=5432): '''Returns a connection and a metadata object'''