Exemplo n.º 1
0
Arquivo: rcli.c Projeto: dseomn/rpstir
static int create2op(
    scm *scmp,
    scmcon *conp,
    char *topdir)
{
    scmkva aone;
    scmkv one;
    scmtab *mtab;
    int sta;

    if (conp == NULL || scmp == NULL || scmp->db == NULL || scmp->db[0] == 0)
    {
        LOG(LOG_ERR, "Internal error in create2op()");
        return (-1);
    }
    if (topdir == NULL || topdir[0] == 0)
    {
        LOG(LOG_ERR, "Must specify a top level repository directory");
        return (-2);
    }
    // step 1: locate the metadata table
    mtab = findtablescm(scmp, "METADATA");
    if (mtab == NULL)
    {
        LOG(LOG_ERR, "Cannot find METADATA table");
        return (-3);
    }
    // step 2: translate "topdir" into an absolute path
    tdir = r2adir(topdir);
    if (tdir == NULL)
    {
        LOG(LOG_ERR, "Invalid directory: %s", topdir);
        return (-4);
    }
    // step 3: init the metadata table
    one.column = "rootdir";
    one.value = tdir;
    aone.vec = &one;
    aone.ntot = 1;
    aone.nused = 1;
    aone.vald = 0;
    sta = insertscm(conp, mtab, &aone);
    if (sta == 0)
        LOG(LOG_NOTICE, "Init metadata table succeeded");
    else
        LOG(LOG_ERR, "Init metadata table failed: %s", geterrorscm(conp));
    return (sta);
}
Exemplo n.º 2
0
/*
 * sets up and performs the database query, and handles the results
 */
static err_code
doQuery(
    char **displays,
    char **filters,
    char *orderp)
{
    scmtab *table = NULL;
    scmsrcha srch;
    scmsrch srch1[MAX_VALS];
    char whereStr[MAX_CONDS * 20];
    char errMsg[1024];
    int srchFlags = SCM_SRCH_DOVALUE_ALWAYS;
    unsigned long blah = 0;
    int i;
    int j;
    err_code status;
    QueryField *field;
    QueryField *field2;
    char *name;
    int maxW = MAX_CONDS * 20;

    (void)setbuf(stdout, NULL);
    scmp = initscm();
    checkErr(scmp == NULL, "Cannot initialize database schema\n");
    connection = connectscm(scmp->dsn, errMsg, 1024);
    checkErr(connection == NULL, "Cannot connect to database: %s\n", errMsg);
    connection->mystat.tabname = objectType;
    table = findtablescm(scmp, tableName(objectType));
    checkErr(table == NULL, "Cannot find table %s\n", objectType);

    /*
     * set up where clause, i.e. the filter
     */
    srch.where = NULL;
    whereStr[0] = 0;

    if (filters == NULL || filters[0] == NULL)
    {
        srch.wherestr = NULL;
    }
    else
    {
        whereStr[0] = (char)0;
        for (i = 0; filters[i] != NULL; i++)
        {
            if (i != 0)
                strncat(whereStr, " AND ", maxW - strlen(whereStr));
            name = strtok(filters[i], ".");
            strncat(whereStr, name, maxW - strlen(whereStr));
            field = findField(name);
            checkErr(field == NULL || field->description == NULL,
                     "Unknown field name: %s\n", name);
            checkErr(field->flags & Q_JUST_DISPLAY,
                     "Field only for display: %s\n", name);
            name = strtok(NULL, ".");
            if (!name)
                checkErr(1, "No comparison operator\n");
            if (strcasecmp(name, "eq") == 0)
            {
                strncat(whereStr, "=", maxW - strlen(whereStr));
            }
            else if (strcasecmp(name, "ne") == 0)
            {
                strncat(whereStr, "<>", maxW - strlen(whereStr));
            }
            else if (strcasecmp(name, "lt") == 0)
            {
                strncat(whereStr, "<", maxW - strlen(whereStr));
            }
            else if (strcasecmp(name, "gt") == 0)
            {
                strncat(whereStr, ">", maxW - strlen(whereStr));
            }
            else if (strcasecmp(name, "le") == 0)
            {
                strncat(whereStr, "<=", maxW - strlen(whereStr));
            }
            else if (strcasecmp(name, "ge") == 0)
            {
                strncat(whereStr, ">=", maxW - strlen(whereStr));
            }
            else
            {
                checkErr(1, "Bad comparison operator: %s\n", name);
            }
            strncat(whereStr, "\"", maxW - strlen(whereStr));
            name = strtok(NULL, "");
            for (j = 0; j < (int)strlen(name); j++)
            {
                if (name[j] == '#')
                    name[j] = ' ';
            }
            char escaped [strlen(name)*2+1];
            mysql_escape_string(escaped, name, strlen(name));

            strncat(whereStr, escaped, maxW - strlen(whereStr));
            strncat(whereStr, "\"", maxW - strlen(whereStr));
        }
        srch.wherestr = whereStr;
    }

    if (validate)
    {
        addQueryFlagTests(whereStr, srch.wherestr != NULL);
        srch.wherestr = whereStr;
    }
    /*
     * set up columns to select
     */
    srch.vec = srch1;
    srch.sname = NULL;
    srch.ntot = MAX_VALS;
    srch.nused = 0;
    srch.vald = 0;
    srch.context = &blah;
    for (i = 0; displays[i] != NULL; i++)
    {
        field = findField(displays[i]);
        checkErr(field == NULL || field->description == NULL,
                 "Unknown field name: %s\n", displays[i]);
        globalFields[i] = field;
        name = (field->dbColumn == NULL) ? displays[i] : field->dbColumn;
        while (name != NULL)
        {
            field2 = findField(name);
            /** @bug ignores error code without explanation */
            addcolsrchscm(&srch, name, field2->sqlType, field2->maxSize);
            if (field->flags & Q_REQ_JOIN)
                srchFlags = srchFlags | SCM_SRCH_DO_JOIN;
            name =
                (name == field->otherDBColumn) ? NULL : field->otherDBColumn;
        }
    }
    globalFields[i] = NULL;
    if (validate)
    {
        valIndex = srch.nused;
        if (isROA || isManifest || isCRL || isGBR)
        {
            char *ski;
            if (isCRL)
            {
                ski = "aki";
            }
            else
            {
                ski = "ski";
            }
            field2 = findField(ski);
            /** @bug ignores error code without explanation */
            addcolsrchscm(&srch, ski, field2->sqlType, field2->maxSize);
        }
        else if (isCert)
            /** @bug ignores error code without explanation */
            addcolsrchscm(&srch, "local_id", SQL_C_ULONG, 8);
    }

    /*
     * do query
     */
    status = searchscm(connection, table, &srch, NULL, &handleResults,
                       srchFlags, orderp);
    for (i = 0; i < srch.nused; i++)
    {
        free(srch.vec[i].colname);
        free(srch1[i].valptr);
    }
    return status;
}