tmp

You are viewing an old version of this article. View the current version here.

This page describes client-side API for reading and writing Dynamic Columns blobs.

Normally, you should use Dynamic column functions which are run inside the MariaDB server and allow one to access Dynamic Columns content without any client-side libraries.

If you need to read/write dynamic column blobs on the client for some reason, this API enables that.

Where to get it

The API is a part of libmysql C client library. In order to use it, one needs to include this header file

#include <mysql/ma_dyncol.h>

and link against libmysql.

Data structures

DYNAMIC_COLUMN

DYNAMIC_COLUMN represents a packed dynamic column blob. It is essentially a string-with-length and is defined as follows:

/* A generic-purpose arbitrary-length string defined in MySQL Client API */
typedef struct st_dynamic_string
{
  char *str;
  size_t length,max_length,alloc_increment;
} DYNAMIC_STRING;

...

typedef DYNAMIC_STRING DYNAMIC_COLUMN;

DYNAMIC_COLUMN_VALUE

Dynamic columns blob stores {name, value} pairs. DYNAMIC_COLUMN_VALUE structure is used to represent the value in accessible form.

struct st_dynamic_column_value
{
  DYNAMIC_COLUMN_TYPE type;
  union
  {
    long long long_value;
    unsigned long long ulong_value;
    double double_value;
    struct {
      MYSQL_LEX_STRING value;
      CHARSET_INFO *charset;
    } string;
    struct {
      decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
      decimal_t value;
    } decimal;
    MYSQL_TIME time_value;
  } x;
};
typedef struct st_dynamic_column_value DYNAMIC_COLUMN_VALUE;

Every value has a type, which is determined by the type member.

typestructure field
DYN_COL_NULL-
DYN_COL_INTvalue.x.long_value
DYN_COL_UINTvalue.x.ulong_value
DYN_COL_DOUBLEvalue.x.double_value
DYN_COL_STRINGvalue.x.string.value, value.x.string.charset
DYN_COL_DECIMALvalue.x.decimal.value
DYN_COL_DATETIMEvalue.x.time_value
DYN_COL_DATEvalue.x.time_value
DYN_COL_TIMEvalue.x.time_value
DYN_COL_DYNCOLvalue.x.string.value

Notes

  • Values with type DYN_COL_NULL do not ever occur in dynamic columns blobs.
  • Type DYN_COL_DYNCOL means that the value is a packed dynamic blob. This is how nested dynamic columns are done.
  • Before storing a value to value.x.decimal.value, one must call mariadb_dyncol_prepare_decimal() to initialize the space for storage.

enum_dyncol_func_result

enum enum_dyncol_func_result is used as return value.

valuenamemeaning
0ER_DYNCOL_OKOK
0ER_DYNCOL_NO(the same as ER_DYNCOL_OK but for functions which return a YES/NO)
1ER_DYNCOL_YESYES response or success
2ER_DYNCOL_TRUNCATEDOperation succeeded but the data was truncated
-1ER_DYNCOL_FORMATWrong format of the encoded string
-2ER_DYNCOL_LIMITA limit of implementation reached
-3ER_DYNCOL_RESOURCEOut of resources
-4ER_DYNCOL_DATAIncorrect input data
-5ER_DYNCOL_UNKNOWN_CHARSETUnknown character set

Result codes that are less than zero represent error conditions.

Function reference

Functions come in pairs:

  • xxx() operates on the old (pre-MariaDB-10.0.1) data format where columns were identified by numbers.
  • xxx_named() will operate on either old or new data format. If it modifies the blob, it will convert it to the new data format.

You should use xxx_named() functions, unless you need to keep the data compatible with MariaDB versions before 10.0.1.

mariadb_dyncol_create_many

enum enum_dyncol_func_result
mariadb_dyncol_create_many(DYNAMIC_COLUMN *str,
                           uint column_count,
                           uint *column_numbers,
                           DYNAMIC_COLUMN_VALUE *values,
                           my_bool new_string);
enum enum_dyncol_func_result
mariadb_dyncol_create_many_named(DYNAMIC_COLUMN *str,
                                 uint column_count,
                                 MYSQL_LEX_STRING *column_keys,
                                 DYNAMIC_COLUMN_VALUE *values,
                                 my_bool new_string);

where

strOUTCreated packed dynamic columns string will be put here
column_countINNumber of columns in following arrays
column_numbersINColumn numbers array (old format)
column_keysINColumn names array (new format)
valuesINValues of the columns array
new_stringINIf TRUE then the str will be reinitialized (not freed) before usage

mariadb_dyncol_update_many

Following function add columns if they are not present in the string, update a column values if they was already in the string. To delete column you should update its value to NULL.

enum enum_dyncol_func_result
mariadb_dyncol_update_many(DYNAMIC_COLUMN *str,
                           uint column_count,
                           uint *column_numbers,
                           DYNAMIC_COLUMN_VALUE *values);
