コード例 #1
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
int dbi_result_get_int_idx(dbi_result Result, unsigned int fieldidx) {
  long ERROR = 0;
  fieldidx--;
	
  RESULT->conn->error_flag = DBI_ERROR_NONE;

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_INTEGER) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not integer type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
  case DBI_INTEGER_SIZE1:
  case DBI_INTEGER_SIZE2:
  case DBI_INTEGER_SIZE3:
  case DBI_INTEGER_SIZE4:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_long;
  case DBI_INTEGER_SIZE8:
    _verbose_handler(RESULT->conn, "%s: field `%s` is more than 4 bytes wide\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return ERROR;
  }
}
コード例 #2
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
float dbi_result_get_float_idx(dbi_result Result, unsigned int fieldidx) {
  float ERROR = 0.0;
  fieldidx--;
	
  RESULT->conn->error_flag = DBI_ERROR_NONE;
	
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_DECIMAL) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not float type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_DECIMAL_SIZEMASK) {
  case DBI_DECIMAL_SIZE4:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_float;
  case DBI_DECIMAL_SIZE8:
    _verbose_handler(RESULT->conn, "%s: field `%s` is double, not float\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return ERROR;
  }
}
コード例 #3
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
unsigned char *dbi_result_get_binary_copy_idx(dbi_result Result, unsigned int fieldidx) {
  char *ERROR = "ERROR";
  unsigned char *newblob = NULL;
  unsigned long long size;
  fieldidx--;

  RESULT->conn->error_flag = DBI_ERROR_NONE;
	
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return (unsigned char *)strdup(ERROR);
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_BINARY) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not binary type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return (unsigned char *)strdup(ERROR);
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0) return NULL;

  size = dbi_result_get_field_size_idx(Result, fieldidx);
  newblob = malloc(size);
  if (!newblob) {
    _error_handler(RESULT->conn, DBI_ERROR_NOMEM);
    return (unsigned char *)strdup(ERROR);
  }
  memcpy(newblob, RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string, size);
  return newblob;
}
コード例 #4
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
char *dbi_result_get_string_copy_idx(dbi_result Result, unsigned int fieldidx) {
  char *ERROR = "ERROR";
  char *newstring = NULL;
  fieldidx--;
	
  RESULT->conn->error_flag = DBI_ERROR_NONE;
	
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return strdup(ERROR);
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_STRING) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not string type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return strdup(ERROR);
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0
   && RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string == NULL) {
    // mysql returns 0 for the field size of an empty string, so size==0
    // doesn't necessarily mean NULL
    return NULL;
  }

  newstring = strdup(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string);

  if (newstring) {
    return newstring;
  }
  else {
    _error_handler(RESULT->conn, DBI_ERROR_NOMEM);
    return strdup(ERROR);
  }
}
コード例 #5
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
const char *dbi_result_get_string_idx(dbi_result Result, unsigned int fieldidx) {
  const char *ERROR = "ERROR";
  fieldidx--;
	
  RESULT->conn->error_flag = DBI_ERROR_NONE;
	
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_STRING) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not string type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return ERROR;
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0
      && _get_field_flag(RESULT->rows[RESULT->currowidx], fieldidx, DBI_VALUE_NULL)) {
    /* string does not exist */
    return NULL;
  }
  /* else if field size == 0: empty string */
	
  return (const char *)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string);
}
コード例 #6
0
ファイル: dbi.c プロジェクト: 4thAce/collectd
static int cdbi_result_get_field (dbi_result res, /* {{{ */
    unsigned int index, char *buffer, size_t buffer_size)
{
  unsigned short src_type;

  src_type = dbi_result_get_field_type_idx (res, index);
  if (src_type == DBI_TYPE_ERROR)
  {
    ERROR ("dbi plugin: cdbi_result_get: "
        "dbi_result_get_field_type_idx failed.");
    return (-1);
  }

  if (src_type == DBI_TYPE_INTEGER)
  {
    long long value;

    value = dbi_result_get_longlong_idx (res, index);
    ssnprintf (buffer, buffer_size, "%lli", value);
  }
  else if (src_type == DBI_TYPE_DECIMAL)
  {
    double value;

    value = dbi_result_get_double_idx (res, index);
    ssnprintf (buffer, buffer_size, "%63.15g", value);
  }
  else if (src_type == DBI_TYPE_STRING)
  {
    const char *value;

    value = dbi_result_get_string_idx (res, index);
    if (value == NULL)
      sstrncpy (buffer, "", buffer_size);
    else if (strcmp ("ERROR", value) == 0)
      return (-1);
    else
      sstrncpy (buffer, value, buffer_size);
  }
  /* DBI_TYPE_BINARY */
  /* DBI_TYPE_DATETIME */
  else
  {
    const char *field_name;

    field_name = dbi_result_get_field_name (res, index);
    if (field_name == NULL)
      field_name = "<unknown>";

    ERROR ("dbi plugin: Column `%s': Don't know how to handle "
        "source type %hu.",
        field_name, src_type);
    return (-1);
  }

  return (0);
} /* }}} int cdbi_result_get_field */
コード例 #7
0
ファイル: fcgi_in_c_example.c プロジェクト: savonix/dbrs2xml
static xmlNodePtr
query_doc(dbi_result *result, char *query_name)
{
    char *elt;
    unsigned long mylong;
    char buffer[100];
    unsigned int i;

    xmlDocPtr doc = NULL;
    xmlNodePtr top_query = NULL, query_row = NULL;
    doc = xmlNewDoc(BAD_CAST "1.0");
    top_query = xmlNewNode(NULL, BAD_CAST query_name);
    xmlDocSetRootElement(doc, top_query);

    while (dbi_result_next_row(result))
    {

        query_row = xmlNewChild(top_query, NULL, BAD_CAST query_name,NULL);
        for (i=1; i < dbi_result_get_numfields(result) + 1; i++)
        {
            elt = strdup(dbi_result_get_field_name(result,i));
            if (dbi_result_get_field_type_idx(result,i) == 3) {
                if (dbi_result_get_string_idx(result,i))
                {
                    xmlNewChild(query_row, NULL, BAD_CAST elt, BAD_CAST dbi_result_get_string_idx(result,i));
                }
            }
            if (dbi_result_get_field_type_idx(result,i) == 1) {
                if (dbi_result_get_ulonglong_idx(result,i))
                {
                    mylong = dbi_result_get_ulonglong_idx(result,i);
                    sprintf(buffer, "%ld", mylong);
                    xmlNewChild(query_row, NULL, BAD_CAST elt, BAD_CAST buffer);
                }
            }
        }

    }

    return xmlDocGetRootElement(doc);

    xmlFreeDoc(doc);
    xmlCleanupParser();

}
コード例 #8
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
time_t dbi_result_get_datetime_idx(dbi_result Result, unsigned int fieldidx) {
  time_t ERROR = 0;
  fieldidx--;

  RESULT->conn->error_flag = DBI_ERROR_NONE;
	
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_DATETIME) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not datetime type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return ERROR;
  }
	
  return (time_t)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_datetime);
}
コード例 #9
0
ファイル: dbi_result.c プロジェクト: ystk/debian-libdbi
const unsigned char *dbi_result_get_binary_idx(dbi_result Result, unsigned int fieldidx) {
  const char *ERROR = "ERROR";
  fieldidx--;
	
  RESULT->conn->error_flag = DBI_ERROR_NONE;
	
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return (const unsigned char*)ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_BINARY) {
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    _verbose_handler(RESULT->conn, "%s: field `%s` is not binary type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    return (const unsigned char*)ERROR;
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0) return NULL;

  return (const unsigned char *)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string);
}
コード例 #10
0
ファイル: dbi.c プロジェクト: 4thAce/collectd
static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
    udb_query_t *q, udb_query_preparation_area_t *prep_area)
{
  const char *statement;
  dbi_result res;
  size_t column_num;
  char **column_names;
  char **column_values;
  int status;
  size_t i;

  /* Macro that cleans up dynamically allocated memory and returns the
   * specified status. */
#define BAIL_OUT(status) \
  if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
  if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
  if (res != NULL) { dbi_result_free (res); res = NULL; } \
  return (status)

  column_names = NULL;
  column_values = NULL;

  statement = udb_query_get_statement (q);
  assert (statement != NULL);

  res = dbi_conn_query (db->connection, statement);
  if (res == NULL)
  {
    char errbuf[1024];
    ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
        "dbi_conn_query failed: %s",
        db->name, udb_query_get_name (q),
        cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
    BAIL_OUT (-1);
  }
  else /* Get the number of columns */
  {
    unsigned int db_status;

    db_status = dbi_result_get_numfields (res);
    if (db_status == DBI_FIELD_ERROR)
    {
      char errbuf[1024];
      ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
          "dbi_result_get_numfields failed: %s",
          db->name, udb_query_get_name (q),
          cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
      BAIL_OUT (-1);
    }

    column_num = (size_t) db_status;
    DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
        db->name, udb_query_get_name (q), column_num);
  }

  /* Allocate `column_names' and `column_values'. {{{ */
  column_names = calloc (column_num, sizeof (*column_names));
  if (column_names == NULL)
  {
    ERROR ("dbi plugin: calloc failed.");
    BAIL_OUT (-1);
  }

  column_names[0] = calloc (column_num, DATA_MAX_NAME_LEN);
  if (column_names[0] == NULL)
  {
    ERROR ("dbi plugin: calloc failed.");
    BAIL_OUT (-1);
  }
  for (i = 1; i < column_num; i++)
    column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;

  column_values = calloc (column_num, sizeof (*column_values));
  if (column_values == NULL)
  {
    ERROR ("dbi plugin: calloc failed.");
    BAIL_OUT (-1);
  }

  column_values[0] = calloc (column_num, DATA_MAX_NAME_LEN);
  if (column_values[0] == NULL)
  {
    ERROR ("dbi plugin: calloc failed.");
    BAIL_OUT (-1);
  }
  for (i = 1; i < column_num; i++)
    column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
  /* }}} */

  /* Copy the field names to `column_names' */
  for (i = 0; i < column_num; i++) /* {{{ */
  {
    const char *column_name;

    column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
    if (column_name == NULL)
    {
      ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
          "Cannot retrieve name of field %zu.",
          db->name, udb_query_get_name (q), i + 1);
      BAIL_OUT (-1);
    }

    sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
  } /* }}} for (i = 0; i < column_num; i++) */

  udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
      /* plugin = */ "dbi", db->name,
      column_names, column_num, /* interval = */ (db->interval > 0) ? db->interval : 0);

  /* 0 = error; 1 = success; */
  status = dbi_result_first_row (res); /* {{{ */
  if (status != 1)
  {
    char errbuf[1024];
    ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
        "dbi_result_first_row failed: %s. Maybe the statement didn't "
        "return any rows?",
        db->name, udb_query_get_name (q),
        cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
    udb_query_finish_result (q, prep_area);
    BAIL_OUT (-1);
  } /* }}} */

  /* Iterate over all rows and call `udb_query_handle_result' with each list of
   * values. */
  while (42) /* {{{ */
  {
    status = 0;
    /* Copy the value of the columns to `column_values' */
    for (i = 0; i < column_num; i++) /* {{{ */
    {
      status = cdbi_result_get_field (res, (unsigned int) (i + 1),
          column_values[i], DATA_MAX_NAME_LEN);

      if (status != 0)
      {
        ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
            "cdbi_result_get_field (%zu) failed.",
            db->name, udb_query_get_name (q), i + 1);
        status = -1;
        break;
      }
    } /* }}} for (i = 0; i < column_num; i++) */

    /* If all values were copied successfully, call `udb_query_handle_result'
     * to dispatch the row to the daemon. */
    if (status == 0) /* {{{ */
    {
      status = udb_query_handle_result (q, prep_area, column_values);
      if (status != 0)
      {
        ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
            "udb_query_handle_result failed.",
            db->name, udb_query_get_name (q));
      }
    } /* }}} */

    /* Get the next row from the database. */
    status = dbi_result_next_row (res); /* {{{ */
    if (status != 1)
    {
      if (dbi_conn_error (db->connection, NULL) != 0)
      {
        char errbuf[1024];
        WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
            "dbi_result_next_row failed: %s.",
            db->name, udb_query_get_name (q),
            cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
      }
      break;
    } /* }}} */
  } /* }}} while (42) */

  /* Tell the db query interface that we're done with this query. */
  udb_query_finish_result (q, prep_area);

  /* Clean up and return `status = 0' (success) */
  BAIL_OUT (0);
#undef BAIL_OUT
} /* }}} int cdbi_read_database_query */