Beispiel #1
0
static void listmgr_fieldfilter( lmgr_report_t *p_report, lmgr_t * p_mgr,
                                    const report_field_descr_t *report_desc_array,
                                    char *attrstring, char *attrname,
                                    char *having, char **curr_having,
                                    char *where, char **curr_where , int i )
{
    /* is this field filtered ? */
    if ( report_desc_array[i].filter )
    {
        /* TODO support list filters (IN NOT and IN) */
        printdbtype( p_mgr, attrstring, p_report->result_type_array[i],
                     &report_desc_array[i].filter_value.value );

        if ( report_desc_array[i].report_type != REPORT_GROUP_BY )
        {
            /* sum, min, max, etc. are addressed by attr#n */
            if ( having != *curr_having )
                *curr_having += sprintf( *curr_having, " AND " );
            *curr_having +=
                sprintf( *curr_having, "(%s %s %s)", attrname,
                         compar2str( report_desc_array[i].filter_compar ), attrstring );
        }
        else
        {
            /* this is a real db field, can be filtered in a 'where' clause */
            if ( where != *curr_where )
                *curr_where += sprintf( *curr_where, " AND " );
            *curr_where +=
                sprintf( *curr_where, "(%s %s %s)",
                         field_str( report_desc_array[i].attr_index ),
                         compar2str( report_desc_array[i].filter_compar ), attrstring );
        }
    }
}
Beispiel #2
0
static inline void append_filter_cond(GString *str, lmgr_t *p_mgr,
                                      const char *attrname,
                                      const report_field_descr_t *desc,
                                      const struct result *res)
{
    if (!GSTRING_EMPTY(str))
        g_string_append(str, " AND ");

    g_string_append_printf(str, "(%s %s ", attrname,
                           compar2str(desc->filter_compar));

    /** TODO support list filters (IN NOT and IN) */
    printdbtype(&p_mgr->conn, str, res->type, &desc->filter_value.value);

    g_string_append(str, ")");
}
Beispiel #3
0
/**
 * @param table T_MAIN, T_ANNEX
 * @return nbr of fields
 */
int attrset2updatelist( lmgr_t * p_mgr, char *str, const attr_set_t * p_set,
                        table_enum table, int leading_coma )
{
    int            i;
    char          *values_curr = str;
    unsigned int   nbfields = 0;
    db_type_u      typeu;
    int            mask = 1;

    if ( ( table == T_STRIPE_INFO ) || ( table == T_STRIPE_ITEMS ) )
        return -DB_NOT_SUPPORTED;

    for ( i = 0; i < ATTR_COUNT; i++, mask <<= 1 )
    {
        if ( ( p_set->attr_mask & mask ) && MATCH_TABLE( table, i ) )
        {
            if ( is_read_only_field( i ) )
            {
                DisplayLog( LVL_CRIT, LISTMGR_TAG, "Error in %s:%s: attribute '%s' is read-only",
                            __FILE__, __FUNCTION__, field_infos[i].field_name );
                return -DB_READ_ONLY_ATTR;
            }

            if ( leading_coma || ( nbfields > 0 ) )
            {
                *values_curr = ',';
                values_curr++;
            }

            values_curr += sprintf( values_curr, "%s=", field_infos[i].field_name );

            ASSIGN_UNION( typeu, field_infos[i].db_type,
                          ( ( char * ) &p_set->attr_values + field_infos[i].offset ) );

            values_curr += printdbtype( p_mgr, values_curr,
                                field_infos[i].db_type, &typeu );

            nbfields++;
        }
    }
    return nbfields;
}
Beispiel #4
0
/**
 * return FILTERDIR_NONE if there is no filter on dirattrs
 * return FILTERDIR_EMPTY if the test is 'dircount == 0'
 * return FILTERDIR_NONEMPTY if the test is on dircount != 0, >= 0, ...
 */
