void
test_bson_build_full (void)
{
  bson *b, *o;

  b = bson_build_full (BSON_TYPE_DOUBLE, "double", FALSE, 3.14,
                       BSON_TYPE_STRING, "str", FALSE, "hello world", -1,
                       BSON_TYPE_DOCUMENT, "doc", TRUE,
                       bson_build (BSON_TYPE_STRING, "name", "sub-document", -1,
                                   BSON_TYPE_INT32, "answer", 42,
                                   BSON_TYPE_NONE),
                       BSON_TYPE_ARRAY, "array", TRUE,
                       bson_build (BSON_TYPE_INT32, "0", 32,
                                   BSON_TYPE_INT64, "1", (gint64)-42,
                                   BSON_TYPE_NONE),
                       BSON_TYPE_BINARY, "binary0", FALSE, BSON_BINARY_SUBTYPE_GENERIC,
                       "foo\0bar", 7,
                       BSON_TYPE_OID, "_id", FALSE, "1234567890ab",
                       BSON_TYPE_BOOLEAN, "TRUE", FALSE, FALSE,
                       BSON_TYPE_UTC_DATETIME, "date", FALSE, 1294860709000,
                       BSON_TYPE_TIMESTAMP, "ts", FALSE, 1294860709000,
                       BSON_TYPE_NULL, "null", FALSE,
                       BSON_TYPE_REGEXP, "foobar", FALSE, "s/foo.*bar/", "i",
                       BSON_TYPE_JS_CODE, "alert", FALSE, "alert (\"hello world!\");", -1,
                       BSON_TYPE_SYMBOL, "sex", FALSE, "Marilyn Monroe", -1,
                       BSON_TYPE_JS_CODE_W_SCOPE, "print", TRUE, "alert (v);", -1,
                       bson_build (BSON_TYPE_STRING, "v", "hello world", -1,
                                   BSON_TYPE_NONE),
                       BSON_TYPE_INT32, "int32", FALSE, 32,
                       BSON_TYPE_INT64, "int64", FALSE, (gint64)-42,
                       BSON_TYPE_NONE);
  bson_finish (b);

  o = test_bson_generate_full ();

  cmp_ok (bson_size (b), "==", bson_size (o),
          "bson_build_full() and hand crafted BSON object sizes match");

  ok (memcmp (bson_data (b), bson_data (o), bson_size (b)) == 0,
      "bson_build_full() and hand crafted BSON objects match");

  bson_free (b);
  bson_free (o);

  b = bson_build_full (BSON_TYPE_UNDEFINED, "undef", FALSE,
                       BSON_TYPE_NONE);
  ok (b == NULL,
      "bson_build_full() should fail with an unsupported element type");
  b = bson_build_full (BSON_TYPE_STRING, "str", FALSE, "hello", -1,
                       BSON_TYPE_UNDEFINED, "undef", FALSE,
                       BSON_TYPE_NONE);
  ok (b == NULL,
      "bson_build_full() should fail with an unsupported element type");

}
Beispiel #2
0
void
tut_sync_query_complex (void)
{
  mongo_sync_connection *conn;
  mongo_packet *p;
  mongo_sync_cursor *cursor;
  bson *query, *select;
  gint i = 0;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      exit (1);
    }

  query = bson_build_full (BSON_TYPE_DOCUMENT, "$query", TRUE,
			   bson_build (BSON_TYPE_BOOLEAN, "yes?", FALSE,
				       BSON_TYPE_NONE),
			   BSON_TYPE_DOCUMENT, "$orderby", TRUE,
			   bson_build (BSON_TYPE_INT32, "n", 1,
				       BSON_TYPE_NONE),
			   BSON_TYPE_NONE);
  bson_finish (query);

  select = bson_build (BSON_TYPE_INT32, "hello", 1,
		       BSON_TYPE_INT32, "n", 1,
		       BSON_TYPE_INT32, "yes?", 1,
		       BSON_TYPE_NONE);
  bson_finish (select);

  p = mongo_sync_cmd_query (conn, "tutorial.docs", 0,
			    0, 10, query, select);
  if (!p)
    {
      perror ("mongo_sync_cmd_query()");
      exit (1);
    }
  bson_free (query);
  bson_free (select);

  cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p);
  if (!cursor)
    {
      perror ("mongo_sync_cursor_new()");
      exit (1);
    }

  while (mongo_sync_cursor_next (cursor))
    {
      const char *hello;
      gint32 n;
      gboolean yes;

      bson *result;
      bson_cursor *c;

      result = mongo_sync_cursor_get_data (cursor);
      if (!result)
	{
	  perror ("mongo_sync_cursor_get_data()");
	  exit (1);
	}

      c = bson_find (result, "hello");
      if (!bson_cursor_get_string (c, &hello))
	{
	  perror ("bson_cursor_get_string()");
	  exit (1);
	}
      bson_cursor_free (c);

      c = bson_find (result, "n");
      if (!bson_cursor_get_int32 (c, &n))
	{
	  perror ("bson_cursor_get_int32()");
	  exit (1);
	}
      bson_cursor_free (c);

      c = bson_find (result, "yes?");
      if (!bson_cursor_get_boolean (c, &yes))
	{
	  perror ("bson_cursor_get_boolean()");
	  exit (1);
	}
      bson_cursor_free (c);

      printf ("Document #%d: hello=%s; n=%d; yes?=%s\n",
	      i, hello, n, (yes) ? "TRUE" : "FALSE");

      bson_free (result);
      i++;
    }

  mongo_sync_cursor_free (cursor);
  mongo_sync_disconnect (conn);
}