[icinga-checkins] icinga.org: icinga-core/test/cgis: idoutils: move the insert id / sequence stuff into ido2db_query_insert_or_update_contactdefinition_definition_add (refs #3408)

git at icinga.org git at icinga.org
Wed Nov 28 17:24:05 CET 2012


Module: icinga-core
Branch: test/cgis
Commit: 3673c1c096ce7a5a701bc399778a4b7939cf8033
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=3673c1c096ce7a5a701bc399778a4b7939cf8033

Author: Michael Friedrich <michael.friedrich at gmail.com>
Date:   Thu Nov  1 13:45:02 2012 +0100

idoutils: move the insert id / sequence stuff into ido2db_query_insert_or_update_contactdefinition_definition_add (refs #3408)

---

 module/idoutils/include/dbqueries.h |    2 +-
 module/idoutils/src/dbhandlers.c    |   52 +------------------
 module/idoutils/src/dbqueries.c     |   96 ++++++++++++++++++++++++++++++++++-
 3 files changed, 97 insertions(+), 53 deletions(-)

diff --git a/module/idoutils/include/dbqueries.h b/module/idoutils/include/dbqueries.h
index bbf5e2e..8fd82ef 100644
--- a/module/idoutils/include/dbqueries.h
+++ b/module/idoutils/include/dbqueries.h
@@ -105,7 +105,7 @@ int ido2db_query_insert_or_update_timeperiodefinition_definition_add(ido2db_idi
 int ido2db_query_insert_or_update_timeperiodefinition_timeranges_add(ido2db_idi *idi, void **data);
 
 /* CONTACTDEFINITION */
-int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *idi, void **data);
+int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *idi, void **data, unsigned long *id);
 int ido2db_query_insert_or_update_contactdefinition_addresses_add(ido2db_idi *idi, void **data);
 int ido2db_query_insert_or_update_contactdefinition_notificationcommands_add(ido2db_idi *idi, void **data);
 
diff --git a/module/idoutils/src/dbhandlers.c b/module/idoutils/src/dbhandlers.c
index 57c2181..3013635 100644
--- a/module/idoutils/src/dbhandlers.c
+++ b/module/idoutils/src/dbhandlers.c
@@ -8040,57 +8040,7 @@ int ido2db_handle_contactdefinition(ido2db_idi *idi) {
 	data[20] = (void *) &notify_host_flapping;
 	data[21] = (void *) &notify_host_downtime;
 
-	result = ido2db_query_insert_or_update_contactdefinition_definition_add(idi, data);
-
-	if (result == IDO_OK) {
-
-#ifdef USE_LIBDBI /* everything else will be libdbi */
-		switch (idi->dbinfo.server_type) {
-		case IDO2DB_DBSERVER_MYSQL:
-			/* mysql doesn't use sequences */
-			contact_id = dbi_conn_sequence_last(idi->dbinfo.dbi_conn, NULL);
-			ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(ido2db_idi *idi)(%lu) contact_id\n", contact_id);
-			break;
-		case IDO2DB_DBSERVER_PGSQL:
-			/* depending on tableprefix/tablename a sequence will be used */
-			if (asprintf(&buf, "%s_contact_id_seq", ido2db_db_tablenames[IDO2DB_DBTABLE_CONTACTS]) == -1)
-				buf = NULL;
-
-			contact_id = dbi_conn_sequence_last(idi->dbinfo.dbi_conn, buf);
-			ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(ido2db_idi *idi)(%s=%lu) contact_id\n", buf, contact_id);
-			free(buf);
-			break;
-		default:
-			break;
-		}
-#endif
-
-#ifdef USE_PGSQL /* pgsql */
-
-#endif
-
-#ifdef USE_ORACLE /* Oracle ocilib specific */
-		if (asprintf(&seq_name, "seq_contacts") == -1)
-			seq_name = NULL;
-		contact_id = ido2db_oci_sequence_lastid(idi, seq_name);
-		ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(ido2db_idi *idi)(%lu) contact_id\n", contact_id);
-		free(seq_name);
-#endif /* Oracle ocilib specific */
-	}
-
-#ifdef USE_LIBDBI /* everything else will be libdbi */
-	dbi_result_free(idi->dbinfo.dbi_result);
-	idi->dbinfo.dbi_result = NULL;
-#endif
-
-#ifdef USE_PGSQL /* pgsql */
-
-#endif
-
-#ifdef USE_ORACLE /* Oracle ocilib specific */
-
-
-#endif /* Oracle ocilib specific */
+	result = ido2db_query_insert_or_update_contactdefinition_definition_add(idi, data, &contact_id);
 
 	for (x = 0; x < ICINGA_SIZEOF_ARRAY(es); x++)
 		free(es[x]);
diff --git a/module/idoutils/src/dbqueries.c b/module/idoutils/src/dbqueries.c
index 93605eb..b8de75e 100644
--- a/module/idoutils/src/dbqueries.c
+++ b/module/idoutils/src/dbqueries.c
@@ -10233,15 +10233,20 @@ int ido2db_query_insert_or_update_timeperiodefinition_timeranges_add(ido2db_idi
 /* CONTACTDEFINITION                */
 /************************************/
 
-int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *idi, void **data) {
+int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *idi, void **data, unsigned long *id) {
 	int result = IDO_OK;
 #ifdef USE_LIBDBI
         char * query = NULL;
         char * query1 = NULL;
         char * query2 = NULL;
+	char * buf = NULL;
         unsigned long contact_id;
         int mysql_update = FALSE;
 #endif
+#ifdef USE_ORACLE
+	char * seq_name = NULL;
+#endif
+
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_query_insert_or_update_contactdefinition_definition_add() start\n");
 
 	if (idi == NULL)
@@ -10342,8 +10347,47 @@ int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *i
                         	/* send query to db */
                 	        result = ido2db_db_query(idi, query2);
 	                        free(query2);
+
+				if (result == IDO_OK) {
+		                        /* mysql doesn't use sequences */
+                		        *id = dbi_conn_sequence_last(idi->dbinfo.dbi_conn, NULL);
+		                        ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(%lu) contact_id\n", *id);
+				}
+
+	                        dbi_result_free(idi->dbinfo.dbi_result);
+        	                idi->dbinfo.dbi_result = NULL;
 			}
                 }
+		else {
+                        dbi_result_free(idi->dbinfo.dbi_result);
+                        idi->dbinfo.dbi_result = NULL;
+
+			/* we hit an update, fetch the id */
+                        dummy = asprintf(&query, "SELECT contact_id FROM %s WHERE instance_id=%lu AND config_type=%d AND contact_object_id=%lu",
+                                ido2db_db_tablenames[IDO2DB_DBTABLE_CONTACTS],
+                                 *(unsigned long *) data[0],     /* unique constraint start */
+                                 *(int *) data[1],
+                                 *(unsigned long *) data[2]     /* unique constraint end */
+                                );
+
+                        /* send query to db */
+                        if ((result = ido2db_db_query(idi, query)) == IDO_OK) {
+                                if (idi->dbinfo.dbi_result != NULL) {
+                                        if (dbi_result_next_row(idi->dbinfo.dbi_result)) {
+                                                *id = dbi_result_get_ulonglong(idi->dbinfo.dbi_result, "contact_id");
+		                        	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(%lu) contact_id\n", *id);
+                                        }
+
+                                        dbi_result_free(idi->dbinfo.dbi_result);
+                                        idi->dbinfo.dbi_result = NULL;
+                                }
+                        } else {
+                        	dbi_result_free(idi->dbinfo.dbi_result);
+	                        idi->dbinfo.dbi_result = NULL;
+			}
+                        free(query);
+
+		}
                 break;
 
 	case IDO2DB_DBSERVER_PGSQL:
@@ -10410,7 +10454,50 @@ int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *i
 			/* send query to db */
 			result = ido2db_db_query(idi, query2);
 			free(query2);
+
+			if (result == IDO_OK) {
+	                        /* depending on tableprefix/tablename a sequence will be used */
+        	                if (asprintf(&buf, "%s_contact_id_seq", ido2db_db_tablenames[IDO2DB_DBTABLE_CONTACTS]) == -1)
+                	                buf = NULL;
+
+                        	*id = dbi_conn_sequence_last(idi->dbinfo.dbi_conn, buf);
+	                        ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(ido2db_idi *idi)(%s=%lu) contact_id\n", buf, *id);
+	                        free(buf);
+			}
+
+			dbi_result_free(idi->dbinfo.dbi_result);
+			idi->dbinfo.dbi_result = NULL;
 		}