filter_dir_e dir_filter(lmgr_t * p_mgr, const lmgr_filter_t * p_filter, char* filter_str,
                        unsigned int * dir_attr_index)
{
    int i;
    filter_str[0] = '\0';

#ifdef ATTR_INDEX_dircount
    if ( p_filter->filter_type == FILTER_SIMPLE )
    {
        for ( i = 0; i < p_filter->filter_simple.filter_count; i++ )
        {
            unsigned int index = p_filter->filter_simple.filter_index[i];
            if (!is_dirattr(index))
                continue;

            /* condition about empty directory ? */
            if ((index == ATTR_INDEX_dircount)
                  && (p_filter->filter_simple.filter_value[i].val_uint == 0)
                  && (p_filter->filter_simple.filter_compar[i] == EQUAL))
            {
                DisplayLog( LVL_FULL, LISTMGR_TAG, "Special filter on empty directory" );
                strcpy( filter_str, "id NOT IN (SELECT distinct(parent_id) from ENTRIES)" );
                *dir_attr_index = index;
                return FILTERDIR_EMPTY;
            }
            else
            {
                char val[1024];
                db_type_u typeu = p_filter->filter_simple.filter_value[i];
                printdbtype( p_mgr, val, field_infos[index].db_type, &typeu );

                sprintf(filter_str, "dirattr%s%s", compar2str( p_filter->filter_simple.filter_compar[i] ),
                        val);
                *dir_attr_index = index;
                return FILTERDIR_OTHER;
            }
        }
    }
#endif
    return FILTERDIR_NONE;
}
Beispiel #5
0
/**
 * @param table T_MAIN, T_ANNEX
 * @return nbr of fields
 */
