~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to storage/ndb/test/ndbapi/testNativeDefault.cpp

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
 
15
#include <NdbOut.hpp>
 
16
#include <NdbApi.hpp>
 
17
#include <NdbSleep.h>
 
18
#include <NDBT.hpp>
 
19
#include <NdbError.hpp>
 
20
 
 
21
static Ndb_cluster_connection *g_cluster_connection= 0;
 
22
static Ndb* g_ndb = 0;
 
23
static const char* g_tablename1 = "T_DEF1"; //The normal table with default values
 
24
static const char* g_tablename2 = "T_DEF2"; //The table for Test that maximum length defaults work
 
25
//The table for Test that an attempt to insert to a table containing defaults 
 
26
//without supplying a value for a not-null, non-defaulted column still fails
 
27
static const char* g_tablename3 = "T_DEF3"; 
 
28
static NdbDictionary::Dictionary* g_dict = 0;
 
29
static const unsigned int column_count_table1 = 8;
 
30
static const unsigned int column_count_table2 = 2;
 
31
static const unsigned int column_count_table3 = 2;
 
32
 
 
33
static struct NdbError g_ndberror;
 
34
static int create_table();
 
35
 
 
36
static bool
 
37
connect_ndb()
 
38
{
 
39
  g_cluster_connection = new Ndb_cluster_connection();
 
40
  if(g_cluster_connection->connect(12, 5, 1) != 0)
 
41
    return false;
 
42
 
 
43
  g_ndb = new Ndb(g_cluster_connection, "TEST");
 
44
  g_ndb->init();
 
45
  if(g_ndb->waitUntilReady(30) != 0)
 
46
    return false;
 
47
 
 
48
  return true;
 
49
}
 
50
 
 
51
static void
 
52
disconnect_ndb()                                            {
 
53
  delete g_ndb;
 
54
  delete g_cluster_connection;
 
55
  g_ndb = 0;
 
56
//  g_table = 0;
 
57
  g_cluster_connection= 0;
 
58
}
 
59
 
 
60
#define PRINT_ERROR(error) \
 
61
   ndbout << "Error in " << __FILE__ << ", line: " << __LINE__ \
 
62
            << ", code: " << error.code \
 
63
            << ", msg: " << error.message << "." << endl
 
64
#define FAIL(error_msg) \
 
65
  ndbout << error_msg << " at line " << __LINE__ << endl; \
 
66
  return NDBT_FAILED
 
67
 
 
68
static const int            tab1_c1_default= 6; 
 
69
static const float          tab1_c2_default= float(1234.56);
 
70
static const double         tab1_c3_default= 4567.89;
 
71
static const char           tab1_c4_default[]= "aaaaaa      ";
 
72
static const unsigned int   tab1_c4_default_siglen= 12;
 
73
static const char           tab1_c5_default[]= "\x6" "aaaaaa\0\0\0\0";
 
74
static const unsigned int   tab1_c5_default_siglen= 7;
 
75
static const char           tab1_c6_default[]= "aaaaaa      ";
 
76
static const unsigned int   tab1_c6_default_siglen= 0;
 
77
static const char           tab1_c7_default[]= "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
 
78
static const char           tab1_c7_default_siglen= 1;
 
79
 
 
80
static const int            tab2_c1_default_len= 8052 - 4 - 2;
 
81
/* Max row length minus 4 bytes for key, minus 2 bytes for length info */
 
82
static const char           tab2_c1_default_char= 'V';
 
83
 
 
84
static int 
 
85
create_table()
 
