mysql api源码下载_ndbapi_scan.cpp 源代码在线查看 - mysql-5.0.33源码包 资源下载 虫虫电子下载站...

* Permanent error (return -1) */ while (true) { if (retryAttempt >= retryMax) { std::cout return -1; } myTrans = myNdb->startTransaction(); if (myTrans == NULL) { const NdbError err = myNdb->getNdbError(); if (err.status == NdbError::TemporaryError) {milliSleep(50);retryAttempt++;continue; } std::cout return -1; } /** * Get a scan operation. */ myScanOp = myTrans->getNdbScanOperation(myTable); if (myScanOp == NULL) { std::cout getNdbError().message myNdb->closeTransaction(myTrans); return -1; } /** * Define a result set for the scan. */ if( myScanOp->readTuples(NdbOperation::LM_Exclusive) ) { std::cout getNdbError().message myNdb->closeTransaction(myTrans); return -1; } /** * Use NdbScanFilter to define a search critera */ NdbScanFilter filter(myScanOp) ; if(filter.begin(NdbScanFilter::AND) < 0 || filter.cmp(NdbScanFilter::COND_EQ, update_column, before_color) filter.end() { std::cout getNdbError().message myNdb->closeTransaction(myTrans); return -1; } /** * Start scan (NoCommit since we are only reading at this stage); */ if(myTrans->execute(NdbTransaction::NoCommit) != 0) { err = myTrans->getNdbError(); if(err.status == NdbError::TemporaryError){std::cout getNdbError().message myNdb->closeTransaction(myTrans);milliSleep(50);continue; } std::cout getNdbError().code myNdb->closeTransaction(myTrans); return -1; } /** * start of loop: nextResult(true) means that "parallelism" number of * rows are fetched from NDB and cached in NDBAPI */ while((check = myScanOp->nextResult(true)) == 0){ do {/** * Get update operation */ NdbOperation * myUpdateOp = myScanOp->updateCurrentTuple();if (myUpdateOp == 0){ std::cout getNdbError().message myNdb->closeTransaction(myTrans); return -1;}updatedRows++;/** * do the update */ myUpdateOp->setValue(update_column, after_color);/** * nextResult(false) means that the records * cached in the NDBAPI are modified before * fetching more rows from NDB. */ } while((check = myScanOp->nextResult(false)) == 0); /** * NoCommit when all cached tuple have been updated */ if(check != -1) {check = myTrans->execute(NdbTransaction::NoCommit); } /** * Check for errors */ err = myTrans->getNdbError(); if(check == -1) {if(err.status == NdbError::TemporaryError){ std::cout getNdbError().message myNdb->closeTransaction(myTrans); milliSleep(50); continue;} } /** * End of loop */ } /** * Commit all prepared operations */ if(myTrans->execute(NdbTransaction::Commit) == -1) { if(err.status == NdbError::TemporaryError){std::cout getNdbError().message myNdb->closeTransaction(myTrans);milliSleep(50);continue; } } std::cout getNdbError().message myNdb->closeTransaction(myTrans); return 0; } if(myTrans!=0) { std::cout getNdbError().message myNdb->closeTransaction(myTrans); } return -1;}int scan_print(Ndb * myNdb){// Scan all records exclusive and update // them one by one int retryAttempt = 0; const int retryMax = 10; int fetchedRows = 0; int check; NdbError err; NdbTransaction*myTrans; NdbScanOperation*myScanOp; /* Result of reading attribute value, three columns: REG_NO, BRAND, and COLOR */ NdbRecAttr * myRecAttr[3]; const NdbDictionary::Dictionary* myDict= myNdb->getDictionary(); const NdbDictionary::Table *myTable= myDict->getTable("GARAGE"); if (myTable == NULL) APIERROR(myDict->getNdbError()); /** * Loop as long as : * retryMax not reached * failed operations due to TEMPORARY erros * * Exit loop; * retyrMax reached * Permanent error (return -1) */ while (true) { if (retryAttempt >= retryMax) { std::cout return -1; } myTrans = myNdb->startTransaction(); if (myTrans == NULL) { const NdbError err = myNdb->getNdbError(); if (err.status == NdbError::TemporaryError) {milliSleep(50);retryAttempt++;continue; } std::cout return -1; } /* * Define a scan operation. * NDBAPI. */ myScanOp = myTrans->getNdbScanOperation(myTable); if (myScanOp == NULL) { std::cout getNdbError().message myNdb->closeTransaction(myTrans); return -1; } /** * Read without locks, without being placed in lock queue */ if( myScanOp->readTuples(NdbOperation::LM_CommittedRead) == -1) { std::cout getNdbError().message myNdb->closeTransaction(myTrans); return -1; } /** * Define storage for fetched attributes. * E.g., the resulting attributes of executing * myOp->getValue("REG_NO") is placed in myRecAttr[0]. * No data exists in myRecAttr until transaction has commited! */ myRecAttr[0] = myScanOp->getValue("REG_NO"); myRecAttr[1] = myScanOp->getValue("BRAND"); myRecAttr[2] = myScanOp->getValue("COLOR"); if(myRecAttr[0] ==NULL || myRecAttr[1] == NULL || myRecAttr[2]==NULL) {std::cout getNdbError().message myNdb->closeTransaction(myTrans);return -1; } /** * Start scan (NoCommit since we are only reading at this stage); */ if(myTrans->execute(NdbTransaction::NoCommit) != 0){ err = myTrans->getNdbError(); if(err.status == NdbError::TemporaryError){std::cout getNdbError().message myNdb->closeTransaction(myTrans);milliSleep(50);continue; } std::cout std::cout getNdbError().code myNdb->closeTransaction(myTrans); return -1; } /** * start of loop: nextResult(true) means that "parallelism" number of * rows are fetched from NDB and cached in NDBAPI */ while((check = myScanOp->nextResult(true)) == 0){ do {fetchedRows++;/** * print REG_NO unsigned int */std::cout u_32_value() /** * print BRAND character string */std::cout aRef() /** * print COLOR character string */std::cout aRef() /** * nextResult(false) means that the records * cached in the NDBAPI are modified before * fetching more rows from NDB. */ } while((check = myScanOp->nextResult(false)) == 0); } myNdb->closeTransaction(myTrans); return 1; } return -1;}int main(){ ndb_init(); MYSQL mysql; /************************************************************** * Connect to mysql server and create table * **************************************************************/ { if ( !mysql_init(&mysql) ) { std::cout exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 3306, "/tmp/mysql.sock", 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE TEST_DB"); if (mysql_query(&mysql, "USE TEST_DB") != 0) MYSQLERROR(mysql); create_table(mysql); } /************************************************************** * Connect to ndb cluster * **************************************************************/ Ndb_cluster_connection cluster_connection; if (cluster_connection.connect(4, 5, 1)) { std::cout exit(-1); } // Optionally connect and wait for the storage nodes (ndbd's) if (cluster_connection.wait_until_ready(30,0) < 0) { std::cout exit(-1); } Ndb myNdb(&cluster_connection,"TEST_DB"); if (myNdb.init(1024) == -1) { // Set max 1024 parallel transactions APIERROR(myNdb.getNdbError()); exit(-1); } /******************************************* * Check table definition * *******************************************/ int column_color; { const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); const NdbDictionary::Table *t= myDict->getTable("GARAGE"); Car car; if (t->getColumn("COLOR")->getLength() != sizeof(car.color) ||t->getColumn("BRAND")->getLength() != sizeof(car.brand)) { std::cout exit(-1); } column_color= t->getColumn("COLOR")->getColumnNo(); } if(populate(&myNdb) > 0) std::cout if(scan_print(&myNdb) > 0) std::cout std::cout { /** * Note! color needs to be of exact the same size as column defined */ Car tmp; sprintf(tmp.color, "Pink"); if(scan_delete(&myNdb, column_color, tmp.color) > 0) std::cout } if(scan_print(&myNdb) > 0) std::cout { /** * Note! color1 & 2 need to be of exact the same size as column defined */ Car tmp1, tmp2; sprintf(tmp1.color, "Blue"); sprintf(tmp2.color, "Black"); std::cout if(scan_update(&myNdb, column_color, tmp1.color, tmp2.color) > 0) std::cout } if(scan_print(&myNdb) > 0) std::cout return 0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值