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