MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
br_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 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 int
148 CountRows(DBA_BulkReadResultSet_t * rs, int count){
149  int res = 0;
150  for(int i = 0; i<count; i++)
151  if(rs[i].RowFoundIndicator)
152  res++;
153  return res;
154 }
155 
156 extern "C" {
157  static void insertCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
158  static void deleteCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
159  static void updateCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
160  static void readCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
161  static void writeCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
162 }
163 
164 static
165 void Multi(){
166  ndbout << "Testing multi operations" << endl;
167 
168  DBA_ArrayInsertRows(EmpB, EMP_TABLE_DATA, Rows-2, insertCallback);
169  DBA_ArrayInsertRows(AddB, ADD_TABLE_DATA, Rows-2, insertCallback);
170  NdbSleep_SecSleep(1);
171 
172  const int R2 = Rows + Rows;
173 
174  DBA_Binding_t * Bindings[2];
175  DBA_BulkReadResultSet_t DataRead[R2];
176 
177  Bindings[0] = EmpB;
178  Bindings[1] = AddB;
179 
180  for(int i = 0; i<Rows; i++)
181  DataRead[i].DataPtr = &EMP_TABLE_DATA_READ[i];
182 
183  for(int i = 0; i<Rows; i++)
184  DataRead[i+Rows].DataPtr = &ADD_TABLE_DATA_READ[i];
185 
186  NdbSleep_SecSleep(1);
187 
188  DBA_BulkMultiReadRows(Bindings, DataRead, 2, Rows, readCallback);
189  NdbSleep_SecSleep(1);
190  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
191  CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
192 
193  require(CountRows(DataRead, R2) == (R2-4));
194 
195  // Basic delete
196  DBA_ArrayDeleteRows(EmpB, EMP_TABLE_DATA, Rows-2, deleteCallback);
197  DBA_ArrayDeleteRows(AddB, ADD_TABLE_DATA, Rows-2, deleteCallback);
198  NdbSleep_SecSleep(1);
199 }
200 
201 static
202 void BasicPtr(){
203  ndbout << "Testing array of pointer operations" << endl;
204 
205  // Basic insert
206  DBA_ArrayInsertRows(EmpB, EMP_TABLE_DATA, Rows-2, insertCallback);
207  NdbSleep_SecSleep(1);
208 
209  DBA_BulkReadResultSet_t EmpDataRead[Rows];
210  for(int i = 0; i<Rows; i++){
211  EmpDataRead[i].DataPtr = &EMP_TABLE_DATA_READ[i];
212  }
213 
214  DBA_BulkReadRows(EmpB, EmpDataRead, Rows, readCallback);
215  NdbSleep_SecSleep(1);
216  CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
217  require(CountRows(EmpDataRead, Rows) == (Rows-2));
218 
219  // Basic delete
220  DBA_ArrayDeleteRows(EmpB, EMP_TABLE_DATA, Rows-2, deleteCallback);
221  NdbSleep_SecSleep(1);
222 }
223 
224 /*---------------------------------------------------------------------------*/
225 NDB_COMMAND(newton_br, "newton_br",
226  "newton_br", "newton_br", 65535){
227 
228  DbCreate();
229  CreateBindings();
230 
231  BasicPtr();
232  Multi();
233 
234  DBA_Close();
235 
236  return 0;
237 }
238 
239 
240 
244 void
245 callbackStatusCheck( DBA_Error_t status, const char* operation) {
246  ndbout_c("%s: %d", operation, status);
247 }
248 
249 void insertCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
250  callbackStatusCheck(s, "insert");
251 }
252 void deleteCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
253  callbackStatusCheck(s, "delete");
254 }
255 void updateCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
256  callbackStatusCheck(s, "update");
257 }
258 void readCallback ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
259  callbackStatusCheck(s, "read");
260 }
261 void writeCallback ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
262  callbackStatusCheck(s, "write");
263 }
264