## Utility functions

Utility functions — Utility functions

## Stability Level

Stable, unless otherwise indicated

## Synopsis

    const gchar *       gda_g_type_to_string                (GType type);
    GType               gda_g_type_from_string              (const gchar *str);

    gchar *             gda_default_escape_string           (const gchar *string);
    gchar *             gda_default_unescape_string         (const gchar *string);
    guint               gda_identifier_hash                 (const gchar *id);
    gboolean            gda_identifier_equal                (const gchar *id1,
                                                             const gchar *id2);
    gchar **            gda_completion_list_get             (GdaConnection *cnc,
                                                             const gchar *sql,
                                                             gint start,
                                                             gint end);

    gchar *             gda_rfc1738_encode                  (const gchar *string);
    gboolean            gda_rfc1738_decode                  (gchar *string);
    void                gda_dsn_split                       (const gchar *string,
                                                             gchar **out_dsn,
                                                             gchar **out_username,
                                                             gchar **out_password);
    void                gda_connection_string_split         (const gchar *string,
                                                             gchar **out_cnc_params,
                                                             gchar **out_provider,
                                                             gchar **out_username,
                                                             gchar **out_password);

    gboolean            gda_parse_iso8601_date              (GDate *gdate,
                                                             const gchar *value);
    gboolean            gda_parse_iso8601_time              (GdaTime *timegda,
                                                             const gchar *value);
    gboolean            gda_parse_iso8601_timestamp         (GdaTimestamp *timestamp,
                                                             const gchar *value);

## Description

Some useful functions.

## Details

### gda_g_type_to_string ()

    const gchar *       gda_g_type_to_string                (GType type);

Converts a GType to its string representation (use `gda_g_type_from_string()` for the operation in the other direction).

This function wraps `g_type_name()` but for common types it provides an easier to understand and remember name. For Example the G_TYPE_STRING is converted to "string" whereas `g_type_name()` converts it to "gchararray".

`type` : | Type to convert from.
Returns : | the GDA's string representing the given GType or the name returned by g_type_name.

### gda_g_type_from_string ()

    GType               gda_g_type_from_string              (const gchar *str);

Converts a named type to ts GType type (also see the `gda_g_type_to_string()` function).

This function is a wrapper around the `g_type_from_name()` function, but also recognizes some type synonyms such as:

- "int" for G_TYPE_INT

- "uint" for G_TYPE_UINT

- "int64" for G_TYPE_INT64

- "uint64" for G_TYPE_UINT64

- "char" for G_TYPE_CHAR

- "uchar" for G_TYPE_UCHAR

- "short" for GDA_TYPE_SHORT

- "ushort" for GDA_TYPE_USHORT

- "string" for G_TYPE_STRING

- "date" for G_TYPE_DATE

- "time" for GDA_TYPE_TIME

- "timestamp" for GDA_TYPE_TIMESTAMP

- "boolean" for G_TYPE_BOOLEAN

- "blob" for GDA_TYPE_BLOB

- "binary" for GDA_TYPE_BINARY

- "null" for GDA_TYPE_NULL

`str` : | the name of a GType, as returned by `gda_g_type_to_string()`.
Returns : | the GType represented by the given `str`, or G_TYPE_INVALID if not found

### gda_default_escape_string ()

    gchar *             gda_default_escape_string           (const gchar *string);

Escapes `string` to make it understandable by a DBMS. The escape method is very common and replaces any occurrence of "'" with "''" and "\" with "\\"

`string` : | string to escape
Returns : | a new string

### gda_default_unescape_string ()

    gchar *             gda_default_unescape_string         (const gchar *string);

Do the reverse of `gda_default_escape_string()`: transforms any "''" into "'", any "\\" into "\" and any "\'" into "'".

`string` : | string to unescape
Returns : | a new unescaped string, or `NULL` in an error was found in `string`

### gda_identifier_hash ()

    guint               gda_identifier_hash                 (const gchar *id);

computes a hash string from `id`, to be used in hash tables as a GHashFunc

`id` : | an identifier string
Returns : | a new hash

### gda_identifier_equal ()

    gboolean            gda_identifier_equal                (const gchar *id1,
                                                             const gchar *id2);

Does the same as strcmp(`id1`, `id2`), but handles the case where id1 and/or id2 are enclosed in double quotes. can also be used in hash tables as a GEqualFunc.

`id1` : | an identifier string
`id2` : | an identifier string
Returns : | TRUE if `id1` and `id2` are equal.

### gda_completion_list_get ()

    gchar **            gda_completion_list_get             (GdaConnection *cnc,
                                                             const gchar *sql,
                                                             gint start,
                                                             gint end);

Creates an array of strings (terminated by a `NULL`) corresponding to possible completions. If no completion is available, then the returned array contains just one NULL entry, and if it was not possible to try to compute a completions list, then `NULL` is returned.

`cnc` : | a GdaConnection object
`sql` : | a partial SQL statement which is the context of the completion proposal
`start` : | starting position within `sql` of the "token" to complete (starts at 0)
`end` : | ending position within `sql` of the "token" to complete
Returns : | a new array of strings, or `NULL` (use `g_strfreev()` to free the returned array). [transfer full][array zero-terminated=1][allow-none]

