Clean up error messages for consistency.

Wed, 26 Nov 2008 10:43:45 +0000

author
nrich@ii.net
date
Wed, 26 Nov 2008 10:43:45 +0000
changeset 4
c50b0e6f25d6
parent 3
b61020ca4753
child 5
e66b88e013ca

Clean up error messages for consistency.

DBI.lua file | annotate | diff | comparison | revisions
dbd/common.h file | annotate | diff | comparison | revisions
dbd/mysql/connection.c file | annotate | diff | comparison | revisions
dbd/mysql/statement.c file | annotate | diff | comparison | revisions
dbd/postgresql/connection.c file | annotate | diff | comparison | revisions
dbd/postgresql/statement.c file | annotate | diff | comparison | revisions
dbd/sqlite3/connection.c file | annotate | diff | comparison | revisions
dbd/sqlite3/statement.c file | annotate | diff | comparison | revisions
--- a/DBI.lua	Wed Nov 26 10:01:03 2008 +0000
+++ b/DBI.lua	Wed Nov 26 10:43:45 2008 +0000
@@ -56,6 +56,8 @@
     return connection_class.New(name, username, password, host, port)
 end
 
+-- Help function to do prepare and execute in 
+-- a single step
 function Do(dbh, sql, ...)
     local sth,err = dbh:prepare(sql)
 
--- a/dbd/common.h	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/common.h	Wed Nov 26 10:43:45 2008 +0000
@@ -73,7 +73,7 @@
 
 /*
  *
- * Describes SQL to Lua API conversions
+ * Describes SQL to Lua API type conversions
  *
  */
 
@@ -87,3 +87,27 @@
     LUA_PUSH_MAX
 } lua_push_type_t;
 
+/*
+ *
+ * Common error strings
+ * defined here for consistency in driver implementations
+ *
+ */
+
+#define	DBI_ERR_CONNECTION_FAILED   "Failed to connect to database: %s"
+#define DBI_ERR_DB_UNAVAILABLE	    "Database not available"
+#define DBI_ERR_EXECUTE_INVALID	    "Execute called on a closed or invalid statement"
+#define DBI_ERR_EXECUTE_FAILED	    "Execute failed %s"
+#define DBI_ERR_FETCH_INVALID	    "Fetch called on a closed or invalid statement"
+#define DBI_ERR_FETCH_FAILED	    "Fetch failed %s"
+#define DBI_ERR_PARAM_MISCOUNT	    "Statement expected %d parameters but received %d"
+#define	DBI_ERR_BINDING_UNKNOWN	    "Binding unknown or unsupported type"
+#define DBI_ERR_BINDING_PARAMS	    "Error binding statement parameters: %s"
+#define DBI_ERR_BINDING_EXEC	    "Error executing statement parameters: %s"
+#define DBI_ERR_FETCH_NO_EXECUTE    "Fetch called before execute"
+#define DBI_ERR_BINDING_RESULTS	    "Error binding statement results: %s"
+#define DBI_ERR_UNKNOWN_PUSH	    "Unknown push type in result set"
+#define DBI_ERR_ALLOC_STATEMENT	    "Error allocating statement handle: %s"
+#define DBI_ERR_PREP_STATEMENT	    "Error preparing statement handle: %s"
+#define DBI_ERR_INVALID_PORT	    "Invalid port: %d"
+#define DBI_ERR_ALLOC_RESULT	    "Error allocating result set: %s"
--- a/dbd/mysql/connection.c	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/mysql/connection.c	Wed Nov 26 10:43:45 2008 +0000
@@ -44,7 +44,7 @@
 
     if (!mysql_real_connect(conn->mysql, host, user, password, db, port, unix_socket, client_flag)) {
 	lua_pushnil(L);
-	lua_pushfstring(L, "Failed to connect to database: %s", mysql_error(conn->mysql));
+	lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, mysql_error(conn->mysql));
 	return 2;
     }
 
@@ -97,7 +97,7 @@
     }
 
     lua_pushnil(L);    
-    lua_pushstring(L, "Database not available");    
+    lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE);    
 
     return 2;
 }
--- a/dbd/mysql/statement.c	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/mysql/statement.c	Wed Nov 26 10:43:45 2008 +0000
@@ -64,7 +64,7 @@
 
     if (!statement->stmt) {
 	lua_pushboolean(L, 0);
-	lua_pushstring(L, "execute called on a closed or invalid statement");
+	lua_pushstring(L, DBI_ERR_EXECUTE_INVALID);
 	return 2;
     }
 
