MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
basic.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
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 
21 /****** THIS LINE IS 80 CHARACTERS WIDE - DO *NOT* EXCEED 80 CHARACTERS! ****/
22 extern "C" {
23 #include <dba.h>
24 }
25 
26 #include "common.hpp"
27 
28 #include <NdbOut.hpp>
29 #include <NdbSleep.h>
30 #include <NdbMain.h>
31 
32 static const
33 DBA_ColumnDesc_t EmpColDesc[] = {
34  { "emp_no", DBA_INT, PCN_SIZE_OF( Employee, EmpNo ), PCN_TRUE },
35  { "first_name", DBA_CHAR, PCN_SIZE_OF( Employee, FirstName ), PCN_FALSE },
36  { "last_name", DBA_CHAR, PCN_SIZE_OF( Employee, LastName ), PCN_FALSE }
37 };
38 
39 static const
40 DBA_ColumnDesc_t AddColDesc[] = {
41  { "emp_no", DBA_INT, PCN_SIZE_OF( Address, EmpNo ), PCN_TRUE },
42  { "street_name", DBA_CHAR, PCN_SIZE_OF( Address, StreetName ), PCN_FALSE},
43  { "street_no", DBA_INT, PCN_SIZE_OF( Address, StreetNo ), PCN_FALSE},
44  { "city", DBA_CHAR, PCN_SIZE_OF( Address, City ), PCN_FALSE}
45 } ;
46 
47 static const
48 DBA_ColumnBinding_t EmpBindings[] = {
49  DBA_BINDING( "emp_no", DBA_INT, Employee, EmpNo ),
50  DBA_BINDING( "last_name", DBA_CHAR, Employee, LastName ),
51  DBA_BINDING( "first_name", DBA_CHAR, Employee, FirstName)
52 };
53 
54 static const
55 DBA_ColumnBinding_t AddBindings[] = {
56  DBA_BINDING( "emp_no", DBA_INT, Address, EmpNo ),
57  DBA_BINDING( "street_name", DBA_CHAR, Address, StreetName ),
58  DBA_BINDING( "street_no", DBA_INT, Address, StreetNo ),
59  DBA_BINDING( "city", DBA_CHAR, Address, City )
60 };
61 
62 static DBA_Binding_t * EmpB;
63 static DBA_Binding_t * AddB;
64 
65 static const int Rows = 6;
66 
67 static
68 Employee_t EMP_TABLE_DATA[] = {
69  { 1242, "Joe", "Dalton" },
70  { 123, "Lucky", "Luke" },
71  { 456, "Averell", "Dalton" },
72  { 8976, "Gaston", "Lagaffe" },
73  { 1122, "Jolly", "Jumper" },
74  { 3211, "Leffe", "Pagrotsky" }
75 };
76 
77 static
78 Employee_t EMP_TABLE_DATA_READ[] = {
79  { 1242, "", "" },
80  { 123, "", "" },
81  { 456, "", "" },
82  { 8976, "", "" },
83  { 1122, "", "" },
84  { 3211, "", "" }
85 };
86 
87 static
88 Address_t ADD_TABLE_DATA[] = {
89  { 1242, "Lonesome Street", 12, "Crime Town" },
90  { 123, "Pistol Road", 13, "Fort Mount" },
91  { 456, "Banking Blv.", 43, "Las Vegas" },
92  { 8976, "ChancylleZee", 54, "Paris" },
93  { 1122, "Lucky", 111, "Wild West" },
94  { 3211, "Parlament St.", 11, "Stockholm" }
95 };
96 
97 static
98 Address_t ADD_TABLE_DATA_READ[] = {
99  { 1242, "", 0, "" },
100  { 123, "", 0, "" },
101  { 456, "", 0, "" },
102  { 8976, "", 0, "" },
103  { 1122, "", 0, "" },
104  { 3211, "", 0, "" }
105 };
106 
107 static const char EMP_TABLE[] = "employees";
108 static const char ADD_TABLE[] = "addresses";
109 
110 static const int EmpNbCol = 3;
111 static const int AddNbCol = 4;
112 
113 static
114 void
115 DbCreate(void){
116 
117  ndbout << "Opening database" << endl;
118  require( DBA_Open() == DBA_NO_ERROR );
119 
120  ndbout << "Creating tables" << endl;
121  require( DBA_CreateTable( EMP_TABLE, EmpNbCol, EmpColDesc ) == DBA_NO_ERROR );
122  require( DBA_CreateTable( ADD_TABLE, AddNbCol, AddColDesc ) == DBA_NO_ERROR );
123 
124  ndbout << "Checking for table existance" << endl;
125  require( DBA_TableExists( EMP_TABLE ) );
126  require( DBA_TableExists( ADD_TABLE ) );
127 }
128 
129 static
130 void
131 CreateBindings(void){
132  ndbout << "Creating bindings" << endl;
133 
134  EmpB = DBA_CreateBinding(EMP_TABLE,
135  EmpNbCol,
136  EmpBindings,
137  sizeof(Employee_t) );
138  require(EmpB != 0);
139 
140  AddB = DBA_CreateBinding(ADD_TABLE,
141  AddNbCol,
142  AddBindings,
143  sizeof(Address_t) );
144  require(AddB != 0);
145 }
146 
147 extern "C" {
148  static void insertCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
149  static void deleteCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
150  static void updateCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
151  static void readCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
152  static void writeCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
153 }
154 
155 static
156 void BasicArray(){
157  ndbout << "Testing basic array operations" << endl;
158 
159  // Basic insert
160  DBA_ArrayInsertRows(EmpB, EMP_TABLE_DATA, Rows-2, insertCallback);
161  NdbSleep_SecSleep(1);
162  DBA_ArrayReadRows(EmpB, EMP_TABLE_DATA_READ, Rows-2, readCallback);
163  NdbSleep_SecSleep(1);
164  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
165 
166  // Basic update
167  AlterRows(EMP_TABLE_DATA, Rows-2);
168  DBA_ArrayUpdateRows(EmpB, EMP_TABLE_DATA, Rows-2, updateCallback);
169  NdbSleep_SecSleep(1);
170  DBA_ArrayReadRows(EmpB, EMP_TABLE_DATA_READ, Rows-2, readCallback);
171  NdbSleep_SecSleep(1);
172  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
173 
174  // Basic write
175  AlterRows(EMP_TABLE_DATA, Rows);
176  DBA_ArrayWriteRows(EmpB, EMP_TABLE_DATA, Rows, writeCallback);
177  NdbSleep_SecSleep(1);
178  DBA_ArrayReadRows(EmpB, EMP_TABLE_DATA_READ, Rows, readCallback);
179  NdbSleep_SecSleep(1);
180  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
181 
182  // Basic delete
183  DBA_ArrayDeleteRows(EmpB, EMP_TABLE_DATA, Rows, deleteCallback);
184  NdbSleep_SecSleep(1);
185 }
186 
187 static
188 void Multi(){
189  ndbout << "Testing multi operations" << endl;
190 
191  const int R2 = Rows + Rows;
192 
193  DBA_Binding_t * Bindings[R2];
194  void * DATA[R2];
195  void * DATA_READ[R2];
196  for(int i = 0; i<Rows; i++){
197  Bindings[2*i] = EmpB;
198  Bindings[2*i+1] = AddB;
199 
200  DATA[2*i] = &EMP_TABLE_DATA[i];
201  DATA[2*i+1] = &ADD_TABLE_DATA[i];
202 
203  DATA_READ[2*i] = &EMP_TABLE_DATA_READ[i];
204  DATA_READ[2*i+1] = &ADD_TABLE_DATA_READ[i];
205  }
206 
207  // Basic insert
208  DBA_MultiInsertRow(Bindings, DATA, R2-4, insertCallback);
209  NdbSleep_SecSleep(1);
210  DBA_MultiReadRow (Bindings, DATA_READ, R2-4, readCallback);
211  NdbSleep_SecSleep(1);
212 
213  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
214  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
215 
216  // Basic update
217  AlterRows(EMP_TABLE_DATA, Rows-2);
218  AlterRows(ADD_TABLE_DATA, Rows-2);
219  DBA_MultiUpdateRow(Bindings, DATA, R2-4, updateCallback);
220  NdbSleep_SecSleep(1);
221  DBA_MultiReadRow (Bindings, DATA_READ, R2-4, readCallback);
222  NdbSleep_SecSleep(1);
223  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
224  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
225 
226  // Basic write
227  AlterRows(EMP_TABLE_DATA, Rows);
228  AlterRows(ADD_TABLE_DATA, Rows);
229  DBA_MultiWriteRow(Bindings, DATA, R2, writeCallback);
230  NdbSleep_SecSleep(1);
231  DBA_MultiReadRow (Bindings, DATA_READ, R2, readCallback);
232  NdbSleep_SecSleep(1);
233  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
234  CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
235 
236  // Basic delete
237  DBA_MultiDeleteRow(Bindings, DATA, R2, deleteCallback);
238  NdbSleep_SecSleep(1);
239 }
240 
241 static
242 void BasicPtr(){
243  ndbout << "Testing array of pointer operations" << endl;
244  Employee_t * EmpData[Rows];
245  Employee_t * EmpDataRead[Rows];
246  for(int i = 0; i<Rows; i++){
247  EmpData[i] = &EMP_TABLE_DATA[i];
248  EmpDataRead[i] = &EMP_TABLE_DATA_READ[i];
249  }
250 
251  void * const * EMP_TABLE_DATA2 = (void * const *)EmpData;
252  void * const * EMP_TABLE_DATA_READ2 = (void * const *)EmpDataRead;
253 
254  // Basic insert
255  DBA_InsertRows(EmpB, EMP_TABLE_DATA2, Rows-2, insertCallback);
256  NdbSleep_SecSleep(1);
257  DBA_ReadRows (EmpB, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
258  NdbSleep_SecSleep(1);
259  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
260 
261  // Basic update
262  AlterRows(EMP_TABLE_DATA, Rows-2);
263  DBA_UpdateRows(EmpB, EMP_TABLE_DATA2, Rows-2, updateCallback);
264  NdbSleep_SecSleep(1);
265  DBA_ReadRows (EmpB, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
266  NdbSleep_SecSleep(1);
267  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
268 
269  // Basic write
270  AlterRows (EMP_TABLE_DATA, Rows);
271  DBA_WriteRows(EmpB, EMP_TABLE_DATA2, Rows, writeCallback);
272  NdbSleep_SecSleep(1);
273  DBA_ReadRows (EmpB, EMP_TABLE_DATA_READ2, Rows, readCallback);
274  NdbSleep_SecSleep(1);
275  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
276 
277  // Basic delete
278  DBA_DeleteRows(EmpB, EMP_TABLE_DATA2, Rows, deleteCallback);
279  NdbSleep_SecSleep(1);
280 }
281 
282 /*---------------------------------------------------------------------------*/
283 NDB_COMMAND(newton_basic, "newton_basic",
284  "newton_basic", "newton_basic", 65535){
285 
286  DbCreate();
287  CreateBindings();
288 
289  BasicArray();
290  BasicPtr();
291  Multi();
292 
293  DBA_Close();
294 
295  return 0;
296 }
297 
298 
299 
303 void
304 callbackStatusCheck( DBA_Error_t status, const char* operation) {
305  ndbout_c("%s: %d", operation, status);
306 }
307 
308 void insertCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
309  callbackStatusCheck(s, "insert");
310 }
311 void deleteCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
312  callbackStatusCheck(s, "delete");
313 }
314 void updateCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
315  callbackStatusCheck(s, "update");
316 }
317 void readCallback ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
318  callbackStatusCheck(s, "read");
319 }
320 void writeCallback ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
321  callbackStatusCheck(s, "write");
322 }
323