Dynamic Columns API

You are viewing an old version of this article. View the current version here.
#include <mysql/ma_dyncol.h>

The functions are present in both libmysql and libmysqld.

Result codes of functions

General rule is that if result code less then 0 it is an error.

Following codes are defined in enum enum_dyncol_func_result:

valuenamemeaning
0ER_DYNCOL_OKA procedure sucessfully finished
0ER_DYNCOL_NO(the same as ER_DYNCOL_OK but for functions which requite YES/NO responce
1ER_DYNCOL_YESYES responce and sucess (see above)
2ER_DYNCOL_TRUNCATEDOK, but data was truncated
-1ER_DYNCOL_FORMATWrong format of the encoded string
-2ER_DYNCOL_LIMITA limit of implimentation reached
-3ER_DYNCOL_RESOURCEOut of resourses
-4ER_DYNCOL_DATAIncorrect input data (other then encoded string)
-5ER_DYNCOL_UNKNOWN_CHARSETUnknown character set

Type of dynamic column encoded string

We add special type for dynamic column encoded string: DYNAMIC_COLUMN but in fact it is DYNAMIC_STRING.

Types of data used by dynamic string routines

The types defined as a DYNAMIC_COLUMN_TYPE (enum enum_dynamic_column_type).

typemeaning
DYN_COL_NULLmeans NULL, never stored in the column (absence mean NULL)
DYN_COL_INTSigned Integer (long)
DYN_COL_UINTUnsigned integer (unsigned long)
DYN_COL_DOUBLEFloating point double (double
DYN_COL_STRINGString (MYSQL_LEX_STRING and CHARSET_INFO)
DYN_COL_DECIMALMariaDB/MySQL Decimal (fixed point) value
DYN_COL_DATETIMEDate and tyme
DYN_COL_DATEDate
DYN_COL_TIMETime
DYN_COL_DYNCOLThe same as String with binary encoding but content other encoded dynamic columns row (valid only for new 10.0.1 format with names)

Storing unpacked values

Unpacked (not encoded to the string) values of dynamic columns stored in the following structure

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;

Values should be requested from different underlying unuon fieds depending on type (value.type)

typefield
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 and 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

Before putting value into value.x.decimal.value mariadb_dyncol_prepare_decimal should be called for correct initialization of the value and buffer where decimal will be stored.

Functions

Mostly there is 2 pairs of function, one works only with old (numeric) format other one can read old (numeric) and new (with names) format but produce string in the new format.

To check packed dynamic string format use mariadb_dyncol_has_names function

Creating string from unpacked value array

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

Changing dynamic column packed string content

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

Checking column existance

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

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

List of 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.

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.

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)

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.

Check packed dynamic column integrity

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

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

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

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.

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.