@@ -76,7 +76,7 @@
          * and the client library will segfault if these do no match
          */ 
 	lua_pushboolean(L, 0);
-	lua_pushfstring(L, "Statement expected %d paramaters but received %d", expected_params, num_bind_params); 
+	lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); 
 	return 2;
     }
 
@@ -117,18 +117,18 @@
 		break;
 
 	    default:
-		error_message = "Binding unknown or unsupported type"; 
+		error_message = DBI_ERR_BINDING_UNKNOWN; 
 		goto cleanup;
 	}
     }
 
     if (mysql_stmt_bind_param(statement->stmt, bind)) {
-	error_message = "Error binding statement parameters: %s";
+	error_message = DBI_ERR_BINDING_PARAMS;
 	goto cleanup;
     }
 
     if (mysql_stmt_execute(statement->stmt)) {
-	error_message = "Error executing statement: %s";
+	error_message = DBI_ERR_BINDING_EXEC;
 	goto cleanup;
     }
 
@@ -157,12 +157,12 @@
     const char *error_message = NULL;
 
     if (!statement->stmt) {
-	luaL_error(L, "fetch called on a closed or invalid statement");
+	luaL_error(L, DBI_ERR_FETCH_INVALID);
 	return 0;
     }
 
     if (!statement->metadata) {
-	luaL_error(L, "fetch called before execute");
+	luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE);
 	return 0;
     }
 
@@ -194,7 +194,7 @@
 	}
 
 	if (mysql_stmt_bind_result(statement->stmt, bind)) {
-	    error_message = "Error binding results: %s";
+	    error_message = DBI_ERR_BINDING_RESULTS;
 	    goto cleanup;
 	}
 
@@ -237,7 +237,7 @@
 			LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer));
 		    }
 		} else {
-		    luaL_error(L, "Unknown push type in result set");
+		    luaL_error(L, DBI_ERR_UNKNOWN_PUSH);
 		}
 	    }
 	} else {
@@ -297,13 +297,13 @@
 
     if (!stmt) {
 	lua_pushnil(L);
-	lua_pushfstring(L, "Error allocating statement handle: %s", mysql_error(conn->mysql));
+	lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql));
 	return 2;
     }
 
     if (mysql_stmt_prepare(stmt, sql_query, sql_len)) {
 	lua_pushnil(L);
-	lua_pushfstring(L, "Error preparing statement handle: %s", mysql_stmt_error(stmt));
+	lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt));
 	return 2;
     }
 
--- a/dbd/postgresql/connection.c	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/postgresql/connection.c	Wed Nov 26 10:43:45 2008 +0000
@@ -31,7 +31,7 @@
 		snprintf(portbuf, sizeof(portbuf), "%d", pport);
 		port = portbuf;
 	    } else {
-		luaL_error(L, "Invalid port %d", pport);
+		luaL_error(L, DBI_ERR_INVALID_PORT, pport);
 	    }
 	}
     case 4: 
@@ -55,7 +55,7 @@
 
     if (PQstatus(conn->postgresql) != CONNECTION_OK) {
 	lua_pushnil(L);
-	lua_pushfstring(L, "Failed to connect to database: %s", PQerrorMessage(conn->postgresql));
+	lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, PQerrorMessage(conn->postgresql));
 	return 2;
     }
 
@@ -111,7 +111,7 @@
     }
 
     lua_pushnil(L);    
-    lua_pushstring(L, "Database not available");
+    lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE);
     return 2;
 }
 
--- a/dbd/postgresql/statement.c	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/postgresql/statement.c	Wed Nov 26 10:43:45 2008 +0000
@@ -181,14 +181,14 @@
 
     if (!result) {
 	lua_pushboolean(L, 0);
-	lua_pushstring(L, "Unable to allocate result handle");
+	lua_pushfstring(L, DBI_ERR_ALLOC_RESULT,  PQerrorMessage(statement->postgresql));
 	return 2;
     }
     
     status = PQresultStatus(result);
     if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
 	lua_pushboolean(L, 0);
