Date: Wednesday, December 7, 2005 @ 11:00:25
  Author: zsolt
    Path: /cvsroot/carob/libmysequoia

Modified: include/CarobMySQL.hpp (1.4 -> 1.5) include/Utils.hpp (1.2 ->
          1.3) src/CarobMySQL.cpp (1.5 -> 1.6) src/MySQLAPI.cpp (1.3 ->
          1.4) src/Utils.cpp (1.2 -> 1.3)

Error handling functions implementation


------------------------+
 include/CarobMySQL.hpp |    4 -
 include/Utils.hpp      |   99 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/CarobMySQL.cpp     |   36 +++++++++--------
 src/MySQLAPI.cpp       |   20 ++++++++-
 src/Utils.cpp          |   19 +++++++++
 5 files changed, 154 insertions(+), 24 deletions(-)


Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.4 
libmysequoia/include/CarobMySQL.hpp:1.5
--- libmysequoia/include/CarobMySQL.hpp:1.4     Tue Dec  6 09:37:37 2005
+++ libmysequoia/include/CarobMySQL.hpp Wed Dec  7 11:00:24 2005
@@ -212,9 +212,9 @@
   /**
    * Set the current mysql error to the specified one. The mysql error message 
will be filled from the error list.
    * @param errcode mysql error code
-   * @param sqlstate ansi sql specific error message
+   * @param sqlstate ansi sql specific error code
    */
-  void set_error(int errcode, const char *sqlstate);
+  void set_error(int errcode, int sqlstate);
 
   /**
    * Set the current mysql error to the specified one
Index: libmysequoia/include/Utils.hpp
diff -u libmysequoia/include/Utils.hpp:1.2 libmysequoia/include/Utils.hpp:1.3
--- libmysequoia/include/Utils.hpp:1.2  Tue Dec  6 09:25:35 2005
+++ libmysequoia/include/Utils.hpp      Wed Dec  7 11:00:24 2005
@@ -24,9 +24,104 @@
 
 #include <string.h>
 
+const char *error_sqlstate[]=
+{
+  "00000",
+  "HY000"
+};
+
+const int SQLT_OK = 0;
+const int SQLT_UNKNOWN = 1;
+
+const char *error_messages[]=
+{
+  "Unknown MySQL error",
+  "",
+  "",
+  "Can't connect to MySQL server on '%-.100s' (%d)",
+  "Can't create TCP/IP socket (%d)",
+  "Unknown MySQL server host '%-.100s' (%d)",
+  "MySQL server has gone away",
+  "Protocol mismatch; server version = %d, client version = %d",
+  "MySQL client ran out of memory",
+  "Wrong host info",
+  "",
+  "%-.100s via TCP/IP",
+  "Error in server handshake",
+  "Lost connection to MySQL server during query",
+  "Commands out of sync; you can't run this command now",
+  "",
+  "",
+  "",
+  "",
+  "Can't initialize character set %-.32s (path: %-.100s)",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "SSL connection error",
+  "",
+  "",
+  "Invalid use of null pointer",
+  "Statement not prepared",
+  "No data supplied for parameters in prepared statement",
+  "Data truncated",
+  "No parameters exist in the statement",
+  "Invalid parameter number",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "",
+  "Wrong or unknown protocol",
+  "Invalid connection handle",
+  "",
+  "Row retrieval was canceled by mysql_stmt_close() call",
+  "Attempt to read column without prior row fetch",
+  "Prepared statement contains no metadata",
+  "Attempt to read a row while there is no result set associated with the 
statement",
+  "This feature is not implemented yet",
+  ""
+};
+
+#define FREE_AND_NULL(p) if (p) {delete p; p=0;}
+
+/**
+ * Creates a duplicate of the null terminated string and return the pointer to 
it.
+ * @param src pointer to the source string
+ * @return a pointer to the duplicate string
+ */
 char *cstrdup(const char *src);
+
+/**
+ * Creates a duplicate of the string of given length and return the pointer to 
it.
+ * @param src pointer to the source string. Can contain null character.
+ * @param len the length of the string
+ * @return a pointer to the duplicate string
+ */
 char *cstrdup(const char *src, int len);
 
-#define FREE_AND_NULL(p) if (p) {delete p; p=0;}
+/**
+ * Returns a client error message associated with the error code.
+ * @param code MySQL client error code
+ * @return a the detailed error message or null if the error code was not a 
client error
+ */
+const char *get_error_msg(const int code);
+
+/**
+ * Converts the error code (usually returned by sequoia) to a mysql error code.
+ * @param code sequoia error code
+ * @return mapped MySQL error code
+ */
+int convert_errcode(const int code);
 
-#endif /*_UTILS_HPP*/
+#endif /* _UTILS_HPP */
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.5 libmysequoia/src/CarobMySQL.cpp:1.6
--- libmysequoia/src/CarobMySQL.cpp:1.5 Tue Dec  6 09:37:37 2005
+++ libmysequoia/src/CarobMySQL.cpp     Wed Dec  7 11:00:24 2005
@@ -22,6 +22,9 @@
 #include <CarobMySQL.hpp>
 #include <Utils.hpp>
 
+/* MySQL error messages */
+#include <errmsg.h>
+
 using namespace CarobNS;
 
 CarobMYSQL::CarobMYSQL (): connectionPtr(NULL), stmtPtr(NULL), 
