/*
 * Copyright 2009, 2010 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program 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 General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package compid.data.db_derby;

/**
 * Container of SQL query strings. No internationalization!
 * @author Lari Natri
 */
public class SQL {

    public static final String GETALLDATASETS =
            "SELECT * FROM DATASETINFO";

    public static final String GETDISTINCTPEPSINDATASET =
        "SELECT DISTINCT PEPTIDE.PEPSEQUENCE " +
        "FROM PEPTIDE, PEPTIDEHIT, QUERY, DATASETINFO " +
        "WHERE DATASETINFO.DATASETINFO_ID = ? " +
          "AND QUERY.DATASETINFO_ID = DATASETINFO.DATASETINFO_ID " +
          "AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID " +
          "AND PEPTIDE.PEPTIDE_ID = PEPTIDEHIT.PEPTIDE_ID";

    public static final String GETDISTINCTPROTIDPEPSINDATASET =
        "SELECT DISTINCT PEPTIDE.PEPSEQUENCE "+
        "FROM PEPTIDE, PEPTIDEHIT, QUERY, DATASETINFO, PROTPEPTHITSJOIN " +
        "WHERE DATASETINFO.DATASETINFO_ID = ? " +
          "AND QUERY.DATASETINFO_ID = DATASETINFO.DATASETINFO_ID " +
          "AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID " +
          "AND PEPTIDE.PEPTIDE_ID = PEPTIDEHIT.PEPTIDE_ID " +
          "AND PROTPEPTHITSJOIN.PEPTIDEHIT_ID = PEPTIDEHIT.PEPTIDEHIT_ID";

    public static final String GETPEPSINDATASET =
            "SELECT PEPTIDEHIT.PEPTIDEHIT_ID," +
            "       PEPTIDEHIT.PG_CONF,"+
            "       PEPTIDEHIT.MS_IONSSCORE,"+
            "       QUERY.QUERY_ID,"+
            "       QUERY.DATASETINFO_ID,"+
            "       QUERY.SUBMITTEDMASS,"+
            "       QUERY.CHARGE,"+
            "       QUERY.SCANTITLE,"+
            "       PEPTIDE.PEPTIDE_ID,"+
            "       PEPTIDE.PEPSEQUENCE"+
            "  FROM PEPTIDEHIT,"+
            "       QUERY,"+
            "       PEPTIDE"+
            " WHERE QUERY.DATASETINFO_ID = ?"+
            "   AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID"+
            "   AND PEPTIDE.PEPTIDE_ID = PEPTIDEHIT.PEPTIDE_ID";

    public static final String GETPEPSINPROTSINDATASET =
            "SELECT PEPTIDEHIT.PEPTIDEHIT_ID," +
            "       PEPTIDEHIT.PG_CONF,"+
            "       PEPTIDEHIT.MS_IONSSCORE,"+
            "       QUERY.QUERY_ID,"+
            "       QUERY.DATASETINFO_ID,"+
            "       QUERY.SUBMITTEDMASS,"+
            "       QUERY.CHARGE,"+
            "       QUERY.SCANTITLE,"+
            "       PEPTIDE.PEPTIDE_ID,"+
            "       PEPTIDE.PEPSEQUENCE"+
            "  FROM PROTPEPTHITSJOIN,"+ // for prot
            "       PEPTIDEHIT,"+
            "       QUERY,"+
            "       PEPTIDE"+
            " WHERE QUERY.DATASETINFO_ID = ?"+
            "   AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID"+
            "   AND PEPTIDE.PEPTIDE_ID = PEPTIDEHIT.PEPTIDE_ID"+
            "   AND PEPTIDEHIT.PEPTIDEHIT_ID = PROTPEPTHITSJOIN.PEPTIDEHIT_ID"; // for prot

    public static final String GETQUERYCOUNTSPERPROTINDATASET =
            "SELECT PROTPEPTHITSJOIN.PROTEINHIT_ID, COUNT(PROTPEPTHITSJOIN.PEPTIDEHIT_ID) AS QUERIES"+
            " FROM PROTPEPTHITSJOIN, PEPTIDEHIT, QUERY" +
            " WHERE QUERY.DATASETINFO_ID = ?" +
            " AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID"+
            " AND PEPTIDEHIT.PEPTIDEHIT_ID = PROTPEPTHITSJOIN.PEPTIDEHIT_ID"+
            " GROUP BY PROTPEPTHITSJOIN.PROTEINHIT_ID";

