/*
 * pxtables.c
 *
 * Copyright (C) 2006  Joshua Hoblitt
 *
 * 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; version 2 of the License.
 *
 * 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
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <pslib.h>
#include "pxtools.h"

#define CREATE_TABLE(createFunc) \
    if (!createFunc(config->dbh)) { \
        psError(PS_ERR_UNKNOWN, false, "dbh access failed"); \
        status = false; \
    }

#define DROP_TABLE(dropFunc) \
    if (!dropFunc(config->dbh)) { \
        psError(PS_ERR_UNKNOWN, false, "dbh access failed"); \
        status = false; \
    }

bool pxCreateTables(pxConfig *config) {
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool            status = true;
    CREATE_TABLE(expTagCounterCreateTable);
    // the counter must be initialized to 0
    if (!p_psDBRunQuery(config->dbh, "INSERT INTO expTagCounter VALUES (0)")) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        status = false; 
    }
    CREATE_TABLE(summitExpCreateTable);
    CREATE_TABLE(summitImfileCreateTable);
    CREATE_TABLE(pzPendingExpCreateTable);
    CREATE_TABLE(pzPendingImfileCreateTable);
    CREATE_TABLE(pzDoneExpCreateTable);
    CREATE_TABLE(pzDoneImfileCreateTable);
    CREATE_TABLE(newExpCreateTable);
    CREATE_TABLE(newImfileCreateTable);
    CREATE_TABLE(rawExpCreateTable);
    CREATE_TABLE(rawImfileCreateTable);
    CREATE_TABLE(guidePendingExpCreateTable);
    CREATE_TABLE(chipPendingExpCreateTable);
    CREATE_TABLE(chipPendingImfileCreateTable);
    CREATE_TABLE(chipProcessedExpCreateTable);
    CREATE_TABLE(chipProcessedImfileCreateTable);
    CREATE_TABLE(chipMaskCreateTable);
    CREATE_TABLE(camPendingExpCreateTable);
    CREATE_TABLE(camProcessedExpCreateTable);
    CREATE_TABLE(camMaskCreateTable);
    CREATE_TABLE(detRunCreateTable);
    CREATE_TABLE(detInputExpCreateTable);
    CREATE_TABLE(detProcessedImfileCreateTable);
    CREATE_TABLE(detProcessedExpCreateTable);
    CREATE_TABLE(detStackedImfileCreateTable);
    CREATE_TABLE(detNormalizedStatImfileCreateTable);
    CREATE_TABLE(detNormalizedImfileCreateTable);
    CREATE_TABLE(detNormalizedExpCreateTable);
    CREATE_TABLE(detResidImfileCreateTable);
    CREATE_TABLE(detResidExpCreateTable);
    CREATE_TABLE(detRunSummaryCreateTable);
    CREATE_TABLE(warpRunCreateTable);
    CREATE_TABLE(warpInputExpCreateTable);
    CREATE_TABLE(warpSkyCellMapCreateTable);
    CREATE_TABLE(warpSkyfileCreateTable);
    CREATE_TABLE(diffRunCreateTable);
    CREATE_TABLE(diffInputSkyfileCreateTable);
    CREATE_TABLE(diffSkyfileCreateTable);
    CREATE_TABLE(stackRunCreateTable);
    CREATE_TABLE(stackInputSkyfileCreateTable);
    CREATE_TABLE(stackSumSkyfileCreateTable);

    return true;
}

bool pxDeleteTables(pxConfig *config) {
    bool            status = true;
    char line[128], answer[128];

    PS_ASSERT_PTR_NON_NULL(config, false);

    fprintf (stdout, "*** delete the chip tables? ***\n");
    fprintf (stdout, "*** to delete the tables, answer YES, and give password ***\n");
    fprintf (stdout, "*** WARNING: this action is permanent ***\n\n");

    fprintf (stdout, "delete the chip tables (YES/[n]): ");
    fgets (line, 128, stdin);
    sscanf (line, "%s", answer);
    if (strcmp (answer, "YES"))  {
        psError(PS_ERR_UNKNOWN, true, "tables NOT deleleted");
        return false;
    }

    fprintf (stdout, "enter dbh connection password: ");
    fgets (line, 128, stdin);
    sscanf (line, "%s", answer);

    {
        bool status;
        psString dbPassword = psMetadataLookupStr(&status, config->modules->site, "DBPASSWORD");
        if (strcmp (answer, dbPassword)) {
            psError(PS_ERR_UNKNOWN, true, "tables NOT deleleted");
            return false;
        }
    }

    // disable foreign key constrants
    if (!p_psDBRunQuery(config->dbh, "SET FOREIGN_KEY_CHECKS=0")) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        status = false; 
    }

    DROP_TABLE(expTagCounterDropTable);
    DROP_TABLE(summitExpDropTable);
    DROP_TABLE(summitImfileDropTable);
    DROP_TABLE(pzPendingExpDropTable);
    DROP_TABLE(pzPendingImfileDropTable);
    DROP_TABLE(pzDoneExpDropTable);
    DROP_TABLE(pzDoneImfileDropTable);
    DROP_TABLE(newExpDropTable);
    DROP_TABLE(newImfileDropTable);
    DROP_TABLE(rawExpDropTable);
    DROP_TABLE(rawImfileDropTable);
    DROP_TABLE(guidePendingExpDropTable);
    DROP_TABLE(chipPendingExpDropTable);
    DROP_TABLE(chipPendingImfileDropTable);
    DROP_TABLE(chipProcessedExpDropTable);
    DROP_TABLE(chipProcessedImfileDropTable);
    DROP_TABLE(chipMaskDropTable);
    DROP_TABLE(camPendingExpDropTable);
    DROP_TABLE(camProcessedExpDropTable);
    DROP_TABLE(camMaskDropTable);
    DROP_TABLE(detRunDropTable);
    DROP_TABLE(detInputExpDropTable);
    DROP_TABLE(detProcessedImfileDropTable);
    DROP_TABLE(detProcessedExpDropTable);
    DROP_TABLE(detStackedImfileDropTable);
    DROP_TABLE(detNormalizedStatImfileDropTable);
    DROP_TABLE(detNormalizedImfileDropTable);
    DROP_TABLE(detNormalizedExpDropTable);
    DROP_TABLE(detResidImfileDropTable);
    DROP_TABLE(detResidExpDropTable);
    DROP_TABLE(detRunSummaryDropTable);
    DROP_TABLE(warpRunDropTable);
    DROP_TABLE(warpInputExpDropTable);
    DROP_TABLE(warpSkyCellMapDropTable);
    DROP_TABLE(warpSkyfileDropTable);
    DROP_TABLE(diffRunDropTable);
    DROP_TABLE(diffInputSkyfileDropTable);
    DROP_TABLE(diffSkyfileDropTable);
    DROP_TABLE(stackRunDropTable);
    DROP_TABLE(stackInputSkyfileDropTable);
    DROP_TABLE(stackSumSkyfileDropTable);

    // re-enable foreign key constrants
    if (!p_psDBRunQuery(config->dbh, "SET FOREIGN_KEY_CHECKS=1")) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        status = false; 
    }

    return status;
}
