Пример #1
0
PHP_METHOD(MongoDB, repair)
{
	zend_bool cloned=0, original=0;
	zval *cmd, *retval;
	mongo_db *db;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &cloned, &original) == FAILURE) {
		return;
	}

	PHP_MONGO_GET_DB(getThis());

	MAKE_STD_ZVAL(cmd);
	array_init(cmd);
	add_assoc_long(cmd, "repairDatabase", 1);
	add_assoc_bool(cmd, "preserveClonedFilesOnFailure", cloned);
	add_assoc_bool(cmd, "backupOriginalFiles", original);

	retval = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);

	zval_ptr_dtor(&cmd);

	if (retval) {
		RETVAL_ZVAL(retval, 0, 1);
	}
}
Пример #2
0
PHP_METHOD(MongoDB, getReadPreference)
{
    mongo_db *db;
    PHP_MONGO_GET_DB(getThis());

    array_init(return_value);
    add_assoc_string(return_value, "type", mongo_read_preference_type_to_name(db->read_pref.type), 1);
    php_mongo_add_tagsets(return_value, &db->read_pref);
}
Пример #3
0
PHP_METHOD(MongoDB, setSlaveOkay) {
  zend_bool slave_okay = 1;
  mongo_db *db;

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

  PHP_MONGO_GET_DB(getThis());

  RETVAL_BOOL(db->slave_okay);
  db->slave_okay = slave_okay;
}
Пример #4
0
PHP_METHOD(MongoDB, setSlaveOkay)
{
    zend_bool slave_okay = 1;
    mongo_db *db;

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

    PHP_MONGO_GET_DB(getThis());

    RETVAL_BOOL(db->read_pref.type != MONGO_RP_PRIMARY);
    db->read_pref.type = slave_okay ? MONGO_RP_SECONDARY_PREFERRED : MONGO_RP_PRIMARY;
}
Пример #5
0
PHP_METHOD(MongoCollection, __construct) {
  zval *parent, *name, *zns, *w, *wtimeout;
  mongo_collection *c;
  mongo_db *db;
  char *ns, *name_str;
  int name_len;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &parent, mongo_ce_DB, &name_str, &name_len) == FAILURE) {
    zval *object = getThis();
    ZVAL_NULL(object);
    return;
  }

  // check for empty collection name
  if (name_len == 0) {
#if ZEND_MODULE_API_NO >= 20060613
    zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "MongoDB::__construct(): invalid name %s", name_str);
#else
    zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC, "MongoDB::__construct(): invalid name %s", name_str);
#endif /* ZEND_MODULE_API_NO >= 20060613 */
    return;
  }

  c = (mongo_collection*)zend_object_store_get_object(getThis() TSRMLS_CC);

  PHP_MONGO_GET_DB(parent);

  c->link = db->link;
  zval_add_ref(&db->link);

  c->parent = parent;
  zval_add_ref(&parent);

  MAKE_STD_ZVAL(name);
  ZVAL_STRINGL(name, name_str, name_len, 1);
  c->name = name;

  spprintf(&ns, 0, "%s.%s", Z_STRVAL_P(db->name), Z_STRVAL_P(name));

  MAKE_STD_ZVAL(zns);
  ZVAL_STRING(zns, ns, 0);
  c->ns = zns;
  mongo_read_preference_copy(&db->read_pref, &c->read_pref);

  w = zend_read_property(mongo_ce_DB, parent, "w", strlen("w"), NOISY TSRMLS_CC);
  zend_update_property_long(mongo_ce_Collection, getThis(), "w", strlen("w"), Z_LVAL_P(w) TSRMLS_CC);
  wtimeout = zend_read_property(mongo_ce_DB, parent, "wtimeout", strlen("wtimeout"), NOISY TSRMLS_CC);
  zend_update_property_long(mongo_ce_Collection, getThis(), "wtimeout", strlen("wtimeout"), Z_LVAL_P(wtimeout) TSRMLS_CC);
}
Пример #6
0
/* {{{ MongoDB::setReadPreference(string read_preference [, array tags ])
 * Sets a read preference to be used for all read queries.*/
