diff --git a/source/c.txt b/source/c.txt index 7257b3739..836d49c22 100644 --- a/source/c.txt +++ b/source/c.txt @@ -4,6 +4,17 @@ MongoDB C Driver ================ +.. facet:: + :name: programming_language + :values: c + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: code example, get started, sample app + .. contents:: On this page :local: :backlinks: none diff --git a/source/includes/connection-snippets/scram/c-connection-no-stable-api.c b/source/includes/connection-snippets/scram/c-connection-no-stable-api.c index 9e9c7cd08..e53b93f5c 100644 --- a/source/includes/connection-snippets/scram/c-connection-no-stable-api.c +++ b/source/includes/connection-snippets/scram/c-connection-no-stable-api.c @@ -1,42 +1,52 @@ #include -int main (int argc, char **argv) -{ - mongoc_client_t *client = NULL; - bson_error_t error = {0}; - mongoc_database_t *database = NULL; - bson_t *command = NULL, reply; - - - // Initialize the MongoDB C Driver. - mongoc_init (); - - // Replace the with your MongoDB deployment's connection string. - client = mongoc_client_new(""); - - // Get a handle on the "admin" database. - database = mongoc_client_get_database (client, "admin"); - - // Ping the database. - command = BCON_NEW("ping", BCON_INT32(1)); - if (mongoc_database_command_simple(database, command, NULL, &reply, &error)) - { - printf("Pinged your deployment. You successfully connected to MongoDB!\n"); - } - else - { - // Error condition. - printf("Error: %s\n", error.message); - return 0; - } - - - // Perform Cleanup. - bson_destroy (&reply); - bson_destroy (command); - mongoc_database_destroy (database); - mongoc_client_destroy (client); - mongoc_cleanup (); - - return 0; -} +int main(void) { + mongoc_client_t *client = NULL; + bson_error_t error = {0}; + mongoc_database_t *database = NULL; + bson_t *command = NULL; + bson_t reply = BSON_INITIALIZER; + int rc = 0; + bool ok = true; + + // Initialize the MongoDB C Driver. + mongoc_init(); + + client = mongoc_client_new(""); + if (!client) { + fprintf(stderr, "Failed to create a MongoDB client.\n"); + rc = 1; + goto cleanup; + } + + // Get a handle on the "admin" database. + database = mongoc_client_get_database(client, "admin"); + if (!database) { + fprintf(stderr, "Failed to get a MongoDB database handle.\n"); + rc = 1; + goto cleanup; + } + + // Ping the database. + command = BCON_NEW("ping", BCON_INT32(1)); + ok = mongoc_database_command_simple( + database, command, NULL, &reply, &error + ); + if (!ok) { + fprintf(stderr, "error: %s\n", error.message); + rc = 1; + goto cleanup; + } + bson_destroy(&reply); + + printf("Pinged your deployment. You successfully connected to MongoDB!\n"); + +// Perform cleanup. +cleanup: + bson_destroy(command); + mongoc_database_destroy(database); + mongoc_client_destroy(client); + mongoc_cleanup(); + + return rc; +} \ No newline at end of file diff --git a/source/includes/connection-snippets/scram/c-connection.c b/source/includes/connection-snippets/scram/c-connection.c index 039576fd9..a2c651c26 100644 --- a/source/includes/connection-snippets/scram/c-connection.c +++ b/source/includes/connection-snippets/scram/c-connection.c @@ -1,51 +1,69 @@ #include -int main (int argc, char **argv) -{ - mongoc_client_t *client = NULL; - bson_error_t error = {0}; - mongoc_server_api_t *api = NULL; - mongoc_database_t *database = NULL; - bson_t *command = NULL, reply; - - // Initialize the MongoDB C Driver. - mongoc_init (); - - // Replace the with your MongoDB deployment's connection string. - client = mongoc_client_new(""); - - // Set the version of the Stable API on the client. - api = mongoc_server_api_new (MONGOC_SERVER_API_V1); - if (!mongoc_client_set_server_api (client, api, &error)) - { - // Error condition. - printf("Error: %s\n", error.message); - return 0; - } - - // Get a handle on the "admin" database. - database = mongoc_client_get_database (client, "admin"); - - // Ping the database. - command = BCON_NEW("ping", BCON_INT32(1)); - if (mongoc_database_command_simple(database, command, NULL, &reply, &error)) - { +int main(void) { + mongoc_client_t *client = NULL; + bson_error_t error = {0}; + mongoc_server_api_t *api = NULL; + mongoc_database_t *database = NULL; + bson_t *command = NULL; + bson_t reply = BSON_INITIALIZER; + int rc = 0; + bool ok = true; + + // Initialize the MongoDB C Driver. + mongoc_init(); + + client = mongoc_client_new(""); + if (!client) { + fprintf(stderr, "Failed to create a MongoDB client.\n"); + rc = 1; + goto cleanup; + } + + // Set the version of the Stable API on the client. + api = mongoc_server_api_new(MONGOC_SERVER_API_V1); + if (!api) { + fprintf(stderr, "Failed to create a MongoDB server API.\n"); + rc = 1; + goto cleanup; + } + + ok = mongoc_client_set_server_api(client, api, &error); + if (!ok) { + fprintf(stderr, "error: %s\n", error.message); + rc = 1; + goto cleanup; + } + + // Get a handle on the "admin" database. + database = mongoc_client_get_database(client, "admin"); + if (!database) { + fprintf(stderr, "Failed to get a MongoDB database handle.\n"); + rc = 1; + goto cleanup; + } + + // Ping the database. + command = BCON_NEW("ping", BCON_INT32(1)); + ok = mongoc_database_command_simple( + database, command, NULL, &reply, &error + ); + if (!ok) { + fprintf(stderr, "error: %s\n", error.message); + rc = 1; + goto cleanup; + } + bson_destroy(&reply); + printf("Pinged your deployment. You successfully connected to MongoDB!\n"); - } - else - { - // Error condition. - printf("Error: %s\n", error.message); - return 0; - } - - - // Perform Cleanup. - bson_destroy (&reply); - bson_destroy (command); - mongoc_database_destroy (database); - mongoc_client_destroy (client); - mongoc_cleanup (); - - return 0; -} + +// Perform cleanup. +cleanup: + bson_destroy(command); + mongoc_database_destroy(database); + mongoc_server_api_destroy(api); + mongoc_client_destroy(client); + mongoc_cleanup(); + + return rc; +} \ No newline at end of file