/* {{{ proto MongoGridFSFile MongoGridFSCursor::current()
   Returns the current file */
PHP_METHOD(MongoGridFSCursor, current)
{
	zval temp;
	zval *gridfs;
	zval *flags;
	mongo_cursor *cursor = (mongo_cursor*)zend_object_store_get_object(getThis() TSRMLS_CC);
	MONGO_CHECK_INITIALIZED(cursor->zmongoclient, MongoGridFSCursor);

	if (!cursor->current) {
		RETURN_NULL();
	}

	MAKE_STD_ZVAL(flags);
	ZVAL_LONG(flags, cursor->opts);

	object_init_ex(return_value, mongo_ce_GridFSFile);

	gridfs = zend_read_property(mongo_ce_GridFSCursor, getThis(), "gridfs", strlen("gridfs"), NOISY TSRMLS_CC);

	MONGO_METHOD3(MongoGridFSFile, __construct, &temp, return_value, gridfs, cursor->current, flags);
	zval_ptr_dtor(&flags);
}
Exemple #2
0
PHP_METHOD(MongoDB, command) {
  zval limit, *temp, *cmd, *cursor, *ns, *options = 0;
  mongo_db *db;
  mongo_link *link;
  char *cmd_ns;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &cmd, &options) == FAILURE) {
    return;
  }
  if (IS_SCALAR_P(cmd)) {
    zend_error(E_WARNING, "MongoDB::command() expects parameter 1 to be an array or object");
    return;
  }

  PHP_MONGO_GET_DB(getThis());

  // create db.$cmd
  MAKE_STD_ZVAL(ns);
  cmd_ns = get_cmd_ns(Z_STRVAL_P(db->name), Z_STRLEN_P(db->name));
  ZVAL_STRING(ns, cmd_ns, 0);

  // create cursor
  MAKE_STD_ZVAL(cursor);
  object_init_ex(cursor, mongo_ce_Cursor);
  MAKE_STD_ZVAL(temp);
  ZVAL_NULL(temp);

  MONGO_METHOD3(MongoCursor, __construct, temp, cursor, db->link, ns, cmd);

  zval_ptr_dtor(&ns);
  zval_ptr_dtor(&temp);
  MAKE_STD_ZVAL(temp);
  ZVAL_NULL(temp);

  // limit
  Z_TYPE(limit) = IS_LONG;
  Z_LVAL(limit) = -1;
  MONGO_METHOD1(MongoCursor, limit, temp, cursor, &limit);

  zval_ptr_dtor(&temp);

  if (options) {
    zval **timeout;
    if (zend_hash_find(HASH_P(options), "timeout", strlen("timeout")+1, (void**)&timeout) == SUCCESS) {
      MAKE_STD_ZVAL(temp);
      ZVAL_NULL(temp);
      MONGO_METHOD1(MongoCursor, timeout, temp, cursor, *timeout);
      zval_ptr_dtor(&temp);
    }
  }

  // make sure commands aren't be sent to slaves
  PHP_MONGO_GET_LINK(db->link);
  if (link->rs) {
    zval slave_okay;
    Z_TYPE(slave_okay) = IS_BOOL;
    Z_LVAL(slave_okay) = 0;

    MAKE_STD_ZVAL(temp);
    ZVAL_NULL(temp);
    MONGO_METHOD1(MongoCursor, slaveOkay, temp, cursor, &slave_okay);
    zval_ptr_dtor(&temp);
  }

  // query
  MONGO_METHOD(MongoCursor, getNext, return_value, cursor);
  clear_exception(return_value TSRMLS_CC);

  zend_objects_store_del_ref(cursor TSRMLS_CC);
  zval_ptr_dtor(&cursor);
}
PHP_METHOD(MongoCursor, count) {
  zval *db_z, *coll, *query;
  mongo_cursor *cursor;
  mongo_collection *c;
  mongo_db *db;
  zend_bool all = 0;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &all) == FAILURE) {
    return;
  }

  PHP_MONGO_GET_CURSOR(getThis());

  // fake a MongoDB object
  MAKE_STD_ZVAL(db_z);
  object_init_ex(db_z, mongo_ce_DB);
  db = (mongo_db*)zend_object_store_get_object(db_z TSRMLS_CC);
  db->link = cursor->resource;
  MAKE_STD_ZVAL(db->name);
  ZVAL_STRING(db->name, estrndup(cursor->ns, strchr(cursor->ns, '.') - cursor->ns), 0);

  // fake a MongoCollection object
  MAKE_STD_ZVAL(coll);
  object_init_ex(coll, mongo_ce_Collection);
  c = (mongo_collection*)zend_object_store_get_object(coll TSRMLS_CC);
  MAKE_STD_ZVAL(c->ns);
  ZVAL_STRING(c->ns, estrdup(cursor->ns), 0);
  MAKE_STD_ZVAL(c->name);
  ZVAL_STRING(c->name, estrdup(cursor->ns + (strchr(cursor->ns, '.') - cursor->ns) + 1), 0);
  c->parent = db_z;

  if (cursor->query) {
    zval **inner_query = 0;

    if (!cursor->special) {
      query = cursor->query;
      zval_add_ref(&query);
    }
    else if (zend_hash_find(HASH_P(cursor->query), "$query", strlen("$query")+1, (void**)&inner_query) == SUCCESS) {
      query = *inner_query;
      zval_add_ref(&query);
    }
  }
  else {
    MAKE_STD_ZVAL(query);
    array_init(query);
  }

  if (all) {
    zval *limit_z, *skip_z;

    MAKE_STD_ZVAL(limit_z);
    MAKE_STD_ZVAL(skip_z);

    ZVAL_LONG(limit_z, cursor->limit);
    ZVAL_LONG(skip_z, cursor->skip);

    MONGO_METHOD3(MongoCollection, count, return_value, coll, query, limit_z, skip_z);

    zval_ptr_dtor(&limit_z);
    zval_ptr_dtor(&skip_z);
  }
  else {
    MONGO_METHOD1(MongoCollection, count, return_value, coll, query);
  }

  zval_ptr_dtor(&query);

  c->parent = 0;
  zend_objects_store_del_ref(coll TSRMLS_CC);
  zval_ptr_dtor(&coll);

  db->link = 0;
  zend_objects_store_del_ref(db_z TSRMLS_CC);
  zval_ptr_dtor(&db_z);
}
Exemple #4
0
PHP_METHOD(MongoDB, command) {
    zval limit, *temp, *cmd, *cursor, *ns, *options = 0;
    mongo_db *db;
    mongoclient *link;
    char *cmd_ns;
    mongo_cursor *cursor_tmp;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &cmd, &options) == FAILURE) {
        return;
    }

    MUST_BE_ARRAY_OR_OBJECT(1, cmd);

    PHP_MONGO_GET_DB(getThis());

    // create db.$cmd
    MAKE_STD_ZVAL(ns);
    cmd_ns = get_cmd_ns(Z_STRVAL_P(db->name), Z_STRLEN_P(db->name));
    ZVAL_STRING(ns, cmd_ns, 0);

    // create cursor
    MAKE_STD_ZVAL(cursor);
    object_init_ex(cursor, mongo_ce_Cursor);
    MAKE_STD_ZVAL(temp);
    ZVAL_NULL(temp);

    MONGO_METHOD3(MongoCursor, __construct, temp, cursor, db->link, ns, cmd);

    zval_ptr_dtor(&ns);
    zval_ptr_dtor(&temp);
    MAKE_STD_ZVAL(temp);
    ZVAL_NULL(temp);

    // limit
    Z_TYPE(limit) = IS_LONG;
    Z_LVAL(limit) = -1;
    MONGO_METHOD1(MongoCursor, limit, temp, cursor, &limit);

    zval_ptr_dtor(&temp);

    if (options) {
        zval **timeout;
        if (zend_hash_find(HASH_P(options), "timeout", strlen("timeout")+1, (void**)&timeout) == SUCCESS) {
            MAKE_STD_ZVAL(temp);
            ZVAL_NULL(temp);
            MONGO_METHOD1(MongoCursor, timeout, temp, cursor, *timeout);
            zval_ptr_dtor(&temp);
        }
    }

    /* Make sure commands aren't be sent to slaves */
    /* TODO: The read preferences spec has a list of commands that *can* be send
     * to slave */
    /* This should be refactored alongside with the getLastError redirection in
     * collection.c/append_getlasterror. The Cursor creation should be done through
     * an init method. */
    PHP_MONGO_GET_LINK(db->link);
    cursor_tmp = (mongo_cursor*)zend_object_store_get_object(cursor TSRMLS_CC);
    mongo_manager_log(link->manager, MLOG_CON, MLOG_INFO, "forcing primary for command");
    php_mongo_connection_force_primary(cursor_tmp);


    // query
    MONGO_METHOD(MongoCursor, getNext, return_value, cursor);
    clear_exception(return_value TSRMLS_CC);

    zend_objects_store_del_ref(cursor TSRMLS_CC);
    zval_ptr_dtor(&cursor);
}