Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 32146e0395
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 49 lines (43 sloc) 1.603 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#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);
    }
}
Something went wrong with that request. Please try again.