-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.py
125 lines (111 loc) · 2.13 KB
/
queries.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
BOOK_FILTER_STR = """
and book_id in ({})
"""
BOOK_COUNT = """
select count(*)
from books_book
"""
BOOK_DETAILS = """
SELECT
gutenberg_id,
COALESCE(download_count,0) as count,
media_type,
title
FROM books_book
ORDER BY count DESC
LIMIT {}
"""
BOOK_DETAILS_BY_ID = """
SELECT
gutenberg_id,
COALESCE(download_count,0) as count,
media_type,
title
FROM books_book
WHERE gutenberg_id in ({})
ORDER BY count DESC
LIMIT {}
"""
BOOK_IDS_BY_LANGUAGE = """
select book_id
from books_book_languages bbl
join books_language bl
on bbl.language_id = bl.id
where code in ({})
"""
BOOK_IDS_BY_MIME_TYPE = """
select book_id
from books_format bf
where mime_type like '%{}%'
"""
BOOK_IDS_BY_AUTHOR = """
select book_id
from books_book_authors bba
join books_author ba
on bba.author_id = ba.id
where LOWER(ba.name) like '%{}%'
"""
BOOK_IDS_BY_TITLE = """
select gutenberg_id
from books_book bba
where LOWER(bba.title) like '%{}%'
"""
BOOK_IDS_BY_TOPIC = """
select book_id
from books_book_subjects bbs
join books_subject bs
on bbs.subject_id = bs.id
where {0}
UNION
select book_id
from books_book_bookshelves bbb
join books_bookshelf bs
on bbb.bookshelf_id = bs.id
where {0}
"""
BOOK_ID_BY_AUTHOR_AND_TITLE = """
select bb.gutenberg_id
from books_book_authors bba
join books_author ba
on bba.author_id = ba.id
join books_book bb
on bba.book_id = bb.gutenberg_id
where LOWER(ba.name) like '%{}%'
and lower(bb.title) like '%{}%'
"""
BOOK_AUTHORS = """
select
ba.name,
ba.birth_year,
ba.death_year
from books_book_authors bba
join books_author ba
on bba.author_id = ba.id
where bba.book_id = {}
"""
BOOKSHELVES = """
select name
from books_book_bookshelves bbb
join books_bookshelf bb
on bbb.bookshelf_id = bb.id
where bbb.book_id = {}
"""
BOOK_LANGUAGES = """
select code
from books_book_languages bbl
join books_language bl
on bbl.language_id = bl.id
where bbl.book_id = {}
"""
BOOK_FORMAT = """
select mime_type, url
from books_format bf
where bf.book_id = {}
"""
BOOK_SUBJECTS = """
select name
from books_book_subjects bbs
join books_subject bs
on bbs.subject_id = bs.id
where bbs.book_id = {}
"""