/*
 * chipqueue.c
 *
 * Copyright (C) 2007  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 "pxtools.h"
#include "pxdata.h"
#include "chiptool.h"

bool chipQueueExpTag(pxConfig *config,
                     psString exp_tag,
                     psString workdir,
                     psString label,
                     psString recipe,
                     psString expgroup,
                     psString dvodb)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    // load the SQL to enqueue our exp_tags from disk once
    static psString queuerawexp_query = NULL;
    if (!queuerawexp_query) {
        queuerawexp_query = pxDataGet("chiptool_queuerawexp.sql");
        psMemSetPersistent(queuerawexp_query, true);
        if (!queuerawexp_query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            return false;
        }
    }

    static psString queuerawimfile_query = NULL;
    if (!queuerawimfile_query) {
        queuerawimfile_query = pxDataGet("chiptool_queuerawimfile.sql");
        psMemSetPersistent(queuerawimfile_query, true);
        if (!queuerawexp_query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            return false;
        }
    }

    // queue the exp
    if (!p_psDBRunQuery(config->dbh, queuerawexp_query,
                workdir  ? workdir  : "NULL",
                label    ? label    : "NULL",
                recipe   ? recipe   : "NULL",
                expgroup ? expgroup : "NULL",
                dvodb    ? dvodb    : "NULL",
                exp_tag
    )) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    // just to be safe, we should have changed at least one row
    if (psDBAffectedRows(config->dbh) < 1) {
        psError(PS_ERR_UNKNOWN, false,
                "no rows affected - should have changed at least one row");
        return false;
    }

    // queue the imfiles for the exp we just queued
    if (!p_psDBRunQuery(config->dbh, queuerawimfile_query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    // just to be safe, we should have changed at least one row
    if (psDBAffectedRows(config->dbh) < 1) {
        psError(PS_ERR_UNKNOWN, false,
                "no rows affected - should have changed at least one row");
        return false;
    }

    return true;
}
