-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.py
63 lines (51 loc) · 1.65 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pymongo as m
from bson.objectid import ObjectId
import config
client = m.MongoClient(config.mongo_hostname, config.mongo_port)
db = client[config.mongo_db]
if (len(config.mongo_username.strip()) > 0
and len(config.mongo_password.strip()) > 0):
db.authenticate(config.mongo_username, config.mongo_password)
queries = db[config.mongo_collection]
def get_tags_list(tags):
return [ tag.strip() for tag in tags.strip().split(",") \
if len(tag.strip()) > 0]
def insert_query(title, sql, tags, desc, who):
_tags = get_tags_list(tags)
queries.insert({
"title": title,
"sql": sql,
"tags": _tags,
"desc": desc,
"who": who
})
def update_query(id, title, sql, tags, desc, who):
_tags = get_tags_list(tags)
queries.update({
"_id": ObjectId(id)
},
{ "$set" : {
"title": title,
"sql": sql,
"tags": _tags,
"desc": desc,
"who": who
}}, upsert=False)
def get_queries_for_search(searchword):
title =list(queries.find({"title":{"$regex": searchword, "$options": "$i"}}))
tags = list(queries.find({"tags":{"$regex": searchword, "$options":"$i" }}))
who = list(queries.find({"who":{"$regex": searchword, "$options":"$i" }}))
if len(title) > 0:
return title
elif len(tags) > 0:
return tags
elif len(who) > 0:
return who
else:
return []
def delete_query(id):
queries.remove({"_id": ObjectId(id)})
def get_queries():
return list(queries.find())
def get_query_details(id):
return queries.find_one(ObjectId(id))