Index: cdr/cdr_pgsql.c =================================================================== --- cdr/cdr_pgsql.c (revision 110609) +++ cdr/cdr_pgsql.c (working copy) @@ -66,6 +66,7 @@ char *name; char *type; int len; + char notnull; AST_RWLIST_ENTRY(columns) list; }; @@ -138,8 +139,17 @@ AST_RWLIST_TRAVERSE(&psql_columns, cur, list) { /* For fields not set, simply skip them */ ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0); - if (!value) + if (!value) { + if (cur->notnull) { + /* Field is NOT NULL, must include it anyway */ + LENGTHEN_BUF1(strlen(cur->name)); + lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", cur->name); + LENGTHEN_BUF2(3); + strcat(sql2, "'',"); + lensql2 += 3; + } continue; + } LENGTHEN_BUF1(strlen(cur->name)); lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", cur->name); @@ -445,13 +455,13 @@ conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword); if (PQstatus(conn) != CONNECTION_BAD) { char sqlcmd[256]; - char *fname, *ftype, *flen; + char *fname, *ftype, *flen, *fnotnull; int i, rows; ast_debug(1, "Successfully connected to PostgreSQL database.\n"); connected = 1; /* Query the columns */ - snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen from pg_class c, pg_attribute a, pg_type t where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", table); + snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull from pg_class c, pg_attribute a, pg_type t where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", table); result = PQexec(conn, sqlcmd); if (PQresultStatus(result) != PGRES_TUPLES_OK) { pgerror = PQresultErrorMessage(result); @@ -466,6 +476,7 @@ fname = PQgetvalue(result, i, 0); ftype = PQgetvalue(result, i, 1); flen = PQgetvalue(result, i, 2); + fnotnull = PQgetvalue(result, i, 3); ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype); cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2); if (cur) { @@ -474,6 +485,11 @@ cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1; strcpy(cur->name, fname); strcpy(cur->type, ftype); + if (*fnotnull == 't') { + cur->notnull = 1; + } else { + cur->notnull = 0; + } AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list); } }