MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ptr_binding_test.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 ColDesc[] = {
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  { "street_name",DBA_CHAR, PCN_SIZE_OF( Address, StreetName ), PCN_FALSE },
38  { "street_no", DBA_INT, PCN_SIZE_OF( Address, StreetNo ), PCN_FALSE },
39  { "city", DBA_CHAR, PCN_SIZE_OF( Address, City ), PCN_FALSE }
40 };
41 static const int NbCol = 6;
42 
43 static const
44 DBA_ColumnBinding_t AddBindings[] = {
45  //DBA_BINDING( "emp_no", DBA_INT, Address, EmpNo ),
46  DBA_BINDING( "street_name", DBA_CHAR, Address, StreetName ),
47  DBA_BINDING( "street_no", DBA_INT, Address, StreetNo ),
48  DBA_BINDING( "city", DBA_CHAR, Address, City )
49 };
50 
51 static const
52 int AddBindingRows = sizeof(AddBindings)/sizeof(DBA_ColumnBinding_t);
53 
54 static const
55 DBA_ColumnBinding_t EmpBindings[] = {
56  DBA_BINDING( "emp_no", DBA_INT, Employee, EmpNo ),
57  DBA_BINDING( "last_name", DBA_CHAR, Employee, LastName ),
58  DBA_BINDING( "first_name", DBA_CHAR, Employee, FirstName),
59  DBA_BINDING_PTR(Employee, EmployeeAddress, AddBindings, AddBindingRows)
60 };
61 static const
62 int EmpBindingRows = sizeof(EmpBindings)/sizeof(DBA_ColumnBinding_t);
63 
64 static DBA_Binding_t * Bind;
65 
66 static const int Rows = 6;
67 
68 static
69 Address_t ADD_TABLE_DATA[] = {
70  { 1242, "Lonesome Street", 12, "Crime Town" },
71  { 123, "Pistol Road", 13, "Fort Mount" },
72  { 456, "Banking Blv.", 43, "Las Vegas" },
73  { 8976, "ChancylleZee", 54, "Paris" },
74  { 1122, "Lucky", 111, "Wild West" },
75  { 3211, "Parlament St.", 11, "Stockholm" }
76 };
77 
78 static
79 Address_t ADD_TABLE_DATA_READ[] = {
80  { 1242, "", 0, "" },
81  { 123, "", 0, "" },
82  { 456, "", 0, "" },
83  { 8976, "", 0, "" },
84  { 1122, "", 0, "" },
85  { 3211, "", 0, "" }
86 };
87 
88 static
89 Employee_t EMP_TABLE_DATA[] = {
90  { 1242, "Joe", "Dalton" , &ADD_TABLE_DATA[0] },
91  { 123, "Lucky", "Luke" , &ADD_TABLE_DATA[1] },
92  { 456, "Averell", "Dalton" , &ADD_TABLE_DATA[2] },
93  { 8976, "Gaston", "Lagaffe", &ADD_TABLE_DATA[3] },
94  { 1122, "Jolly", "Jumper" , &ADD_TABLE_DATA[4] },
95  { 3211, "Leffe", "Pagrotsky", &ADD_TABLE_DATA[5] },
96 };
97 
98 static
99 Employee_t EMP_TABLE_DATA_READ[] = {
100  { 1242, "", "", &ADD_TABLE_DATA_READ[0] },
101  { 123, "", "", &ADD_TABLE_DATA_READ[1] },
102  { 456, "", "", &ADD_TABLE_DATA_READ[2] },
103  { 8976, "", "", &ADD_TABLE_DATA_READ[3] },
104  { 1122, "", "", &ADD_TABLE_DATA_READ[4] },
105  { 3211, "", "", &ADD_TABLE_DATA_READ[5] }
106 };
107 
108 static const char TABLE[] = "employee_address";
109 
110 static
111 void
112 DbCreate(void){
113 
114  ndbout << "Opening database" << endl;
115  require( DBA_Open() == DBA_NO_ERROR );
116 
117  ndbout << "Creating tables" << endl;
118  require( DBA_CreateTable( TABLE, NbCol, ColDesc ) == DBA_NO_ERROR );
119 
120  ndbout << "Checking for table existance" << endl;
121  require( DBA_TableExists( TABLE ) );
122 }
123 
124 static
125 void
126 CreateBindings(void){
127  ndbout << "Creating bindings" << endl;
128 
129  Bind = DBA_CreateBinding(TABLE,
130  EmpBindingRows,
131  EmpBindings,
132  sizeof(Employee_t) );
133 
134  require(Bind != 0);
135 }
136 
137 extern "C" {
138  static void insertCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
139  static void deleteCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
140  static void updateCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
141  static void readCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
142  static void writeCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
143 }
144 
145 static
146 void BasicArray(){
147  ndbout << "Testing basic array operations" << endl;
148 
149  // Basic insert
150  DBA_ArrayInsertRows(Bind, EMP_TABLE_DATA, Rows-2, insertCallback);
151  NdbSleep_SecSleep(1);
152  DBA_ArrayReadRows (Bind, EMP_TABLE_DATA_READ, Rows-2, readCallback);
153  NdbSleep_SecSleep(1);
154  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
155  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
156 
157  // Basic update
158  AlterRows (EMP_TABLE_DATA, Rows-2);
159  AlterRows (ADD_TABLE_DATA, Rows-2);
160  DBA_ArrayUpdateRows(Bind, EMP_TABLE_DATA, Rows-2, updateCallback);
161  NdbSleep_SecSleep(1);
162  DBA_ArrayReadRows (Bind, EMP_TABLE_DATA_READ, Rows-2, readCallback);
163  NdbSleep_SecSleep(1);
164  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
165  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
166 
167  // Basic write
168  AlterRows (EMP_TABLE_DATA, Rows);
169  AlterRows (ADD_TABLE_DATA, Rows);
170  DBA_ArrayWriteRows(Bind, EMP_TABLE_DATA, Rows, writeCallback);
171  NdbSleep_SecSleep(1);
172  DBA_ArrayReadRows (Bind, EMP_TABLE_DATA_READ, Rows, readCallback);
173  NdbSleep_SecSleep(1);
174  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
175  CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
176 
177  // Basic delete
178  DBA_ArrayDeleteRows(Bind, EMP_TABLE_DATA, Rows, deleteCallback);
179  NdbSleep_SecSleep(1);
180 }
181 
182 static
183 void BasicPtr(){
184  ndbout << "Testing array of pointer operations" << endl;
185  Employee_t * EmpData[Rows];
186  Employee_t * EmpDataRead[Rows];
187  for(int i = 0; i<Rows; i++){
188  EmpData[i] = &EMP_TABLE_DATA[i];
189  EmpDataRead[i] = &EMP_TABLE_DATA_READ[i];
190  }
191 
192  void * const * EMP_TABLE_DATA2 = (void * const *)EmpData;
193  void * const * EMP_TABLE_DATA_READ2 = (void * const *)EmpDataRead;
194 
195  // Basic insert
196  DBA_InsertRows(Bind, EMP_TABLE_DATA2, Rows-2, insertCallback);
197  NdbSleep_SecSleep(1);
198  DBA_ReadRows (Bind, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
199  NdbSleep_SecSleep(1);
200  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
201  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
202 
203  // Basic update
204  AlterRows (ADD_TABLE_DATA, Rows-2);
205  AlterRows (EMP_TABLE_DATA, Rows-2);
206  DBA_UpdateRows(Bind, EMP_TABLE_DATA2, Rows-2, updateCallback);
207  NdbSleep_SecSleep(1);
208  DBA_ReadRows (Bind, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
209  NdbSleep_SecSleep(1);
210  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
211  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
212 
213  // Basic write
214  AlterRows (ADD_TABLE_DATA, Rows);
215  AlterRows (EMP_TABLE_DATA, Rows);
216  DBA_WriteRows(Bind, EMP_TABLE_DATA2, Rows, writeCallback);
217  NdbSleep_SecSleep(1);
218  DBA_ReadRows (Bind, EMP_TABLE_DATA_READ2, Rows, readCallback);
219  NdbSleep_SecSleep(1);
220  CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
221  CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
222 
223  // Basic delete
224  DBA_DeleteRows(Bind, EMP_TABLE_DATA2, Rows, deleteCallback);
225  NdbSleep_SecSleep(1);
226 }
227 
228 /*---------------------------------------------------------------------------*/
229 NDB_COMMAND(newton_pb, "newton_pb",
230  "newton_pb", "newton_pb", 65535){
231 
232  DbCreate();
233  CreateBindings();
234 
235  BasicArray();
236  BasicPtr();
237 
238  DBA_Close();
239 
240  return 0;
241 }
242 
246 void
247 callbackStatusCheck( DBA_Error_t status, const char* operation) {
248  ndbout_c("%s: %d", operation, status);
249 }
250 
251 void insertCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
252  callbackStatusCheck(s, "insert");
253 }
254 void deleteCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
255  callbackStatusCheck(s, "delete");
256 }
257 void updateCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
258  callbackStatusCheck(s, "update");
259 }
260 void readCallback ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
261  callbackStatusCheck(s, "read");
262 }
263 void writeCallback ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
264  callbackStatusCheck(s, "write");
265 }
266