19 #include <NdbError.hpp>
22 static Ndb* g_ndb = 0;
23 static const char* g_tablename1 =
"T_DEF1";
24 static const char* g_tablename2 =
"T_DEF2";
27 static const char* g_tablename3 =
"T_DEF3";
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;
40 if(g_cluster_connection->
connect(12, 5, 1) != 0)
43 g_ndb =
new Ndb(g_cluster_connection,
"TEST");
54 delete g_cluster_connection;
57 g_cluster_connection= 0;
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; \
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;
80 static const int tab2_c1_default_len= 8052 - 4 - 2;
82 static const char tab2_c1_default_char=
'V';
88 if ((g_dict->
getTable(g_tablename1) != 0) &&
95 if ((g_dict->
getTable(g_tablename2) != 0) &&
102 if ((g_dict->
getTable(g_tablename3) != 0) &&
110 tab.setLogging(
false);
113 tab2.setLogging(
false);
116 tab3.setLogging(
false);
121 col.setPrimaryKey(
true);
122 col.setNullable(FALSE);
123 col.setAutoIncrement(TRUE);
124 col.setDefaultValue(NULL);
131 col.setDefaultValue(&tab1_c1_default,
sizeof(
int));
138 col.setDefaultValue(&tab1_c2_default,
sizeof(
float));
145 col.setDefaultValue(&tab1_c3_default,
sizeof(
double));
153 col.setDefaultValue(tab1_c4_default, 12);
161 col.setDefaultValue(tab1_c5_default, tab1_c5_default_siglen);
171 col.setNullable(TRUE);
172 col.setDefaultValue(tab1_c6_default, tab1_c6_default_siglen);
181 col.setDefaultValue(tab1_c7_default, tab1_c7_default_siglen);
188 col.setPrimaryKey(
true);
189 col.setNullable(FALSE);
190 col.setAutoIncrement(FALSE);
191 col.setDefaultValue(NULL, 0);
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);
203 col.setLength(tab2_c1_default_len);
204 col.setDefaultValue(default_data, tab2_c1_default_len + 2);
211 col.setPrimaryKey(
true);
212 col.setNullable(FALSE);
213 col.setAutoIncrement(FALSE);
214 col.setDefaultValue(NULL, 0);
221 col.setNullable(FALSE);
222 col.setDefaultValue(NULL, 0);
249 ndb_error_check(
const struct NdbError& error,
unsigned int line)
251 if (error.
code != 850)
254 ndbout <<
" at line " << line <<
"\n";
260 #define CHECK_ERROR(error) { \
261 if (ndb_error_check(error, __LINE__) == NDBT_FAILED) \
262 return NDBT_FAILED; \
276 tab1.setLogging(
false);
280 col.setPrimaryKey(
true);
281 col.setNullable(FALSE);
282 col.setDefaultValue(NULL);
286 {
int default_data = 6;
289 col.setDefaultValue(&default_data, 8);
299 FAIL(
"Create table should not have succeeded");
304 tab2.setLogging(
false);
308 col.setPrimaryKey(
true);
309 col.setNullable(FALSE);
310 col.setAutoIncrement(TRUE);
311 col.setDefaultValue(NULL);
315 {
const char default_data[] =
"aaaaaa";
319 col.setDefaultValue(default_data, 6);
329 FAIL(
"Create table should not have succeeded");
337 tab3.setLogging(
false);
341 col.setPrimaryKey(
true);
342 col.setNullable(FALSE);
343 col.setAutoIncrement(TRUE);
344 col.setDefaultValue(NULL);
348 {
char default_data[20];
349 memset(default_data, 0, 20);
350 Uint8 * p = (Uint8*)default_data;
352 memcpy(default_data + 1,
"aaaaaaaaaa", 10);
356 col.setDefaultValue(default_data, 11);
366 FAIL(
"Create table should not have succeeded");
374 tab4.setLogging(
false);
377 unsigned int default_val=22;
379 col.setPrimaryKey(
true);
380 col.setNullable(FALSE);
381 col.setAutoIncrement(TRUE);
382 col.setDefaultValue(&default_val,
sizeof(default_val));
388 FAIL(
"Create table should not have succeeded");
403 tab5.setLogging(
false);
407 col.setPrimaryKey(
true);
408 col.setNullable(FALSE);
409 col.setAutoIncrement(TRUE);
410 col.setDefaultValue(NULL);
414 {
char default_data[20];
415 memset(default_data, 0, 20);
416 Uint8 * p = (Uint8*)default_data;
418 memcpy(default_data + 1,
"aaaaaaaaaa", 15);
423 col.setDefaultValue(default_data, 10);
433 FAIL(
"Create table should not have succeeded");
442 if ((g_dict != 0) && ( g_dict->
getTable(g_tablename1) != 0))
448 if ((g_dict != 0) && ( g_dict->
getTable(g_tablename2) != 0))
454 if ((g_dict != 0) && ( g_dict->
getTable(g_tablename3) != 0))
463 static int do_insert()
474 if (myTransaction == NULL)
482 if (myOperation == NULL || myOperation1 == NULL)
490 myOperation->
equal(
"PK", 1);
492 myOperation1->
equal(
"PK", 2);
503 if (myOperation2 == NULL)
511 myOperation2->
equal(
"PK", 1);
519 if (myOperation2 == NULL)
527 myOperation2->
equal(
"PK", 2);
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);
535 myOperation2->
setValue(
"C1", default_data);
550 if (myTable3 == NULL)
557 if (myTransaction3 == NULL)
564 if (myOperation3 == NULL)
571 myOperation3->
equal(
"PK", 1);
583 ndbout <<
"Expected error 839" << endl;
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; \
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; \
619 if (myTable == NULL || myTable2 == NULL || myTable3 == NULL)
626 if (myTransaction == NULL)
634 if (myScanOp == NULL)
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");
657 for (
unsigned int i = 0;
i < column_count_table1;
i++)
658 if (myRecAttr[
i] == NULL)
667 if (myScanOp2 == NULL)
681 myRecAttr2[0] = myScanOp2->
getValue(
"PK");
682 myRecAttr2[1] = myScanOp2->
getValue(
"C1");
683 if (myRecAttr2[0] == NULL || myRecAttr2[1] == NULL)
692 if (myScanOp3 == NULL)
706 myRecAttr3[0] = myScanOp3->
getValue(
"PK");
707 myRecAttr3[1] = myScanOp3->
getValue(
"C1");
708 if (myRecAttr3[0] == NULL || myRecAttr3[1] == NULL)
725 ndbout<<
"Table: " << g_tablename1 << endl;
730 while((check = myScanOp->
nextResult(
true)) == 0){
736 for (Uint32
i = 0;
i < column_count_table1;
i++)
739 CHECK_VAL_EQ((
i == 6), myRecAttr[
i]->isNULL());
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);
753 ndbout <<
"Error with transaction " << check <<
" "
759 ndbout<<
"Table: " << g_tablename2 << endl;
761 while((check = myScanOp2->
nextResult(
true)) == 0){
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);
776 CHECK_BYTES_EQ(default_data, valPtr, tab2_c1_default_len + 2);
778 }
while((check = myScanOp2->
nextResult(
false)) == 0);
785 int main(
int argc,
char* argv[])
790 ndbout <<
"testNativeDefault started" << endl;
794 ndbout <<
"Failed to connect to NDB" << endl;
795 return NDBT_ProgramExit(NDBT_FAILED);
797 ndbout <<
"connected.." << endl;
799 ndbout <<
"checking create table errors..." << endl;
800 if ((ret = create_table_error()) != NDBT_OK)
801 return NDBT_ProgramExit(ret);
803 ndbout <<
"creating table..." << endl;
805 return NDBT_ProgramExit(ret);
807 ndbout <<
"inserting..." << endl;
808 if ((ret = do_insert()) != NDBT_OK)
809 return NDBT_ProgramExit(ret);
811 ndbout <<
"reading..." << endl;
812 if ((ret = do_read()) != NDBT_OK)
813 return NDBT_ProgramExit(ret);
815 if ((ret = drop_table()) != NDBT_OK)
816 return NDBT_ProgramExit(ret);
818 ndbout <<
"done!" << endl;
822 ndbout <<
"All tests successful" << endl;
823 return NDBT_ProgramExit(NDBT_OK);