### gda_rfc1738_encode ()

    gchar *             gda_rfc1738_encode                  (const gchar *string);

Encodes `string` using the RFC 1738 recommendations: the `<>"#%{}|\^~[]'`;/?:@=&` and space characters are replaced by `"%`ab`"` where `ab` is the hexadecimal number corresponding to the character.

`string` : | a string to encode
Returns : | a new string. [transfer full]

### gda_rfc1738_decode ()

    gboolean            gda_rfc1738_decode                  (gchar *string);

Decodes `string` using the RFC 1738 recommendations: the `<>"#%{}|\^~[]'`;/?:@=&` and space characters are replaced by `"%`ab`"` where `ab` is the hexadecimal number corresponding to the character.

`string` should respect the RFC 1738 encoding. If this is not the case (for example if there is a "`2z`" because 2z is not an hexadecimal value), then the part with the problem is not decoded, and the function returns FALSE.

`string` is decoded in place, no new string gets created.

`string` : | a string to decode
Returns : | TRUE if no error occurred.

### gda_dsn_split ()

    void                gda_dsn_split                       (const gchar *string,
                                                             gchar **out_dsn,
                                                             gchar **out_username,
                                                             gchar **out_password);

Extract the DSN, username and password from `string`. in `string`, the various parts are strings which are expected to be encoded using an RFC 1738 compliant encoding. If they are specified, the returned username and password strings are correctly decoded.

`out_username` and `out_password` may be set to `NULL` depending on `string`'s format.

`string` : | a string in the "[<username>[:<password>]@]<DSN>" form
`out_dsn` : | a place to store the new string containing the <DSN> part
`out_username` : | a place to store the new string containing the <username> part
`out_password` : | a place to store the new string containing the <password> part

### gda_connection_string_split ()

    void                gda_connection_string_split         (const gchar *string,
                                                             gchar **out_cnc_params,
                                                             gchar **out_provider,
                                                             gchar **out_username,
                                                             gchar **out_password);

Extract the provider, connection parameters, username and password from `string`. in `string`, the various parts are strings which are expected to be encoded using an RFC 1738 compliant encoding. If they are specified, the returned provider, username and password strings are correctly decoded.

For example all the following connection strings:

    PostgreSQL://meme:pass@DB_NAME=mydb;HOST=server
    PostgreSQL://meme@DB_NAME=mydb;HOST=server;PASSWORD=pass
    PostgreSQL://meme@DB_NAME=mydb;PASSWORD=pass;HOST=server
    PostgreSQL://meme@PASSWORD=pass;DB_NAME=mydb;HOST=server
    PostgreSQL://DB_NAME=mydb;HOST=server;USERNAME=meme;PASSWORD=pass
    PostgreSQL://DB_NAME=mydb;HOST=server;PASSWORD=pass;USERNAME=meme
    PostgreSQL://DB_NAME=mydb;USERNAME=meme;PASSWORD=pass;HOST=server
    PostgreSQL://PASSWORD=pass;USERNAME=meme;DB_NAME=mydb;HOST=server
    PostgreSQL://:pass@USERNAME=meme;DB_NAME=mydb;HOST=server
    PostgreSQL://:pass@DB_NAME=mydb;HOST=server;USERNAME=meme

will return the following new strings (double quotes added here to delimit strings):

    out_cnc_params: "DB_NAME=mydb;HOST=server"
    out_provider: "PostgreSQL"
    out_username: "meme"
    out_password: "pass"

`string` : | a string in the "[<provider>://][<username>[:<password>]@]<connection_params>" form
`out_cnc_params` : | a place to store the new string containing the <connection_params> part
`out_provider` : | a place to store the new string containing the <provider> part
`out_username` : | a place to store the new string containing the <username> part
`out_password` : | a place to store the new string containing the <password> part

### gda_parse_iso8601_date ()

    gboolean            gda_parse_iso8601_date              (GDate *gdate,
                                                             const gchar *value);

Extracts date parts from `value`, and sets `gdate`'s contents

Accepted date format is "YYYY-MM-DD" (more or less than 4 digits for years and less than 2 digits for month and day are accepted). Years must be in the 1-65535 range, a limitation imposed by GDate.

`gdate` : | a pointer to a GDate structure which will be filled
`value` : | a string
Returns : | `TRUE` if no error occurred

### gda_parse_iso8601_time ()

    gboolean            gda_parse_iso8601_time              (GdaTime *timegda,
                                                             const gchar *value);

Extracts time parts from `value`, and sets `timegda`'s contents

Accepted date format is "HH:MM:SS[.ms][TZ]" where TZ is +hour or -hour

`timegda` : | a pointer to a GdaTime structure which will be filled
`value` : | a string
Returns : | TRUE if no error occurred

### gda_parse_iso8601_timestamp ()

    gboolean            gda_parse_iso8601_timestamp         (GdaTimestamp *timestamp,
                                                             const gchar *value);

Extracts date and time parts from `value`, and sets `timestamp`'s contents

Accepted date format is "YYYY-MM-DD HH:MM:SS[.ms][TZ]" where TZ is +hour or -hour

`timestamp` : | a pointer to a GdaTimeStamp structure which will be filled
`value` : | a string
Returns : | TRUE if no error occurred