-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRelationalQueryProcessor.py
93 lines (82 loc) · 3.51 KB
/
RelationalQueryProcessor.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
from pandas import DataFrame
from processor import Processor
from queryProcessor import QueryProcessor
import pandas as pd
from sqlite3 import connect
from pandas import read_sql
class RelationalQueryProcessor(QueryProcessor):
def __init__(self):
super().__init__()
def getAllAnnotations(self):
with connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM Annotations"
df_sql = read_sql(query, con)
return df_sql
def getAllImages(self):
with connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM Images"
df_sql = read_sql(query, con)
return df_sql
def getAllEntites(self):
with connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM EntitiesWithMetadata"
df_sql = read_sql(query, con)
return df_sql
def getAnnotationsWithBody(self, bodyId: str):
with connect(self.dbPathOrUrl) as con:
query = f"""
SELECT * FROM Annotations
JOIN Images ON Annotations.annotation_bodies == Images.images_internal_id
WHERE image_ids = '{bodyId}'
"""
df_sql = read_sql(query, con)
return df_sql
def getAnnotationsWithBodyAndTarget(self, bodyId: str, targetId: str):
with connect(self.dbPathOrUrl) as con:
query = f"""
SELECT * FROM Annotations
JOIN Images ON Annotations.annotation_bodies == Images.images_internal_id
JOIN EntitiesWithMetadata ON Annotations.annotation_targets == EntitiesWithMetadata.metadata_internal_id
WHERE image_ids = '{bodyId}' AND id = '{targetId}'
"""
df_sql = read_sql(query, con)
return df_sql
def getAnnotationsWithTarget(self, targetId: str):
with connect(self.dbPathOrUrl) as con:
query = f"""
SELECT * FROM Annotations
JOIN EntitiesWithMetadata ON Annotations.annotation_targets == EntitiesWithMetadata.metadata_internal_id
WHERE id = '{targetId}'
"""
df_sql = read_sql(query, con)
return df_sql
def getEntitiesWithTitle(self, title: str):
with connect(self.dbPathOrUrl) as con:
query = f"""
SELECT *, TRIM(Creators.creator_name) AS new_creator
FROM EntitiesWithMetadata
LEFT JOIN Creators ON EntitiesWithMetadata.creator == Creators.creator_internal_id WHERE title = '{title}'
"""
df_sql = read_sql(query, con)
return df_sql
def getEntitiesWithCreator(self, creatorName: str):
with connect(self.dbPathOrUrl) as con:
query = f"""
SELECT *,
TRIM(Creators.creator_name) AS new_creator
FROM EntitiesWithMetadata
LEFT JOIN Creators ON EntitiesWithMetadata.creator == Creators.creator_internal_id
WHERE new_creator = '{creatorName}'
"""
df_sql = read_sql(query, con)
return df_sql
def getImagesWithTarget(self, targetId: str):
with connect(self.dbPathOrUrl) as con:
query = f"""
SELECT * FROM Images
LEFT JOIN Annotations ON Images.images_internal_id == Annotations.annotation_bodies
LEFT JOIN EntitiesWithMetadata ON Annotations.annotation_targets == EntitiesWithMetadata.metadata_internal_id
WHERE EntitiesWithMetadata.id = '{targetId}'
"""
df_sql = read_sql(query, con)
return df_sql