~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/ndbapi-examples/ndbapi_simple/ndbapi_simple.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* 
 
17
 *  ndbapi_simple.cpp: Using synchronous transactions in NDB API
 
18
 *
 
19
 *  Correct output from this program is:
 
20
 *
 
21
 *  ATTR1 ATTR2
 
22
 *    0    10
 
23
 *    1     1
 
24
 *    2    12
 
25
 *  Detected that deleted tuple doesn't exist!
 
26
 *    4    14
 
27
 *    5     5
 
28
 *    6    16
 
29
 *    7     7
 
30
 *    8    18
 
31
 *    9     9
 
32
 *
 
33
 */
 
34
 
 
35
#include <mysql.h>
 
36
#include <NdbApi.hpp>
 
37
// Used for cout
 
38
#include <stdio.h>
 
39
#include <iostream>
 
40
 
 
41
static void run_application(MYSQL &, Ndb_cluster_connection &);
 
42
 
 
43
#define PRINT_ERROR(code,msg) \
 
44
  std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
 
45
            << ", code: " << code \
 
46
            << ", msg: " << msg << "." << std::endl
 
47
#define MYSQLERROR(mysql) { \
 
48
  PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
 
49
  exit(-1); }
 
50
#define APIERROR(error) { \
 
51
  PRINT_ERROR(error.code,error.message); \
 
52
  exit(-1); }
 
53
 
 
54
int main(int argc, char** argv)
 
55
{
 
56
  if (argc != 3)
 
57
  {
 
58
    std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n";
 
59
    exit(-1);
 
60
  }
 
61
  // ndb_init must be called first
 
62
  ndb_init();
 
63
 
 
64
  // connect to mysql server and cluster and run application
 
65
  {
 
66
    char * mysqld_sock  = argv[1];
 
67
    const char *connectstring = argv[2];
 
68
    // Object representing the cluster
 
69
    Ndb_cluster_connection cluster_connection(connectstring);
 
70
 
 
71
    // Connect to cluster management server (ndb_mgmd)
 
72
    if (cluster_connection.connect(4 /* retries               */,
 
73
                                   5 /* delay between retries */,
 
74
                                   1 /* verbose               */))
 
75
    {
 
76
      std::cout << "Cluster management server was not ready within 30 secs.\n";
 
77
      exit(-1);
 
78
    }
 
79
 
 
80
    // Optionally connect and wait for the storage nodes (ndbd's)
 
81
    if (cluster_connection.wait_until_ready(30,0) < 0)
 
82
    {
 
83
      std::cout << "Cluster was not ready within 30 secs.\n";
 
84
      exit(-1);
 
85
    }
 
86
 
 
87
    // connect to mysql server
 
88
    MYSQL mysql;
 
89
    if ( !mysql_init(&mysql) ) {
 
90
      std::cout << "mysql_init failed\n";
 
91
      exit(-1);
 
92
    }
 
93
    if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
 
94
                             0, mysqld_sock, 0) )
 
95
      MYSQLERROR(mysql);
 
96
    
 
97
    // run the application code
 
98
    run_application(mysql, cluster_connection);
 
99
  }
 
100
 
 
101
  ndb_end(0);
 
102
 
 
103
  return 0;
 
104
}
 
105
 
 
106
static void create_table(MYSQL &);
 
107
static void drop_table(MYSQL &);
 
108
static void do_insert(Ndb &);
 
109
static void do_update(Ndb &);
 
110
static void do_delete(Ndb &);
 
111
static void do_read(Ndb &);
 
112
 
 
113
static void run_application(MYSQL &mysql,
 
114
                            Ndb_cluster_connection &cluster_connection)
 
115
{
 
116
  /********************************************
 
117
   * Connect to database via mysql-c          *
 
118
   ********************************************/
 
119
  mysql_query(&mysql, "CREATE DATABASE TEST_DB_1");
 
120
  if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql);
 
121
  create_table(mysql);
 
122
 
 
123
  /********************************************
 
124
   * Connect to database via NdbApi           *
 
125
   ********************************************/
 
126
  // Object representing the database
 
127
  Ndb myNdb( &cluster_connection, "TEST_DB_1" );
 
128
  if (myNdb.init()) APIERROR(myNdb.getNdbError());
 
129
 
 
130
  /*
 
131
   * Do different operations on database
 
132
   */
 
133
  do_insert(myNdb);
 
134
  do_update(myNdb);
 
135
  do_delete(myNdb);
 
136
  do_read(myNdb);
 
137
  drop_table(mysql);
 
138
  mysql_query(&mysql, "DROP DATABASE TEST_DB_1");
 
139
}
 
140
 
 
141
/*********************************************************
 
142
 * Create a table named MYTABLENAME if it does not exist *
 
143
 *********************************************************/
 
144
static void create_table(MYSQL &mysql)
 
145
{
 
146
  if (mysql_query(&mysql, 
 
147
                  "CREATE TABLE"
 
148
                  "  MYTABLENAME"
 
149
                  "    (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
 
150
                  "     ATTR2 INT UNSIGNED NOT NULL)"
 
151
                  "  ENGINE=NDB"))
 
152
    MYSQLERROR(mysql);
 
153
}
 
