2 Module : Gargantext.Database.Bashql
3 Description : BASHQL to deal with Gargantext Database.
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 * BASHQL is a Domain Specific Language to deal with the Database
12 * BASHQL = functional (Bash * SQL)
14 * Which language to chose when working with a database ? To make it
15 simple, instead of all common Object Relational Mapping (ORM) [1]
16 strategy used nowadays inspired more by object logic than functional
17 logic, the semantics of BASHQL with focus on the function first.
19 * BASHQL focus on the function, i.e. use bash language function name,
20 and make it with SQL behind the scene. Then BASHQL is inspired more
21 by Bash language [2] than SQL and then follows its main commands as
22 specification and documentation.
25 1. Theoritical: database and FileSystems are each thought as a single
26 category, assumption based on theoretical work on databases by David Spivak [0].
27 2. Practical argument: basic bash commands are a daily practice among
31 1. Choose a command you like in Bash
32 2. Implement it in Haskell-SQL according to Gargantext Shema (Tree like
34 3. Translate it in BASHQL (follow previous implementations)
35 4. Make a pull request (enjoy the community)
37 * Implementation strategy: Functional adapations are made to the
38 gargantext languages options and SQL optimization are done continuously
39 during the project. For the Haskellish part, you may be inspired by
40 Turtle implementation written by Gabriel Gonzales [3] which shows how to
41 write Haskell bash translations.
44 - FileSystem is now a NodeSystem where each File is a Node in a Directed Graph (DG).
48 [0] MIT Press has published "Category theory for the sciences". The book
49 can also be purchased on Amazon. Here are reviews by the MAA, by the
52 [1] https://en.wikipedia.org/wiki/Object-relational_mapping
54 [2] https://en.wikipedia.org/wiki/Bash_(Unix_shell)
56 [3] https://github.com/Gabriel439/Haskell-Turtle-Library
60 {-# LANGUAGE NoImplicitPrelude #-}
61 {-# LANGUAGE FlexibleContexts #-}
63 module Gargantext.Database.Bashql ( get, get'
69 , postCorpus, postAnnuaire
73 import Control.Monad.Reader -- (Reader, ask)
75 import Data.Text (Text, pack)
77 import Data.Aeson.Types
78 import Data.List (last, concat)
80 import Gargantext.Core.Types
81 import Gargantext.Database.Utils (connectGargandb)
82 import Gargantext.Database.Node
83 import Gargantext.Prelude
85 import Opaleye hiding (FromField)
90 -- type PWD a = PWD UserId [a]
92 --data PWD' a = a | PWD' [a]
94 -- | TODO get Children or Node
95 get :: PWD -> Cmd [Node Value]
97 get pwd = Cmd . ReaderT $ \conn -> runQuery conn $ selectNodesWithParentID (last pwd)
99 -- | Home, need to filter with UserId
101 home = map node_id <$> Cmd (ReaderT (getNodesWithParentId 0 Nothing))
103 -- | ls == get Children
104 ls :: PWD -> Cmd [Node Value]
108 tree :: PWD -> Cmd [Node Value]
111 children <- mapM (\n -> get [node_id n]) ns
112 pure $ ns <> concat children
116 post :: PWD -> [NodeWrite'] -> Cmd Int64
119 post pth ns = Cmd . ReaderT $ mkNode (last pth) ns
121 --postR :: PWD -> [NodeWrite'] -> Cmd [Int]
122 --postR [] _ _ = pure [0]
123 --postR _ [] _ = pure [0]
124 --postR pth ns c = mkNodeR (last pth) ns c
126 --rm :: Connection -> PWD -> [NodeId] -> IO Int
129 del :: [NodeId] -> Cmd Int
131 del ns = deleteNodes ns
134 --put :: Connection -> PWD -> [a] -> IO Int64
138 -- cd (Home UserId) | (Node NodeId)
143 type CorpusName = Text
145 postCorpus :: ToJSON a => CorpusName -> (a -> Text) -> [a] -> Cmd NewNode
146 postCorpus corpusName title ns = do
149 postNode uid pid ( Node' NodeCorpus corpusName emptyObject
150 (map (\n -> Node' Document (title n) (toJSON n) []) ns)
154 -- import IMTClient as C
155 -- postAnnuaire "Annuaire IMT" (\n -> (maybe "" identity (C.prenom n)) <> " " <> (maybe "" identity (C.nom n))) (take 30 annuaire)
156 postAnnuaire :: ToJSON a => CorpusName -> (a -> Text) -> [a] -> Cmd NewNode
157 postAnnuaire corpusName title ns = do
160 postNode uid pid ( Node' Annuaire corpusName emptyObject
161 (map (\n -> Node' UserPage (title n) (toJSON n) []) ns)
164 --------------------------------------------------------------
166 --------------------------------------------------------------
169 get' :: PWD -> IO [Node Value]
175 ls' :: IO [Node Value]
180 tree' :: IO [Node Value]
189 postNode uid pid ( Node' NodeCorpus (pack "Premier corpus") emptyObject [ Node' Document (pack "Doc1") emptyObject []
190 , Node' Document (pack "Doc2") emptyObject []
191 , Node' Document (pack "Doc3") emptyObject []
196 -- myCorpus <- Prelude.map doc2hyperdataDocument <$> toDocs <$> snd <$> readCsv "doc/corpus_imt/Gargantext_Corpus_small.csv"
197 -- There is an error in the CSV parsing...
198 -- let myCorpus' = Prelude.filter (\n -> T.length (maybe "" identity (hyperdataDocument_title n)) > 30) myCorpus
201 del' :: [NodeId] -> IO Int
202 del' ns = runCmd' $ del ns
204 -- corporaOf :: Username -> IO [Corpus]
206 runCmd' :: Cmd a -> IO a
208 c <- connectGargandb "gargantext.ini"