From b0e7daeec8893edee857e1598a96f63d3d6b7295 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 3 Aug 2014 10:57:05 +0200 Subject: [PATCH] ag_db_save_chart() now sets save_data->db_id after a successful INSERT --- src/ag-db.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ag-db.c b/src/ag-db.c index 4cc6792..2a77b80 100644 --- a/src/ag-db.c +++ b/src/ag-db.c @@ -596,7 +596,8 @@ ag_db_save_data_free(AgDbSave *save_data) * @err: a #GError for storing errors * * Saves @save_data to the database. If its db_id field is -1, a new record is - * created; otherwise the row with the given ID will be updated. + * created. In this case, @save_data is updated and db_id is set to the actual + * data record ID. Otherwise the row with the given ID will be updated. * * Returns: TRUE if the save succeeds, FALSE otherwise */ @@ -703,6 +704,30 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GError **err) ); save_success = FALSE; + } else { + // Get inserted row's id + GdaDataModel *result = ag_db_select( + db, + &local_err, + "SELECT last_insert_rowid()" + ); + + if (result == NULL) { + if (err) { + *err = g_error_copy(local_err); + } + + // TODO: a more reasonable return value should be given here + save_success = FALSE; + } else { + const GValue *value = gda_data_model_get_value_at( + result, + 0, 0, + NULL + ); + + save_data->db_id = g_value_get_int(value); + } } } else { g_value_init(&db_id, G_TYPE_INT);