Example #1
0
time_t dbi_result_get_datetime(dbi_result Result, const char *fieldname) {
  time_t ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag;

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return ERROR;
  }
  return dbi_result_get_datetime_idx(Result, fieldidx+1);
}
Example #2
0
/* helpers to get correctly converted values from DB*/
static long rrd_fetch_dbi_long(dbi_result *result,int idx) {
    char *ptmp="";
    long value=DNAN;
    /* get the attributes for this filed */
    unsigned int attr=dbi_result_get_field_attribs_idx(result,idx);
    unsigned int type=dbi_result_get_field_type_idx(result,idx);
    /* return NAN if NULL */
    if(dbi_result_field_is_null_idx(result,idx)) {
        return DNAN;
    }
    /* do some conversions */
    switch (type) {
    case DBI_TYPE_STRING:
        ptmp=(char*)dbi_result_get_string_idx(result,idx);
        value=atoi(ptmp);
        break;
    case DBI_TYPE_INTEGER:
        if        (attr & DBI_INTEGER_SIZE1) {
            value=dbi_result_get_char_idx(result,idx);
        } else if (attr & DBI_INTEGER_SIZE2) {
            value=dbi_result_get_short_idx(result,idx);
        } else if (attr & DBI_INTEGER_SIZE3) {
            value=dbi_result_get_int_idx(result,idx);
        } else if (attr & DBI_INTEGER_SIZE4) {
            value=dbi_result_get_int_idx(result,idx);
        } else if (attr & DBI_INTEGER_SIZE8) {
            value=dbi_result_get_longlong_idx(result,idx);
        } else {
            value=DNAN;
            if (getenv("RRDDEBUGSQL")) {
                fprintf(stderr,"RRDDEBUGSQL: %li: column %i unsupported attribute flags %i for type INTEGER\n",time(NULL),idx,attr );
            }
        }
        break;
    case DBI_TYPE_DECIMAL:
        if        (attr & DBI_DECIMAL_SIZE4) {
            value=floor(dbi_result_get_float_idx(result,idx));
        } else if (attr & DBI_DECIMAL_SIZE8) {
            value=floor(dbi_result_get_double_idx(result,idx));
        } else {
            value=DNAN;
            if (getenv("RRDDEBUGSQL")) {
                fprintf(stderr,"RRDDEBUGSQL: %li: column %i unsupported attribute flags %i for type DECIMAL\n",time(NULL),idx,attr );
            }
        }
        break;
    case DBI_TYPE_BINARY:
        attr=dbi_result_get_field_length_idx(result,idx);
        ptmp=(char*)dbi_result_get_binary_copy_idx(result,idx);
        ptmp[attr-1]=0;
        /* check for "known" libdbi error */
        if (strncmp("ERROR",ptmp,5)==0) {
            if (!getenv("RRD_NO_LIBDBI_BUG_WARNING")) {
                fprintf(stderr,"rrdtool_fetch_libDBI: you have possibly triggered a bug in libDBI by using a (TINY,MEDIUM,LONG) TEXT field with mysql\n  this may trigger a core dump in at least one version of libdbi\n  if you are not touched by this bug and you find this message annoying\n  please set the environment-variable RRD_NO_LIBDBI_BUG_WARNING to ignore this message\n");
            }
        }
        /* convert to number */
        value=atoi(ptmp);
        /* free pointer */
        free(ptmp);
        break;
    case DBI_TYPE_DATETIME:
        value=dbi_result_get_datetime_idx(result,idx);
        break;
    default:
        if (getenv("RRDDEBUGSQL")) {
            fprintf(stderr,"RRDDEBUGSQL: %li: column %i unsupported type: %i with attribute %i\n",time(NULL),idx,type,attr );
        }
        value=DNAN;
        break;
    }
    return value;
}