summaryrefslogtreecommitdiff
path: root/storage/ndb/test/newtonapi/basic_test/ptr_binding/ptr_binding_test.cpp
blob: 1498782c929762b86f6b711259d0902f3fda2ed6 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* 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; version 2 of the License.

   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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */



/****** THIS LINE IS 80 CHARACTERS WIDE - DO *NOT* EXCEED 80 CHARACTERS! ****/
extern "C" {
#include <dba.h>
}

#include "common.hpp"

#include <NdbOut.hpp>
#include <NdbSleep.h>
#include <NdbMain.h>

static const
DBA_ColumnDesc_t ColDesc[] = {
  { "emp_no",     DBA_INT,  PCN_SIZE_OF( Employee, EmpNo ),      PCN_TRUE },
  { "first_name", DBA_CHAR, PCN_SIZE_OF( Employee, FirstName ),  PCN_FALSE },
  { "last_name",  DBA_CHAR, PCN_SIZE_OF( Employee, LastName ),   PCN_FALSE },
  { "street_name",DBA_CHAR, PCN_SIZE_OF( Address,  StreetName ), PCN_FALSE },
  { "street_no",  DBA_INT,  PCN_SIZE_OF( Address,  StreetNo ),   PCN_FALSE },
  { "city",       DBA_CHAR, PCN_SIZE_OF( Address,  City ),       PCN_FALSE }
};
static const int NbCol = 6;

static const
DBA_ColumnBinding_t AddBindings[] = {
  //DBA_BINDING( "emp_no",      DBA_INT,  Address, EmpNo ),
  DBA_BINDING( "street_name", DBA_CHAR, Address, StreetName ),
  DBA_BINDING( "street_no",   DBA_INT,  Address, StreetNo ),  
  DBA_BINDING( "city",        DBA_CHAR, Address, City )
};

static const 
int AddBindingRows = sizeof(AddBindings)/sizeof(DBA_ColumnBinding_t);

static const
DBA_ColumnBinding_t EmpBindings[] = {
  DBA_BINDING( "emp_no",      DBA_INT,  Employee, EmpNo ),
  DBA_BINDING( "last_name",   DBA_CHAR, Employee, LastName ),
  DBA_BINDING( "first_name",  DBA_CHAR, Employee, FirstName),
  DBA_BINDING_PTR(Employee, EmployeeAddress, AddBindings, AddBindingRows)
};
static const 
int EmpBindingRows = sizeof(EmpBindings)/sizeof(DBA_ColumnBinding_t);

static DBA_Binding_t * Bind;

static const int Rows = 6;

static 
Address_t ADD_TABLE_DATA[] = {
  { 1242, "Lonesome Street", 12, "Crime Town" },
  { 123,  "Pistol Road",     13, "Fort Mount" },
  { 456,  "Banking Blv.",    43, "Las Vegas"  },
  { 8976, "ChancylleZee",    54, "Paris" },
  { 1122, "Lucky",          111, "Wild West"  },
  { 3211, "Parlament St.",   11, "Stockholm" }
};

static
Address_t ADD_TABLE_DATA_READ[] = {
  { 1242, "", 0, "" },
  { 123,  "", 0, "" },
  { 456,  "", 0, "" },
  { 8976, "", 0, "" },
  { 1122, "", 0, "" },
  { 3211, "", 0, "" }
};

static
Employee_t EMP_TABLE_DATA[] = {
  { 1242, "Joe",     "Dalton" ,   &ADD_TABLE_DATA[0] },
  { 123,  "Lucky",   "Luke"   ,   &ADD_TABLE_DATA[1] },
  { 456,  "Averell", "Dalton" ,   &ADD_TABLE_DATA[2] },
  { 8976, "Gaston",  "Lagaffe",   &ADD_TABLE_DATA[3] },
  { 1122, "Jolly",   "Jumper" ,   &ADD_TABLE_DATA[4] },
  { 3211, "Leffe",   "Pagrotsky", &ADD_TABLE_DATA[5] },
};

static
Employee_t EMP_TABLE_DATA_READ[] = {
  { 1242, "", "", &ADD_TABLE_DATA_READ[0] },
  { 123,  "", "", &ADD_TABLE_DATA_READ[1] },
  { 456,  "", "", &ADD_TABLE_DATA_READ[2] },
  { 8976, "", "", &ADD_TABLE_DATA_READ[3] },
  { 1122, "", "", &ADD_TABLE_DATA_READ[4] },
  { 3211, "", "", &ADD_TABLE_DATA_READ[5] }
};

static const char TABLE[] = "employee_address";

static
void 
DbCreate(void){

  ndbout << "Opening database" << endl;
  require( DBA_Open() == DBA_NO_ERROR );
  
  ndbout << "Creating tables" << endl;
  require( DBA_CreateTable( TABLE, NbCol, ColDesc ) == DBA_NO_ERROR );
  
  ndbout << "Checking for table existance" << endl;
  require( DBA_TableExists( TABLE ) );
} 