86
{
 
87
  g_dict = g_ndb->getDictionary();
 
88
  if ((g_dict->getTable(g_tablename1) != 0) &&
 
89
      (g_dict->dropTable(g_tablename1) != 0))
 
90
  {
 
91
    PRINT_ERROR(g_dict->getNdbError());
 
92
    return NDBT_FAILED;
 
93
  }
 
94
 
 
95
  if ((g_dict->getTable(g_tablename2) != 0) &&
 
96
      (g_dict->dropTable(g_tablename2) != 0))
 
97
  {
 
98
    PRINT_ERROR(g_dict->getNdbError());
 
99
    return NDBT_FAILED;
 
100
  }
 
101
 
 
102
  if ((g_dict->getTable(g_tablename3) != 0) &&
 
103
      (g_dict->dropTable(g_tablename3) != 0))
 
104
  {
 
105
    PRINT_ERROR(g_dict->getNdbError());
 
106
    return NDBT_FAILED;
 
107
  }
 
108
 
 
109
  NdbDictionary::Table tab(g_tablename1);
 
110
  tab.setLogging(false);
 
111
  
 
112
  NdbDictionary::Table tab2(g_tablename2);
 
113
  tab2.setLogging(false);
 
114
 
 
115
  NdbDictionary::Table tab3(g_tablename3);
 
116
  tab3.setLogging(false);
 
117
 
 
118
  // col PK - Uint32
 
119
  { NdbDictionary::Column col("PK");
 
120
    col.setType(NdbDictionary::Column::Unsigned);
 
121
    col.setPrimaryKey(true);
 
122
    col.setNullable(FALSE);
 
123
    col.setAutoIncrement(TRUE);
 
124
    col.setDefaultValue(NULL);
 
125
    tab.addColumn(col);
 
126
  }
 
127
 
 
128
  { 
 
129
    NdbDictionary::Column col("C1");
 
130
    col.setType(NdbDictionary::Column::Int);
 
131
    col.setDefaultValue(&tab1_c1_default,sizeof(int));
 
132
    tab.addColumn(col);
 
133
  }
 
134
  
 
135
  { 
 
136
    NdbDictionary::Column col("C2");
 
137
    col.setType(NdbDictionary::Column::Float);
 
138
    col.setDefaultValue(&tab1_c2_default, sizeof(float));
 
139
    tab.addColumn(col);
 
140
  }
 
141
 
 
142
  { 
 
143
    NdbDictionary::Column col("C3");
 
144
    col.setType(NdbDictionary::Column::Double);
 
145
    col.setDefaultValue(&tab1_c3_default, sizeof(double));
 
146
    tab.addColumn(col);
 
147
  }
 
148
 
 
149
  { 
 
150
    NdbDictionary::Column col("C4");
 
151
    col.setType(NdbDictionary::Column::Char);
 
152
    col.setLength(12);
 
153
    col.setDefaultValue(tab1_c4_default, 12);
 
154
    tab.addColumn(col);
 
155
  }
 
156
 
 
157
  { 
 
158
    NdbDictionary::Column col("C5");
 
159
    col.setType(NdbDictionary::Column::Varchar);
 
160
    col.setLength(199);
 
161
    col.setDefaultValue(tab1_c5_default, tab1_c5_default_siglen);
 
162
    tab.addColumn(col);
 
163
  }
 
164
 
 
165
 
 
166
  { 
 
167
    /* Test non-null pointer passed, but with zero length? */
 
168
    NdbDictionary::Column col("C6");
 
169
    col.setType(NdbDictionary::Column::Char);
 
170
    col.setLength(12);
 
171
    col.setNullable(TRUE);
 
172
    col.setDefaultValue(tab1_c6_default, tab1_c6_default_siglen);
 
173
    tab.addColumn(col);
 
174
  }
 
175
 
 
176
  //Test that a zero-length VARCHAR default works
 
177
  { 
 
178
    NdbDictionary::Column col("C7");
 
179
    col.setType(NdbDictionary::Column::Varchar);
 
180
    col.setLength(10);
 
181
    col.setDefaultValue(tab1_c7_default, tab1_c7_default_siglen);
 
182
    tab.addColumn(col);
 
183
  }
 
184
 
 
185
  //create table T_DEF2
 
186
  { NdbDictionary::Column col("PK");
 
187
    col.setType(NdbDictionary::Column::Unsigned);
 
188
    col.setPrimaryKey(true);
 
189
    col.setNullable(FALSE);
 
190
    col.setAutoIncrement(FALSE);
 
191
    col.setDefaultValue(NULL, 0);
 
192
    tab2.addColumn(col);
 
193
  }
 
194
 
 
195
  //Test that maximum length defaults work
 
196
  { 
 
197
    char default_data[tab2_c1_default_len + 2];
 
198
    default_data[0] = (tab2_c1_default_len >> 0) & 0xff;
 
199
    default_data[1] = (tab2_c1_default_len >> 8) & 0xff;
 
200
    memset(default_data + 2, tab2_c1_default_char, tab2_c1_default_len);
 
201
    NdbDictionary::Column col("C1");
 
202
    col.setType(NdbDictionary::Column::Longvarchar);
 
203
    col.setLength(tab2_c1_default_len);
 
204
    col.setDefaultValue(default_data, tab2_c1_default_len + 2);
 
205
    tab2.addColumn(col);
 
206
  }
 
207
 
 
208
  //Create table T_DEF3
 
209
  { NdbDictionary::Column col("PK");
 
210
    col.setType(NdbDictionary::Column::Unsigned);
 
211
    col.setPrimaryKey(true);
 
212
    col.setNullable(FALSE);
 
213
    col.setAutoIncrement(FALSE);
 
214
    col.setDefaultValue(NULL, 0);
 
215
    tab3.addColumn(col);
 
216
  }
 
217
 
 
218
  //For column without supplying a value for a not-null, non-defaulted column
 
219
  { NdbDictionary::Column col("C1");
 
220
    col.setType(NdbDictionary::Column::Unsigned);
 
221
    col.setNullable(FALSE);
 
222
    col.setDefaultValue(NULL, 0);
 
223
    tab3.addColumn(col);
 
224
  }
 
225
  
 
226
  // create table
 
227
  if(g_dict->createTable(tab) != 0)
 
228
  {
 
229
    PRINT_ERROR(g_dict->getNdbError());
 
230
    return NDBT_FAILED;
 
231
  }
 
232
 
 
233
  if(g_dict->createTable(tab2) != 0)
 
234
  {
 
235
    PRINT_ERROR(g_dict->getNdbError());
 
236
    return NDBT_FAILED;
 
237
  }
 
238
 
 
239
  if(g_dict->createTable(tab3) != 0)
 
240
  {
 
241
    PRINT_ERROR(g_dict->getNdbError());
 
242
    return NDBT_FAILED;
 
243
  }
 
244
 
 
245
  return NDBT_OK;
 
246
}
 