enum enum_dyncol_func_result
mariadb_dyncol_update_many_named(DYNAMIC_COLUMN *str,
                                 uint column_count,
                                 MYSQL_LEX_STRING *column_keys,
                                 DYNAMIC_COLUMN_VALUE *values);
strIN/OUTPacked dynamic columns string to modify.
column_countINNumber of columns in following arrays
column_numbersINColumn numbers array (old format)
column_keysINColumn names array (new format)
valuesINValues of the columns array

mariadb_dyncol_exists

enum enum_dyncol_func_result
mariadb_dyncol_exists(DYNAMIC_COLUMN *str, uint column_number);
enum enum_dyncol_func_result
mariadb_dyncol_exists_named(DYNAMIC_COLUMN *str, MYSQL_LEX_STRING *column_key);
strINPacked dynamic columns string.
column_numberINColumn number (old format)
column_keyINColumn name (new format)

The function returns YES/NO or Error code

mariadb_dyncol_column_count

Get number of non-NULL columns

enum enum_dyncol_func_result
mariadb_dyncol_column_count(DYNAMIC_COLUMN *str, uint *column_count);
strINPacked dynamic columns string.
column_countOUTNumber of not NULL columns in the dynamic columns string

mariadb_dyncol_list

List non-NULL columns

enum enum_dyncol_func_result
mariadb_dyncol_list(DYNAMIC_COLUMN *str, uint *column_count, uint **column_numbers);
enum enum_dyncol_func_result
mariadb_dyncol_list_named(DYNAMIC_COLUMN *str, uint *column_count,
                          MYSQL_LEX_STRING **column_keys);
strINPacked dynamic columns string.
column_countOUTNumber of columns in following arrays
column_numbersOUTColumn numbers array (old format). Caller should free this array.
column_keysOUTColumn names array (new format). Caller should free this array.

mariadb_dyncol_get

Get one column value

enum enum_dyncol_func_result
mariadb_dyncol_get(DYNAMIC_COLUMN *org, uint column_number,
                   DYNAMIC_COLUMN_VALUE *value);
enum enum_dyncol_func_result
mariadb_dyncol_get_named(DYNAMIC_COLUMN *str, MYSQL_LEX_STRING *column_key,
                         DYNAMIC_COLUMN_VALUE *value);
strINPacked dynamic columns string.
column_numberINColumn numbers array (old format)
column_keyINColumn names array (new format)
valueOUTValue of the column

If the column is not found NULL returned as a value of the column.

mariadb_dyncol_unpack

Get all non-NULL columns values

enum enum_dyncol_func_result
mariadb_dyncol_unpack(DYNAMIC_COLUMN *str,
                      uint *column_count,
                      MYSQL_LEX_STRING **column_keys,
                      DYNAMIC_COLUMN_VALUE **values);
strINPacked dynamic columns string to unpack.
column_countOUTNumber of columns in following arrays
column_keysOUTColumn names array (should be free by caller)
valuesOUTValues of the columns array (should be free by caller)

mariadb_dyncol_has_names

Check packed dynamic column string format

my_bool mariadb_dyncol_has_names(DYNAMIC_COLUMN *str);
strINPacked dynamic columns string.

The function check only one byte of the string so it is quite efficient, but also wrong data tolerant.

mariadb_dyncol_check

Check packed dynamic column integrity

enum enum_dyncol_func_result
mariadb_dyncol_check(DYNAMIC_COLUMN *str);
strINPacked dynamic columns string.

mariadb_dyncol_json

Get dynamic columns as JSON object

enum enum_dyncol_func_result
mariadb_dyncol_json(DYNAMIC_COLUMN *str, DYNAMIC_STRING *json);
strINPacked dynamic columns string.
jsonOUTJSON representation

mariadb_dyncol_val_(TYPE)

Get dynamic column value as one of base 3 types

enum enum_dyncol_func_result
mariadb_dyncol_val_str(DYNAMIC_STRING *str, DYNAMIC_COLUMN_VALUE *val,
                       CHARSET_INFO *cs, my_bool quote);
enum enum_dyncol_func_result
mariadb_dyncol_val_long(longlong *ll, DYNAMIC_COLUMN_VALUE *val);
enum enum_dyncol_func_result
mariadb_dyncol_val_double(double *dbl, DYNAMIC_COLUMN_VALUE *val);
str or ll or dblOUTvalue of the column
valINValue

mariadb_dyncol_prepare_decimal

Initialization of dynamic string value before assigning decimal value

void mariadb_dyncol_prepare_decimal(DYNAMIC_COLUMN_VALUE *value);
valueOUTValue of the column

This function correctly lick decimal buffer to the decimal value.

Initialization of dynamic string value

#define mariadb_dyncol_value_init(V) (V)->type= DYN_COL_NULL

Above is a correct way to initialize "empty" dynamic column value.

mariadb_dyncol_column_cmp_named

Comparator of two column names

int mariadb_dyncol_column_cmp_named(const MYSQL_LEX_STRING *s1,
                                    const MYSQL_LEX_STRING *s2);

Columns stored sorted inside the packed dynamic column string, it is how routines compare the names.

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.