Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 32146e0395
Fetching contributors…

Cannot retrieve contributors at this time

49 lines (43 sloc) 1.603 kB
#include "mongo.h"
#include "ps_mongodb.h"
#define GET_CONN(s) ((mongo*)PARROT_MONGODBCONTEXT(s)->conn)
pmclass MongoDbContext dynpmc auto_attrs {
ATTR void *conn;
VTABLE void init() {
mongo * conn = (mongo *)malloc(sizeof(mongo));
PARROT_MONGODBCONTEXT(SELF)->conn = conn;
}
VTABLE void destroy() {
mongo * conn = GET_CONN(SELF);
if (conn) {
mongo_destroy(conn);
free(conn);
}
}
METHOD connect(STRING * server, INTVAL port)
{
mongo * conn = GET_CONN(SELF);
char * const cserver = Parrot_str_to_cstring(INTERP, server);
INTVAL stat = mongo_connect(conn, cserver, port);
if (stat) {
switch(conn->err) {
case MONGO_CONN_NO_SOCKET:
Parrot_ex_throw_from_c_args(INTERP, 0, NULL, "Could not connect to server %Ss:%d, could not create a socket", server, port);
break;
case MONGO_CONN_FAIL:
Parrot_ex_throw_from_c_args(INTERP, 0, NULL, "Could not connect to server %Ss:%d, mongodb might not be listening", server, port);
break;
}
}
}
METHOD insert(STRING *collection, PMC * bson_doc)
{
mongo * conn = GET_CONN(SELF);
INTVAL stat;
STRING_TO_CSTRING(INTERP, collection, cc,
stat = mongo_insert(conn, cc, VTABLE_get_pointer(INTERP, bson_doc));
);
if (stat)
Parrot_ex_throw_from_c_args(INTERP, 0, NULL, "Failed to insert document with error %d", conn->err);
}
}
Jump to Line
Something went wrong with that request. Please try again.