247
 
 
248
static int
 
249
ndb_error_check(const struct NdbError& error, unsigned int line)
 
250
{
 
251
  if (error.code != 850)
 
252
  {
 
253
    PRINT_ERROR(error);
 
254
    ndbout << " at line " << line << "\n";
 
255
    return NDBT_FAILED;
 
256
  }
 
257
  return NDBT_OK;
 
258
}
 
259
 
 
260
#define CHECK_ERROR(error) {                           \
 
261
  if (ndb_error_check(error, __LINE__) == NDBT_FAILED) \
 
262
    return NDBT_FAILED;                                \
 
263
  }
 
264
 
 
265
static int
 
266
create_table_error()
 
267
{
 
268
  g_dict = g_ndb->getDictionary();
 
269
 
 
270
  /*
 
271
   * 1. The following test case is for fixed columns that
 
272
   *    there are too long or too short default values.
 
273
   */
 
274
  //for too long default value
 
275
  NdbDictionary::Table tab1("T_DEF_TEST1");
 
276
  tab1.setLogging(false);
 
277
 
 
278
  { NdbDictionary::Column col("PK");
 
279
    col.setType(NdbDictionary::Column::Unsigned);
 
280
    col.setPrimaryKey(true);
 
281
    col.setNullable(FALSE);
 
282
    col.setDefaultValue(NULL);
 
283
    tab1.addColumn(col);
 
284
  }
 
285
 
 
286
  { int default_data = 6;
 
287
    NdbDictionary::Column col("C1");
 
288
    col.setType(NdbDictionary::Column::Int);
 
289
    col.setDefaultValue(&default_data, 8);
 
290
    tab1.addColumn(col);
 
291
  }
 
292
 
 
293
  if(g_dict->createTable(tab1) != 0)
 
294
  {
 
295
    CHECK_ERROR(g_dict->getNdbError());
 
296
  }
 
297
  else
 
298
  {
 
299
    FAIL("Create table should not have succeeded");
 
300
  }
 
301
  
 
302
  //for too short default value
 
303
  NdbDictionary::Table tab2("T_DEF_TEST2");
 
304
  tab2.setLogging(false);
 
305
 
 
306
  { NdbDictionary::Column col("PK");
 
307
    col.setType(NdbDictionary::Column::Unsigned);
 
308
    col.setPrimaryKey(true);
 
309
    col.setNullable(FALSE);
 
310
    col.setAutoIncrement(TRUE);
 
311
    col.setDefaultValue(NULL);
 
312
    tab2.addColumn(col);
 
313
  }
 
314
 
 
315
  { const char default_data[] = "aaaaaa";
 
316
    NdbDictionary::Column col("C4");
 
317
    col.setType(NdbDictionary::Column::Char);
 
318
    col.setLength(12);
 
319
    col.setDefaultValue(default_data, 6);
 
320
    tab2.addColumn(col);
 
321
  }
 
322
 
 
323
  if(g_dict->createTable(tab2) != 0)
 
324
  {
 
325
    CHECK_ERROR(g_dict->getNdbError());
 
326
  }
 
327
  else
 
328
  {
 
329
    FAIL("Create table should not have succeeded");
 
330
  }
 
331
 
 
332
  /*
 
333
   * 2. The following test case is for Var-type columns that
 
334
   *    there are too long default values.
 
335
   */
 
336
  NdbDictionary::Table tab3("T_DEF_TEST3");
 
337
  tab3.setLogging(false);
 
338
 
 
339
  { NdbDictionary::Column col("PK");
 
340
    col.setType(NdbDictionary::Column::Unsigned);
 
341
    col.setPrimaryKey(true);
 
342
    col.setNullable(FALSE);
 
343
    col.setAutoIncrement(TRUE);
 
344
    col.setDefaultValue(NULL);
 
345
    tab3.addColumn(col);
 
346
  }
 
347
 
 
348
  { char default_data[20];
 
349
    memset(default_data, 0, 20);
 
350
    Uint8 * p = (Uint8*)default_data;
 
351
    *p = 10;
 
352
    memcpy(default_data + 1, "aaaaaaaaaa", 10);
 
353
    NdbDictionary::Column col("C5");
 
354
    col.setType(NdbDictionary::Column::Varchar);
 
355
    col.setLength(9);
 
356
    col.setDefaultValue(default_data, 11);
 
357
    tab3.addColumn(col);
 
358
  }
 
359
 
 
360
  if(g_dict->createTable(tab3) != 0)
 
361
  {
 
362
    CHECK_ERROR(g_dict->getNdbError());
 
363
  }
 
364
  else
 
365
  {
 
366
    FAIL("Create table should not have succeeded");
 
367
  }
 
368
 
 
369
 
 
370
  /*
 
371
   * 3. Test attempt to set default value for primary key
 
372
   */
 
373
  NdbDictionary::Table tab4("T_DEF_TEST4");
 
374
  tab4.setLogging(false);
 
375
 
 
376
  { NdbDictionary::Column col("PK");
 
377
    unsigned int default_val=22;
 
378
    col.setType(NdbDictionary::Column::Unsigned);
 
379
    col.setPrimaryKey(true);
 
380
    col.setNullable(FALSE);
 
381
    col.setAutoIncrement(TRUE);
 
382
    col.setDefaultValue(&default_val, sizeof(default_val));
 
383
    tab4.addColumn(col);
 
384
  }
 
385
 
 
386
  if(g_dict->createTable(tab4) == 0)
 
387
  {
 
388
    FAIL("Create table should not have succeeded");
 
389
  }
 
390
 
 
391
  if(g_dict->getNdbError().code != 792)
 
392
  {
 
393
    PRINT_ERROR(g_dict->getNdbError());
 
394
    return NDBT_FAILED;
 
395
  }
 
396
 
 
397
  /*
 
398
   * 4. The following test case is for Var-type columns that
 
399
   *    there are too long default values, where the passed
 
400
   *    value is short, but the implied value is too long
 
401
   */
 
402
  NdbDictionary::Table tab5("T_DEF_TEST5");
 
403
  tab5.setLogging(false);
 
404
 
 
405
  { NdbDictionary::Column col("PK");
 
406
    col.setType(NdbDictionary::Column::Unsigned);
 
407
    col.setPrimaryKey(true);
 
408
    col.setNullable(FALSE);
 
409
    col.setAutoIncrement(TRUE);
 
410
    col.setDefaultValue(NULL);
 
411
    tab5.addColumn(col);
 
412
  }
 
413
 
 
414
  { char default_data[20];
 
415
    memset(default_data, 0, 20);
 
416
    Uint8 * p = (Uint8*)default_data;
 
417
    *p = 15;
 
418
    memcpy(default_data + 1, "aaaaaaaaaa", 15);
 
419
    NdbDictionary::Column col("C5");
 
420
    col.setType(NdbDictionary::Column::Varchar);
 
421
    col.setLength(9);
 
422
    /* Within range, but contained VARCHAR is too long */
 
423
    col.setDefaultValue(default_data, 10);
 
424
    tab5.addColumn(col);
 
425
  }
 
426
 
 
427
  if(g_dict->createTable(tab5) != 0)
 
428
  {
 
429
    CHECK_ERROR(g_dict->getNdbError());
 
430
  }
 
431
  else
 
432
  {
 
433
    FAIL("Create table should not have succeeded");
 
434
  }
 
435
 
 
436
  return NDBT_OK;
 
437
}
 
