From: Marco van Wieringen Date: Sat, 3 Apr 2010 14:10:53 +0000 (+0200) Subject: Implement my_ingres_currval function. X-Git-Tag: Release-5.0.2~122 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=3cc269cc16c56f0d33efb61f0c51c38819e99418;p=bacula%2Fbacula Implement my_ingres_currval function. --- diff --git a/bacula/src/cats/ingres.c b/bacula/src/cats/ingres.c index 11860066fe..f09c30cea6 100755 --- a/bacula/src/cats/ingres.c +++ b/bacula/src/cats/ingres.c @@ -562,7 +562,7 @@ int my_ingres_query(B_DB *mdb, const char *query) /* SELECT */ Dmsg0(500,"my_ingres_query (SELECT) starting...\n"); mdb->result = INGquery(mdb, mdb->db, query); - if ( mdb->result != NULL ) { + if (mdb->result != NULL) { Dmsg1(500, "we have a result\n", query); // how many fields in the set? @@ -606,8 +606,44 @@ void my_ingres_free_result(B_DB *mdb) int my_ingres_currval(B_DB *mdb, const char *table_name) { - // TODO! - return -1; + /* + * Obtain the current value of the sequence that + * provides the serial value for primary key of the table. + * + * currval is local to our session. It is not affected by + * other transactions. + * + * Determine the name of the sequence. + * As we name all sequences as _seq this is easy. + */ + + char sequence[NAMEDATALEN-1]; + char query[NAMEDATALEN+50]; + INGresult *result; + int id = 0; + + bstrncpy(sequence, table_name, sizeof(sequence)); + bstrncat(sequence, "_seq", sizeof(sequence)); + + bsnprintf(query, sizeof(query), "SELECT %s.currval FROM %s", sequence, table_name); + + Dmsg1(500, "my_ingres_currval invoked with '%s'\n", query); + + result = INGquery(mdb, mdb->db, query); + + if (!result) { + Dmsg1(50, "Query failed: %s\n", query); + goto bail_out; + } + + Dmsg0(500, "exec done"); + + id = atoi(INGgetvalue(result, 0, 0)); + +bail_out: + INGclear(result); + + return id; } #ifdef HAVE_BATCH_FILE_INSERT