-	lua_pushfstring(L, "Unable to execute statment: %s", PQresultErrorMessage(result));
+	lua_pushfstring(L, DBI_ERR_BINDING_EXEC, PQresultErrorMessage(result));
 	return 2;
     }
     
@@ -209,7 +209,7 @@
     int num_columns;
 
     if (!statement->result) {
-	luaL_error(L, "fetch called on a closed or invalid statement");
+	luaL_error(L, DBI_ERR_FETCH_INVALID);
 	return 0;
     }
 
@@ -284,7 +284,7 @@
                     LUA_PUSH_ARRAY_BOOL(d, val);
                 }
             } else {
-                luaL_error(L, "Unknown push type in result set");
+                luaL_error(L, DBI_ERR_UNKNOWN_PUSH);
             }
 	}
     }
@@ -339,7 +339,7 @@
 
     if (!result) {
 	lua_pushnil(L);
-	lua_pushstring(L, "Unable to allocate prepare result handle");
+	lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, PQerrorMessage(statement->postgresql));
 	return 2;
     }
     
@@ -349,7 +349,7 @@
 	PQclear(result);
 
 	lua_pushnil(L);
-	lua_pushfstring(L, "Unable to prepare statment: %s", err_string);
+	lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, err_string);
 	return 2;
     }
 
--- a/dbd/sqlite3/connection.c	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/sqlite3/connection.c	Wed Nov 26 10:43:45 2008 +0000
@@ -22,7 +22,7 @@
 
     if (sqlite3_open(db, &conn->sqlite) != SQLITE_OK) {
 	lua_pushnil(L);
-	lua_pushfstring(L, "Failed to connect to database: %s", sqlite3_errmsg(conn->sqlite));
+	lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, sqlite3_errmsg(conn->sqlite));
 	return 2;
     }
 
@@ -76,7 +76,7 @@
     }
 
     lua_pushnil(L);    
-    lua_pushstring(L, "Connection not available");
+    lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE);
     return 2;
 }
 
--- a/dbd/sqlite3/statement.c	Wed Nov 26 10:01:03 2008 +0000
+++ b/dbd/sqlite3/statement.c	Wed Nov 26 10:43:45 2008 +0000
@@ -74,7 +74,7 @@
 
     if (!statement->stmt) {
 	lua_pushboolean(L, 0);
-	lua_pushstring(L, "execute called on a closed or invalid handle");
+	lua_pushstring(L, DBI_ERR_EXECUTE_INVALID);
 	return 2;
     }
 
@@ -85,7 +85,7 @@
      */
     if (sqlite3_reset(statement->stmt) != SQLITE_OK) {
 	lua_pushboolean(L, 0);
-	lua_pushfstring(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite));
+	lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite));
 	return 2;
     }
 
@@ -110,12 +110,17 @@
 	    break;
     }   
 
-    if (err || step(statement) == 0) {
+    if (err) {
 	lua_pushboolean(L, 0);
-	lua_pushfstring(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite));
+	lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite));
 	return 2;
     }
 
+    if (!step(statement)) {
+	lua_pushboolean(L, 0);
+	lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite));
+    }
+
     lua_pushboolean(L, 1);
     return 1;
 }
@@ -128,7 +133,7 @@
     int num_columns;
 
     if (!statement->stmt) {
-	luaL_error(L, "fetch called on a closed or invalid handle");
+	luaL_error(L, DBI_ERR_FETCH_INVALID);
 	return 0;
     }
 
@@ -192,7 +197,7 @@
                     LUA_PUSH_ARRAY_BOOL(d, val);
                 }
             } else {
-                luaL_error(L, "Unknown push type in result set");
+                luaL_error(L, DBI_ERR_UNKNOWN_PUSH);
             }
 	}
     } else {
@@ -207,7 +212,7 @@
 	    /* 
 	     * reset needs to be called to retrieve the 'real' error message
 	     */
-	    luaL_error(L, "Failed to fetch statement: %s", sqlite3_errmsg(statement->sqlite));
+	    luaL_error(L, DBI_ERR_FETCH_FAILED, sqlite3_errmsg(statement->sqlite));
 	}
     }
 
@@ -248,7 +253,7 @@
 
     if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) {
 	lua_pushnil(L);
-	lua_pushfstring(L, "Failed to prepare statement: %s", sqlite3_errmsg(statement->sqlite));	
+	lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite));	
 	return 2;
     } 
 

mercurial