X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=vim_db.py;h=d6ac871052310da46846aa5a5fed35507f9b2b2a;hb=82232585e3ee87fd3228e7814f393a7d6c05fc79;hp=cdc3dcce3bfca31050c256d0be524a7e29d554d4;hpb=e9317ff999987c09369b9eab6bcd044ced8f1048;p=osm%2Fopenvim.git diff --git a/vim_db.py b/vim_db.py index cdc3dcc..d6ac871 100644 --- a/vim_db.py +++ b/vim_db.py @@ -217,33 +217,41 @@ class vim_db(): 'WHERE': dict of key:values, translated to key=value AND ... (Optional) 'WHERE_NOT': dict of key:values, translated to key!=value AND ... (Optional) 'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional) + 'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional) 'LIMIT': limit of number of rows (Optional) + 'DISTINCT': make a select distinct to remove repeated elements Return: a list with dictionarys at each row ''' #print sql_dict - select_= "SELECT " + ("*" if 'SELECT' not in sql_dict else ",".join(map(str,sql_dict['SELECT'])) ) + select_ = "SELECT " + if sql_dict.get("DISTINCT"): + select_ += "DISTINCT " + select_ += ("*" if not sql_dict.get('SELECT') else ",".join(map(str,sql_dict['SELECT'])) ) #print 'select_', select_ from_ = "FROM " + str(sql_dict['FROM']) #print 'from_', from_ where_and = None where_or = None - if 'WHERE' in sql_dict and len(sql_dict['WHERE']) > 0: - w=sql_dict['WHERE'] - where_and = " AND ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) - if 'WHERE_NOT' in sql_dict and len(sql_dict['WHERE_NOT']) > 0: - w=sql_dict['WHERE_NOT'] - where_and_not = " AND ".join(map( lambda x: str(x) + (" is not Null" if w[x] is None else "!='"+str(w[x])+"'"), w.keys()) ) + w = sql_dict.get('WHERE') + if w: + where_and = " AND ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) + w = sql_dict.get('WHERE_NOT') + if w: + where_and_not = " AND ".join(map( lambda x: str(x) + (" is not Null" if w[x] is None else "!='"+str(w[x])+"'"), w.keys()) ) if where_and: where_and += " AND " + where_and_not else: where_and = where_and_not - if 'WHERE_OR' in sql_dict and len(sql_dict['WHERE_OR']) > 0: - w=sql_dict['WHERE_OR'] + w = sql_dict.get('WHERE_OR') + if w: where_or = " OR ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) if where_and!=None and where_or!=None: - where_ = "WHERE (" + where_and + ") OR " + where_or + if sql_dict.get("WHERE_AND_OR") == "AND": + where_ = "WHERE " + where_and + " AND (" + where_or + ")" + else: + where_ = "WHERE (" + where_and + ") OR " + where_or elif where_and!=None and where_or==None: where_ = "WHERE " + where_and elif where_and==None and where_or!=None: @@ -251,7 +259,7 @@ class vim_db(): else: where_ = "" #print 'where_', where_ - limit_ = "LIMIT " + str(sql_dict['LIMIT']) if 'LIMIT' in sql_dict else "" + limit_ = "LIMIT " + str(sql_dict['LIMIT']) if sql_dict.get("LIMIT") else "" #print 'limit_', limit_ cmd = " ".join( (select_, from_, where_, limit_) ) for retry_ in range(0,2):