+                else {
+                        dbi_result_free(idi->dbinfo.dbi_result);
+                        idi->dbinfo.dbi_result = NULL;
+                
+                        /* we hit an update, fetch the id */
+                        dummy = asprintf(&query, "SELECT contact_id FROM %s WHERE instance_id=%lu AND config_type=%d AND contact_object_id=%lu",
+                                ido2db_db_tablenames[IDO2DB_DBTABLE_CONTACTS],
+                                 *(unsigned long *) data[0],     /* unique constraint start */
+                                 *(int *) data[1],
+                                 *(unsigned long *) data[2]     /* unique constraint end */
+                                );
+
+                        /* send query to db */
+                        if ((result = ido2db_db_query(idi, query)) == IDO_OK) {
+                                if (idi->dbinfo.dbi_result != NULL) {
+                                        if (dbi_result_next_row(idi->dbinfo.dbi_result)) {
+                                                *id = dbi_result_get_ulonglong(idi->dbinfo.dbi_result, "contact_id");
+                                                ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(%lu) contact_id\n", *id);
+                                        }
+
+                                        dbi_result_free(idi->dbinfo.dbi_result);
+                                        idi->dbinfo.dbi_result = NULL;
+                                }
+                        } else {
+                                dbi_result_free(idi->dbinfo.dbi_result);
+                                idi->dbinfo.dbi_result = NULL;
+                        }
+                        free(query);
+
+                }
 		break;
 	default:
 		break;
@@ -10522,6 +10609,13 @@ int ido2db_query_insert_or_update_contactdefinition_definition_add(ido2db_idi *i
 	/* commit statement */
 	OCI_Commit(idi->dbinfo.oci_connection);
 
+	if (asprintf(&seq_name, "seq_contacts") == -1)
+	seq_name = NULL;
+
+	*id = ido2db_oci_sequence_lastid(idi, seq_name);
+	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_contactdefinition(%lu) contact_id\n", *id);
+	free(seq_name);
+
 	/* do not free statement yet! */
 #endif
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_query_insert_or_update_contactdefinition_definition_add() end\n");





More information about the icinga-checkins mailing list