From 704b9ff20b643de80ea4e87b593d5f548162ded5 Mon Sep 17 00:00:00 2001 From: Dan Langille Date: Tue, 23 Dec 2003 16:44:32 +0000 Subject: [PATCH] Add my_postgresql_max_length which caculates the maximum width for all values in a field. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@942 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/cats/postgresql.c | 68 ++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/bacula/src/cats/postgresql.c b/bacula/src/cats/postgresql.c index 2a3cb5736f..ae8c478738 100644 --- a/bacula/src/cats/postgresql.c +++ b/bacula/src/cats/postgresql.c @@ -328,29 +328,62 @@ POSTGRESQL_ROW my_postgresql_fetch_row(B_DB *mdb) return row; } +int my_postgresql_max_length(B_DB *mdb, int field_num) { + // + // for a given column, find the max length + // + int max_length; + int i; + int this_length; + + max_length = 0; + for (i = 0; i < mdb->num_rows; i++) { + if (PQgetisnull(mdb->result, i, field_num)) { + this_length = 4; // "NULL" + } else { + this_length = strlen(PQgetvalue(mdb->result, i, field_num)); + } + + if (max_length < this_length) { + max_length = this_length; + } + } + + return max_length; +} + POSTGRESQL_FIELD * my_postgresql_fetch_field(B_DB *mdb) { - mdb->field.name = PQfname (mdb->result, mdb->field_number); + int i; + + Dmsg0(50, "my_postgresql_fetch_field starts\n"); + if (mdb->fields == NULL) { + Dmsg1(50, "allocating space for %d fields\n", mdb->num_fields); + mdb->fields = (POSTGRESQL_FIELD *) + malloc(sizeof(POSTGRESQL_FIELD) * mdb->num_fields); + + for (i = 0; i < mdb->num_fields; i++) { + Dmsg1(50, "filling field %d\n", i); + mdb->fields[i].name = PQfname (mdb->result, i); - // I am not sure this returns the max width of the result set - mdb->field.max_length = PQfsize (mdb->result, mdb->field_number); + // I am not sure this returns the max width of the result set + mdb->fields[i].max_length = my_postgresql_max_length(mdb, i); - // I am not sure this returns what we can use - mdb->field.type = PQftype (mdb->result, mdb->field_number); + // I am not sure this returns what we can use + mdb->fields[i].type = PQftype (mdb->result, i); -// if (mdb->num_rows > 0) { -// Dmsg1(50, "asking for information on field '%d' type='%d' and IsNull=%d\n", -// mdb->field.flags = PQgetisnull(mdb->result, mdb->row_number, mdb->field_number); -// } - mdb->field.flags = 0; + mdb->fields[i].flags = 0; - Dmsg4(50, "my_postgresql_fetch_field finds field '%s' has length='%d' type='%d' and IsNull=%d\n", - mdb->field.name, mdb->field.max_length, mdb->field.type, mdb->field.flags); + Dmsg4(50, "my_postgresql_fetch_field finds field '%s' has length='%d' type='%d' and IsNull=%d\n", + mdb->fields[i].name, mdb->fields[i].max_length, mdb->fields[i].type, + mdb->fields[i].flags); + } // end for + } // end if - // increment this for the next time around - mdb->field_number++; + // increment field number for the next time around - return &mdb->field; + Dmsg0(50, "my_postgresql_fetch_field finishes\n"); + return &mdb->fields[mdb->field_number++]; } void my_postgresql_data_seek(B_DB *mdb, int row) @@ -407,6 +440,11 @@ void my_postgresql_free_result (B_DB *mdb) free(mdb->row); mdb->row = NULL; } + + if (mdb->fields) { + free(mdb->fields); + mdb->fields = NULL; + } } int my_postgresql_currval(B_DB *mdb, char *table_name) -- 2.39.5