#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# See LICENSE.txt included in this distribution for the specific
# language governing permissions and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at LICENSE.txt.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
#

createTableRepositories=\
  CREATE TABLE OPENGROK.REPOSITORIES (\
    ID INT GENERATED ALWAYS AS IDENTITY \
        CONSTRAINT REPOSITORIES_PK PRIMARY KEY, \
    PATH VARCHAR(32672) \
        CONSTRAINT REPOSITORIES_PATH_UNIQUE UNIQUE NOT NULL)

createTableDirectories=\
  CREATE TABLE OPENGROK.DIRECTORIES (\
    ID INT CONSTRAINT DIRECTORIES_PK PRIMARY KEY, \
    PATH VARCHAR(32672) NOT NULL, \
    PARENT INT CONSTRAINT DIRS_FK_DIRS \
        REFERENCES OPENGROK.DIRECTORIES ON DELETE CASCADE, \
    CONSTRAINT DIRECTORIES_PARENT_PATH_UNIQUE UNIQUE (PARENT, PATH), \
    REPOSITORY INT NOT NULL \
        CONSTRAINT DIRS_FK_REPO \
          REFERENCES OPENGROK.REPOSITORIES ON DELETE CASCADE, \
    CONSTRAINT DIRECTORIES_REPO_PATH_UNIQUE UNIQUE (REPOSITORY, PATH))

createTableFiles=\
  CREATE TABLE OPENGROK.FILES (\
    ID INT CONSTRAINT FILES_PK PRIMARY KEY, \
    NAME VARCHAR(32672) NOT NULL, \
    DIRECTORY INT NOT NULL \
        CONSTRAINT FILES_FK_DIR \
          REFERENCES OPENGROK.DIRECTORIES ON DELETE CASCADE, \
    CONSTRAINT FILES_DIR_NAME_UNIQUE UNIQUE (DIRECTORY, NAME))

createTableAuthors=\
  CREATE TABLE OPENGROK.AUTHORS (\
    ID INT CONSTRAINT AUTHORS_PK PRIMARY KEY, \
    REPOSITORY INT NOT NULL \
        CONSTRAINT AUTHORS_FK_REPO \
          REFERENCES OPENGROK.REPOSITORIES ON DELETE CASCADE, \
    NAME VARCHAR(32672), \
    CONSTRAINT AUTHORS_REPO_NAME_UNIQUE UNIQUE (REPOSITORY, NAME))

createTableChangesets=\
  CREATE TABLE OPENGROK.CHANGESETS (\
    ID INT CONSTRAINT CHANGESETS_PK PRIMARY KEY, \
    REPOSITORY INT NOT NULL \
        CONSTRAINT CHANGESETS_FK_REPO \
            REFERENCES OPENGROK.REPOSITORIES ON DELETE CASCADE, \
    REVISION VARCHAR(1024) NOT NULL, \
    AUTHOR INT NOT NULL \
        CONSTRAINT CHANGESETS_FK_AUTH \
          REFERENCES OPENGROK.AUTHORS ON DELETE CASCADE, \
    TIME TIMESTAMP NOT NULL, \
    MESSAGE VARCHAR(32672) NOT NULL, \
    CONSTRAINT CHANGESETS_REPO_REV_UNIQUE UNIQUE (REPOSITORY, REVISION))

createIndexChangesetsRepoIdDesc=\
  CREATE UNIQUE INDEX IDX_CHANGESETS_REPO_ID_DESC ON \
    OPENGROK.CHANGESETS(REPOSITORY, ID DESC)

createTableDirchanges=\
  CREATE TABLE OPENGROK.DIRCHANGES (\
    DIRECTORY INT NOT NULL \
        CONSTRAINT DIRCHANGES_FK_DIR \
          REFERENCES OPENGROK.DIRECTORIES ON DELETE CASCADE, \
    CHANGESET INT NOT NULL \
        CONSTRAINT DIRCHANGES_FK_CS \
          REFERENCES OPENGROK.CHANGESETS ON DELETE CASCADE, \
    CONSTRAINT DIRCHANGES_PK PRIMARY KEY (DIRECTORY, CHANGESET))

createTableFilemoves=\
  CREATE TABLE OPENGROK.FILEMOVES (\
    FILE INT NOT NULL \
        CONSTRAINT FILEMOVES_FK_FILE \
          REFERENCES OPENGROK.FILES ON DELETE CASCADE, \
    CHANGESET INT NOT NULL \
        CONSTRAINT FILEMOVES_FK_CS \
          REFERENCES OPENGROK.CHANGESETS ON DELETE CASCADE, \
    CONSTRAINT FILEMOVES_PK PRIMARY KEY (FILE, CHANGESET))