static
void
CreateBindings(void){
  ndbout << "Creating bindings" << endl;

  Bind = DBA_CreateBinding(TABLE, 
			   EmpBindingRows,
			   EmpBindings,
			   sizeof(Employee_t) );
  
  require(Bind != 0);
}

extern "C" {
  static void insertCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  static void deleteCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  static void updateCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  static void readCallback  ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  static void writeCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
}

static
void BasicArray(){
  ndbout << "Testing basic array operations" << endl;
  
  // Basic insert
  DBA_ArrayInsertRows(Bind, EMP_TABLE_DATA, Rows-2, insertCallback);
  NdbSleep_SecSleep(1);
  DBA_ArrayReadRows  (Bind, EMP_TABLE_DATA_READ, Rows-2, readCallback);
  NdbSleep_SecSleep(1);
  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
  
  // Basic update
  AlterRows  (EMP_TABLE_DATA, Rows-2);
  AlterRows  (ADD_TABLE_DATA, Rows-2);
  DBA_ArrayUpdateRows(Bind, EMP_TABLE_DATA, Rows-2, updateCallback);
  NdbSleep_SecSleep(1);
  DBA_ArrayReadRows  (Bind, EMP_TABLE_DATA_READ, Rows-2, readCallback);
  NdbSleep_SecSleep(1);
  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
  
  // Basic write
  AlterRows  (EMP_TABLE_DATA, Rows);
  AlterRows  (ADD_TABLE_DATA, Rows);
  DBA_ArrayWriteRows(Bind, EMP_TABLE_DATA, Rows, writeCallback);
  NdbSleep_SecSleep(1);
  DBA_ArrayReadRows (Bind, EMP_TABLE_DATA_READ, Rows, readCallback);
  NdbSleep_SecSleep(1);
  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
  CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
  
  // Basic delete
  DBA_ArrayDeleteRows(Bind, EMP_TABLE_DATA, Rows, deleteCallback);
  NdbSleep_SecSleep(1);
}

static
void BasicPtr(){
  ndbout << "Testing array of pointer operations" << endl;
  Employee_t * EmpData[Rows];
  Employee_t * EmpDataRead[Rows];
  for(int i = 0; i<Rows; i++){
    EmpData[i]     = &EMP_TABLE_DATA[i];
    EmpDataRead[i] = &EMP_TABLE_DATA_READ[i];
  }
    
  void * const * EMP_TABLE_DATA2      = (void * const *)EmpData;
  void * const * EMP_TABLE_DATA_READ2 = (void * const *)EmpDataRead;
    
  // Basic insert
  DBA_InsertRows(Bind, EMP_TABLE_DATA2, Rows-2, insertCallback);
  NdbSleep_SecSleep(1);
  DBA_ReadRows  (Bind, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
  NdbSleep_SecSleep(1);
  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
    
  // Basic update
  AlterRows  (ADD_TABLE_DATA, Rows-2);
  AlterRows  (EMP_TABLE_DATA, Rows-2);
  DBA_UpdateRows(Bind, EMP_TABLE_DATA2, Rows-2, updateCallback);
  NdbSleep_SecSleep(1);
  DBA_ReadRows  (Bind, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
  NdbSleep_SecSleep(1);
  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);

    // Basic write
  AlterRows  (ADD_TABLE_DATA, Rows);
  AlterRows  (EMP_TABLE_DATA, Rows);
  DBA_WriteRows(Bind, EMP_TABLE_DATA2, Rows, writeCallback);
  NdbSleep_SecSleep(1);
  DBA_ReadRows (Bind, EMP_TABLE_DATA_READ2, Rows, readCallback);
  NdbSleep_SecSleep(1);
  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
  CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
  
    // Basic delete
  DBA_DeleteRows(Bind, EMP_TABLE_DATA2, Rows, deleteCallback);
  NdbSleep_SecSleep(1);
}

/*---------------------------------------------------------------------------*/
NDB_COMMAND(newton_pb, "newton_pb", 
	    "newton_pb", "newton_pb", 65535){

  DbCreate();
  CreateBindings();

  BasicArray();
  BasicPtr();
  
  DBA_Close();

  return 0;
}

/**
 *  callbackStatusCheck  checks whether or not the operation succeeded
 */
void
callbackStatusCheck( DBA_Error_t status, const char* operation) {
  ndbout_c("%s: %d", operation, status);
}

void insertCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  callbackStatusCheck(s, "insert");
}
void deleteCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  callbackStatusCheck(s, "delete");
}
void updateCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  callbackStatusCheck(s, "update");
}
void readCallback  ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  callbackStatusCheck(s, "read");
}
void writeCallback  ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  callbackStatusCheck(s, "write");
}