[syslog-ng] [PATCH (3.4) 3/3] afsql: Move the INSERT construction out of insert_db()

Gergely Nagy algernon at balabit.hu
Fri Apr 27 13:53:16 CEST 2012


To improve the flow of the afsql_dd_insert_db() function, moved the
code to construct the insert statement out into a new function:
afsql_dd_construct_query().

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 modules/afsql/afsql.c |  125 +++++++++++++++++++++++++------------------------
 1 file changed, 65 insertions(+), 60 deletions(-)

diff --git a/modules/afsql/afsql.c b/modules/afsql/afsql.c
index 4f5ffec..bed2ca9 100644
--- a/modules/afsql/afsql.c
+++ b/modules/afsql/afsql.c
@@ -769,6 +769,68 @@ afsql_dd_insert_fail_handler(AFSqlDestDriver *self, LogMessage *msg,
   return TRUE;
 }
 
+static GString *
+afsql_dd_construct_query(AFSqlDestDriver *self, GString *table,
+                         LogMessage *msg)
+{
+  GString *value;
+  GString *query_string;
+  gint i;
+
+  value = g_string_sized_new(256);
+  query_string = g_string_sized_new(512);
+
+  g_string_printf(query_string, "INSERT INTO %s (", table->str);
+  for (i = 0; i < self->fields_len; i++)
+    {
+      g_string_append(query_string, self->fields[i].name);
+      if (i != self->fields_len - 1)
+        g_string_append(query_string, ", ");
+    }
+  g_string_append(query_string, ") VALUES (");
+
+  for (i = 0; i < self->fields_len; i++)
+    {
+      gchar *quoted;
+
+      if (self->fields[i].value == NULL)
+        {
+          /* the config used the 'default' value for this column -> the fields[i].value is NULL, use SQL default */
+          g_string_append(query_string, "DEFAULT");
+        }
+      else
+        {
+          log_template_format(self->fields[i].value, msg, &self->template_options, LTZ_SEND, self->seq_num, NULL, value);
+
+          if (self->null_value && strcmp(self->null_value, value->str) == 0)
+            {
+              g_string_append(query_string, "NULL");
+            }
+          else
+            {
+              dbi_conn_quote_string_copy(self->dbi_ctx, value->str, &quoted);
+              if (quoted)
+                {
+                  g_string_append(query_string, quoted);
+                  free(quoted);
+                }
+              else
+                {
+                  g_string_append(query_string, "''");
+                }
+            }
+        }
+
+      if (i != self->fields_len - 1)
+        g_string_append(query_string, ", ");
+    }
+  g_string_append(query_string, ")");
+
+  g_string_free(value, TRUE);
+
+  return query_string;
+}
+
 /**
  * afsql_dd_insert_db:
  *
@@ -780,10 +842,9 @@ afsql_dd_insert_fail_handler(AFSqlDestDriver *self, LogMessage *msg,
 static gboolean
 afsql_dd_insert_db(AFSqlDestDriver *self)
 {
-  GString *table, *query_string, *value;
+  GString *table, *query_string;
   LogMessage *msg;
   gboolean success;
-  gint i;
   LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
 
   afsql_dd_connect(self);
@@ -827,67 +888,12 @@ afsql_dd_insert_db(AFSqlDestDriver *self)
       return afsql_dd_insert_fail_handler(self, msg, &path_options);
     }
 
-  value = g_string_sized_new(256);
-  query_string = g_string_sized_new(512);
-
-  g_string_printf(query_string, "INSERT INTO %s (", table->str);
-  for (i = 0; i < self->fields_len; i++)
-    {
-      g_string_append(query_string, self->fields[i].name);
-      if (i != self->fields_len - 1)
-        g_string_append(query_string, ", ");
-    }
-  g_string_append(query_string, ") VALUES (");
-
-  for (i = 0; i < self->fields_len; i++)
-    {
-      gchar *quoted;
-
-      if (self->fields[i].value == NULL)
-        {
-          /* the config used the 'default' value for this column -> the fields[i].value is NULL, use SQL default */
-          g_string_append(query_string, "DEFAULT");
-        }
-      else
-        {
-          log_template_format(self->fields[i].value, msg, &self->template_options, LTZ_SEND, self->seq_num, NULL, value);
-
-          if (self->null_value && strcmp(self->null_value, value->str) == 0)
-            {
-              g_string_append(query_string, "NULL");
-            }
-          else
-            {
-              dbi_conn_quote_string_copy(self->dbi_ctx, value->str, &quoted);
-              if (quoted)
-                {
-                  g_string_append(query_string, quoted);
-                  free(quoted);
-                }
-              else
-                {
-                  g_string_append(query_string, "''");
-                }
-            }
-        }
-
-      if (i != self->fields_len - 1)
-        g_string_append(query_string, ", ");
-    }
-  g_string_append(query_string, ")");
-
-  /* we have the INSERT statement ready in query_string */
+  query_string = afsql_dd_construct_query(self, table, msg);
 
   if (self->flush_lines_queued == 0 && !afsql_dd_begin_txn(self))
     return FALSE;
 
-  success = TRUE;
-  if (!afsql_dd_run_query(self, query_string->str, FALSE, NULL))
-    {
-      /* error running INSERT on an already validated table, too bad. Try to reconnect. Maybe that helps. */
-      success = FALSE;
-    }
-
+  success = afsql_dd_run_query(self, query_string->str, FALSE, NULL);
   if (success && self->flush_lines_queued != -1)
     {
       self->flush_lines_queued++;
@@ -897,7 +903,6 @@ afsql_dd_insert_db(AFSqlDestDriver *self)
     }
 
   g_string_free(table, TRUE);
-  g_string_free(value, TRUE);
   g_string_free(query_string, TRUE);
 
   msg_set_context(NULL);
-- 
1.7.10




More information about the syslog-ng mailing list