createTableFilechanges=\
  CREATE TABLE OPENGROK.FILECHANGES (\
    FILE INT NOT NULL \
        CONSTRAINT FILECHANGES_FK_FILE \
          REFERENCES OPENGROK.FILES ON DELETE CASCADE, \
    CHANGESET INT NOT NULL \
        CONSTRAINT FILECHANGES_FK_CS \
          REFERENCES OPENGROK.CHANGESETS ON DELETE CASCADE, \
    CONSTRAINT FILECHANGES_PK PRIMARY KEY (FILE, CHANGESET))

hasCacheForDirectory=\
  SELECT 1 FROM OPENGROK.REPOSITORIES R, OPENGROK.DIRECTORIES D \
  WHERE D.REPOSITORY = R.ID AND R.PATH = ? AND D.PATH = ?

getFileHistory=\
  SELECT CS.REVISION, A.NAME, CS.TIME, CS.MESSAGE, CS.ID \
  FROM \
     OPENGROK.CHANGESETS CS, \
     (SELECT * FROM OPENGROK.FILECHANGES UNION SELECT * FROM OPENGROK.FILEMOVES) FC, \
     OPENGROK.FILES F, \
     OPENGROK.DIRECTORIES D, \
     OPENGROK.AUTHORS A, \
     OPENGROK.REPOSITORIES R \
  WHERE R.PATH = ? \
      AND D.path = ? \
      AND F.name = ? \
      AND D.REPOSITORY = R.ID \
      AND CS.ID = FC.CHANGESET \
      AND FC.FILE = F.ID \
      AND D.ID = F.DIRECTORY \
      AND A.ID = CS.AUTHOR \
  ORDER BY CS.ID DESC

getFileHistoryFolded=\
  SELECT CS.REVISION, A.NAME, CS.TIME, CS.MESSAGE, CS.ID \
  FROM \
     OPENGROK.CHANGESETS CS, \
     OPENGROK.FILECHANGES FC, \
     OPENGROK.FILES F, \
     OPENGROK.DIRECTORIES D, \
     OPENGROK.AUTHORS A, \
     OPENGROK.REPOSITORIES R \
  WHERE R.PATH = ? \
      AND D.path = ? \
      AND F.name = ? \
      AND D.REPOSITORY = R.ID \
      AND CS.ID = FC.CHANGESET \
      AND FC.FILE = F.ID \
      AND D.ID = F.DIRECTORY \
      AND A.ID = CS.AUTHOR \
  ORDER BY CS.ID DESC

getFilemovesCount=\
  SELECT COUNT(*) FROM OPENGROK.FILEMOVES

getDirHistory=\
  SELECT CS.REVISION, A.NAME, CS.TIME, CS.MESSAGE, CS.ID \
  FROM \
      OPENGROK.CHANGESETS CS \
          JOIN OPENGROK.DIRCHANGES DC ON CS.ID = DC.CHANGESET \
          JOIN OPENGROK.DIRECTORIES D ON DC.DIRECTORY = D.ID \
          JOIN OPENGROK.AUTHORS A ON A.ID = CS.AUTHOR \
          JOIN OPENGROK.REPOSITORIES R ON D.REPOSITORY = R.ID \
  WHERE \
      R.PATH = ? AND D.PATH = ? \
  ORDER BY CS.ID DESC

getFilesInChangeset=\
  SELECT D.PATH || '/' || F.NAME \
    FROM OPENGROK.DIRECTORIES D, OPENGROK.FILES F, OPENGROK.FILECHANGES FC \
    WHERE D.ID = F.DIRECTORY AND F.ID = FC.FILE \
    AND FC.CHANGESET = ?

getRepository=SELECT ID FROM OPENGROK.REPOSITORIES WHERE PATH = ?

addRepository=INSERT INTO OPENGROK.REPOSITORIES(PATH) VALUES ?

addChangeset=\
  INSERT INTO OPENGROK.CHANGESETS \
  (REPOSITORY, REVISION, AUTHOR, TIME, MESSAGE, ID) \
  VALUES (?,?,?,?,?,?)

addDirchange=INSERT INTO OPENGROK.DIRCHANGES(CHANGESET, DIRECTORY) VALUES (?,?)

addFilechange=INSERT INTO OPENGROK.FILECHANGES(CHANGESET, FILE) \
    VALUES (?,?)

