ptr_binding_test.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:8k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. /****** THIS LINE IS 80 CHARACTERS WIDE - DO *NOT* EXCEED 80 CHARACTERS! ****/
  14. extern "C" {
  15. #include <dba.h>
  16. }
  17. #include "common.hpp"
  18. #include <NdbOut.hpp>
  19. #include <NdbSleep.h>
  20. #include <NdbMain.h>
  21. static const
  22. DBA_ColumnDesc_t ColDesc[] = {
  23.   { "emp_no",     DBA_INT,  PCN_SIZE_OF( Employee, EmpNo ),      PCN_TRUE },
  24.   { "first_name", DBA_CHAR, PCN_SIZE_OF( Employee, FirstName ),  PCN_FALSE },
  25.   { "last_name",  DBA_CHAR, PCN_SIZE_OF( Employee, LastName ),   PCN_FALSE },
  26.   { "street_name",DBA_CHAR, PCN_SIZE_OF( Address,  StreetName ), PCN_FALSE },
  27.   { "street_no",  DBA_INT,  PCN_SIZE_OF( Address,  StreetNo ),   PCN_FALSE },
  28.   { "city",       DBA_CHAR, PCN_SIZE_OF( Address,  City ),       PCN_FALSE }
  29. };
  30. static const int NbCol = 6;
  31. static const
  32. DBA_ColumnBinding_t AddBindings[] = {
  33.   //DBA_BINDING( "emp_no",      DBA_INT,  Address, EmpNo ),
  34.   DBA_BINDING( "street_name", DBA_CHAR, Address, StreetName ),
  35.   DBA_BINDING( "street_no",   DBA_INT,  Address, StreetNo ),  
  36.   DBA_BINDING( "city",        DBA_CHAR, Address, City )
  37. };
  38. static const 
  39. int AddBindingRows = sizeof(AddBindings)/sizeof(DBA_ColumnBinding_t);
  40. static const
  41. DBA_ColumnBinding_t EmpBindings[] = {
  42.   DBA_BINDING( "emp_no",      DBA_INT,  Employee, EmpNo ),
  43.   DBA_BINDING( "last_name",   DBA_CHAR, Employee, LastName ),
  44.   DBA_BINDING( "first_name",  DBA_CHAR, Employee, FirstName),
  45.   DBA_BINDING_PTR(Employee, EmployeeAddress, AddBindings, AddBindingRows)
  46. };
  47. static const 
  48. int EmpBindingRows = sizeof(EmpBindings)/sizeof(DBA_ColumnBinding_t);
  49. static DBA_Binding_t * Bind;
  50. static const int Rows = 6;
  51. static 
  52. Address_t ADD_TABLE_DATA[] = {
  53.   { 1242, "Lonesome Street", 12, "Crime Town" },
  54.   { 123,  "Pistol Road",     13, "Fort Mount" },
  55.   { 456,  "Banking Blv.",    43, "Las Vegas"  },
  56.   { 8976, "ChancylleZee",    54, "Paris" },
  57.   { 1122, "Lucky",          111, "Wild West"  },
  58.   { 3211, "Parlament St.",   11, "Stockholm" }
  59. };
  60. static
  61. Address_t ADD_TABLE_DATA_READ[] = {
  62.   { 1242, "", 0, "" },
  63.   { 123,  "", 0, "" },
  64.   { 456,  "", 0, "" },
  65.   { 8976, "", 0, "" },
  66.   { 1122, "", 0, "" },
  67.   { 3211, "", 0, "" }
  68. };
  69. static
  70. Employee_t EMP_TABLE_DATA[] = {
  71.   { 1242, "Joe",     "Dalton" ,   &ADD_TABLE_DATA[0] },
  72.   { 123,  "Lucky",   "Luke"   ,   &ADD_TABLE_DATA[1] },
  73.   { 456,  "Averell", "Dalton" ,   &ADD_TABLE_DATA[2] },
  74.   { 8976, "Gaston",  "Lagaffe",   &ADD_TABLE_DATA[3] },
  75.   { 1122, "Jolly",   "Jumper" ,   &ADD_TABLE_DATA[4] },
  76.   { 3211, "Leffe",   "Pagrotsky", &ADD_TABLE_DATA[5] },
  77. };
  78. static
  79. Employee_t EMP_TABLE_DATA_READ[] = {
  80.   { 1242, "", "", &ADD_TABLE_DATA_READ[0] },
  81.   { 123,  "", "", &ADD_TABLE_DATA_READ[1] },
  82.   { 456,  "", "", &ADD_TABLE_DATA_READ[2] },
  83.   { 8976, "", "", &ADD_TABLE_DATA_READ[3] },
  84.   { 1122, "", "", &ADD_TABLE_DATA_READ[4] },
  85.   { 3211, "", "", &ADD_TABLE_DATA_READ[5] }
  86. };
  87. static const char TABLE[] = "employee_address";
  88. static
  89. void 
  90. DbCreate(void){
  91.   ndbout << "Opening database" << endl;
  92.   require( DBA_Open() == DBA_NO_ERROR );
  93.   
  94.   ndbout << "Creating tables" << endl;
  95.   require( DBA_CreateTable( TABLE, NbCol, ColDesc ) == DBA_NO_ERROR );
  96.   
  97.   ndbout << "Checking for table existance" << endl;
  98.   require( DBA_TableExists( TABLE ) );
  99. static
  100. void
  101. CreateBindings(void){
  102.   ndbout << "Creating bindings" << endl;
  103.   Bind = DBA_CreateBinding(TABLE, 
  104.    EmpBindingRows,
  105.    EmpBindings,
  106.    sizeof(Employee_t) );
  107.   
  108.   require(Bind != 0);
  109. }
  110. extern "C" {
  111.   static void insertCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  112.   static void deleteCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  113.   static void updateCallback( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  114.   static void readCallback  ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  115.   static void writeCallback ( DBA_ReqId_t, DBA_Error_t, DBA_ErrorCode_t );
  116. }
  117. static
  118. void BasicArray(){
  119.   ndbout << "Testing basic array operations" << endl;
  120.   
  121.   // Basic insert
  122.   DBA_ArrayInsertRows(Bind, EMP_TABLE_DATA, Rows-2, insertCallback);
  123.   NdbSleep_SecSleep(1);
  124.   DBA_ArrayReadRows  (Bind, EMP_TABLE_DATA_READ, Rows-2, readCallback);
  125.   NdbSleep_SecSleep(1);
  126.   CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  127.   CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
  128.   
  129.   // Basic update
  130.   AlterRows  (EMP_TABLE_DATA, Rows-2);
  131.   AlterRows  (ADD_TABLE_DATA, Rows-2);
  132.   DBA_ArrayUpdateRows(Bind, EMP_TABLE_DATA, Rows-2, updateCallback);
  133.   NdbSleep_SecSleep(1);
  134.   DBA_ArrayReadRows  (Bind, EMP_TABLE_DATA_READ, Rows-2, readCallback);
  135.   NdbSleep_SecSleep(1);
  136.   CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  137.   CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
  138.   
  139.   // Basic write
  140.   AlterRows  (EMP_TABLE_DATA, Rows);
  141.   AlterRows  (ADD_TABLE_DATA, Rows);
  142.   DBA_ArrayWriteRows(Bind, EMP_TABLE_DATA, Rows, writeCallback);
  143.   NdbSleep_SecSleep(1);
  144.   DBA_ArrayReadRows (Bind, EMP_TABLE_DATA_READ, Rows, readCallback);
  145.   NdbSleep_SecSleep(1);
  146.   CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
  147.   CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
  148.   
  149.   // Basic delete
  150.   DBA_ArrayDeleteRows(Bind, EMP_TABLE_DATA, Rows, deleteCallback);
  151.   NdbSleep_SecSleep(1);
  152. }
  153. static
  154. void BasicPtr(){
  155.   ndbout << "Testing array of pointer operations" << endl;
  156.   Employee_t * EmpData[Rows];
  157.   Employee_t * EmpDataRead[Rows];
  158.   for(int i = 0; i<Rows; i++){
  159.     EmpData[i]     = &EMP_TABLE_DATA[i];
  160.     EmpDataRead[i] = &EMP_TABLE_DATA_READ[i];
  161.   }
  162.     
  163.   void * const * EMP_TABLE_DATA2      = (void * const *)EmpData;
  164.   void * const * EMP_TABLE_DATA_READ2 = (void * const *)EmpDataRead;
  165.     
  166.   // Basic insert
  167.   DBA_InsertRows(Bind, EMP_TABLE_DATA2, Rows-2, insertCallback);
  168.   NdbSleep_SecSleep(1);
  169.   DBA_ReadRows  (Bind, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
  170.   NdbSleep_SecSleep(1);
  171.   CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  172.   CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
  173.     
  174.   // Basic update
  175.   AlterRows  (ADD_TABLE_DATA, Rows-2);
  176.   AlterRows  (EMP_TABLE_DATA, Rows-2);
  177.   DBA_UpdateRows(Bind, EMP_TABLE_DATA2, Rows-2, updateCallback);
  178.   NdbSleep_SecSleep(1);
  179.   DBA_ReadRows  (Bind, EMP_TABLE_DATA_READ2, Rows-2, readCallback);
  180.   NdbSleep_SecSleep(1);
  181.   CompareRows(EMP_TABLE_DATA, Rows-2, EMP_TABLE_DATA_READ);
  182.   CompareRows(ADD_TABLE_DATA, Rows-2, ADD_TABLE_DATA_READ);
  183.     // Basic write
  184.   AlterRows  (ADD_TABLE_DATA, Rows);
  185.   AlterRows  (EMP_TABLE_DATA, Rows);
  186.   DBA_WriteRows(Bind, EMP_TABLE_DATA2, Rows, writeCallback);
  187.   NdbSleep_SecSleep(1);
  188.   DBA_ReadRows (Bind, EMP_TABLE_DATA_READ2, Rows, readCallback);
  189.   NdbSleep_SecSleep(1);
  190.   CompareRows(EMP_TABLE_DATA, Rows, EMP_TABLE_DATA_READ);
  191.   CompareRows(ADD_TABLE_DATA, Rows, ADD_TABLE_DATA_READ);
  192.   
  193.     // Basic delete
  194.   DBA_DeleteRows(Bind, EMP_TABLE_DATA2, Rows, deleteCallback);
  195.   NdbSleep_SecSleep(1);
  196. }
  197. /*---------------------------------------------------------------------------*/
  198. NDB_COMMAND(newton_pb, "newton_pb", 
  199.     "newton_pb", "newton_pb", 65535){
  200.   DbCreate();
  201.   CreateBindings();
  202.   BasicArray();
  203.   BasicPtr();
  204.   
  205.   DBA_Close();
  206.   return 0;
  207. }
  208. /**
  209.  *  callbackStatusCheck  checks whether or not the operation succeeded
  210.  */
  211. void
  212. callbackStatusCheck( DBA_Error_t status, const char* operation) {
  213.   ndbout_c("%s: %d", operation, status);
  214. }
  215. void insertCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  216.   callbackStatusCheck(s, "insert");
  217. }
  218. void deleteCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  219.   callbackStatusCheck(s, "delete");
  220. }
  221. void updateCallback( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  222.   callbackStatusCheck(s, "update");
  223. }
  224. void readCallback  ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  225.   callbackStatusCheck(s, "read");
  226. }
  227. void writeCallback  ( DBA_ReqId_t, DBA_Error_t s, DBA_ErrorCode_t ){
  228.   callbackStatusCheck(s, "write");
  229. }