# Type db_val_t

This structure represents a value in the database. Several data-types are recognized and converted by the database API:

- DB_INT - Value in the database represents an integer number.

- DB_DOUBLE - Value in the database represents a decimal number.

- DB_STRING - Value in the database represents a string.

- DB_STR - Value in the database represents a string.

- DB_DATETIME - Value in the database represents date and time.

- DB_BLOB - Value in the database represents binary large object.

These data-types are automatically recognized, converted from internal database representation and stored in a variable of corresponding type.

    typedef struct db_val {
        db_type_t type;              /* Type of the value */
        int nul;                     /* NULL flag */
        union {
            int int_val;             /* Integer value */
            double double_val;       /* Double value */
            time_t time_val;         /* Unix time_t value */
            const char* string_val;  /* Zero terminated string */
            str str_val;             /* str structure */
            str blob_val;            /* Structure describing blob */
        } val;
    } db_val_t;

All macros expect pinter to `db_val_t` variable as a parameter.

- `VAL_TYPE(value)` Macro.

  Use this macro if you need to set/get the type of the value

  ## VAL_TYPE Macro

      ...
      VAL_TYPE(val) = DB_INT;
      if (VAL_TYPE(val) == DB_FLOAT)
      ...

- `VAL_NULL(value)` Macro.

  Use this macro if you need to set/get the null flag. Non-zero flag means that the corresponding cell in the database contained no data (NULL value in MySQL terminology).

  ## VAL_NULL Macro

      ...
      if (VAL_NULL(val) == 1) {
          printf("The cell is NULL");
      }
      ...

- `VAL_INT(value)` Macro.

  Use this macro if you need to access `integer` value in `db_val_t` structure.

  ## VAL_INT Macro

      ...
      if (VAL_TYPE(val) == DB_INT) {
          printf("%d", VAL_INT(val));
      }
      ...

- `VAL_DOUBLE(value)` Macro.

  Use this macro if you need to access `double` value in the `db_val_t` structure.

  ## VAL_DOUBLE Macro

      ...
      if (VAL_TYPE(val) == DB_DOUBLE) {
          printf("%f", VAL_DOUBLE(val));
      }
      ...

- `VAL_TIME(value)` Macro.

  Use this macro if you need to access `time_t` value in `db_val_t` structure.

  ## VAL_TIME Macro

      ...
      time_t tim;
      if (VAL_TYPE(val) == DB_DATETIME) {
          tim = VAL_TIME(val);
      }
      ...

- `VAL_STRING(value)` Macro.

  Use this macro if you need to access `string` value in `db_val_t` structure.

  ## VAL_STRING Macro

      ...
      if (VAL_TYPE(val) == DB_STRING) {
          printf("%s", VAL_STRING(val));
      }
      ...

- `VAL_STR(value)` Macro.

  Use this macro if you need to access `str` structure in `db_val_t` structure.

  ## VAL_STR Macro

      ...
      if (VAL_TYPE(val) == DB_STR) {
          printf("%.*s", VAL_STR(val).len, VAL_STR(val).s);
      }
      ...

- `VAL_BLOB(value)` Macro.

  Use this macro if you need to access `blob` value in `db_val_t` structure.

  ## VAL_STR Macro

      ...
      if (VAL_TYPE(val) == DB_BLOB) {
          printf("%.*s", VAL_BLOB(val).len, VAL_BLOB(val).s);
      }
      ...