438
 
 
439
static int 
 
440
drop_table()
 
441
{
 
442
  if ((g_dict != 0) && ( g_dict->getTable(g_tablename1) != 0))
 
443
  {
 
444
    if(g_dict->dropTable(g_tablename1))
 
445
      PRINT_ERROR(g_dict->getNdbError());
 
446
  }
 
447
 
 
448
  if ((g_dict != 0) && ( g_dict->getTable(g_tablename2) != 0))
 
449
  {
 
450
    if(g_dict->dropTable(g_tablename2))
 
451
      PRINT_ERROR(g_dict->getNdbError());
 
452
  }
 
453
 
 
454
  if ((g_dict != 0) && ( g_dict->getTable(g_tablename3) != 0))
 
455
  {
 
456
    if(g_dict->dropTable(g_tablename3))
 
457
      PRINT_ERROR(g_dict->getNdbError());
 
458
  }
 
459
 
 
460
  return NDBT_OK;
 
461
}
 
462
 
 
463
static int do_insert()
 
464
{
 
465
  const NdbDictionary::Table *myTable= g_dict->getTable(g_tablename1);
 
466
 
 
467
  if (myTable == NULL)
 
468
  {
 
469
    PRINT_ERROR(g_dict->getNdbError());
 
470
    return NDBT_FAILED; 
 
471
  }
 
472
 
 
473
  NdbTransaction *myTransaction= g_ndb->startTransaction(); 
 
474
  if (myTransaction == NULL)
 
475
  {
 
476
    PRINT_ERROR(g_ndb->getNdbError());
 
477
    return NDBT_FAILED;
 
478
  }
 
479
 
 
480
  NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
 
481
  NdbOperation *myOperation1= myTransaction->getNdbOperation(myTable); 
 
482
  if (myOperation == NULL || myOperation1 == NULL) 
 
483
  {
 
484
    PRINT_ERROR(myTransaction->getNdbError());
 
485
    g_ndb->closeTransaction(myTransaction);
 
486
    return NDBT_FAILED;
 
487
  }
 
488
 
 
489
  myOperation->insertTuple();
 
490
  myOperation->equal("PK", 1);
 
491
  myOperation1->insertTuple();
 
492
  myOperation1->equal("PK", 2);
 
493
 
 
494
  //insert the second table T_DEF2
 
495
  const NdbDictionary::Table *myTable2= g_dict->getTable(g_tablename2);
 
496
 
 
497
  if (myTable == NULL)
 
498
  {
 
499
    PRINT_ERROR(g_dict->getNdbError());
 
500
    return NDBT_FAILED;
 
501
  }
 
502
  NdbOperation *myOperation2 = myTransaction->getNdbOperation(myTable2);
 
503
  if (myOperation2 == NULL)
 
504
  {
 
505
    PRINT_ERROR(myTransaction->getNdbError());
 
506
    g_ndb->closeTransaction(myTransaction);
 
507
    return NDBT_FAILED;
 
508
  }
 
509
 
 
510
  myOperation2->insertTuple();
 
511
  myOperation2->equal("PK", 1);
 
512
 
 
513
 
 
514
  /* Test insert of max length tuple with max length default
 
515
   * Could theoretically expose kernel overflow with default 
 
516
   * + supplied value
 
517
   */
 
518
  myOperation2=myTransaction->getNdbOperation(myTable2);
 
519
  if (myOperation2 == NULL)
 
520
  {
 
521
    PRINT_ERROR(myTransaction->getNdbError());
 
522
    g_ndb->closeTransaction(myTransaction);
 
523
    return NDBT_FAILED;
 
524
  }
 
525
  
 
526
  myOperation2->insertTuple();
 
527
  myOperation2->equal("PK", 2);
 
528
 
 
529
  {
 
530
    char default_data[tab2_c1_default_len + 2];
 
531
    default_data[0] = (tab2_c1_default_len >> 0) & 0xff;
 
532
    default_data[1] = (tab2_c1_default_len >> 8) & 0xff;
 
533
    memset(default_data + 2, tab2_c1_default_char, tab2_c1_default_len);
 
534
    
 
535
    myOperation2->setValue("C1", default_data);
 
536
  }
 
537
 
 
538
  if (myTransaction->execute(NdbTransaction::Commit) == -1)
 
539
  {
 
540
    PRINT_ERROR(myTransaction->getNdbError());
 
541
    g_ndb->closeTransaction(myTransaction);
 
542
    return NDBT_FAILED;
 
543
  }
 
544
  
 
545
  g_ndb->closeTransaction(myTransaction);
 
546
 
 
547
  //The following insert should fail, and return error code
 
548
  const NdbDictionary::Table *myTable3= g_dict->getTable(g_tablename3);
 
549
 
 
550
  if (myTable3 == NULL)
 
551
  {
 
552
    PRINT_ERROR(g_dict->getNdbError());
 
553
    return NDBT_FAILED;
 
554
  }
 
555
 
 
556
  NdbTransaction *myTransaction3 = g_ndb->startTransaction();
 
557
  if (myTransaction3 == NULL)
 
558
  {
 
559
    PRINT_ERROR(g_ndb->getNdbError());
 
560
    return NDBT_FAILED;
 
561
  }
 
562
 
 
563
  NdbOperation *myOperation3 = myTransaction3->getNdbOperation(myTable3);
 
564
  if (myOperation3 == NULL)
 
565
  {
 
566
    PRINT_ERROR(myTransaction3->getNdbError());
 
567
    g_ndb->closeTransaction(myTransaction3);
 
568
    return NDBT_FAILED;
 
569
  }
 
570
  myOperation3->insertTuple();
 
571
  myOperation3->equal("PK", 1);
 
572
 
 
573
  /* It should return error code 839 ( msg: Illegal null attribute)
 
574
   * for an attempt to insert to a table containing defaults 
 
575
   * without supplying a value for a not-null, non-defaulted column
 
576
  */
 
577
  if (myTransaction3->execute(NdbTransaction::Commit) == -1)
 
578
  {
 
579
    PRINT_ERROR(myTransaction3->getNdbError());
 
580
    
 
581
    if (myTransaction3->getNdbError().code != 839)
 
582
    {
 
583
      ndbout << "Expected error 839" << endl;
 
584
      g_ndb->closeTransaction(myTransaction3);
 
585
      return NDBT_FAILED;
 
586
    }
 
587
  }
 
588
  g_ndb->closeTransaction(myTransaction3);
 
589
 
 
590
  return NDBT_OK;
 
591
}
 
