From: Andrea Zagli Date: Fri, 15 Nov 2013 08:22:06 +0000 (+0100) Subject: Added function GdaEx::get_new_id (closes #691). X-Git-Tag: 0.4.0~8 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=0862c222c301e8e6acc8f788e7856765572e7bf8;p=libgdaex Added function GdaEx::get_new_id (closes #691). --- diff --git a/src/gdaex.c b/src/gdaex.c index 60d5a1e..39e633a 100644 --- a/src/gdaex.c +++ b/src/gdaex.c @@ -1,7 +1,7 @@ /* * gdaex.c * - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2013 Andrea Zagli * * This file is part of libgdaex. * @@ -3224,6 +3224,39 @@ const gchar return ret; } +guint +gdaex_get_new_id (GdaEx *gdaex, + const gchar *table_name, + const gchar *id_field_name, + const gchar *where) +{ + guint ret; + + gchar *sql; + GdaDataModel *dm; + + g_return_val_if_fail (IS_GDAEX (gdaex), 0); + + ret = 1; + sql = g_strdup_printf ("SELECT COALESCE (MAX (%s), 0) + 1 FROM %s%s%s", + id_field_name, + table_name, + where != NULL ? " WHERE " : "", + where != NULL ? where : ""); + dm = gdaex_query (gdaex, sql); + g_free (sql); + if (dm != NULL && gda_data_model_get_n_rows (dm) > 0) + { + ret = gdaex_data_model_get_value_integer_at (dm, 0, 0); + } + if (dm != NULL) + { + g_object_unref (dm); + } + + return ret; +} + gchar *gdaex_format_money (gdouble number, gint decimals, diff --git a/src/gdaex.h b/src/gdaex.h index 8cae732..fd5d420 100644 --- a/src/gdaex.h +++ b/src/gdaex.h @@ -1,7 +1,7 @@ /* * gdaex.h * - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2013 Andrea Zagli * * This file is part of libgdaex. * @@ -242,6 +242,11 @@ const gchar *gdaex_get_sql_from_hashtable (GdaEx *gdaex, GHashTable *keys, GHashTable *fields); +guint gdaex_get_new_id (GdaEx *gdaex, + const gchar *table_name, + const gchar *id_field_name, + const gchar *where); + gchar *gdaex_format_money (gdouble number, gint decimals, gboolean with_currency_symbol);