From: Andrea Zagli Date: Sun, 22 Dec 2019 09:08:36 +0000 (+0100) Subject: Added functions ::new, ::set_[dsn|cnc_string|gda_connection]. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=0c57c12203ce0a0df32188996e522c88866674c4;p=libgdaex Added functions ::new, ::set_[dsn|cnc_string|gda_connection]. --- diff --git a/.gitignore b/.gitignore index 99cef6d..ba749cc 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,7 @@ tests/grid tests/fill_liststore tests/*.exe tests/select +tests/getsql tests/getsqlfromhashtable tests/sqlbuilder tests/metastore diff --git a/src/gdaex.c b/src/gdaex.c index 7e729a8..b41a846 100644 --- a/src/gdaex.c +++ b/src/gdaex.c @@ -186,7 +186,18 @@ gdaex_init (GdaEx *gdaex) } /** - * gdaex_new_from_dsn: + * gdaex_new: + * + * Returns: the newly created #GdaEx. + */ +GdaEx +*gdaex_new () +{ + return GDAEX (g_object_new (gdaex_get_type (), NULL)); +} + +/** + * gdaex_set_dsn: * @dsn: GDA data source name to connect to. * @username: user name to use to connect. * @password: password for @username. @@ -194,12 +205,14 @@ gdaex_init (GdaEx *gdaex) * If @username and @password are both NULL or empty, it will be used those * defined into datasource. * - * Returns: the newly created #GdaEx. + * Returns: @TRUE on newly created #GdaEx. */ -GdaEx -*gdaex_new_from_dsn (const gchar *dsn, const gchar *username, const gchar *password) +gboolean +gdaex_set_dsn (GdaEx *gdaex, + const gchar *dsn, + const gchar *username, + const gchar *password) { - GdaEx *gdaex; GdaExPrivate *priv; gchar *new_user; gchar *new_pwd; @@ -207,19 +220,13 @@ GdaEx GError *error; + g_return_val_if_fail (IS_GDAEX (gdaex), FALSE); + if (dsn == NULL || strcmp (g_strstrip (g_strdup (dsn)), "") == 0) { /* TO DO */ g_warning (_("Datasource cannot be empty.")); - return NULL; - } - - gdaex = GDAEX (g_object_new (gdaex_get_type (), NULL)); - if (gdaex == NULL) - { - /* TO DO */ - g_warning (_("Unable to create GdaEx object.")); - return NULL; + return FALSE; } priv = GDAEX_GET_PRIVATE (gdaex); @@ -279,40 +286,36 @@ GdaEx { g_warning (_("Error creating database connection: %s"), error->message != NULL ? error->message : _("no details")); - return NULL; + return FALSE; } gdaex_create_connection_parser (gdaex); - return gdaex; + return TRUE; } /** - * gdaex_new_from_string: + * gdaex_set:gda_connection: * @cnc_string: the connection string. * - * Returns: the newly created #GdaEx. + * Set connection string. + * + * Returns: the @TRUE on newly created #GdaEx. */ -GdaEx -*gdaex_new_from_string (const gchar *cnc_string) +gboolean +gdaex_set_cnc_string (GdaEx *gdaex, + const gchar *cnc_string) { GError *error; - GdaEx *gdaex; GdaExPrivate *priv; + g_return_val_if_fail (IS_GDAEX (gdaex), FALSE); + if (cnc_string == NULL || strcmp (g_strstrip (g_strdup (cnc_string)), "") == 0) { /* TO DO */ g_warning (_("cnc_string must not be empty.")); - return NULL; - } - - gdaex = GDAEX (g_object_new (gdaex_get_type (), NULL)); - if (gdaex == NULL) - { - /* TO DO */ - g_warning (_("Unable to create GdaEx object.")); - return NULL; + return FALSE; } priv = GDAEX_GET_PRIVATE (gdaex); @@ -328,11 +331,100 @@ GdaEx { g_warning (_("Error creating database connection: %s"), error->message != NULL ? error->message : _("no details.")); - return NULL; + return FALSE; } gdaex_create_connection_parser (gdaex); + return TRUE; +} + +/** + * gdaex_set:gda_connection: + * @conn: a #GdaConnection. + * + * Set #GdaConnection. + * + * Returns: the @TRUE on newly created #GdaEx. + */ +gboolean +gdaex_set_gda_connection (GdaEx *gdaex, + GdaConnection *conn) +{ + GdaExPrivate *priv; + + g_return_val_if_fail (IS_GDAEX (gdaex), FALSE); + g_return_val_if_fail (GDA_IS_CONNECTION (conn), FALSE); + + priv = GDAEX_GET_PRIVATE (gdaex); + + priv->gda_conn = conn; + + gdaex_create_connection_parser (gdaex); + + return TRUE; +} + +/** + * gdaex_new_from_dsn: + * @dsn: GDA data source name to connect to. + * @username: user name to use to connect. + * @password: password for @username. + * + * If @username and @password are both NULL or empty, it will be used those + * defined into datasource. + * + * Returns: the newly created #GdaEx. + */ +GdaEx +*gdaex_new_from_dsn (const gchar *dsn, const gchar *username, const gchar *password) +{ + GdaEx *gdaex; + + gdaex = gdaex_new (); + if (gdaex == NULL) + { + /* TO DO */ + g_warning (_("Unable to create GdaEx object.")); + return NULL; + } + + if (!gdaex_set_dsn (gdaex, dsn, username, password)) + { + /* TO DO */ + g_warning (_("Unable to create GdaEx object.")); + g_clear_object (&gdaex); + } + + return gdaex; +} + +/** + * gdaex_new_from_string: + * @cnc_string: the connection string. + * + * Returns: the newly created #GdaEx. + */ +GdaEx +*gdaex_new_from_string (const gchar *cnc_string) +{ + GdaEx *gdaex; + + gdaex = gdaex_new (); + if (gdaex == NULL) + { + /* TO DO */ + g_warning (_("Unable to create GdaEx object.")); + return NULL; + } + + if (!gdaex_set_cnc_string (gdaex, cnc_string)) + { + /* TO DO */ + g_warning (_("Unable to create GdaEx object.")); + g_clear_object (&gdaex); + } + return gdaex; } @@ -348,17 +440,21 @@ GdaEx *gdaex_new_from_connection (GdaConnection *conn) { GdaEx *gdaex; - GdaExPrivate *priv; - - g_return_val_if_fail (GDA_IS_CONNECTION (conn), NULL); - - gdaex = GDAEX (g_object_new (gdaex_get_type (), NULL)); - - priv = GDAEX_GET_PRIVATE (gdaex); - priv->gda_conn = conn; + gdaex = gdaex_new (); + if (gdaex == NULL) + { + /* TO DO */ + g_warning (_("Unable to create GdaEx object.")); + return NULL; + } - gdaex_create_connection_parser (gdaex); + if (!gdaex_set_gda_connection (gdaex, conn)) + { + /* TO DO */ + g_warning (_("Unable to create GdaEx object.")); + g_clear_object (&gdaex); + } return gdaex; } diff --git a/src/gdaex.h b/src/gdaex.h index 9bcb3ff..55cff45 100644 --- a/src/gdaex.h +++ b/src/gdaex.h @@ -62,6 +62,16 @@ struct _GdaExClass GType gdaex_get_type (void) G_GNUC_CONST; +GdaEx *gdaex_new (void); + +gboolean gdaex_set_dsn (GdaEx *gdaex, + const gchar *dsn, + const gchar *username, + const gchar *password); +gboolean gdaex_set_cnc_string (GdaEx *gdaex, + const gchar *cnc_string); +gboolean gdaex_set_gda_connection (GdaEx *gdaex, + GdaConnection *conn); GdaEx *gdaex_new_from_dsn (const gchar *dsn, const gchar *username, diff --git a/tests/select.c b/tests/select.c index 9330052..10b3639 100644 --- a/tests/select.c +++ b/tests/select.c @@ -36,6 +36,7 @@ main (int argc, char **argv) { g_error ("Unable to connect to the db."); } + gdaex_set_cnc_string (gdaex, g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=grid.db", TESTSDIR)); sql = g_strdup_printf ("SELECT * FROM clients"); dm = gdaex_query (gdaex, sql);