Documentation Home
MySQL NDB Cluster API Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 3.6Mb
PDF (A4) - 3.6Mb


MySQL NDB Cluster API Developer Guide  /  ...  /  NDB API Basic Delete Example

2.5.1.4 NDB API Basic Delete Example

This example shows deleting a row from a table already created and populated previously (see Section 2.5.1.2, “NDB API Basic Insertion Example”). It performs the deletion using a single NdbOperation within a transaction, and handles errors using NdbError.

You can verify afterwards that the row was deleted by running the read example (Section 2.5.1.3, “NDB API Basic Reading Example”), or by executing SELECT * FROM basic in the mysql client.

The source code for this example can also be found in the file storage/ndb/ndbapi-examples/ndbapi_basic/ndbapi_basic_delete.cpp.

#include <iostream>
#include <cstdlib>
#include <string>

#include <NdbApi.hpp>

class BasicDelete
{
  public:
    BasicDelete(const char * connectstring)
      : m_connection(connectstring), m_ndb(&m_connection, "ndbapi_examples") {}

    bool init();
    bool do_delete(long long);

  private:
    Ndb_cluster_connection m_connection;
    Ndb m_ndb;

    inline bool on_error(const struct NdbError &error,
                         const std::string &explanation)
    {
      // prints error in format:
      // ERROR <NdbErrorCode>: <NdbError message>
      //    explanation what went wrong on higher level (in the example code)
      std::cout << "ERROR "<< error.code << ": " << error.message << std::endl;
      std::cout << explanation << std::endl;
      return false;
    }
};

int main(int argc, char **argv)
{
  if (argc != 3)
  {
    std::cout << "Usage: ndb_ndbapi_basic_delete <connectstring> <key: int>"
              << std::endl;
    return EXIT_FAILURE;
  }

  const char *connectstring = argv[1];
  const long long key = std::strtoll(argv[2], nullptr, 10);

  ndb_init();
  {
    BasicDelete example(connectstring);
    if (!example.init()) return EXIT_FAILURE;

    // Let's verify delete
    if (example.do_delete(key))
      std::cout << "Done, check your database:\n"
                << "\t SELECT * FROM ndbapi_examples.basic;\n"
                << "\t or run the example: ndb_ndbapi_basic_read"
                << std::endl;
    else return EXIT_FAILURE;
  }
  ndb_end(0);

  return EXIT_SUCCESS;
}

bool BasicDelete::do_delete(long long key)
{
  const NdbDictionary::Dictionary *dict = m_ndb.getDictionary();
  const NdbDictionary::Table *table = dict->getTable("basic");

  if (table == nullptr)
    return on_error(dict->getNdbError(),
                    "Failed to access 'ndbapi_examples.basic'");

  // The delete operation will be performed within single transaction
  NdbTransaction *transaction = m_ndb.startTransaction(table);
  if(transaction == nullptr)
    return on_error(m_ndb.getNdbError(), "Failed to start transaction");

  NdbOperation *operation = transaction->getNdbOperation(table);
  if(operation == nullptr)
    return on_error(transaction->getNdbError(),
                    "Failed to start delete operation");

  operation->deleteTuple();
  operation->equal("ATTR1", key);

  if (transaction->execute(NdbTransaction::Commit) != 0)
    return on_error(transaction->getNdbError(),
        "Failed to execute transaction");

  m_ndb.closeTransaction(transaction);

  return true;
}

bool BasicDelete::init()
{
  if (m_connection.connect() != 0)
  {
    std::cout << "Cannot connect to cluster management server" << std::endl;
    return false;
  }

  if (m_connection.wait_until_ready(30, 0) != 0)
  {
    std::cout << "Cluster was not ready within 30 secs" << std::endl;
    return false;
  }

  if (m_ndb.init() != 0)
    return on_error(m_ndb.getNdbError(), "Failed to initialize ndb object");

  return true;
}