592
 
 
593
#define CHECK_VAL_EQ(ref, test) {                \
 
594
    if ((ref) != (test)) {                       \
 
595
      ndbout << "Equality failed at line " << __LINE__ << "\n" \
 
596
             << test << " != " << ref << "\n"; \
 
597
      return NDBT_FAILED;                      \
 
598
    } }
 
599
 
 
600
#define CHECK_BYTES_EQ(ref, test, len) {        \
 
601
    if (memcmp((ref), (test), (len))) {         \
 
602
      ndbout << "Bytes differ at line " << __LINE__ << "\n"; \
 
603
      return NDBT_FAILED;                                    \
 
604
    }} 
 
605
 
 
606
 
 
607
 
 
608
 
 
609
static int do_read()
 
610
{
 
611
  NdbRecAttr *myRecAttr[column_count_table1];
 
612
  NdbRecAttr *myRecAttr2[column_count_table2];
 
613
  NdbRecAttr *myRecAttr3[column_count_table3];
 
614
 
 
615
  const NdbDictionary::Table *myTable= g_dict->getTable(g_tablename1);
 
616
  const NdbDictionary::Table *myTable2 = g_dict->getTable(g_tablename2);
 
617
  const NdbDictionary::Table *myTable3 = g_dict->getTable(g_tablename3);
 
618
 
 
619
  if (myTable == NULL || myTable2 == NULL || myTable3 == NULL)
 
620
  {
 
621
    PRINT_ERROR(g_dict->getNdbError());
 
622
    return NDBT_FAILED;
 
623
  }
 
624
 
 
625
  NdbTransaction *myTransaction= g_ndb->startTransaction();
 
626
  if (myTransaction == NULL)
 
627
  {
 
628
    PRINT_ERROR(g_ndb->getNdbError());
 
629
    return NDBT_FAILED;
 
630
  }
 
631
 
 
632
  //Define Scan Operation for T_DEF1
 
633
  NdbScanOperation *myScanOp = myTransaction->getNdbScanOperation(myTable);
 
634
  if (myScanOp == NULL)
 
635
  {
 
636
    PRINT_ERROR(myTransaction->getNdbError());
 
637
    g_ndb->closeTransaction(myTransaction);
 
638
    return NDBT_FAILED;
 
639
  }
 
640
 
 
641
  if(myScanOp->readTuples(NdbOperation::LM_CommittedRead) == -1)
 
642
  {
 
643
    PRINT_ERROR(myTransaction->getNdbError());
 
644
    g_ndb->closeTransaction(myTransaction);
 
645
    return NDBT_FAILED;
 
646
  }
 
647
 
 
648
  myRecAttr[0] = myScanOp->getValue("PK");
 
649
  myRecAttr[1] = myScanOp->getValue("C1");
 
650
  myRecAttr[2] = myScanOp->getValue("C2");
 
651
  myRecAttr[3] = myScanOp->getValue("C3");
 
652
  myRecAttr[4] = myScanOp->getValue("C4");
 
653
  myRecAttr[5] = myScanOp->getValue("C5");
 
654
  myRecAttr[6] = myScanOp->getValue("C6");
 
655
  myRecAttr[7] = myScanOp->getValue("C7");
 
656
 
 
657
  for (unsigned int i = 0; i < column_count_table1; i++)
 
658
    if (myRecAttr[i] == NULL)
 
659
    {
 
660
      PRINT_ERROR(myTransaction->getNdbError());
 
661
      g_ndb->closeTransaction(myTransaction);
 
662
      return NDBT_FAILED;
 
663
    }
 
664
 
 
665
  //Define Scan Operation for T_DEF2
 
666
  NdbScanOperation *myScanOp2 = myTransaction->getNdbScanOperation(myTable2);
 
667
  if (myScanOp2 == NULL)
 
668
  {
 
669
    PRINT_ERROR(myTransaction->getNdbError());
 
670
    g_ndb->closeTransaction(myTransaction);
 
671
    return NDBT_FAILED;
 
672
  }
 
673
 
 
674
  if(myScanOp2->readTuples(NdbOperation::LM_CommittedRead) == -1)
 
675
  {
 
676
    PRINT_ERROR(myTransaction->getNdbError());
 
677
    g_ndb->closeTransaction(myTransaction);
 
678
    return NDBT_FAILED;
 
679
  }
 
680
 
 
681
  myRecAttr2[0] = myScanOp2->getValue("PK");
 
682
  myRecAttr2[1] = myScanOp2->getValue("C1");
 
683
  if (myRecAttr2[0] == NULL || myRecAttr2[1] == NULL)
 
684
  {
 
685
    PRINT_ERROR(myTransaction->getNdbError());
 
686
    g_ndb->closeTransaction(myTransaction);
 
687
    return NDBT_FAILED;
 
688
  }
 
689
 
 
690
  //Define Scan Operation for T_DEF3  
 
691
  NdbScanOperation *myScanOp3 = myTransaction->getNdbScanOperation(myTable3);
 
692
  if (myScanOp3 == NULL)
 
693
  {
 
694
    PRINT_ERROR(myTransaction->getNdbError());
 
695
    g_ndb->closeTransaction(myTransaction);
 
696
    return NDBT_FAILED;
 
697
  }
 
698
 
 
699
  if(myScanOp3->readTuples(NdbOperation::LM_CommittedRead) == -1)
 
700
  {
 
701
    PRINT_ERROR(myTransaction->getNdbError());
 
702
    g_ndb->closeTransaction(myTransaction);
 
703
    return NDBT_FAILED;
 
704
  }
 
705
 
 
706
  myRecAttr3[0] = myScanOp3->getValue("PK");
 
707
  myRecAttr3[1] = myScanOp3->getValue("C1");
 
708
  if (myRecAttr3[0] == NULL || myRecAttr3[1] == NULL)
 
709
  {
 
710
    PRINT_ERROR(myTransaction->getNdbError());
 
711
    g_ndb->closeTransaction(myTransaction);
 
712
    return NDBT_FAILED;
 
713
  }
 
714
 
 
715
  //Execute the Transcation for the 3 scan operations
 
716
  if(myTransaction->execute(NdbTransaction::NoCommit) != 0)
 
717
  {
 
718
    PRINT_ERROR(myTransaction->getNdbError());
 
719
    g_ndb->closeTransaction(myTransaction);
 
720
    return NDBT_FAILED;
 
721
  }
 
722
 
 
723
  //The following print out the result
 
724
  int check;
 
725
  ndbout<< "Table: " << g_tablename1 << endl;
 
726
//   ndbout << "PK";
 
727
//   for (unsigned int i = 0; i < column_count_table1; i++)
 
728
//     ndbout << "\tC" << i ;
 
729
//   ndbout << endl;
 
730
  while((check = myScanOp->nextResult(true)) == 0){
 
731
    do {
 
732
//       for (Uint32 i = 0; i < column_count_table1; i++)
 
733
//         ndbout << *myRecAttr[i] << "\t";
 
734
//       ndbout << endl;
 
735
      
 
736
      for (Uint32 i = 0; i < column_count_table1; i++)
 
737
      {
 
738
        /* Check that all columns are non NULL except c6 */
 
739
        CHECK_VAL_EQ((i == 6), myRecAttr[i]->isNULL());
 
740
      }
 
741
 
 
742
      CHECK_VAL_EQ(tab1_c1_default, (int) myRecAttr[1]->int32_value());
 
743
      CHECK_VAL_EQ(tab1_c2_default, myRecAttr[2]->float_value());
 
744
      CHECK_VAL_EQ(tab1_c3_default, myRecAttr[3]->double_value());
 
745
      CHECK_BYTES_EQ(tab1_c4_default, (const char*) myRecAttr[4]->aRef(), tab1_c4_default_siglen);
 
746
      CHECK_BYTES_EQ(tab1_c5_default, (const char*) myRecAttr[5]->aRef(), tab1_c5_default_siglen); 
 
747
      CHECK_BYTES_EQ(tab1_c6_default, (const char*) myRecAttr[6]->aRef(), tab1_c6_default_siglen);
 
748
      CHECK_BYTES_EQ(tab1_c7_default, (const char*) myRecAttr[7]->aRef(), tab1_c7_default_siglen);
 
749
    } while((check = myScanOp->nextResult(false)) == 0);
 
750
 
 
751
    if(check == -1)
 
752
    {
 
753
      ndbout << "Error with transaction " << check << " " 
 
754
             << myTransaction->getNdbError().code << "\n";
 
755
      return NDBT_FAILED;
 
756
    }
 
757
  }
 
758
 
 
759
  ndbout<< "Table: " << g_tablename2 << endl;
 
760
  // ndbout << "PK\tC1" << endl;
 
761
  while((check = myScanOp2->nextResult(true)) == 0){
 
762
    do {
 
763
//       for (Uint32 i = 0; i < column_count_table2; i++)
 
764
//       {
 
765
//         if (i == 1)
 
766
//           ndbout << myRecAttr2[i]->get_size_in_bytes() << "  ";
 
767
//         ndbout << *myRecAttr2[i] << "\t";
 
768
//       }
 
769
//       ndbout << endl;
 
770
      const char* valPtr= (const char*)myRecAttr2[1]->aRef();
 
771
      char default_data[tab2_c1_default_len + 2];
 
772
      default_data[0] = (tab2_c1_default_len >> 0) & 0xff;
 
773
      default_data[1] = (tab2_c1_default_len >> 8) & 0xff;
 
774
      memset(default_data + 2, tab2_c1_default_char, tab2_c1_default_len);
 
775
 
 
776
      CHECK_BYTES_EQ(default_data, valPtr, tab2_c1_default_len + 2);
 
777
 
 
778
    } while((check = myScanOp2->nextResult(false)) == 0);
 
779
  }
 
780
 
 
781
 
 
782
  return NDBT_OK;
 
783
}
 
