Utility functions

Utility functions — Utility functions

Stability Level

Stable, unless otherwise indicated

Functions

Description

Some useful functions.

Functions

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".

Parameters

type

Type to convert from.

 

Returns

the GDA's string representing the given GType or the name returned by g_type_name.

[transfer none]


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 G_TYPE_DATE_TIME

  • "boolean" for G_TYPE_BOOLEAN

  • "blob" for GDA_TYPE_BLOB

  • "binary" for GDA_TYPE_BINARY

  • "null" for GDA_TYPE_NULL

Parameters

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 "\"

Parameters

string

string to escape

 

Returns

a new string.

[transfer full][nullable]


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 "'".

Parameters

string

string to unescape

 

Returns

a new unescaped string, or NULL in an error was found in string .

[transfer full][nullable]


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

Parameters

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.

Parameters

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.

Parameters

cnc

a GdaConnection object

 

sql

a partial SQL statement which is the context of the completion proposal, may also start with a "." for Gda's tools which use internal commands

 

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][nullable]


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.

Parameters

string

a string to encode

 

Returns

a new string or NULL.

[transfer full][nullable]


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.

Parameters

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.

Parameters

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"

Parameters

string

a string in the "&lt;provider&gt;://@]<connection_params>" form

 

out_cnc_params

a place to store the new string containing the <connection_params> part.

[out]

out_provider

a place to store the new string containing the <provider> part.

[out]

out_username

a place to store the new string containing the <username> part.

[out]

out_password

(nullable): a place to store the new string containing the <password> part, or NULL.

[out]

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.

Parameters

gdate

a pointer to a GDate structure which will be filled

 

value

a string

 

Returns

TRUE if value has been sucessfuly parsed as a valid date (see g_date_valid()).


gda_parse_iso8601_time ()

GdaTime *
gda_parse_iso8601_time (const gchar *value);

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

Accepted date format is "HH:MM:SS.ms" where TZ is +hour or -hour. If no time zone is given UTC is used.

Parameters

value

a string

 

Returns

a new parsed GdaTime.

[transfer full]


gda_parse_formatted_date ()

gboolean
gda_parse_formatted_date (GDate *gdate,
                          const gchar *value,
                          GDateDMY first,
                          GDateDMY second,
                          GDateDMY third,
                          gchar sep);

This function is similar to gda_parse_iso8601_date() (with first being G_DATE_YEAR , second being G_DATE_MONTH , third being G_DATE_DAY and sep being '-') but allows one to specify the expected date format.

Parameters

gdate

a pointer to a GDate structure which will be filled

 

value

a string to be parsed

 

first

a GDateDMY specifying which of year, month or day appears first (in the first bytes) in value

 

second

a GDateDMY specifying which of year, month or day appears second (in the first bytes) in value

 

third

a GDateDMY specifying which of year, month or day appears third (in the first bytes) in value

 

sep

spcifies the expected separator character bewteen year, month and day (for example '-')

 

Returns

TRUE if value has been sucessfuly parsed as a valid date (see g_date_valid()).

Since: 5.2


gda_parse_formatted_time ()

GdaTime *
gda_parse_formatted_time (const gchar *value,
                          gchar sep);

Parameters

value

a string

 

sep

the time separator, usually ':'. If equal to 0 , then the expexted format will be HHMMSS...

 

Returns

a new parsed GdaTime.

[transfer full]

Since: 6.0


gda_parse_formatted_timestamp ()

GDateTime *
gda_parse_formatted_timestamp (const gchar *value,
                               GDateDMY first,
                               GDateDMY second,
                               GDateDMY third,
                               gchar sep);

This function is similar to g_date_time_new_from_iso8601() (with first being G_DATE_YEAR , second being G_DATE_MONTH , third being G_DATE_DAY and sep being '-') but allows one to specify the expected date format.

Parameters

value

a string to be parsed

 

first

a GDateDMY specifying which of year, month or day appears first (in the first bytes) in value

 

second

a GDateDMY specifying which of year, month or day appears second (in the first bytes) in value

 

third

a GDateDMY specifying which of year, month or day appears third (in the first bytes) in value

 

sep

specifies the expected separator character between year, month and day (for example '-')

 

Returns

a new GDateTime if value has been successfully parsed as a valid date (see g_date_valid()).

[nullable][transfer full]

Since: 5.2


gda_alphanum_to_text ()

gchar *
gda_alphanum_to_text (gchar *text);

Does the opposite of gda_text_to_alphanum(), in the same string

Parameters

text

a string

 

Returns

text if conversion succeeded or NULL if an error occurred.

[transfer full][nullable]


gda_text_to_alphanum ()

gchar *
gda_text_to_alphanum (const gchar *text);

The "encoding" consists in replacing non alphanumeric character with the string "__gdaXX" where XX is the hex. representation of the non alphanumeric char.

Parameters

text

the text to convert

 

Returns

a new string.

[transfer full]