-- Insert MovieURI into Movies ; MovieID is auto incremental
INSERT INTO  Movies (MovieURI) VALUES (?);

-- Get MovieID where MovieURI equal given value
SELECT MovieID FROM Movies WHERE MovieURI = ?;

-- SetPageId
INSERT INTO  WikiPageIDs (MovieID, PageID) VALUES (?,?);

-- Get MovieId by PageID ... ( to create WikipediaAbstract)
SELECT MovieID FROM WikiPageIDs WHERE PageID = ?;

-- SetAbstract ...

INSERT INTO  WikipediaAbstracts (MovieID, Abstract) VALUES (?,?);


-- SetOrigin
---
INSERT INTO  Origins (OriginName) VALUES ("dataset.csv"),("reverse.csv");

-- GetOrigin
SELECT OriginID FROM Origins WHERE OriginName = ?;

-- Subject, Relationship, Object, RDF
INSERT INTO  Subjects (SubjectURI, OriginID) VALUES (?,?);
INSERT INTO  Relationships (RelationshipURI) VALUES (?);
INSERT INTO  Objects (ObjectURI, OriginID) VALUES (?,?);

SELECT SubjectID FROM Subjects WHERE SubjectURI = ?;
SELECT RelationshipID FROM Relationships WHERE RelationshipURI = ?;
SELECT ObjectID FROM Objects WHERE ObjectURI = ?;


INSERT INTO  RDFs (MovieID, SubjectID, RelationshipID, ObjectID) VALUES (?,?,?,?);

-- Prefixes
INSERT INTO Abbreviations(URI, Abbreviation) VALUES (?,?);
INSERT INTO Objects_Abbreviations(ObjectID, AbrreviationID) VALUES (?,?);
INSERT INTO Relationships_Abbreviations(RelationshipID, AbbreviationID) VALUES (?,?);
INSERT INTO Subjects_Abbreviations(SubjectID, AbbreviationID) VALUES (?,?);

-- Please be sure it is a URI before running this query
--  and take at least until the domain and the first path part
SELECT AbbreviationID FROM Abbreviations WHERE URI LIKE ?;

-- Query to retrieve data
SELECT MovieID, GROUP_CONCAT('<SOT>' || '<SUB>' || SubjectURI || '<REL>' || RelationshipURI || '<OBJ>' || ObjectURI || '<EOT>', '') as RDF_String, Abstract
FROM RDFs
INNER JOIN Subjects USING (SubjectID)
INNER JOIN Relationships USING (RelationshipID)
INNER JOIN Objects USING (ObjectID)
INNER JOIN WikipediaAbstracts USING (MovieID)
GROUP BY MovieID;