784
 
 
785
int main(int argc, char* argv[])
 
786
{
 
787
  int ret;
 
788
  ndb_init();
 
789
 
 
790
  ndbout << "testNativeDefault started" << endl;
 
791
 
 
792
  if (!connect_ndb())
 
793
  {
 
794
    ndbout << "Failed to connect to NDB" << endl;
 
795
    return NDBT_ProgramExit(NDBT_FAILED);
 
796
  }
 
797
  ndbout << "connected.." << endl;
 
798
 
 
799
  ndbout << "checking create table errors..." << endl;
 
800
  if ((ret = create_table_error()) != NDBT_OK)
 
801
    return NDBT_ProgramExit(ret);
 
802
 
 
803
  ndbout << "creating table..." << endl;
 
804
  if ((ret = create_table()) != NDBT_OK)
 
805
    return NDBT_ProgramExit(ret);
 
806
 
 
807
  ndbout << "inserting..." << endl;
 
808
  if ((ret = do_insert()) != NDBT_OK)
 
809
    return NDBT_ProgramExit(ret);
 
810
 
 
811
  ndbout << "reading..." << endl;
 
812
  if ((ret = do_read()) != NDBT_OK)
 
813
    return NDBT_ProgramExit(ret);
 
814
 
 
815
  if ((ret = drop_table()) != NDBT_OK)
 
816
    return NDBT_ProgramExit(ret);
 
817
 
 
818
  ndbout << "done!" << endl;
 
819
 
 
820
  disconnect_ndb();
 
821
 
 
822
  ndbout << "All tests successful" << endl;
 
823
  return NDBT_ProgramExit(NDBT_OK);
 
824
}