addFilemove=INSERT INTO OPENGROK.FILEMOVES(CHANGESET, FILE) \
    VALUES (?,?)

getAuthors=SELECT NAME, ID FROM OPENGROK.AUTHORS WHERE REPOSITORY = ?

addAuthor=INSERT INTO OPENGROK.AUTHORS (REPOSITORY, NAME, ID) VALUES (?,?,?)

getDirectories=SELECT PATH, ID FROM OPENGROK.DIRECTORIES WHERE REPOSITORY = ?

getFiles=\
  SELECT D.PATH || '/' || F.NAME, F.ID \
  FROM OPENGROK.FILES F, OPENGROK.DIRECTORIES D \
  WHERE D.REPOSITORY = ? AND F.DIRECTORY = D.ID

addDirectory=INSERT INTO OPENGROK.DIRECTORIES(REPOSITORY, PATH, ID, PARENT) \
  VALUES (?,?,?,?)

addFile=INSERT INTO OPENGROK.FILES(DIRECTORY, NAME, ID) VALUES (?,?,?)

getLatestCachedRevision=\
  SELECT REVISION FROM OPENGROK.CHANGESETS WHERE ID = \
    (SELECT MAX(CS.ID) FROM OPENGROK.CHANGESETS CS, OPENGROK.REPOSITORIES R \
            WHERE CS.REPOSITORY = R.ID AND R.PATH = ?)

getChangesetIdForRevision=\
  SELECT ID FROM OPENGROK.CHANGESETS WHERE REVISION = ? AND REPOSITORY = ?

getMaxFileId=SELECT MAX(ID) FROM OPENGROK.FILES

getMaxDirId=SELECT MAX(ID) FROM OPENGROK.DIRECTORIES

getMaxChangesetId=SELECT MAX(ID) FROM OPENGROK.CHANGESETS

getMaxAuthorId=SELECT MAX(ID) FROM OPENGROK.AUTHORS

# Cascading deletes will take care of deleting all data associated with the
# repository from the other tables.
clearRepository=DELETE FROM OPENGROK.REPOSITORIES WHERE ID=?

# Statements used when upgrading a database from 0.11 format to 0.12 format.
alterTableDirectoriesParent=ALTER TABLE OPENGROK.DIRECTORIES \
  ADD COLUMN PARENT INT CONSTRAINT DIRS_FK_DIRS \
        REFERENCES OPENGROK.DIRECTORIES ON DELETE CASCADE

alterTableDirectoriesParentPathConstraint=ALTER TABLE OPENGROK.DIRECTORIES \
  ADD CONSTRAINT DIRECTORIES_PARENT_PATH_UNIQUE UNIQUE (PARENT, PATH)

getAllDirectories=SELECT * FROM OPENGROK.DIRECTORIES

updateDirectoriesParent=UPDATE OPENGROK.DIRECTORIES SET PARENT = \
    CASE \
      WHEN PATH = '' THEN NULL \
      ELSE (SELECT ID FROM OPENGROK.DIRECTORIES WHERE REPOSITORY=? AND PATH=?) \
    END \
  WHERE ID=?

# Get the last modified times of all files/subdirectories in a directory.
getLastModifiedTimes= \
SELECT T.NAME, MAX(CS.TIME) FROM \
( \
  SELECT F.DIRECTORY, F.NAME, MAX(FC.CHANGESET) FROM OPENGROK.FILECHANGES FC, \
         OPENGROK.FILES F WHERE FC.FILE = F.ID GROUP BY F.DIRECTORY, F.NAME \
  UNION ALL \
  SELECT SD.PARENT, SUBSTR(SD.PATH, LENGTH(D.PATH)+2), MAX(DC.CHANGESET) \
         FROM OPENGROK.DIRCHANGES DC, OPENGROK.DIRECTORIES SD, \
              OPENGROK.DIRECTORIES D \
         WHERE DC.DIRECTORY = SD.ID AND SD.PARENT = D.ID \
         GROUP BY SD.PARENT, SUBSTR(SD.PATH, LENGTH(D.PATH)+2) \
) T(PARENT, NAME, ID), \
OPENGROK.CHANGESETS CS, OPENGROK.DIRECTORIES D, OPENGROK.REPOSITORIES R \
WHERE R.PATH=? AND D.PATH=? AND R.ID = D.REPOSITORY AND \
      CS.ID = T.ID AND T.PARENT=D.ID \
GROUP BY T.NAME