PHP_METHOD(MongoDB, setReadPreference)
{
    char *read_preference;
    int   read_preference_len;
    mongo_db *db;
    HashTable  *tags = NULL;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|h", &read_preference, &read_preference_len, &tags) == FAILURE) {
        return;
    }

    PHP_MONGO_GET_DB(getThis());

    if (php_mongo_set_readpreference(&db->read_pref, read_preference, tags TSRMLS_CC)) {
        RETURN_TRUE;
    } else {
        RETURN_FALSE;
    }
}
Пример #7
0
static void php_mongo_db_profiling_level(INTERNAL_FUNCTION_PARAMETERS, int get)
{
	long level;
	zval *cmd, *cmd_return;
	zval **ok;
	mongo_db *db;

	if (get) {
		if (zend_parse_parameters_none() == FAILURE) {
			return;
		}
		level = -1;
	} else {
		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &level) == FAILURE) {
			return;
		}
	}

	PHP_MONGO_GET_DB(getThis());

	MAKE_STD_ZVAL(cmd);
	array_init(cmd);
	add_assoc_long(cmd, "profile", level);

	cmd_return = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);

	zval_ptr_dtor(&cmd);

	if (!cmd_return) {
		return;
	}

	if (
		zend_hash_find(HASH_P(cmd_return), "ok", 3, (void**)&ok) == SUCCESS &&
		((Z_TYPE_PP(ok) == IS_BOOL && Z_BVAL_PP(ok)) || Z_DVAL_PP(ok) == 1)
	) {
		zend_hash_find(HASH_P(cmd_return), "was", 4, (void**)&ok);
		RETVAL_ZVAL(*ok, 1, 0);
	} else {
		RETVAL_NULL();
	}
	zval_ptr_dtor(&cmd_return);
}
Пример #8
0
PHP_METHOD(MongoDB, drop)
{
	zval *cmd, *retval;
	mongo_db *db;

	if (zend_parse_parameters_none() == FAILURE) {
		return;
	}

	PHP_MONGO_GET_DB(getThis());

	MAKE_STD_ZVAL(cmd);
	array_init(cmd);
	add_assoc_long(cmd, "dropDatabase", 1);

	retval = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);

	zval_ptr_dtor(&cmd);

	if (retval) {
		RETURN_ZVAL(retval, 0, 1);
	}
}
Пример #9
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);
}
Пример #10
0
PHP_METHOD(MongoDB, getSlaveOkay) {
  mongo_db *db;
  PHP_MONGO_GET_DB(getThis());
  RETURN_BOOL(db->slave_okay);
}
Пример #11
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);
}
Пример #12
0
PHP_METHOD(MongoDB, getSlaveOkay)
{
    mongo_db *db;
    PHP_MONGO_GET_DB(getThis());
    RETURN_BOOL(db->read_pref.type != MONGO_RP_PRIMARY);
}
Пример #13
0
PHP_METHOD(MongoDB, createCollection)
{
	zval *cmd = NULL, *temp, *options = NULL;
	char *collection;
	int   collection_len;
	zend_bool capped = 0;
	long size = 0, max = 0;
	mongo_db *db;

	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &collection, &collection_len, &capped, &size, &max) == SUCCESS) {
		MAKE_STD_ZVAL(cmd);
		array_init(cmd);

		add_assoc_stringl(cmd, "create", collection, collection_len, 1);

		if (size) {
			add_assoc_long(cmd, "size", size);
		}

		if (capped) {
			php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This method now accepts arguments as an options array instead of the three optional arguments for capped, size and max elements");
			add_assoc_bool(cmd, "capped", 1);
			if (max) {
				add_assoc_long(cmd, "max", max);
			}
		}

	} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &collection, &collection_len, &options) == SUCCESS) {
		zval *tmp_copy;

		/* We create a new array here, instead of just tagging "create" =>
		 * <name> at the end of the array. This is because MongoDB wants the
		 * name of the command as first element in the array. */
		MAKE_STD_ZVAL(cmd);
		array_init(cmd);
		add_assoc_stringl(cmd, "create", collection, collection_len, 1);
		if (options) {
			zend_hash_merge(Z_ARRVAL_P(cmd), Z_ARRVAL_P(options), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
		}
	} else {

		return;
	}

	PHP_MONGO_GET_DB(getThis());

	temp = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, options, 0, NULL TSRMLS_CC);

	zval_ptr_dtor(&cmd);
	if (temp) {
		zval_ptr_dtor(&temp);
	}

	if (!EG(exception)) {
		zval *zcollection;

		/* get the collection we just created */
		zcollection = php_mongo_db_selectcollection(getThis(), collection, collection_len TSRMLS_CC);
		if (zcollection) {
			/* Only copy the zval into return_value if it worked. If
			 * zcollection is NULL here, an exception is set */
			RETURN_ZVAL(zcollection, 0, 1);
		}
	}
}