Skip to content

Commit

Permalink
Add __tostring method to connection and statement objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrich committed Sep 1, 2009
1 parent 672194b commit a664e1a
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 0 deletions.
14 changes: 14 additions & 0 deletions dbd/db2/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ static int connection_gc(lua_State *L) {
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},
Expand All @@ -255,6 +266,9 @@ int dbd_db2_connection(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/db2/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,17 @@ static int statement_gc(lua_State *L) {
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;
Expand Down Expand Up @@ -519,6 +530,9 @@ int dbd_db2_statement(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/mysql/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ static int connection_gc(lua_State *L) {
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},
Expand All @@ -212,6 +223,9 @@ int dbd_mysql_connection(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/mysql/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,17 @@ static int statement_gc(lua_State *L) {
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);

Expand Down Expand Up @@ -456,6 +467,9 @@ int dbd_mysql_statement(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/oracle/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ static int connection_gc(lua_State *L) {
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
Expand Down Expand Up @@ -247,6 +258,9 @@ int dbd_oracle_connection(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/oracle/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ static int statement_gc(lua_State *L) {
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;
Expand Down Expand Up @@ -528,6 +539,9 @@ int dbd_oracle_statement(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/postgresql/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ static int connection_gc(lua_State *L) {
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},
Expand All @@ -282,6 +293,9 @@ int dbd_postgresql_connection(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/postgresql/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ static int statement_gc(lua_State *L) {
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;
Expand Down Expand Up @@ -426,6 +437,9 @@ int dbd_postgresql_statement(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/sqlite3/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ static int connection_gc(lua_State *L) {
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
Expand Down Expand Up @@ -229,6 +240,9 @@ int dbd_sqlite3_connection(lua_State *L) {
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;
Expand Down
14 changes: 14 additions & 0 deletions dbd/sqlite3/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ static int statement_gc(lua_State *L) {
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;

Expand Down Expand Up @@ -386,6 +397,9 @@ int dbd_sqlite3_statement(lua_State *L) {
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;
Expand Down

0 comments on commit a664e1a

Please sign in to comment.