summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2011-10-07 17:49:39 +0200
committerDavid Sommerseth <davids@redhat.com>2011-10-07 18:11:06 +0200
commita30e5a76d3e0c5096d729cb3929e5297d34dcf1d (patch)
tree9cb368110af827b8d15dda9505af6cb5bec250f2
parent5ce546f047f30d40b52ac647a5012a7f09b7e30d (diff)
downloadrteval-a30e5a76d3e0c5096d729cb3929e5297d34dcf1d.tar.gz
rteval-a30e5a76d3e0c5096d729cb3929e5297d34dcf1d.tar.xz
rteval-a30e5a76d3e0c5096d729cb3929e5297d34dcf1d.zip
Moved PostgreSQL related array function into pgsql.c
This is to put all PostgreSQL related functions in one place. Using the init_xmlparser() the function needed to format arrays are provided to the xmlparser code paths. Signed-off-by: David Sommerseth <davids@redhat.com>
-rw-r--r--server/parser/pgsql.c65
-rw-r--r--server/parser/xmlparser.c63
-rw-r--r--server/parser/xmlparser.h4
3 files changed, 77 insertions, 55 deletions
diff --git a/server/parser/pgsql.c b/server/parser/pgsql.c
index 44032e5..8ea5d08 100644
--- a/server/parser/pgsql.c
+++ b/server/parser/pgsql.c
@@ -44,6 +44,14 @@
#include <log.h>
#include <statuses.h>
+/** forward declaration, to be able to setup dbhelper_func pointers */
+static char * pgsql_BuildArray(LogContext *log, xmlNode *sql_n);
+
+/** Helper functions the xmlparser might beed */
+static dbhelper_func pgsql_helpers = {
+ .dbh_FormatArray = &(pgsql_BuildArray)
+};
+
/**
* Connect to a database, based on the given configuration
*
@@ -106,7 +114,7 @@ dbconn *db_connect(eurephiaVALUES *cfg, unsigned int id, LogContext *log) {
if( dbr ) {
PQclear(dbr);
}
-
+ init_xmlparser(&pgsql_helpers);
return ret;
}
@@ -414,6 +422,61 @@ eurephiaVALUES *pgsql_INSERT(dbconn *dbc, xmlDoc *sqldoc) {
return res;
}
+/**
+ * @copydoc sqldataValueArray()
+ */
+static char * pgsql_BuildArray(LogContext *log, xmlNode *sql_n) {
+ char *ret = NULL, *ptr = NULL;
+ xmlNode *node = NULL;
+ size_t retlen = 0;
+
+ ret = malloc_nullsafe(log, 2);
+ if( ret == NULL ) {
+ writelog(log, LOG_ERR,
+ "Failed to allocate memory for a new PostgreSQL array");
+ return NULL;
+ }
+ strncat(ret, "{", 1);
+
+ /* Iterate all ./value/value elements and build up a PostgreSQL specific array */
+ foreach_xmlnode(sql_n->children, node) {
+ if( (node->type != XML_ELEMENT_NODE)
+ || xmlStrcmp(node->name, (xmlChar *) "value") != 0 ) {
+ // Skip uninteresting nodes
+ continue;
+ }
+ ptr = sqldataValueHash(log, node);
+ if( ptr ) {
+ retlen += strlen(ptr) + 4;
+ ret = realloc(ret, retlen);
+ if( ret == NULL ) {
+ writelog(log, LOG_ERR,
+ "Failed to allocate memory to expand "
+ "array to include '%s'", ptr);
+ free_nullsafe(ret);
+ free_nullsafe(ptr);
+ return NULL;
+ }
+ /* Newer PostgreSQL servers expects numbers to be without quotes */
+ if( isNumber(ptr) == 0 ) {
+ /* Data is a string */
+ strncat(ret, "'", 1);
+ strncat(ret, ptr, strlen(ptr));
+ strncat(ret, "',", 2);
+ } else {
+ /* Data is a number */
+ strncat(ret, ptr, strlen(ptr));
+ strncat(ret, ",", 1);
+ }
+ free_nullsafe(ptr);
+ }
+ }
+ /* Replace the last comma with a close-array marker */
+ ret[strlen(ret)-1] = '}';
+ ret[strlen(ret)] = 0;
+ return ret;
+}
+
/**
* Start an SQL transaction (SQL BEGIN)
diff --git a/server/parser/xmlparser.c b/server/parser/xmlparser.c
index 0d6e837..0ff6e72 100644
--- a/server/parser/xmlparser.c
+++ b/server/parser/xmlparser.c
@@ -108,7 +108,7 @@ int isNumber(const char * str)
*/
void init_xmlparser(dbhelper_func const * dbhelpers)
{
- xmlparser_dbhelpers = dbhelpers;
+ xmlparser_dbhelpers = dbhelpers;
}
@@ -204,7 +204,7 @@ xmlDoc *parseToSQLdata(LogContext *log, xsltStylesheet *xslt, xmlDoc *indata_d,
* @return Returns a pointer to a new buffer containing the value on success, otherwise NULL.
* This memory buffer must be free'd after usage.
*/
-static char *sqldataValueHash(LogContext *log, xmlNode *sql_n) {
+char * sqldataValueHash(LogContext *log, xmlNode *sql_n) {
const char *hash = NULL, *isnull = NULL;
SHA1Context shactx;
uint8_t shahash[SHA1_HASH_SIZE];
@@ -250,7 +250,7 @@ static char *sqldataValueHash(LogContext *log, xmlNode *sql_n) {
/**
* Extract the content of a //sqldata/records/record/value[@type='array']/value node set
- * and format it as an PostgreSQL array
+ * and format it in suitable array format for the database backend.
*
* @param log Log context
* @param sql_n sqldata values node containing the value to extract and format as an array.
@@ -260,55 +260,12 @@ static char *sqldataValueHash(LogContext *log, xmlNode *sql_n) {
*/
static char * sqldataValueArray(LogContext *log, xmlNode *sql_n)
{
- char *ret = NULL, *ptr = NULL;
- xmlNode *node = NULL;
- size_t retlen = 0;
-
- ret = malloc_nullsafe(log, 2);
- if( ret == NULL ) {
- writelog(log, LOG_ERR,
- "Failed to allocate memory for a new PostgreSQL array");
- return NULL;
- }
- strncat(ret, "{", 1);
+ if( xmlparser_dbhelpers == NULL ) {
+ writelog(log, LOG_ERR, "Programming error: xmlparser is not initialised");
+ return NULL;
+ }
- /* Iterate all ./value/value elements and build up a PostgreSQL specific array */
- foreach_xmlnode(sql_n->children, node) {
- if( (node->type != XML_ELEMENT_NODE)
- || xmlStrcmp(node->name, (xmlChar *) "value") != 0 ) {
- // Skip uninteresting nodes
- continue;
- }
- ptr = sqldataValueHash(log, node);
- if( ptr ) {
- retlen += strlen(ptr) + 4;
- ret = realloc(ret, retlen);
- if( ret == NULL ) {
- writelog(log, LOG_ERR,
- "Failed to allocate memory to expand "
- "array to include '%s'", ptr);
- free_nullsafe(ret);
- free_nullsafe(ptr);
- return NULL;
- }
- /* Newer PostgreSQL servers expects numbers to be without quotes */
- if( isNumber(ptr) == 0 ) {
- /* Data is a string */
- strncat(ret, "'", 1);
- strncat(ret, ptr, strlen(ptr));
- strncat(ret, "',", 2);
- } else {
- /* Data is a number */
- strncat(ret, ptr, strlen(ptr));
- strncat(ret, ",", 1);
- }
- free_nullsafe(ptr);
- }
- }
- /* Replace the last comma with a close-array marker */
- ret[strlen(ret)-1] = '}';
- ret[strlen(ret)] = 0;
- return ret;
+ return xmlparser_dbhelpers->dbh_FormatArray(log, sql_n);
}
@@ -367,7 +324,7 @@ int sqldataGetFid(LogContext *log, xmlNode *sql_n, const char *fname) {
if( xmlparser_dbhelpers == NULL ) {
writelog(log, LOG_ERR, "Programming error: xmlparser is not initialised");
- return NULL;
+ return -2;
}
if( !sql_n || (xmlStrcmp(sql_n->name, (xmlChar *) "sqldata") != 0) ) {
@@ -546,7 +503,7 @@ int sqldataGetRequiredSchemaVer(LogContext *log, xmlNode *sqldata_root)
if( xmlparser_dbhelpers == NULL ) {
writelog(log, LOG_ERR, "Programming error: xmlparser is not initialised");
- return NULL;
+ return -1;
}
if( !sqldata_root || (xmlStrcmp(sqldata_root->name, (xmlChar *) "sqldata") != 0) ) {
diff --git a/server/parser/xmlparser.h b/server/parser/xmlparser.h
index f2ce057..79c79a1 100644
--- a/server/parser/xmlparser.h
+++ b/server/parser/xmlparser.h
@@ -44,10 +44,11 @@ typedef struct {
* Database specific helper functions
*/
typedef struct {
- const char *(*dbh_FormatArray)(LogContext *log, xmlNode *sql_n); /** Formats data as arrays */
+ char *(*dbh_FormatArray)(LogContext *log, xmlNode *sql_n); /** Formats data as arrays */
} dbhelper_func;
void init_xmlparser(dbhelper_func const * dbhelpers);
+char * sqldataValueHash(LogContext *log, xmlNode *sql_n);
xmlDoc *parseToSQLdata(LogContext *log, xsltStylesheet *xslt, xmlDoc *indata_d, parseParams *params);
char *sqldataExtractContent(LogContext *log, xmlNode *sql_n);
int sqldataGetFid(LogContext *log, xmlNode *sqld, const char *fname);
@@ -55,4 +56,5 @@ char *sqldataGetValue(LogContext *log, xmlDoc *sqld, const char *fname, int reci
xmlDoc *sqldataGetHostInfo(LogContext *log, xsltStylesheet *xslt, xmlDoc *summaryxml,
int syskey, char **hostname, char **ipaddr);
int sqldataGetRequiredSchemaVer(LogContext *log, xmlNode *sqldata_root);
+
#endif