summaryrefslogtreecommitdiff
path: root/ndb/examples/ndbapi_example1/ndbapi_example1.cpp
blob: 03a84aa249bf4c105cc4dbd947f356ea81f30a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

// 
//  ndbapi_example1.cpp: Using synchronous transactions in NDB API
//
//  Correct output from this program is:
//
//  ATTR1 ATTR2
//    0    10
//    1     1
//    2    12
//  Detected that deleted tuple doesn't exist!
//    4    14
//    5     5
//    6    16
//    7     7
//    8    18
//    9     9

#include <NdbApi.hpp>

// Used for cout
#include <stdio.h>
#include <iostream>

#define APIERROR(error) \
  { std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \
              << error.code << ", msg: " << error.message << "." << std::endl; \
    exit(-1); }

int main()
{
  ndb_init();
  Ndb* myNdb = new Ndb( "TEST_DB_1" );  // Object representing the database
  NdbDictionary::Table myTable;
  NdbDictionary::Column myColumn;

  NdbConnection	 *myConnection;         // For other transactions
  NdbOperation	 *myOperation;          // For other operations
  NdbRecAttr     *myRecAttr;            // Result of reading attribute value
  
  /********************************************
   * Initialize NDB and wait until it's ready *
   ********************************************/
  if (myNdb->init() == -1) { 
    APIERROR(myNdb->getNdbError());
    exit(-1);
  }

  if (myNdb->waitUntilReady(30) != 0) {
    std::cout << "NDB was not ready within 30 secs." << std::endl;
    exit(-1);
  }
  
  NdbDictionary::Dictionary* myDict = myNdb->getDictionary();
  
  /*********************************************************
   * Create a table named MYTABLENAME if it does not exist *
   *********************************************************/
  if (myDict->getTable("MYTABLENAME") != NULL) {
    std::cout << "NDB already has example table: MYTABLENAME." << std::endl; 
    exit(-1);
  } 

  myTable.setName("MYTABLENAME");
  
  myColumn.setName("ATTR1");
  myColumn.setType(NdbDictionary::Column::Unsigned);
  myColumn.setLength(1);
  myColumn.setPrimaryKey(true);
  myColumn.setNullable(false);
  myTable.addColumn(myColumn);

  myColumn.setName("ATTR2");
  myColumn.setType(NdbDictionary::Column::Unsigned);
  myColumn.setLength(1);
  myColumn.setPrimaryKey(false);
  myColumn.setNullable(false);
  myTable.addColumn(myColumn);

  if (myDict->createTable(myTable) == -1) 
      APIERROR(myDict->getNdbError());

  /**************************************************************************
   * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
   **************************************************************************/
  for (int i = 0; i < 5; i++) {
    myConnection = myNdb->startTransaction();
    if (myConnection == NULL) APIERROR(myNdb->getNdbError());
    
    myOperation = myConnection->getNdbOperation("MYTABLENAME");	
    if (myOperation == NULL) APIERROR(myConnection->getNdbError());
    
    myOperation->insertTuple();
    myOperation->equal("ATTR1", i);
    myOperation->setValue("ATTR2", i);

    myOperation = myConnection->getNdbOperation("MYTABLENAME");	
    if (myOperation == NULL) APIERROR(myConnection->getNdbError());

    myOperation->insertTuple();
    myOperation->equal("ATTR1", i+5);
    myOperation->setValue("ATTR2", i+5);
    
    if (myConnection->execute( Commit ) == -1)
      APIERROR(myConnection->getNdbError());
    
    myNdb->closeTransaction(myConnection);
  }
  
  /*****************************************************************
   * Update the second attribute in half of the tuples (adding 10) *
   *****************************************************************/
  for (int i = 0; i < 10; i+=2) {
    myConnection = myNdb->startTransaction();
    if (myConnection == NULL) APIERROR(myNdb->getNdbError());
    
    myOperation = myConnection->getNdbOperation("MYTABLENAME");	
    if (myOperation == NULL) APIERROR(myConnection->getNdbError());
    
    myOperation->updateTuple();
    myOperation->equal( "ATTR1", i );
    myOperation->setValue( "ATTR2", i+10);
    
    if( myConnection->execute( Commit ) == -1 ) 
      APIERROR(myConnection->getNdbError());
    
    myNdb->closeTransaction(myConnection);
  }
  
  /*************************************************
   * Delete one tuple (the one with primary key 3) *
   *************************************************/
  myConnection = myNdb->startTransaction();
  if (myConnection == NULL) APIERROR(myNdb->getNdbError());
  
  myOperation = myConnection->getNdbOperation("MYTABLENAME");	
  if (myOperation == NULL) 
    APIERROR(myConnection->getNdbError());
  
  myOperation->deleteTuple();
  myOperation->equal( "ATTR1", 3 );
  
  if (myConnection->execute(Commit) == -1) 
    APIERROR(myConnection->getNdbError());
  
  myNdb->closeTransaction(myConnection);
  
  /*****************************
   * Read and print all tuples *
   *****************************/
  std::cout << "ATTR1 ATTR2" << std::endl;
  
  for (int i = 0; i < 10; i++) {
    myConnection = myNdb->startTransaction();
    if (myConnection == NULL) APIERROR(myNdb->getNdbError());
    
    myOperation = myConnection->getNdbOperation("MYTABLENAME");	
    if (myOperation == NULL) APIERROR(myConnection->getNdbError());
    
    myOperation->readTuple();
    myOperation->equal("ATTR1", i);
    
    myRecAttr = myOperation->getValue("ATTR2", NULL);
    if (myRecAttr == NULL) APIERROR(myConnection->getNdbError());
    
    if(myConnection->execute( Commit ) == -1)
      if (i == 3) {
	std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
      } else {
	APIERROR(myConnection->getNdbError());
      }
    
    if (i != 3) {
      printf(" %2d    %2d\n", i, myRecAttr->u_32_value());
    }
    myNdb->closeTransaction(myConnection);
  }
  delete myNdb;
}