[icinga-checkins] icinga.org: icinga-core/mbrooks/cgis: idoutils: dump host contactgroups and parenthosts multiline mbuf as bulk inserts #1110

git at icinga.org git at icinga.org
Fri Jul 29 00:39:57 CEST 2011


Module: icinga-core
Branch: mbrooks/cgis
Commit: 4bd8fc1604aea4c52cbf1f5c7d36738429fa1cd4
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=4bd8fc1604aea4c52cbf1f5c7d36738429fa1cd4

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Fri Jul 22 20:19:00 2011 +0200

idoutils: dump host contactgroups and parenthosts multiline mbuf as bulk inserts #1110

refs #1110

---

 module/idoutils/src/dbhandlers.c |  168 ++++++++++++++++++++++++++++++++++---
 1 files changed, 154 insertions(+), 14 deletions(-)

diff --git a/module/idoutils/src/dbhandlers.c b/module/idoutils/src/dbhandlers.c
index aa5f2e4..ebd14ee 100644
--- a/module/idoutils/src/dbhandlers.c
+++ b/module/idoutils/src/dbhandlers.c
@@ -5520,6 +5520,30 @@ int ido2db_handle_hostdefinition(ido2db_idi *idi) {
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_hostdefinition() free es\n");
 
 	/* save parent hosts to db */
+        ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_hostdefinition() parent_hosts start\n");
+
+#ifdef USE_LIBDBI
+
+        /* build a multiple insert value array */
+        if(asprintf(&buf1, "INSERT INTO %s (instance_id, host_id, parent_host_object_id) VALUES ",
+                        ido2db_db_tablenames[IDO2DB_DBTABLE_HOSTPARENTHOSTS]
+                        )==-1)
+                buf1=NULL;
+#endif
+
+#ifdef USE_ORACLE /* Oracle ocilib specific */
+
+        /* build a multiple insert value array */
+        if(asprintf(&buf1, "INSERT INTO %s (id, instance_id, host_id, parent_host_object_id) SELECT seq_%s.nextval, x1, x2, x3 from (",
+                        ido2db_db_tablenames[IDO2DB_DBTABLE_HOSTPARENTHOSTS],
+                        ido2db_db_tablenames[IDO2DB_DBTABLE_HOSTPARENTHOSTS]
+                        )==-1)
+                buf1=NULL;
+
+#endif /* Oracle ocilib specific */
+
+        first=1;
+
 	mbuf = idi->mbuf[IDO2DB_MBUF_PARENTHOST];
 	for (x = 0; x < mbuf.used_lines; x++) {
 
@@ -5530,15 +5554,56 @@ int ido2db_handle_hostdefinition(ido2db_idi *idi) {
 		result = ido2db_get_object_id_with_insert(idi, IDO2DB_OBJECTTYPE_HOST,
 				mbuf.buffer[x], NULL, &member_id);
 
-		/* save entry to db */
-	        data[0] = (void *) &idi->dbinfo.instance_id;
-        	data[1] = (void *) &host_id;
-	        data[2] = (void *) &member_id;
+                buf=buf1; /* save this pointer for later free'ing */
+
+#ifdef USE_LIBDBI
+                if(asprintf(&buf1, "%s%s(%lu,%lu,%lu)",
+                                buf1,
+                                (first==1?"":","),
+                                idi->dbinfo.instance_id,
+                                host_id,
+                                member_id
+                                )==-1)
+                        buf1=NULL;
+#endif
+
+#ifdef USE_ORACLE /* Oracle ocilib specific */
+                if(first==1) {
+                        if(asprintf(&buf1, "%s SELECT %lu as x1, %lu as x2, %lu as x3 FROM DUAL ",
+                                        buf1,
+                                        idi->dbinfo.instance_id,
+                                        host_id,
+                                        member_id
+                                        )==-1)
+                                buf1=NULL;
+                } else {
+                        if(asprintf(&buf1, "%s UNION ALL SELECT %lu, %lu, %lu FROM DUAL ",
+                                        buf1,
+                                        idi->dbinfo.instance_id,
+                                        host_id,
+                                        member_id
+                                        )==-1)
+                                buf1=NULL;
+                }
+#endif /* Oracle ocilib specific */
 
-	        result = ido2db_query_insert_or_update_hostdefinition_parenthosts_add(idi, data);
+                free(buf);
+                first=0;
+
+        }
+
+#ifdef USE_ORACLE /* Oracle ocilib specific */
+
+        if(asprintf(&buf1, "%s)", buf1)==-1)
+                buf1=NULL;
+
+#endif /* Oracle ocilib specific */
+
+        if(first==0){
+                result=ido2db_db_query(idi, buf1);
 
 #ifdef USE_LIBDBI /* everything else will be libdbi */
-		dbi_result_free(idi->dbinfo.dbi_result);
+                dbi_result_free(idi->dbinfo.dbi_result);
 #endif
 
 #ifdef USE_PGSQL /* pgsql */
@@ -5547,12 +5612,41 @@ int ido2db_handle_hostdefinition(ido2db_idi *idi) {
 
 #ifdef USE_ORACLE /* Oracle ocilib specific */
 
+        /* statement handle not re-binded */
+        OCI_StatementFree(idi->dbinfo.oci_statement);
 
 #endif /* Oracle ocilib specific */
 
-	}
+        }
+
+        free(buf1);
+
 
 	/* save contact groups to db */
+        ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_hostdefinition() parent_hosts start\n");
+
+#ifdef USE_LIBDBI
+
+        /* build a multiple insert value array */
+        if(asprintf(&buf1, "INSERT INTO %s (instance_id, host_id, contactgroup_object_id) VALUES ",
+                        ido2db_db_tablenames[IDO2DB_DBTABLE_HOSTCONTACTGROUPS]
+                        )==-1)
+                buf1=NULL;
+#endif
+
+#ifdef USE_ORACLE /* Oracle ocilib specific */
+
+        /* build a multiple insert value array */
+        if(asprintf(&buf1, "INSERT INTO %s (id, instance_id, host_id, contactgroup_object_id) SELECT seq_%s.nextval, x1, x2, x3 from (",
+                        ido2db_db_tablenames[IDO2DB_DBTABLE_HOSTCONTACTGROUPS],
+                        ido2db_db_tablenames[IDO2DB_DBTABLE_HOSTCONTACTGROUPS]
+                        )==-1)
+                buf1=NULL;
+
+#endif /* Oracle ocilib specific */
+
+        first=1;
+
 	mbuf = idi->mbuf[IDO2DB_MBUF_CONTACTGROUP];
 	for (x = 0; x < mbuf.used_lines; x++) {
 
@@ -5564,15 +5658,55 @@ int ido2db_handle_hostdefinition(ido2db_idi *idi) {
 				IDO2DB_OBJECTTYPE_CONTACTGROUP, mbuf.buffer[x], NULL,
 				&member_id);
 
-                /* save entry to db */
-                data[0] = (void *) &idi->dbinfo.instance_id;
-                data[1] = (void *) &host_id;
-                data[2] = (void *) &member_id;
+                buf=buf1; /* save this pointer for later free'ing */
+
+#ifdef USE_LIBDBI
+                if(asprintf(&buf1, "%s%s(%lu,%lu,%lu)",
+                                buf1,
+                                (first==1?"":","),
+                                idi->dbinfo.instance_id,
+                                host_id,
+                                member_id
+                                )==-1)
+                        buf1=NULL;
+#endif
+
+#ifdef USE_ORACLE /* Oracle ocilib specific */
+                if(first==1) {
+                        if(asprintf(&buf1, "%s SELECT %lu as x1, %lu as x2, %lu as x3 FROM DUAL ",
+                                        buf1,
+                                        idi->dbinfo.instance_id,
+                                	host_id,
+                                	member_id
+                                        )==-1)
+                                buf1=NULL;
+                } else {
+                        if(asprintf(&buf1, "%s UNION ALL SELECT %lu, %lu, %lu FROM DUAL ",
+                                        buf1,
+                                        idi->dbinfo.instance_id,
+                                	host_id,
+                                	member_id
+                                        )==-1)
+                                buf1=NULL;
+                }
+#endif /* Oracle ocilib specific */
 
-                result = ido2db_query_insert_or_update_hostdefinition_contactgroups_add(idi, data);
+                free(buf);
+                first=0;
+        }
+
+#ifdef USE_ORACLE /* Oracle ocilib specific */
+
+        if(asprintf(&buf1, "%s)", buf1)==-1)
+                buf1=NULL;
+
+#endif /* Oracle ocilib specific */
+
+        if(first==0){
+                result=ido2db_db_query(idi, buf1);
 
 #ifdef USE_LIBDBI /* everything else will be libdbi */
-		dbi_result_free(idi->dbinfo.dbi_result);
+                dbi_result_free(idi->dbinfo.dbi_result);
 #endif
 
 #ifdef USE_PGSQL /* pgsql */
@@ -5581,9 +5715,15 @@ int ido2db_handle_hostdefinition(ido2db_idi *idi) {
 
 #ifdef USE_ORACLE /* Oracle ocilib specific */
 
+        /* statement handle not re-binded */
+        OCI_StatementFree(idi->dbinfo.oci_statement);
 
 #endif /* Oracle ocilib specific */
-	}
+
+        }
+
+        free(buf1);
+
 
 	/* save contacts to db */
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_handle_hostdefinition() host_contacts start\n");





More information about the icinga-checkins mailing list