int attrset2valuelist( lmgr_t * p_mgr, char *str, const attr_set_t * p_set,
                       table_enum table, int leading_coma )
{
    int            i;
    char          *values_curr = str;
    unsigned int   nbfields = 0;
    db_type_u      typeu;
    int            mask = 1;

    if ( ( table == T_STRIPE_INFO ) || ( table == T_STRIPE_ITEMS ) )
        return -DB_NOT_SUPPORTED;

    for ( i = 0; i < ATTR_COUNT; i++, mask <<= 1 )
    {
        if ( p_set->attr_mask & mask )
        {
            if ( MATCH_TABLE( table, i ) )
            {
                if ( leading_coma || ( nbfields > 0 ) )
                {
                    *values_curr = ',';
                    values_curr++;
                }

                ASSIGN_UNION( typeu, field_infos[i].db_type,
                              ( ( char * ) &p_set->attr_values + field_infos[i].offset ) );

                values_curr += printdbtype( p_mgr, values_curr,
                                            field_infos[i].db_type, &typeu );

                nbfields++;
            }
        }
    }
    /* null terminating char */
    values_curr[0] = '\0';
    return nbfields;
}
Beispiel #6
0
int filter2str( lmgr_t * p_mgr, char *str, const lmgr_filter_t * p_filter,
                table_enum table, int leading_and, int prefix_table )
{
    int            i;
    unsigned int   nbfields = 0;
    db_type_u      typeu;
    char          *values_curr = str;
    char           fname[128];

    if ( p_filter->filter_type == FILTER_SIMPLE )
    {

        for ( i = 0; i < p_filter->filter_simple.filter_count; i++ )
        {
            unsigned int   index = p_filter->filter_simple.filter_index[i];
            int match =  MATCH_TABLE( table, index )
                         || (( table == T_STRIPE_ITEMS )
                              && ( field_infos[index].db_type == DB_STRIPE_ITEMS ))
                         || (( table == T_STRIPE_INFO )
                              && ( field_infos[index].db_type == DB_STRIPE_INFO ) );

            fname[0] = '\0';

            /* filter on generated fields are not allowed */
            if ( field_infos[index].flags & DIR_ATTR )
            {
                DisplayLog( LVL_FULL, LISTMGR_TAG, "Special filter on dir attribute '%s'", field_infos[index].field_name );
                continue;
            }
            else if ( field_infos[index].flags & GENERATED )
            {
                DisplayLog( LVL_CRIT, LISTMGR_TAG, "Cannot use filter on generated field '%s'", field_infos[index].field_name );
                return -DB_INVALID_ARG;
            }

            if ( match )
            {
                /* add prefixes or parenthesis, etc. */
                if ( leading_and || ( nbfields > 0 ) )
                {
                    if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_OR )
                        values_curr += sprintf( values_curr, " OR " );
                    else
                        values_curr += sprintf( values_curr, " AND " );
                }

                if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_BEGIN )
                     values_curr += sprintf( values_curr, "( " );

                if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_NOT )
                {
                    if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_ALLOW_NULL )
                        /* (NOT (x <cmp> <val>) OR x IS NULL) */
                        values_curr += sprintf( values_curr, " (NOT (" );
                    else
                        /* NOT (x <cmp> <val>) */
                         values_curr += sprintf( values_curr, " NOT (" );
                }

                if ( (p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_ALLOW_NULL )
                     && !(p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_NOT ) )
                {
                    /* (x <cmp> <val> OR x IS NULL) */
                    values_curr[0] = '(';
                    values_curr++;
                }
            }

            if ( MATCH_TABLE( table, index ) )
            {
                if ( prefix_table )
                {
                    if ( table == T_MAIN )
                        sprintf(fname, "%s.", MAIN_TABLE );
                    else if ( table == T_ANNEX )
                        sprintf(fname, "%s.",  ANNEX_TABLE );
                }

                strcat( fname, field_infos[index].field_name );

                values_curr +=
                    sprintf( values_curr, "%s%s", fname,
                             compar2str( p_filter->filter_simple.filter_compar[i] ) );

                typeu = p_filter->filter_simple.filter_value[i];

                values_curr += printdbtype( p_mgr, values_curr,
                                            field_infos[index].db_type, &typeu );

                nbfields++;
            }
            else if ( ( table == T_STRIPE_ITEMS )
                      && ( field_infos[index].db_type == DB_STRIPE_ITEMS ) )
            {
                if ( prefix_table )
                    sprintf(fname, "%s.", STRIPE_ITEMS_TABLE );

                strcat( fname, "storage_item" );

                values_curr +=
                    sprintf( values_curr, "%s%s%u", fname,
                             compar2str( p_filter->filter_simple.filter_compar[i] ),
                             p_filter->filter_simple.filter_value[i].val_uint );

                nbfields++;
            }
            else if ( ( table == T_STRIPE_INFO )
                      && ( field_infos[index].db_type == DB_STRIPE_INFO ) )
            {

                /* We XXX Assume that the only possible filter here is on pool_name */

                if ( prefix_table )
                    sprintf(fname, "%s.", STRIPE_INFO_TABLE );

                strcat( fname, "pool_name" );

                values_curr +=
                    sprintf( values_curr, "%s%s'%s'", fname,
                             compar2str( p_filter->filter_simple.filter_compar[i] ),
                             p_filter->filter_simple.filter_value[i].val_str );

                nbfields++;
            }

            if ( match )
            {
                if ( (p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_ALLOW_NULL )
                     && !(p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_NOT ) )
                {
                    values_curr += sprintf( values_curr, " OR %s IS NULL)", fname );
                }

                if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_NOT )
                {
                    if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_ALLOW_NULL )
                        /* (NOT (x <cmp> <val>) OR x IS NULL) */
                        values_curr += sprintf( values_curr, ") OR %s IS NULL)", fname );
                    else
                        /* NOT (x <cmp> <val>) */
                        values_curr += sprintf( values_curr, ") " );
                }

                if ( p_filter->filter_simple.filter_flags[i] & FILTER_FLAG_END )
                     values_curr += sprintf( values_curr, ") " );
            }

        } /* end for */
    }
    else
    {
        return -DB_NOT_SUPPORTED;
    }

    return nbfields;
}                               /* filter2str */