예제 #1
0
class collection database::create_collection(stdx::string_view name,
                                             const options::create_collection& options) {
    bson_error_t error;

    libbson::scoped_bson_t opts_bson{options.to_document()};
    auto result = libmongoc::database_create_collection(_get_impl().database_t, name.data(),
                                                        opts_bson.bson(), &error);

    if (!result) {
        throw_exception<operation_exception>(error);
    }

    return mongocxx::collection(*this, static_cast<void*>(result));
}
#include <bsoncxx/types.hpp>
#include <bsoncxx/types/value.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/options/create_collection.hpp>

using namespace bsoncxx;
using namespace mongocxx;

using builder::stream::close_document;
using builder::stream::finalize;
using builder::stream::open_document;

TEST_CASE("create_collection", "[create_collection]") {
    instance::current();

    options::create_collection cc;

    SECTION("Can be exported to a document") {
        auto rule = builder::stream::document{} << "brain" << open_document << "$exists" << true
                                                << close_document << finalize;

        validation_criteria validation;
        validation.rule(rule.view());
        validation.level(validation_criteria::validation_level::k_strict);

        cc.validation_criteria(validation);
        cc.capped(true);
        cc.size(256);
        cc.max(100);
        cc.no_padding(true);
예제 #3
0
파일: database.cpp 프로젝트: ilri/odktools
class collection database::create_collection(bsoncxx::string::view_or_value name,
                                             const options::create_collection& options) {
    document options_builder{};

    if (options.auto_index_id()) {
        options_builder << "autoIndexId" << *options.auto_index_id();
    }

    if (options.capped()) {
        options_builder << "capped" << *options.capped();
    }

    if (options.collation()) {
        options_builder << "collation" << *options.collation();
    }

    if (options.max()) {
        options_builder << "max" << *options.max();
    }

    if (options.no_padding()) {
        options_builder << "flags" << (*options.no_padding() ? 0x10 : 0x00);
    }

    if (options.size()) {
        options_builder << "size" << *options.size();
    }

    if (options.storage_engine()) {
        options_builder << "storageEngine" << *options.storage_engine();
    }

    if (options.validation_criteria()) {
        auto validation_level_to_string = [](validation_criteria::validation_level level) {
            switch (level) {
                case validation_criteria::validation_level::k_off:
                    return "off";
                case validation_criteria::validation_level::k_moderate:
                    return "moderate";
                case validation_criteria::validation_level::k_strict:
                    return "strict";
            }
            MONGOCXX_UNREACHABLE;
        };

        auto validation_action_to_string = [](validation_criteria::validation_action action) {
            switch (action) {
                case validation_criteria::validation_action::k_warn:
                    return "warn";
                case validation_criteria::validation_action::k_error:
                    return "error";
            }
            MONGOCXX_UNREACHABLE;
        };

        auto validation_criteria = *options.validation_criteria();

        if (validation_criteria.rule()) {
            options_builder << "validator" << *validation_criteria.rule();
        }

        if (validation_criteria.level()) {
            options_builder << "validationLevel"
                            << validation_level_to_string(*validation_criteria.level());
        }

        if (validation_criteria.action()) {
            options_builder << "validationAction"
                            << validation_action_to_string(*validation_criteria.action());
        }
    }

    bson_error_t error;
    libbson::scoped_bson_t opts_bson{options_builder.view()};
    auto result = libmongoc::database_create_collection(
        _get_impl().database_t, name.terminated().data(), opts_bson.bson(), &error);
    if (!result) {
        throw_exception<operation_exception>(error);
    }

    return mongocxx::collection(*this, result);
}
예제 #4
0
class collection database::create_collection(
    bsoncxx::string::view_or_value name,
    const options::create_collection& collection_options,
    const stdx::optional<class write_concern>& write_concern) {
    bsoncxx::builder::basic::document options_builder;

    if (write_concern) {
        options_builder.append(kvp("writeConcern", write_concern->to_document()));
    }

    if (collection_options.auto_index_id()) {
        options_builder.append(kvp("autoIndexId", *collection_options.auto_index_id()));
    }

    if (collection_options.capped()) {
        options_builder.append(kvp("capped", *collection_options.capped()));
    }

    if (collection_options.collation()) {
        options_builder.append(kvp("collation", *collection_options.collation()));
    }

    if (collection_options.max()) {
        options_builder.append(kvp("max", *collection_options.max()));
    }

    if (collection_options.no_padding()) {
        options_builder.append(kvp("flags", (*collection_options.no_padding() ? 0x10 : 0x00)));
    }

    if (collection_options.size()) {
        options_builder.append(kvp("size", *collection_options.size()));
    }

    if (collection_options.storage_engine()) {
        options_builder.append(kvp("storageEngine", *collection_options.storage_engine()));
    }

    if (collection_options.validation_criteria()) {
        auto validation_level_to_string = [](validation_criteria::validation_level level) {
            switch (level) {
                case validation_criteria::validation_level::k_off:
                    return "off";
                case validation_criteria::validation_level::k_moderate:
                    return "moderate";
                case validation_criteria::validation_level::k_strict:
                    return "strict";
            }
            MONGOCXX_UNREACHABLE;
        };

        auto validation_action_to_string = [](validation_criteria::validation_action action) {
            switch (action) {
                case validation_criteria::validation_action::k_warn:
                    return "warn";
                case validation_criteria::validation_action::k_error:
                    return "error";
            }
            MONGOCXX_UNREACHABLE;
        };

        auto validation_criteria = *collection_options.validation_criteria();

        if (validation_criteria.rule()) {
            options_builder.append(kvp("validator", *validation_criteria.rule()));
        }

        if (validation_criteria.level()) {
            options_builder.append(
                kvp("validationLevel", validation_level_to_string(*validation_criteria.level())));
        }

        if (validation_criteria.action()) {
            options_builder.append(kvp("validationAction",
                                       validation_action_to_string(*validation_criteria.action())));
        }
    }

    bson_error_t error;
    libbson::scoped_bson_t opts_bson{options_builder.view()};
    auto result = libmongoc::database_create_collection(
        _get_impl().database_t, name.terminated().data(), opts_bson.bson(), &error);
    if (!result) {
        throw_exception<operation_exception>(error);
    }

    return mongocxx::collection(*this, result);
}