    public static final String GETDISTINCTPEPCOUNTSPERPROTINDATASET =
            "SELECT PROTPEPTHITSJOIN.PROTEINHIT_ID, COUNT(DISTINCT PEPTIDE.PEPTIDE_ID) AS PEPTIDES"+
            " FROM PROTPEPTHITSJOIN, PEPTIDEHIT, QUERY, PEPTIDE" +
            " WHERE QUERY.DATASETINFO_ID = ?" +
            " AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID"+
            " AND PEPTIDEHIT.PEPTIDE_ID = PEPTIDE.PEPTIDE_ID"+
            " AND PEPTIDEHIT.PEPTIDEHIT_ID = PROTPEPTHITSJOIN.PEPTIDEHIT_ID"+
            " GROUP BY PROTPEPTHITSJOIN.PROTEINHIT_ID";

    public static final String GETPROTSBYSEQID =
            "SELECT DISTINCT *" +
            " FROM PROTEIN " +
            " WHERE PROTEIN.PROTSEQ_ID = ?"; // NOI18N

    public static final String GETPROTSWITHNOSEQBYDATASETID =
            "SELECT DISTINCT PROTEIN.PROTEIN_ID " +
            "FROM PROTEINHIT, PROTEIN " +
            "WHERE PROTEINHIT.DATASETINFO_ID = ? " +
              "AND PROTEIN.PROTEIN_ID = PROTEINHIT.PROTEIN_ID " +
              "AND PROTEIN.PROTSEQ_ID = 1"; // NOI18N

    public static final String GETPROTSWITHSAMESEQUENCE =
            "SELECT DISTINCT *" +
            " FROM PROTEIN " +
            " WHERE PROTSEQ_ID IN" +
            "   (SELECT PROTSEQ_ID" +
            "     FROM PROTEIN " +
            "     WHERE PROTEIN_ID = ?"; // NOI18N

    public static final String GETPROTEINSINDATASET =
            "SELECT DISTINCT PROTEIN.PROTEIN_ID " +
            "FROM PEPTIDEHIT, QUERY, " +
                 "DATASETINFO, PROTPEPTHITSJOIN, PROTEINHIT, " +
                 "PROTEIN " +
            "WHERE DATASETINFO.DATASETINFO_ID = ? " +
              "AND QUERY.DATASETINFO_ID = DATASETINFO.DATASETINFO_ID " +
              "AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID " +
              "AND PROTPEPTHITSJOIN.PEPTIDEHIT_ID = PEPTIDEHIT.PEPTIDEHIT_ID " +
              "AND PROTPEPTHITSJOIN.PROTEINHIT_ID = PROTEINHIT.PROTEINHIT_ID " +
              "AND PROTEIN.PROTEIN_ID = PROTEINHIT.PROTEIN_ID"; // NOI18N

    public static final String GETPROTEINBYPROTHITID =
        "SELECT PROTEIN.PROTEIN_ID, PROTEIN.ACCESSION, PROTEIN.DESCRIPTION, PROTEIN.SPECIES, " +
                "PROTEINHIT.PROTEINHIT_ID, PROTEINHIT.DATASETINFO_ID, PROTEINHIT.GROUPID, PROTEINHIT.GROUPMEMBERSTATUS, " +
                "PROTSEQ.PROTSEQ_ID, PROTSEQ.SEQUENCE, PROTSEQ.STATUS, " +
                "PROTDBINFO.PROTDBINFO_ID, PROTDBINFO.ACCPREFIX, PROTDBINFO.DBNAME, PROTDBINFO.DBDESCR, " +
                "PROTEINHIT.MS_PROTSCORE, PROTEINHIT.PG_UNUSED, PROTEINHIT.PG_TOTAL "+
        " FROM PROTEIN, PROTEINHIT, PROTDBINFO, PROTSEQ" +
        " WHERE PROTEINHIT.PROTEIN_ID = PROTEIN.PROTEIN_ID " +
        " AND PROTSEQ.PROTSEQ_ID = PROTEIN.PROTSEQ_ID" +
        " AND PROTDBINFO.PROTDBINFO_ID = PROTEIN.PROTDBINFO_ID" +
        " AND PROTEINHIT.PROTEINHIT_ID = ?";