liveResultPtr(NULL)
@@ -60,16 +63,14 @@
   
   if (unix_socket)
   {
-    //TODO error handling
-    //set_error(CR_NOT_IMPLEMENTED, unknown_sqlstate);
+    set_error(CR_NOT_IMPLEMENTED, SQLT_UNKNOWN);
     return false;
   }
 
   /* Must supply at least a user name, or connection to sequoia will fail */
   if (!user || !*user)
   {
-    //TODO error handling
-    //set_error(CR_NULL_POINTER, unknown_sqlstate);
+    set_error(CR_NULL_POINTER, SQLT_UNKNOWN);
     return false;
   }
 
@@ -96,7 +97,7 @@
     {
       //TODO handle not enough memory
       delete_connection();
-      set_connect_info(host, user, passwd, 0, port ? port : 25322);
+      set_connect_info(host, user, passwd, db, port ? port : 25322);
 
       connectionPtr = newConnectionPtr;
       stmtPtr = connectionPtr->createStatement();
@@ -106,7 +107,7 @@
   } 
   catch (CarobException &e)
   {
-    //set_error(0, toString(e.description).c_str(), 
toString(e.getSQLState).c_str());
+    //set_error(, toString(e.description).c_str(), 
toString(e.getSQLState).c_str());
     //TODO error handling
     return 0;
   }
@@ -121,9 +122,7 @@
       0, mysqlPtr->my.client_flag);
   else
   {
-    //set_error(CR_NULL_POINTER, unknown_sqlstate);
-    //TODO error handling
-    
+    set_error(CR_NULL_POINTER, SQLT_UNKNOWN);
     return false;
   }
 }
@@ -242,18 +241,21 @@
 }
 
 void
-CarobMYSQL::set_error(int errcode, const char *sqlstate)
+CarobMYSQL::set_error(int errcode, int sqlstate)
 {
-//  set_error(errcode, ER(errcode), sqlstate);
-  //TODO find the real error message
-  set_error(errcode, "", sqlstate);
+  set_error(errcode, error_messages[errcode-CR_MIN_ERROR], 
error_sqlstate[sqlstate]);
 }
 
 void
 CarobMYSQL::set_error(int errcode, const char *errmsg, const char *sqlstate)
 {
-  mysqlPtr->my.net.last_errno = errcode;
-  //strcpy(net->last_error, ER(errcode));
-  strcpy(mysqlPtr->my.net.last_error, errmsg);
-  strcpy(mysqlPtr->my.net.sqlstate, sqlstate);
+  mysqlPtr->my.net.last_errno = convert_errcode(errcode);
+  if (errmsg)
+    strcpy(mysqlPtr->my.net.last_error, errmsg);
+  else
+    *mysqlPtr->my.net.last_error = 0;
+  if (sqlstate)
+    strcpy(mysqlPtr->my.net.sqlstate, sqlstate);
+  else
+    *mysqlPtr->my.net.sqlstate = 0;
 }
Index: libmysequoia/src/MySQLAPI.cpp
diff -u libmysequoia/src/MySQLAPI.cpp:1.3 libmysequoia/src/MySQLAPI.cpp:1.4
--- libmysequoia/src/MySQLAPI.cpp:1.3   Tue Dec  6 09:25:35 2005
+++ libmysequoia/src/MySQLAPI.cpp       Wed Dec  7 11:00:24 2005
@@ -21,6 +21,8 @@
 
 #include <CarobMySQL.hpp>
 
+#include <Utils.hpp>
+
 /* Library initialization, shutdown */
 
 /* Gets or initializes a MYSQL structure. */
@@ -266,22 +268,34 @@
 
 /* Error handling */
 
+/* Returns the error number for the most recently invoked MySQL function. */
 unsigned int STDCALL
 mysql_errno (MYSQL * mysql)
 {
-       return 0;
+       if (mysql)
+    return mysql->net.last_errno;
+  else
+    return CR_NULL_POINTER;
 }
 
+/* Returns the error message for the most recently invoked MySQL function. */
 const char *STDCALL
 mysql_error (MYSQL * mysql)
 {
-       return 0;
+  if (mysql)
+    return mysql->net.last_error;
+  else
+    return error_messages[CR_NULL_POINTER-CR_MIN_ERROR];
 }
 
+/* Returns the SQLSTATE error code for the last error. */
 const char *STDCALL
 mysql_sqlstate (MYSQL * mysql)
 {
-       return 0;
+  if (mysql)
+    return mysql->net.sqlstate;
+  else
+    return error_sqlstate[SQLT_UNKNOWN];
 }
 
 unsigned int STDCALL
Index: libmysequoia/src/Utils.cpp
diff -u libmysequoia/src/Utils.cpp:1.2 libmysequoia/src/Utils.cpp:1.3
--- libmysequoia/src/Utils.cpp:1.2      Tue Dec  6 09:25:35 2005
+++ libmysequoia/src/Utils.cpp  Wed Dec  7 11:00:24 2005
@@ -21,6 +21,9 @@
 
 #include <Utils.hpp>
 
+/* MySQL include */
+#include <errmsg.h>
+
 char *cstrdup(const char *src)
 {
   return src ? cstrdup(src, strlen(src)) : 0;
@@ -38,3 +41,19 @@
   else
     return 0;
 }
+
+const char *get_error_msg(const int code)
+{
+  if (code >= CR_MIN_ERROR && code <= CR_MAX_ERROR)
+    return error_messages[code-CR_MIN_ERROR];
+  else
+    return error_messages[CR_MIN_ERROR];
+}
+
+int convert_errcode(const int code)
+{
+  if (code >= CR_MIN_ERROR && code <= CR_MAX_ERROR)
+    return code;
+  //TODO do more error conversion !!
+  return CR_MIN_ERROR;
+}

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to