You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.2 KiB

ndb_init() to all ndb programs ndb/examples/ndbapi_async_example/ndbapi_async.cpp: ndb_init() ndb/examples/ndbapi_example1/ndbapi_example1.cpp: ndb_init() ndb/examples/ndbapi_example2/ndbapi_example2.cpp: ndb_init() ndb/examples/ndbapi_example3/ndbapi_example3.cpp: ndb_init() ndb/examples/ndbapi_example4/ndbapi_example4.cpp: ndb_init() ndb/examples/ndbapi_example5/ndbapi_example5.cpp: ndb_init() ndb/examples/ndbapi_scan_example/ndbapi_scan.cpp: ndb_init() ndb/examples/select_all/select_all.cpp: ndb_init() ndb/include/ndb_global.h: ndb_init() ndb/src/common/util/Makefile.am: ndb_init() ndb/src/kernel/blocks/backup/read.cpp: ndb_init() ndb/src/kernel/blocks/backup/restore/main.cpp: ndb_init() ndb/src/kernel/main.cpp: ndb_init() ndb/src/kernel/vm/Configuration.cpp: ndb_init() ndb/src/mgmclient/main.cpp: ndb_init() ndb/src/mgmsrv/main.cpp: ndb_init() ndb/src/mgmsrv/mkconfig/mkconfig.cpp: ndb_init() ndb/src/ndbapi/Ndbinit.cpp: ndb_init() ndb/test/ndbapi/acid.cpp: ndb_init() ndb/test/ndbapi/acid2.cpp: ndb_init() ndb/test/ndbapi/benchronja.cpp: ndb_init() ndb/test/ndbapi/bulk_copy.cpp: ndb_init() ndb/test/ndbapi/cdrserver.cpp: ndb_init() ndb/test/ndbapi/celloDb.cpp: ndb_init() ndb/test/ndbapi/create_all_tabs.cpp: ndb_init() ndb/test/ndbapi/create_tab.cpp: ndb_init() ndb/test/ndbapi/drop_all_tabs.cpp: ndb_init() ndb/test/ndbapi/flexAsynch.cpp: ndb_init() ndb/test/ndbapi/flexBench.cpp: ndb_init() ndb/test/ndbapi/flexHammer.cpp: ndb_init() ndb/test/ndbapi/flexScan.cpp: ndb_init() ndb/test/ndbapi/flexTT.cpp: ndb_init() ndb/test/ndbapi/flexTimedAsynch.cpp: ndb_init() ndb/test/ndbapi/flex_bench_mysql.cpp: ndb_init() ndb/test/ndbapi/index.cpp: ndb_init() ndb/test/ndbapi/index2.cpp: ndb_init() ndb/test/ndbapi/initronja.cpp: ndb_init() ndb/test/ndbapi/interpreterInTup.cpp: ndb_init() ndb/test/ndbapi/mainAsyncGenerator.cpp: ndb_init() ndb/test/ndbapi/msa.cpp: ndb_init() ndb/test/ndbapi/restarter.cpp: ndb_init() ndb/test/ndbapi/restarter2.cpp: ndb_init() ndb/test/ndbapi/restarts.cpp: ndb_init() ndb/test/ndbapi/size.cpp: ndb_init() ndb/test/ndbapi/slow_select.cpp: ndb_init() ndb/test/ndbapi/testBackup.cpp: ndb_init() ndb/test/ndbapi/testBasic.cpp: ndb_init() ndb/test/ndbapi/testBasicAsynch.cpp: ndb_init() ndb/test/ndbapi/testBlobs.cpp: ndb_init() ndb/test/ndbapi/testDataBuffers.cpp: ndb_init() ndb/test/ndbapi/testDeadlock.cpp: ndb_init() ndb/test/ndbapi/testDict.cpp: ndb_init() ndb/test/ndbapi/testGrep.cpp: ndb_init() ndb/test/ndbapi/testGrepVerify.cpp: ndb_init() ndb/test/ndbapi/testIndex.cpp: ndb_init() ndb/test/ndbapi/testInterpreter.cpp: ndb_init() ndb/test/ndbapi/testMgm.cpp: ndb_init() ndb/test/ndbapi/bank/bankCreator.cpp: ndb_init() ndb/test/ndbapi/bank/bankMakeGL.cpp: ndb_init() ndb/test/ndbapi/bank/bankSumAccounts.cpp: ndb_init() ndb/test/ndbapi/bank/bankTimer.cpp: ndb_init() ndb/test/ndbapi/bank/bankTransactionMaker.cpp: ndb_init() ndb/test/ndbapi/bank/bankValidateAllGLs.cpp: ndb_init() ndb/test/ndbapi/bank/testBank.cpp: ndb_init() ndb/test/ndbapi/testNdbApi.cpp: ndb_init() ndb/test/ndbapi/testNodeRestart.cpp: ndb_init() ndb/test/ndbapi/testOIBasic.cpp: ndb_init() ndb/test/ndbapi/testOperations.cpp: ndb_init() ndb/test/ndbapi/testOrderedIndex.cpp: ndb_init() ndb/test/ndbapi/testReadPerf.cpp: ndb_init() ndb/test/ndbapi/testRestartGci.cpp: ndb_init() ndb/test/ndbapi/testScan.cpp: ndb_init() ndb/test/ndbapi/testScanInterpreter.cpp: ndb_init() ndb/test/ndbapi/testScanPerf.cpp: ndb_init() ndb/test/ndbapi/testSystemRestart.cpp: ndb_init() ndb/test/ndbapi/testTimeout.cpp: ndb_init() ndb/test/ndbapi/testTransactions.cpp: ndb_init() ndb/test/ndbapi/test_event.cpp: ndb_init() ndb/test/run-test/main.cpp: ndb_init() ndb/test/src/NDBT_Test.cpp: ndb_init() ndb/test/tools/copy_tab.cpp: ndb_init() ndb/test/tools/cpcc.cpp: ndb_init() ndb/test/tools/create_index.cpp: ndb_init() ndb/test/tools/hugoCalculator.cpp: ndb_init() ndb/test/tools/hugoFill.cpp: ndb_init() ndb/test/tools/hugoLoad.cpp: ndb_init() ndb/test/tools/hugoLockRecords.cpp: ndb_init() ndb/test/tools/hugoPkDelete.cpp: ndb_init() ndb/test/tools/hugoPkRead.cpp: ndb_init() ndb/test/tools/hugoPkReadRecord.cpp: ndb_init() ndb/test/tools/hugoPkUpdate.cpp: ndb_init() ndb/test/tools/hugoScanRead.cpp: ndb_init() ndb/test/tools/hugoScanUpdate.cpp: ndb_init() ndb/test/tools/restart.cpp: ndb_init() ndb/test/tools/transproxy.cpp: ndb_init() ndb/test/tools/verify_index.cpp: ndb_init() ndb/tools/delete_all.cpp: ndb_init() ndb/tools/desc.cpp: ndb_init() ndb/tools/drop_index.cpp: ndb_init() ndb/tools/drop_tab.cpp: ndb_init() ndb/tools/listTables.cpp: ndb_init() ndb/tools/ndbsql.cpp: ndb_init() ndb/tools/select_all.cpp: ndb_init() ndb/tools/select_count.cpp: ndb_init() ndb/tools/waiter.cpp: ndb_init()
22 years ago
  1. /* Copyright (C) 2003 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. //
  14. // ndbapi_example1.cpp: Using synchronous transactions in NDB API
  15. //
  16. // Correct output from this program is:
  17. //
  18. // ATTR1 ATTR2
  19. // 0 10
  20. // 1 1
  21. // 2 12
  22. // Detected that deleted tuple doesn't exist!
  23. // 4 14
  24. // 5 5
  25. // 6 16
  26. // 7 7
  27. // 8 18
  28. // 9 9
  29. #include <NdbApi.hpp>
  30. // Used for cout
  31. #include <stdio.h>
  32. #include <iostream>
  33. #define APIERROR(error) \
  34. { std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \
  35. << error.code << ", msg: " << error.message << "." << std::endl; \
  36. exit(-1); }
  37. int main()
  38. {
  39. ndb_init();
  40. Ndb* myNdb = new Ndb( "TEST_DB_1" ); // Object representing the database
  41. NdbDictionary::Table myTable;
  42. NdbDictionary::Column myColumn;
  43. NdbConnection *myConnection; // For other transactions
  44. NdbOperation *myOperation; // For other operations
  45. NdbRecAttr *myRecAttr; // Result of reading attribute value
  46. /********************************************
  47. * Initialize NDB and wait until it's ready *
  48. ********************************************/
  49. if (myNdb->init() == -1) {
  50. APIERROR(myNdb->getNdbError());
  51. exit(-1);
  52. }
  53. if (myNdb->waitUntilReady(30) != 0) {
  54. std::cout << "NDB was not ready within 30 secs." << std::endl;
  55. exit(-1);
  56. }
  57. NdbDictionary::Dictionary* myDict = myNdb->getDictionary();
  58. /*********************************************************
  59. * Create a table named MYTABLENAME if it does not exist *
  60. *********************************************************/
  61. if (myDict->getTable("MYTABLENAME") != NULL) {
  62. std::cout << "NDB already has example table: MYTABLENAME." << std::endl;
  63. exit(-1);
  64. }
  65. myTable.setName("MYTABLENAME");
  66. myColumn.setName("ATTR1");
  67. myColumn.setType(NdbDictionary::Column::Unsigned);
  68. myColumn.setLength(1);
  69. myColumn.setPrimaryKey(true);
  70. myColumn.setNullable(false);
  71. myTable.addColumn(myColumn);
  72. myColumn.setName("ATTR2");
  73. myColumn.setType(NdbDictionary::Column::Unsigned);
  74. myColumn.setLength(1);
  75. myColumn.setPrimaryKey(false);
  76. myColumn.setNullable(false);
  77. myTable.addColumn(myColumn);
  78. if (myDict->createTable(myTable) == -1)
  79. APIERROR(myDict->getNdbError());
  80. /**************************************************************************
  81. * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
  82. **************************************************************************/
  83. for (int i = 0; i < 5; i++) {
  84. myConnection = myNdb->startTransaction();
  85. if (myConnection == NULL) APIERROR(myNdb->getNdbError());
  86. myOperation = myConnection->getNdbOperation("MYTABLENAME");
  87. if (myOperation == NULL) APIERROR(myConnection->getNdbError());
  88. myOperation->insertTuple();
  89. myOperation->equal("ATTR1", i);
  90. myOperation->setValue("ATTR2", i);
  91. myOperation = myConnection->getNdbOperation("MYTABLENAME");
  92. if (myOperation == NULL) APIERROR(myConnection->getNdbError());
  93. myOperation->insertTuple();
  94. myOperation->equal("ATTR1", i+5);
  95. myOperation->setValue("ATTR2", i+5);
  96. if (myConnection->execute( Commit ) == -1)
  97. APIERROR(myConnection->getNdbError());
  98. myNdb->closeTransaction(myConnection);
  99. }
  100. /*****************************************************************
  101. * Update the second attribute in half of the tuples (adding 10) *
  102. *****************************************************************/
  103. for (int i = 0; i < 10; i+=2) {
  104. myConnection = myNdb->startTransaction();
  105. if (myConnection == NULL) APIERROR(myNdb->getNdbError());
  106. myOperation = myConnection->getNdbOperation("MYTABLENAME");
  107. if (myOperation == NULL) APIERROR(myConnection->getNdbError());
  108. myOperation->updateTuple();
  109. myOperation->equal( "ATTR1", i );
  110. myOperation->setValue( "ATTR2", i+10);
  111. if( myConnection->execute( Commit ) == -1 )
  112. APIERROR(myConnection->getNdbError());
  113. myNdb->closeTransaction(myConnection);
  114. }
  115. /*************************************************
  116. * Delete one tuple (the one with primary key 3) *
  117. *************************************************/
  118. myConnection = myNdb->startTransaction();
  119. if (myConnection == NULL) APIERROR(myNdb->getNdbError());
  120. myOperation = myConnection->getNdbOperation("MYTABLENAME");
  121. if (myOperation == NULL)
  122. APIERROR(myConnection->getNdbError());
  123. myOperation->deleteTuple();
  124. myOperation->equal( "ATTR1", 3 );
  125. if (myConnection->execute(Commit) == -1)
  126. APIERROR(myConnection->getNdbError());
  127. myNdb->closeTransaction(myConnection);
  128. /*****************************
  129. * Read and print all tuples *
  130. *****************************/
  131. std::cout << "ATTR1 ATTR2" << std::endl;
  132. for (int i = 0; i < 10; i++) {
  133. myConnection = myNdb->startTransaction();
  134. if (myConnection == NULL) APIERROR(myNdb->getNdbError());
  135. myOperation = myConnection->getNdbOperation("MYTABLENAME");
  136. if (myOperation == NULL) APIERROR(myConnection->getNdbError());
  137. myOperation->readTuple();
  138. myOperation->equal("ATTR1", i);
  139. myRecAttr = myOperation->getValue("ATTR2", NULL);
  140. if (myRecAttr == NULL) APIERROR(myConnection->getNdbError());
  141. if(myConnection->execute( Commit ) == -1)
  142. if (i == 3) {
  143. std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
  144. } else {
  145. APIERROR(myConnection->getNdbError());
  146. }
  147. if (i != 3) {
  148. printf(" %2d %2d\n", i, myRecAttr->u_32_value());
  149. }
  150. myNdb->closeTransaction(myConnection);
  151. }
  152. delete myNdb;
  153. }