diff --git a/generic/bson.c b/generic/bson.c index c4735a6..7de0077 100644 --- a/generic/bson.c +++ b/generic/bson.c @@ -249,6 +249,40 @@ mongotcl_cmdNameObjToBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson **b return TCL_OK; } + +/* + *---------------------------------------------------------------------- + * + * mongotcl_cmdNameObjSetBson -- + * + * Take a command name and a pointer to a bson structure, find the Tcl + * command info structure, confirm it's bson, destroy the old bson + * structure there and point the command to the passed-in bson + * structure. + * + *---------------------------------------------------------------------- + */ +int +mongotcl_cmdNameObjSetBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson *newBson) { + Tcl_CmdInfo cmdInfo; + mongotcl_bsonClientData *bd; + + if (!Tcl_GetCommandInfo (interp, Tcl_GetString(commandNameObj), &cmdInfo)) { + return TCL_ERROR; + } + + if (cmdInfo.objClientData == NULL || ((mongotcl_bsonClientData *)cmdInfo.objClientData)->bson_magic != MONGOTCL_BSON_MAGIC) { + Tcl_AppendResult (interp, "Error: '", Tcl_GetString (commandNameObj), "' is not a bson object", NULL); + return TCL_ERROR; + } + + bd = (mongotcl_bsonClientData *)cmdInfo.objClientData; + + bson_destroy(bd->bson); + bd->bson = newBson; + return TCL_OK; +} + /* *---------------------------------------------------------------------- diff --git a/generic/cursor.c b/generic/cursor.c index 516b38e..79ca634 100644 --- a/generic/cursor.c +++ b/generic/cursor.c @@ -334,12 +334,16 @@ mongotcl_cursorObjectObjCmd(ClientData cData, Tcl_Interp *interp, int objc, Tcl_ } case OPT_CURSOR_BSON: { + char *bsonCommandObject; + if (objc != 3) { Tcl_WrongNumArgs (interp, 2, objv, "bsonName"); return TCL_ERROR; } + + bsonCommandObject = Tcl_GetString (objv[2]); - return mongotcl_create_bson_command (interp, Tcl_GetString (objv[2]), mongo_cursor_bson (mc->cursor)); + return mongotcl_cmdNameObjSetBson (interp, bsonCommandObject, mongo_cursor_bson (mc->cursor)); } case OPT_CURSOR_NEXT: { diff --git a/generic/mongotcl.h b/generic/mongotcl.h index fe6bb0b..f14bea3 100644 --- a/generic/mongotcl.h +++ b/generic/mongotcl.h @@ -12,11 +12,11 @@ #define MONGO_HAVE_STDINT -#define MONGOTCL_BSON_MAGIC 0xf33df00d +#define MONGOTCL_BSON_MAGIC 0xf33df007 -#define MONGOTCL_MONGO_MAGIC 0xf33dd00d +#define MONGOTCL_MONGO_MAGIC 0xf33db007 -#define MONGOTCL_CURSOR_MAGIC 0xf33dc00c +#define MONGOTCL_CURSOR_MAGIC 0xf33dc007 #include @@ -25,6 +25,9 @@ extern int mongotcl_cmdNameObjToBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson **bson); +extern int +mongotcl_cmdNameObjSetBson (Tcl_Interp *interp, Tcl_Obj *commandNameObj, bson *newBson); + extern int mongotcl_mongoObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objvp[]);