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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
{-
Copyright (C) 2022 Yuchen Pei.
This file is part of fsd.
fsd is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
fsd is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with fsd. If not, see <https://www.gnu.org/licenses/>.
-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module FSD.Db where
import Control.Monad
import FSD.Copyright
import Data.Text (Text)
import Data.Text qualified as T
import Database.SQLite.Simple
import FSD.Types
newtype FsDb = FsDb {getConn :: Connection}
withFsDb :: FilePath -> (FsDb -> IO a) -> IO a
withFsDb path f = withConnection path (f . FsDb)
-- creations
initConn :: FsDb -> IO ()
initConn (FsDb conn) = do
execute_
conn
"CREATE TABLE IF NOT EXISTS sources \
\( package TEXT NOT NULL PRIMARY KEY ON CONFLICT REPLACE \
\, version TEXT NOT NULL \
\, file TEXT NOT NULL \
\, homepage TEXT \
\)"
execute_
conn
"CREATE TABLE IF NOT EXISTS packages \
\( package TEXT NOT NULL PRIMARY KEY ON CONFLICT REPLACE \
\, version TEXT NOT NULL \
\, homepage TEXT \
\, tags TEXT NOT NULL \
\)"
execute_
conn
"CREATE TABLE IF NOT EXISTS translation \
\( package TEXT NOT NULL PRIMARY KEY ON CONFLICT REPLACE \
\, shortDesc TEXT NOT NULL \
\, fullDesc TEXT NOT NULL \
\)"
execute_
conn
"CREATE TABLE IF NOT EXISTS changelog \
\( package TEXT NOT NULL PRIMARY KEY ON CONFLICT REPLACE \
\, version TEXT NOT NULL \
\, author TEXT NOT NULL \
\, date TEXT NOT NULL \
\)"
execute_
conn
"CREATE TABLE IF NOT EXISTS copyright \
\( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT \
\, package TEXT NOT NULL \
\, entryId INTEGER NOT NULL \
\, copyright TEXT \
\, license TEXT NOT NULL \
\, note TEXT \
\, CONSTRAINT uniqname UNIQUE (package, entryId) ON CONFLICT REPLACE \
\)"
execute_
conn
"CREATE TABLE IF NOT EXISTS upstream \
\( package TEXT NOT NULL PRIMARY KEY ON CONFLICT REPLACE \
\, name TEXT \
\, contacts TEXT \
\, sources TEXT \
\)"
-- insertions
insertSource :: FsDb -> Source -> IO ()
insertSource (FsDb conn) source =
execute conn "INSERT INTO sources VALUES (?,?,?,?)" source
insertPackage :: FsDb -> Package -> IO ()
insertPackage (FsDb conn) package =
execute conn "INSERT INTO packages VALUES (?,?,?,?)" package
insertTranslation :: FsDb -> Translation -> IO ()
insertTranslation (FsDb conn) translation =
execute conn "INSERT INTO translation VALUES (?,?,?)" translation
insertFSDChangeLogEntry :: FsDb -> FSDChangeLogEntry -> IO ()
insertFSDChangeLogEntry (FsDb conn) change =
execute conn "INSERT INTO changelog VALUES (?,?,?,?)" change
insertCopyright :: FsDb -> Copyright -> IO ()
insertCopyright db (Copyright package licenses) =
zipWithM_ (insertLicense db package) [0 ..] licenses
insertUpstream :: FsDb -> Upstream -> IO ()
insertUpstream (FsDb conn) upstream =
execute conn "INSERT INTO upstream VALUES (?,?,?,?)" upstream
insertLicense :: FsDb -> Text -> Int -> LicenseInfo -> IO ()
insertLicense (FsDb conn) package idx (LicenseInfo copyright license note) =
execute
conn
"INSERT INTO copyright (package,entryId,copyright,license,note) \
\VALUES (?,?,?,?,?)"
(package, idx, copyright, license, note)
-- queries
getDbAll :: FsDb -> IO [Source :. Package :. Translation :. FSDChangeLogEntry :. Upstream]
getDbAll (FsDb conn) =
query_
conn
"SELECT sources.*,packages.*,translation.*,changelog.*,upstream.* \
\FROM sources JOIN packages USING (package,version)\
\ JOIN changelog USING (package,version)\
\ JOIN translation USING (package)\
\ JOIN upstream USING (package)\
\"
getDbPkgNames :: FsDb -> IO [Text]
getDbPkgNames (FsDb conn) = do
rows <- query_ conn
"SELECT DISTINCT package\
\ FROM sources JOIN packages USING (package,version)\
\ JOIN changelog USING (package,version)\
\ JOIN translation USING (package)\
\ JOIN upstream USING (package)\
\ JOIN copyright USING (package)\
\"
return $ fromOnly <$> rows
getDbAllTypes :: Text -> FsDb -> IO (Maybe (Source :. Package :. Translation :. FSDChangeLogEntry :. Upstream))
getDbAllTypes pkgName (FsDb conn) = do
results <- query
conn
"SELECT sources.*,packages.*,translation.*,changelog.*,upstream.* \
\FROM sources JOIN packages USING (package,version)\
\ JOIN changelog USING (package,version)\
\ JOIN translation USING (package)\
\ JOIN upstream USING (package) \
\WHERE package = ?\
\" (Only pkgName)
return $ case results of
[source] -> Just source
otherwise -> Nothing
getDbSource :: Text -> FsDb -> IO (Maybe Source)
getDbSource pkgName (FsDb conn) = do
results <- query conn "SELECT * FROM sources WHERE package = ?" (Only pkgName)
return $ case results of
[source] -> Just source
otherwise -> Nothing
getDbPackage :: Text -> FsDb -> IO (Maybe Package)
getDbPackage pkgName (FsDb conn) = do
results <- query conn "SELECT * FROM packages WHERE package = ?" (Only pkgName)
return $ case results of
[package] -> Just package
otherwise -> Nothing
getDbTranslation :: Text -> FsDb -> IO (Maybe Translation)
getDbTranslation pkgName (FsDb conn) = do
results <- query conn "SELECT * FROM translation WHERE package = ?" (Only pkgName)
return $ case results of
[translation] -> Just translation
otherwise -> Nothing
getDbFSDChangeLogEntry :: Text -> FsDb -> IO (Maybe FSDChangeLogEntry)
getDbFSDChangeLogEntry pkgName (FsDb conn) = do
results <- query conn "SELECT * FROM changelog WHERE package = ?" (Only pkgName)
return $ case results of
[entry] -> Just entry
otherwise -> Nothing
getDbUpstream :: Text -> FsDb -> IO (Maybe Upstream)
getDbUpstream pkgName (FsDb conn) = do
results <- query conn "SELECT * FROM upstream WHERE package = ?" (Only pkgName)
return $ case results of
[upstream] -> Just upstream
otherwise -> Nothing
getDbCopyright :: Text -> FsDb -> IO (Maybe Copyright)
getDbCopyright pkgName (FsDb conn) = do
results <-
query
conn
"SELECT copyright,license,note FROM copyright WHERE package = ?"
(Only pkgName) ::
IO [LicenseInfo]
return $
if (null results) then Nothing else Just $ Copyright pkgName results
|