-- 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, AbbreviationID) 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 ParsedSubjects USING (SubjectID)
INNER JOIN ParsedRelationships USING (RelationshipID)
INNER JOIN ParsedObjects USING (ObjectID)
INNER JOIN WikipediaAbstracts USING (MovieID)
    -- insert WHERE here
GROUP BY MovieID;