154
 
 
155
/***********************************
 
156
 * Drop a table named MYTABLENAME 
 
157
 ***********************************/
 
158
static void drop_table(MYSQL &mysql)
 
159
{
 
160
  if (mysql_query(&mysql, 
 
161
                  "DROP TABLE"
 
162
                  "  MYTABLENAME"))
 
163
    MYSQLERROR(mysql);
 
164
}
 
165
 
 
166
/**************************************************************************
 
167
 * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
 
168
 **************************************************************************/
 
169
static void do_insert(Ndb &myNdb)
 
170
{
 
171
  const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
 
172
  const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
 
173
 
 
174
  if (myTable == NULL) 
 
175
    APIERROR(myDict->getNdbError());
 
176
 
 
177
  for (int i = 0; i < 5; i++) {
 
178
    NdbTransaction *myTransaction= myNdb.startTransaction();
 
179
    if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
 
180
    
 
181
    NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
 
182
    if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
 
183
    
 
184
    myOperation->insertTuple();
 
185
    myOperation->equal("ATTR1", i);
 
186
    myOperation->setValue("ATTR2", i);
 
187
 
 
188
    myOperation= myTransaction->getNdbOperation(myTable);
 
189
    if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
 
190
 
 
191
    myOperation->insertTuple();
 
192
    myOperation->equal("ATTR1", i+5);
 
193
    myOperation->setValue("ATTR2", i+5);
 
194
    
 
195
    if (myTransaction->execute( NdbTransaction::Commit ) == -1)
 
196
      APIERROR(myTransaction->getNdbError());
 
197
    
 
198
    myNdb.closeTransaction(myTransaction);
 
199
  }
 
200
}
 
201
 
 
202
/*****************************************************************
 
203
 * Update the second attribute in half of the tuples (adding 10) *
 
204
 *****************************************************************/
 
205
static void do_update(Ndb &myNdb)
 
206
{
 
207
  const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
 
208
  const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
 
209
 
 
210
  if (myTable == NULL) 
 
211
    APIERROR(myDict->getNdbError());
 
212
 
 
213
  for (int i = 0; i < 10; i+=2) {
 
214
    NdbTransaction *myTransaction= myNdb.startTransaction();
 
215
    if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
 
216
    
 
217
    NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
 
218
    if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
 
219
    
 
220
    myOperation->updateTuple();
 
221
    myOperation->equal( "ATTR1", i );
 
222
    myOperation->setValue( "ATTR2", i+10);
 
223
    
 
224
    if( myTransaction->execute( NdbTransaction::Commit ) == -1 ) 
 
225
      APIERROR(myTransaction->getNdbError());
 
226
    
 
227
    myNdb.closeTransaction(myTransaction);
 
228
  }
 
229
}
 
230
  
 
231
/*************************************************
 
232
 * Delete one tuple (the one with primary key 3) *
 
233
 *************************************************/
 
234
static void do_delete(Ndb &myNdb)
 
235
{
 
236
  const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
 
237
  const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
 
238
 
 
239
  if (myTable == NULL) 
 
240
    APIERROR(myDict->getNdbError());
 
241
 
 
242
  NdbTransaction *myTransaction= myNdb.startTransaction();
 
243
  if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
 
244
  
 
245
  NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
 
246
  if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
 
247
  
 
248
  myOperation->deleteTuple();
 
249
  myOperation->equal( "ATTR1", 3 );
 
250
  
 
251
  if (myTransaction->execute(NdbTransaction::Commit) == -1) 
 
252
    APIERROR(myTransaction->getNdbError());
 
253
  
 
254
  myNdb.closeTransaction(myTransaction);
 
255
}
 
256
 
 
257
/*****************************
 
258
 * Read and print all tuples *
 
259
 *****************************/
 
260
static void do_read(Ndb &myNdb)
 
261
{
 
262
  const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
 
263
  const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
 
264
 
 
265
  if (myTable == NULL) 
 
266
    APIERROR(myDict->getNdbError());
 
267
 
 
268
  std::cout << "ATTR1 ATTR2" << std::endl;
 
269
  
 
270
  for (int i = 0; i < 10; i++) {
 
271
    NdbTransaction *myTransaction= myNdb.startTransaction();
 
272
    if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
 
273
    
 
274
    NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
 
275
    if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
 
276
    
 
277
    myOperation->readTuple(NdbOperation::LM_Read);
 
278
    myOperation->equal("ATTR1", i);
 
279
 
 
280
    NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
 
281
    if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
 
282
    
 
283
    if(myTransaction->execute( NdbTransaction::Commit ) == -1)
 
284
      APIERROR(myTransaction->getNdbError());
 
285
    
 
286
    if (myTransaction->getNdbError().classification == NdbError::NoDataFound)
 
287
      if (i == 3)
 
288
        std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
 
289
      else
 
290
        APIERROR(myTransaction->getNdbError());
 
291
 
 
292
    if (i != 3) {
 
293
      printf(" %2d    %2d\n", i, myRecAttr->u_32_value());
 
294
    }
 
295
    myNdb.closeTransaction(myTransaction);
 
296
  }
 
297
}