Add __tostring method to connection and statement objects.

Tue, 01 Sep 2009 13:15:02 +0000

author
nrich@ii.net
date
Tue, 01 Sep 2009 13:15:02 +0000
changeset 32
03ed0ca09837
parent 31
999ef93f0dbc
child 33
6c64c45e7d8f

Add __tostring method to connection and statement objects.

dbd/db2/connection.c file | annotate | diff | comparison | revisions
dbd/db2/statement.c file | annotate | diff | comparison | revisions
dbd/mysql/connection.c file | annotate | diff | comparison | revisions
dbd/mysql/statement.c file | annotate | diff | comparison | revisions
dbd/oracle/connection.c file | annotate | diff | comparison | revisions
dbd/oracle/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/dbd/db2/connection.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/db2/connection.c	Tue Sep 01 13:15:02 2009 +0000
@@ -230,6 +230,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int connection_tostring(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_DB2_CONNECTION);
+    
+    lua_pushfstring(L, "%s: %p", DBD_DB2_CONNECTION, conn);
+
+    return 1;
+}
+
 int dbd_db2_connection(lua_State *L) {
     static const luaL_Reg connection_methods[] = {
 	{"autocommit", connection_autocommit},
@@ -255,6 +266,9 @@
     lua_pushcfunction(L, connection_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, connection_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_DB2_CONNECTION, connection_class_methods);
 
     return 1;    
--- a/dbd/db2/statement.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/db2/statement.c	Tue Sep 01 13:15:02 2009 +0000
@@ -447,6 +447,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int statement_tostring(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT);
+
+    lua_pushfstring(L, "%s: %p", DBD_DB2_STATEMENT, statement);
+
+    return 1;
+}
+
 int dbd_db2_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 
     SQLRETURN rc = SQL_SUCCESS;
     statement_t *statement = NULL;
@@ -519,6 +530,9 @@
     lua_pushcfunction(L, statement_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, statement_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_DB2_STATEMENT, statement_class_methods);
 
     return 1;    
--- a/dbd/mysql/connection.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/mysql/connection.c	Tue Sep 01 13:15:02 2009 +0000
@@ -187,6 +187,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int connection_tostring(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
+
+    lua_pushfstring(L, "%s: %p", DBD_MYSQL_CONNECTION, conn);
+
+    return 1;
+}
+
 int dbd_mysql_connection(lua_State *L) {
     static const luaL_Reg connection_methods[] = {
 	{"autocommit", connection_autocommit},
@@ -212,6 +223,9 @@
     lua_pushcfunction(L, connection_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, connection_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_MYSQL_CONNECTION, connection_class_methods);
 
     return 1;    
--- a/dbd/mysql/statement.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/mysql/statement.c	Tue Sep 01 13:15:02 2009 +0000
@@ -402,6 +402,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int statement_tostring(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT);
+
+    lua_pushfstring(L, "%s: %p", DBD_MYSQL_STATEMENT, statement);
+
+    return 1;
+}
+
 int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 
     unsigned long sql_len = strlen(sql_query);
 
@@ -456,6 +467,9 @@
     lua_pushcfunction(L, statement_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, statement_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods);
 
     return 1;    
--- a/dbd/oracle/connection.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/oracle/connection.c	Tue Sep 01 13:15:02 2009 +0000
@@ -216,6 +216,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int connection_tostring(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_ORACLE_CONNECTION);
+
+    lua_pushfstring(L, "%s: %p", DBD_ORACLE_CONNECTION, conn);
+
+    return 1;
+}
+
 int dbd_oracle_connection(lua_State *L) {
     /*
      * instance methods
@@ -247,6 +258,9 @@
     lua_pushcfunction(L, connection_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, connection_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_ORACLE_CONNECTION, connection_class_methods);
 
     return 1;    
--- a/dbd/oracle/statement.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/oracle/statement.c	Tue Sep 01 13:15:02 2009 +0000
@@ -475,6 +475,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int statement_tostring(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
+
+    lua_pushfstring(L, "%s: %p", DBD_ORACLE_STATEMENT, statement);
+
+    return 1;
+}
+
 int dbd_oracle_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 
     int rc;
     statement_t *statement = NULL;
@@ -528,6 +539,9 @@
     lua_pushcfunction(L, statement_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, statement_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_ORACLE_STATEMENT, statement_class_methods);
 
     return 1;    
--- a/dbd/postgresql/connection.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/postgresql/connection.c	Tue Sep 01 13:15:02 2009 +0000
@@ -257,6 +257,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int connection_tostring(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION);
+
+    lua_pushfstring(L, "%s: %p", DBD_POSTGRESQL_CONNECTION, conn);
+
+    return 1;
+}
+
 int dbd_postgresql_connection(lua_State *L) {
     static const luaL_Reg connection_methods[] = {
 	{"autocommit", connection_autocommit},
@@ -282,6 +293,9 @@
     lua_pushcfunction(L, connection_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, connection_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_POSTGRESQL_CONNECTION, connection_class_methods);
 
     return 1;    
--- a/dbd/postgresql/statement.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/postgresql/statement.c	Tue Sep 01 13:15:02 2009 +0000
@@ -350,6 +350,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int statement_tostring(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT);
+
+    lua_pushfstring(L, "%s: %p", DBD_POSTGRESQL_STATEMENT, statement);
+
+    return 1;
+}
+
 int dbd_postgresql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 
     statement_t *statement = NULL;
     ExecStatusType status;
@@ -426,6 +437,9 @@
     lua_pushcfunction(L, statement_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, statement_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_POSTGRESQL_STATEMENT, statement_class_methods);
 
     return 1;    
--- a/dbd/sqlite3/connection.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/sqlite3/connection.c	Tue Sep 01 13:15:02 2009 +0000
@@ -198,6 +198,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int connection_tostring(lua_State *L) {
+    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION);
+
+    lua_pushfstring(L, "%s: %p", DBD_SQLITE_CONNECTION, conn);
+
+    return 1;
+}
+
 int dbd_sqlite3_connection(lua_State *L) {
     /*
      * instance methods
@@ -229,6 +240,9 @@
     lua_pushcfunction(L, connection_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, connection_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_SQLITE_CONNECTION, connection_class_methods);
 
     return 1;    
--- a/dbd/sqlite3/statement.c	Sat Jun 13 08:55:44 2009 +0000
+++ b/dbd/sqlite3/statement.c	Tue Sep 01 13:15:02 2009 +0000
@@ -342,6 +342,17 @@
     return 0;
 }
 
+/*
+ * __tostring
+ */
+static int statement_tostring(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
+
+    lua_pushfstring(L, "%s: %p", DBD_SQLITE_STATEMENT, statement);
+
+    return 1;
+}
+
 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 
     statement_t *statement = NULL;
 
@@ -386,6 +397,9 @@
     lua_pushcfunction(L, statement_gc);
     lua_setfield(L, -2, "__gc");
 
+    lua_pushcfunction(L, statement_tostring);
+    lua_setfield(L, -2, "__tostring");
+
     luaL_register(L, DBD_SQLITE_STATEMENT, statement_class_methods);
 
     return 1;    

mercurial