From 5ac04f029f2876284e8af10275bdd63e75fc90e7 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Fri, 17 Jun 2016 16:39:51 -0400 Subject: Prepare Postgres code for NULL error fields --- include/urweb/urweb_cpp.h | 2 ++ src/c/urweb.c | 7 +++++++ src/postgres.sml | 12 ++++++------ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/urweb/urweb_cpp.h b/include/urweb/urweb_cpp.h index 48cfa784..0d5f5e0e 100644 --- a/include/urweb/urweb_cpp.h +++ b/include/urweb/urweb_cpp.h @@ -430,4 +430,6 @@ uw_Sqlcache_Value *uw_Sqlcache_check(struct uw_context *, uw_Sqlcache_Cache *, c void *uw_Sqlcache_store(struct uw_context *, uw_Sqlcache_Cache *, char **, uw_Sqlcache_Value *); void *uw_Sqlcache_flush(struct uw_context *, uw_Sqlcache_Cache *, char **); +int strcmp_nullsafe(const char *, const char *); + #endif diff --git a/src/c/urweb.c b/src/c/urweb.c index 286ec7be..afe8457b 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -4968,3 +4968,10 @@ void uw_Sqlcache_flush(uw_context ctx, uw_Sqlcache_Cache *cache, char **keys) { } pthread_rwlock_unlock(&cache->lockIn); } + +int strcmp_nullsafe(const char *str1, const char *str2) { + if (str1) + return strcmp(str1, str2); + else + return 1; +} diff --git a/src/postgres.sml b/src/postgres.sml index ddfe0ad6..404384d2 100644 --- a/src/postgres.sml +++ b/src/postgres.sml @@ -443,7 +443,7 @@ fun init {dbstring, prepared = ss, tables, views, sequences} = newline, newline, string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", - box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {", + box [string "if (!strcmp_nullsafe(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {", box [newline, string "PQclear(res);", newline, @@ -451,7 +451,7 @@ fun init {dbstring, prepared = ss, tables, views, sequences} = newline], string "}", newline, - string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40P01\")) {", + string "if (!strcmp_nullsafe(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40P01\")) {", box [newline, string "PQclear(res);", newline, @@ -629,7 +629,7 @@ fun queryCommon {loc, query, cols, doCols} = string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {", newline, - box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {", + box [string "if (!strcmp_nullsafe(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {", box [newline, string "PQclear(res);", newline, @@ -637,7 +637,7 @@ fun queryCommon {loc, query, cols, doCols} = newline], string "}", newline, - string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40P01\")) {", + string "if (!strcmp_nullsafe(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40P01\")) {", box [newline, string "PQclear(res);", newline, @@ -800,7 +800,7 @@ fun dmlCommon {loc, dml, mode} = string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", newline, - box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {", + box [string "if (!strcmp_nullsafe(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {", box [newline, string "PQclear(res);", newline, @@ -808,7 +808,7 @@ fun dmlCommon {loc, dml, mode} = newline], string "}", newline, - string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40P01\")) {", + string "if (!strcmp_nullsafe(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40P01\")) {", box [newline, string "PQclear(res);", newline, -- cgit v1.2.3