-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.py
97 lines (85 loc) · 4.23 KB
/
Logger.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sqlite3
import threading
from FilePath import FilePathGetter
'''
Logger
db 접근용 클래스
기능
1. 테이블 생성
2. 텍스트 포함 이미지 삽입
3. 텍스트 미포함 이미지 삽입
4. 태그로 이미지 검색
5. 텍스트로 이미지 검색
'''
#TODO : 텍스트 포함, 미포함으로 나눌지 말지 정해야 함.(현재는 나눠진 상태)
class Logger:
def __init__(self, user_id):
#연결
self.db_name = FilePathGetter.getDBName() + "_" + user_id
self.conn = sqlite3.connect("C:\\Users\\yjm6560\\Desktop\\yjm6560\\CE\\graduation_project\\SearchPicture\\" + self.db_name + ".db", check_same_thread=False)
self.cur = self.conn.cursor()
self.lock = threading.Lock()
def createTable(self):
#db 파일 생성
create_query = "CREATE TABLE IF NOT EXISTS " + self.db_name + "(photo_id integer, path TEXT PRIMARY KEY , tag_list TEXT, text_img TEXT)"
self.cur.execute(create_query)
self.conn.commit()
def getPhotoByTag(self, tag_keywords):
#태그로 이미지 검색
select_query = "SELECT photo_id, path, tag_list FROM " + self.db_name + " WHERE"
for keyword in tag_keywords:
select_query += " tag_list LIKE \"%/" + keyword + "/%\"" + " AND "
self.cur.execute(select_query[0:-5])
return self.cur.fetchall()
def getPhotoByText(self, text_keywords):
#텍스트로 이미지 검색
select_query = "SELECT photo_id, path, tag_list, text_img FROM " + self.db_name + " WHERE"
for keyword in text_keywords:
select_query += " text_img LIKE \"%" + keyword + "%\"" + " AND "
self.cur.execute(select_query[0:-5])
return self.cur.fetchall()
def insertNonTextyPhoto(self, photo_id, photo_path, tag_list):
#텍스트 미포함 이미지 삽입
self.lock.acquire()
insert_query = "INSERT INTO " + self.db_name + "(photo_id, path, tag_list) VALUES( ? , ? , ? )"
try:
self.cur.execute(insert_query, (photo_id, photo_path, "/" + "/".join(tag_list) + "/"))
except sqlite3.IntegrityError as e:
select_query = f"SELECT tag_list FROM {self.db_name} WHERE path=\"{photo_path}\""
self.cur.execute(select_query)
obj_list = self.cur.fetchall()
if obj_list and obj_list[0][0] != "":
print("OBJ LIST : ",obj_list)
tag_list = tag_list + obj_list[0][0].split("/")
update_query = f"UPDATE {self.db_name} SET tag_list = ? WHERE path = ?"
self.cur.execute(update_query, ("/" + "/".join(tag_list) + "/", photo_path))
self.lock.release()
self.conn.commit()
def insertTextyPhoto(self, photo_id, photo_path, text):
#텍스트 포함 이미지 삽입
#TODO: 이어진 글자 사이에 띄어쓰기가 있다고 인식해서 일단 띄어쓰기나 엔터 없앰 추후 어떻게 할지 논의
text = text.replace(" ", "", len(text)).replace("\n", "", len(text))
insert_query = "INSERT INTO " + self.db_name + " VALUES( ? , ? , ? , ?)"
try:
self.cur.execute(insert_query, (photo_id, photo_path, "" , text))
except sqlite3.IntegrityError as e:
update_query = f"UPDATE {self.db_name} SET text_img = ? WHERE path = ?"
self.cur.execute(update_query, (text, photo_path))
self.conn.commit()
def getAllPath(self):
getPath_query = f"SELECT path FROM {self.db_name}"
self.cur.execute(getPath_query)
return self.cur.fetchall()
if __name__ == "__main__":
logger = Logger("yjm6560")
logger.cur.execute("DROP TABLE IF EXISTS " + logger.db_name)
logger.createTable()
logger.insertNonTextyPhoto(1, "a", ["note", "book", "pencil"])
logger.insertNonTextyPhoto(2, "b", ["news", "pen", "monitor"])
logger.insertNonTextyPhoto(3, "c", ["news", "phone", "monitor"])
logger.insertNonTextyPhoto(4, "d", ["mouse", "fly", "monitor"])
logger.insertTextyPhoto(5, "e", [], "Latte is horse")
logger.insertTextyPhoto(6, "f", [], "I was a car")
print(logger.getPhotoByText(["horse"]))
print(logger.getPhotoByText(["car", "was"]))
print(logger.getPhotoByTag(["monitor", "pen"]))