]> saetta.ns0.it Git - libgdaex/commitdiff
Added function GdaEx::get_new_id (closes #691).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 15 Nov 2013 08:22:06 +0000 (09:22 +0100)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 15 Nov 2013 08:22:06 +0000 (09:22 +0100)
src/gdaex.c
src/gdaex.h

index 60d5a1e9220959f04e51b36774b9b9f75bf1db89..39e633a1b250e175a6d09c1d87ee22263103e9fb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  gdaex.c
  *
- *  Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2005-2013 Andrea Zagli <azagli@libero.it>
  *
  *  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,
index 8cae732b6d07a491e3e366a2b77cf8d4ed3e4764..fd5d42015b0f7e43bc25b5160d42627b631701cc 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  gdaex.h
  *
- *  Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2005-2013 Andrea Zagli <azagli@libero.it>
  *
  *  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);