    public static final String GETPEPSBYPROTHIT =
            "SELECT DISTINCT PEPTIDE.PEPSEQUENCE, QUERY.SCANTITLE, PEPTIDEHIT.PG_CONF, PEPTIDEHIT.MS_IONSSCORE" +
            " FROM PEPTIDE, PEPTIDEHIT, PROTPEPTHITSJOIN, QUERY" +
            " WHERE PROTPEPTHITSJOIN.PROTEINHIT_ID = ? " +
            " AND PEPTIDEHIT.PEPTIDEHIT_ID = PROTPEPTHITSJOIN.PEPTIDEHIT_ID" +
            " AND PEPTIDE.PEPTIDE_ID = PEPTIDEHIT.PEPTIDE_ID" +
            " AND PEPTIDEHIT.QUERY_ID = QUERY.QUERY_ID";

    public static final String GETALLPROTHITIDSINDATASET =
            "SELECT DISTINCT PROTEINHIT_ID " +
            "FROM PROTEINHIT " +
            "WHERE DATASETINFO_ID = ?";

    public static final String GETWINNERPROTHITIDSINDATASET =
            "SELECT DISTINCT PROTEINHIT_ID " +
            "FROM PROTEINHIT " +
            "WHERE DATASETINFO_ID = ? " +
            "AND GROUPMEMBERSTATUS = 1"; // 1 = winner

    public static final String GETDISTQUERIESWHICHFOUNDPEPSINDATASET =
            "SELECT DISTINCT QUERY.QUERY_ID" +
            "  FROM PEPTIDEHIT, QUERY, DATASETINFO" +
            " WHERE DATASETINFO.DATASETINFO_ID = ?" +
            "   AND QUERY.DATASETINFO_ID = DATASETINFO.DATASETINFO_ID" +
            "   AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID";

    public static final String GETDISTQUERIESWHICHFOUNDPEPSINPROTSINDATASET =
            "SELECT DISTINCT QUERY.QUERY_ID" +
            "  FROM PEPTIDEHIT, QUERY, DATASETINFO, PROTPEPTHITSJOIN" +
            " WHERE DATASETINFO.DATASETINFO_ID = ?" +
            "   AND QUERY.DATASETINFO_ID = DATASETINFO.DATASETINFO_ID" +
            "   AND QUERY.QUERY_ID = PEPTIDEHIT.QUERY_ID" +
            "   AND PROTPEPTHITSJOIN.PEPTIDEHIT_ID = PEPTIDEHIT.PEPTIDEHIT_ID";

    // this will cascade; only PEPTIDE, PROTDB, PROTSEQ and PROTEIN are unaffected (as they most likely should)
    public static final String DELETEDATASETS =
            "DELETE FROM DATASETINFO " +
            "WHERE DATASETINFO_ID = ?";

    public static final String DELETEORPHANEDPEPTIDES =
            "DELETE FROM PEPTIDE WHERE NOT EXISTS " +
            "(SELECT PEPTIDEHIT.PEPTIDE_ID " +
            " FROM PEPTIDEHIT, PEPTIDE " +
            " WHERE PEPTIDEHIT.PEPTIDE_ID = PEPTIDE.PEPTIDE_ID)";

    public static final String UPDATEPROTSEQIDBYPROTID =
            "UPDATE PROTEIN" +
            " SET PROTSEQ_ID = ?"+
            " WHERE PROTEIN_ID = ?"; // NOI18N

    public static final String UPDATEPROTSEQIDBYACCESSION =
            "UPDATE PROTEIN" +
            " SET PROTSEQ_ID = ?" +
            " WHERE PROTSEQ_ID <> ? AND ACCESSION = ?";
}
