Exemplo n.º 1
0
INT32 migExport::_query()
{
   INT32 rc = SDB_OK ;
   bson condition ;
   bson sort ;
   bson_init ( &condition ) ;
   bson_init ( &sort ) ;
   if( _pMigArg->pFiter )
   {
      if ( !jsonToBson2 ( &condition, _pMigArg->pFiter, 0, 1 ) )
      {
         rc = SDB_INVALIDARG ;
         ossPrintf ( "fiter format error"OSS_NEWLINE ) ;
         PD_LOG ( PDERROR, "fiter format error" ) ;
         goto error ;
      }
   }
   else
   {
      bson_empty( &condition ) ;
   }
   if( _pMigArg->pSort )
   {
      if ( !jsonToBson2 ( &sort, _pMigArg->pSort, 0, 1 ) )
      {
         rc = SDB_INVALIDARG ;
         ossPrintf ( "sort format error"OSS_NEWLINE ) ;
         PD_LOG ( PDERROR, "sort format error" ) ;
         goto error ;
      }
   }
   else
   {
      bson_empty( &sort ) ;
   }
   rc = sdbQuery ( _gCollection, &condition, NULL, &sort, NULL, 0, -1,
                   &_gCursor ) ;
   if ( rc )
   {
      if ( SDB_DMS_EOC != rc )
      {
         PD_LOG ( PDERROR, "Failed to query collection, rc = %d", rc ) ;
         goto error ;
      }
      else
      {
         goto done ;
      }
   }
done:
   bson_destroy ( &sort ) ;
   bson_destroy ( &condition ) ;
   return rc ;
error:
   goto done ;
}
Exemplo n.º 2
0
TEST(cursor,sdbCursor_get_over_then_close)
{
   INT32 rc = SDB_OK ;
   rc = initEnv( HOST, SERVER, USER, PASSWD ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   sdbConnectionHandle connection    = 0 ;
   sdbCollectionHandle collection    = 0 ;
   sdbCursorHandle cursor            = 0 ;
   INT32 NUM                         = 1 ;
   bson obj ;
   bson obj1 ;
   bson obj2 ;
   rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   rc = getCollection ( connection,
                        COLLECTION_FULL_NAME,
                        &collection ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   insertRecords ( collection, NUM ) ;
   rc = sdbQuery( collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ;
   ASSERT_EQ( SDB_OK, rc ) ;

   bson_init(&obj);
   bson_init(&obj1);
   bson_init(&obj2);

   rc = sdbNext ( cursor, &obj ) ;
   CHECK_MSG("%s%d\n","rc = ",rc) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   printf( "SdbNext record is:\n" ) ;
   bson_print( &obj ) ;
   printf("\n") ;
   rc = sdbNext ( cursor, &obj1 ) ;
   CHECK_MSG("%s%d\n","rc = ",rc) ;
   ASSERT_EQ( SDB_DMS_EOC, rc ) ;
   rc = sdbNext ( cursor, &obj1 ) ;
   CHECK_MSG("%s%d\n","rc = ",rc) ;
   ASSERT_EQ( SDB_DMS_CONTEXT_IS_CLOSE, rc ) ;

   bson_destroy( &obj ) ;
   bson_destroy( &obj1 ) ;
   bson_destroy( &obj2 ) ;

   rc = sdbCloseCursor( cursor ) ;
   CHECK_MSG("%s%d\n","rc = ",rc) ;
   ASSERT_EQ( SDB_OK, rc ) ;

   sdbDisconnect ( connection ) ;
   sdbReleaseCursor ( cursor ) ;
   sdbReleaseCollection ( collection ) ;
   sdbReleaseConnection ( connection ) ;
}
Exemplo n.º 3
0
TEST(cursor,sdbNext)
{
   INT32 rc = SDB_OK ;
   rc = initEnv( HOST, SERVER, USER, PASSWD ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   sdbConnectionHandle connection    = 0 ;
   sdbCollectionHandle collection    = 0 ;
   sdbCursorHandle cursor            = 0 ;
   INT32 NUM                         = 10 ;
   SINT64 count                      = 0 ;
   bson obj ;
   rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   rc = getCollection ( connection,
                        COLLECTION_FULL_NAME,
                        &collection ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   insertRecords( collection, NUM ) ;
   rc = sdbQuery ( collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   bson_init(&obj);
   rc = sdbCurrent( cursor, &obj ) ;
   printf( "Current record is:\n" ) ;
   bson_print( &obj ) ;
   printf("\n") ;
   bson_destroy( &obj ) ;
   bson_init(&obj);
   rc = sdbNext( cursor, &obj ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   printf( "Next record is:\n" ) ;
   bson_print( &obj ) ;
   printf("\n") ;
   bson_destroy(&obj);
   rc = sdbGetCount( collection, NULL, &count ) ;
   CHECK_MSG("%s%d\n","rc = ",rc) ;
   CHECK_MSG("%s%d%s%lld\n", "NUM = ", NUM, " count = ", count) ;
   ASSERT_EQ ( NUM, count ) ;
   sdbDisconnect ( connection ) ;
   sdbReleaseCursor ( cursor ) ;
   sdbReleaseCollection ( collection ) ;
   sdbReleaseConnection ( connection ) ;
}
Exemplo n.º 4
0
TEST(cursor,sdbCloseCursor)
{
   INT32 rc = SDB_OK ;
   rc = initEnv( HOST, SERVER, USER, PASSWD ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   sdbConnectionHandle connection    = 0 ;
   sdbCollectionHandle collection    = 0 ;
   sdbCursorHandle cursor            = 0 ;
   INT32 NUM                         = 10 ;
   bson obj ;
   bson obj1 ;
   rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   rc = getCollection ( connection,
                        COLLECTION_FULL_NAME,
                        &collection ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   insertRecords ( collection, NUM ) ;
   rc = sdbQuery( collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ;
   bson_init(&obj);
   rc = sdbCurrent( cursor, &obj ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   printf( "Current record is:\n" ) ;
   bson_print( &obj ) ;
   printf("\n") ;
   bson_destroy( &obj ) ;

   rc = sdbCloseCursor( cursor ) ;
   ASSERT_EQ( SDB_OK, rc ) ;
   bson_init( &obj1 ) ;
   rc = sdbCurrent( cursor, &obj1 ) ;
   ASSERT_EQ( rc, SDB_DMS_CONTEXT_IS_CLOSE ) ;

   sdbDisconnect ( connection ) ;
   sdbReleaseCursor ( cursor ) ;
   sdbReleaseCollection ( collection ) ;
   sdbReleaseConnection ( connection ) ;
}
Exemplo n.º 5
0
   INT32 expCLExporter::_query( sdbCollectionHandle &hCL, 
                                sdbCursorHandle &hCusor )
   {
      INT32 rc = SDB_OK ;
      string clFullName ;
      bson select ;
      bson condition ;
      bson sort ;
      bson_init ( &select ) ;
      bson_init ( &condition ) ;
      bson_init ( &sort ) ;

      clFullName = _cl.fullName() ;

      if ( _cl.select.empty() )
      {
         bson_empty( &select ) ;
      }
      else
      {
         if( !json2bson( _cl.select.c_str(), NULL,
                         CJSON_RIGOROUS_PARSE, FALSE, &select ) )
         {
            rc = SDB_INVALIDARG ;
            PD_LOG( PDERROR, "Invalid format of select : %s", 
                   _cl.select.c_str() ) ;
            goto error ;
         }
      }
      
      if ( _cl.filter.empty() )
      {
         bson_empty( &condition ) ;
      }
      else
      {
         if( !json2bson( _cl.filter.c_str(), NULL,
                         CJSON_RIGOROUS_PARSE, FALSE, &condition ) )
         {
            rc = SDB_INVALIDARG ;
            PD_LOG( PDERROR, "Invalid format of filter : %s", 
                   _cl.filter.c_str() ) ;
            goto error ;
         }
      }
      
      if ( _cl.sort.empty() )
      {
         bson_empty( &sort ) ;
      }
      else
      {
         if( !json2bson( _cl.sort.c_str(), NULL,
                         CJSON_RIGOROUS_PARSE, FALSE, &sort ) )
         {
            rc = SDB_INVALIDARG ;
            PD_LOG( PDERROR, "Invalid format of sort : %s", 
                   _cl.sort.c_str() ) ;
            goto error ;
         }
      }

      rc = sdbGetCollection( _hConn, clFullName.c_str(), &hCL ) ;
      if ( SDB_DMS_NOTEXIST == rc )
      {
         PD_LOG( PDERROR, "Collection %s does not exist", clFullName.c_str() ) ;
         goto error ;
      }
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "Failed to get collection %s, rc = %d", 
                 clFullName.c_str(), rc ) ;
         goto error ;
      }
      rc = sdbQuery ( hCL, &condition, &select, &sort, NULL,
                      _cl.skip, _cl.limit, &hCusor ) ;
      if ( SDB_OK != rc )
      {
         PD_LOG( PDERROR, "Failed to query the first record of %s, rc = %d",
                 clFullName.c_str(), rc ) ;
         goto error ;
      }
   done :
      bson_destroy( &select ) ;
      bson_destroy( &condition ) ;
      bson_destroy( &sort ) ;
      return rc ;
   error :
      goto done ;
   }
Exemplo n.º 6
0
INT32 main ( INT32 argc, CHAR **argv )
{
   // initialize local variables
   CHAR *pHostName                   = NULL ;
   CHAR *pServiceName                = NULL ;
   CHAR *pUsr                        = NULL ;
   CHAR *pPasswd                     = NULL ;
   // define a connetion handle; use to connect to database
   sdbConnectionHandle connection    = 0 ;
   // define a collection space handle
   sdbCSHandle collectionspace       = 0 ;
   // define a collection handle
   sdbCollectionHandle collection    = 0 ;
   // define a cursor handle for query
   sdbCursorHandle cursor            = 0 ;
   sdbCursorHandle cursor1           = 0 ;
   // define local variables
   // initialize them before use
   INT32 rc    = SDB_OK ;
   bson obj ;
   bson rule ;
   bson record ;
   bson updatecondition ;
   bson tmp ;
   bson_iterator it ;

   // verify syntax
   if ( 5 != argc )
   {
      displaySyntax ( (CHAR*)argv[0] ) ;
      exit ( 0 ) ;
   }
   // read argument
   pHostName    = (CHAR*)argv[1] ;
   pServiceName = (CHAR*)argv[2] ;
   pUsr         = (CHAR*)argv[3] ;
   pPasswd      = (CHAR*)argv[4] ;

   // connect to database
   rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to connet to database, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   // create collection space
   rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME,
                                   SDB_PAGESIZE_4K, &collectionspace ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to create collection space, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }

   // create collection in a specified colletion space.
   rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to create collection, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   
   // prepare record
   bson_init( &record ) ;
   bson_append_int( &record, "age", 10 ) ;
   rc = bson_finish( &record ) ;
   CHECK_RC ( rc, "Failed to build bson" ) ;

   // insert record into database
   rc = sdbInsert( collection, &record ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to insert, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   bson_destroy( &record ) ;
   // query all the record in this collection
   rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   // get the record from cursor
   bson_init(&obj) ;
   bson_init(&tmp) ;
   rc=sdbNext( cursor, &obj ) ;
   if ( rc!= SDB_OK )
   {
      printf("Failed to get next, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   rc = bson_copy( &tmp, &obj ) ;
   CHECK_RC ( rc, "Failed to copy bson" ) ;
   printf("Before update, the record is:\n") ;
   bson_print( &tmp ) ;
   // set the update condition using "_id"
   bson_find( &it, &obj, "_id" ) ;
   bson_init( &updatecondition ) ;
   bson_append_element( &updatecondition, NULL, &it ) ;
   rc = bson_finish( &updatecondition ) ;
   CHECK_RC ( rc, "Failed to build bson" ) ;
   // set the update rule
   bson_init( &rule ) ;
   bson_append_start_object ( &rule, "$set" ) ;
   bson_append_int ( &rule, "age", 99 ) ;
   bson_append_finish_object ( &rule ) ;
   rc = bson_finish ( &rule ) ;
   CHECK_RC ( rc, "Failed to build bson" ) ;

   // update
   rc = sdbUpdate(collection, &rule, &updatecondition, NULL ) ;
   if ( rc!=SDB_OK )
   {
      printf("Failed to update, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   bson_destroy(&obj) ;
   bson_destroy(&rule) ;
   bson_destroy(&updatecondition) ;
   // query all the record in this collection again
   rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor1 ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   // get record from cursor1
   bson_init(&obj) ;
   rc=sdbNext( cursor1, &obj ) ;
   if ( rc!= SDB_OK )
   {
      printf("Failed to get next, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   printf("after update, the record is:\n") ;
   bson_print( &obj ) ;
   bson_destroy(&obj) ;

done:
   // drop collection space
   rc = sdbDropCollectionSpace( connection, COLLECTION_SPACE_NAME ) ;
   if ( rc != SDB_OK )
   {
     printf("Failed to drop collection space, rc = %d" OSS_NEWLINE, rc ) ;
   }
   // disconnect the connection
   sdbDisconnect ( connection ) ;
   // release the local variables
   sdbReleaseCursor ( cursor ) ;
   sdbReleaseCursor ( cursor1 ) ;
   sdbReleaseCollection ( collection ) ;
   sdbReleaseCS ( collectionspace ) ;
   sdbReleaseConnection ( connection ) ;
   return 0;
error:
   goto done ;
}
Exemplo n.º 7
0
INT32 main ( INT32 argc, CHAR **argv )
{
   // initialize local variables
   CHAR *pHostName                   = NULL ;
   CHAR *pServiceName                = NULL ;
   CHAR *pUsr                        = NULL ;
   CHAR *pPasswd                     = NULL ;
   // define a connetion handle; use to connect to database
   sdbConnectionHandle connection    = 0 ;
   // define a collection space handle
   sdbCSHandle collectionspace       = 0 ;
   // define a collection handle
   sdbCollectionHandle collection    = 0 ;
   // define a cursor handle for query
   sdbCursorHandle cursor            = 0 ;
   // define local variables
   // initialize them before use
   INT32 rc    = SDB_OK ;
   INT32 count = 0 ;
   bson obj ;
   bson rule ;
   bson objList [ NUM_RECORD ] ;

   // verify syntax
   if ( 5 != argc )
   {
      displaySyntax ( (CHAR*)argv[0] ) ;
      exit ( 0 ) ;
   }
   // read argument
   pHostName    = (CHAR*)argv[1] ;
   pServiceName = (CHAR*)argv[2] ;
   pUsr         = (CHAR*)argv[3] ;
   pPasswd      = (CHAR*)argv[4] ;

   // connect to database
   rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ;
   CHECK_RC ( rc, "Failed to connet to database" ) ;

   // create collection space
   rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME,
                                   SDB_PAGESIZE_4K, &collectionspace ) ;
   CHECK_RC ( rc, "Failed to create collection space" ) ;

   // create collection in a specified colletion space.
   // Here,we build it up in the new collection.
   rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ;
   CHECK_RC ( rc, "Failed to create collection" ) ;

   // create record list using objList
   createRecordList ( &objList[0], NUM_RECORD ) ;

   // insert obj and free memory that allocated by createRecordList
   for ( count = 0; count < NUM_RECORD; count++ )
   {
      // all the contents of inserted record are the same except _id
      rc = sdbInsert ( collection, &objList[count] ) ;
      if ( rc )
      {
         printf ( "Failed to insert record, rc = %d" OSS_NEWLINE, rc ) ;
      }
      bson_destroy ( &objList[count] ) ;
   }

   // query all the record in this collection
   // and return the result by the cursor handle
   rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ;
   CHECK_RC ( rc, "Failed to query" ) ;

   // get all the qureied records
   bson_init(&obj);
   while( !( rc=sdbNext( cursor, &obj ) ) )
   {
      bson_print( &obj ) ;
      bson_destroy(&obj) ;
      bson_init(&obj);
   }
   bson_destroy(&obj) ;
   if( rc==SDB_DMS_EOC )
   {
      printf("All the record had been list." OSS_NEWLINE ) ;
   }
   else if( rc!=SDB_OK )
   {
      CHECK_RC ( rc, "Failed to get the record" ) ;
   }

   // drop the specified collection
   rc = sdbDropCollection( collectionspace,COLLECTION_NAME ) ;
   CHECK_RC ( rc, "Failed to drop the specified collection" ) ;

   // drop the specified collection space
   rc = sdbDropCollectionSpace( connection,COLLECTION_SPACE_NAME ) ;
   CHECK_RC ( rc, "Failed to drop the specified collection space" ) ;

done:
   // disconnect the connection
   sdbDisconnect ( connection ) ;
   // release the local variables
   sdbReleaseCursor ( cursor ) ;
   sdbReleaseCollection ( collection ) ;
   sdbReleaseCS ( collectionspace ) ;
   sdbReleaseConnection ( connection ) ;
   return 0;
error:
   goto done ;
}
Exemplo n.º 8
0
INT32 main ( INT32 argc, CHAR **argv )
{
   // initialize local variables
   CHAR *pHostName                   = NULL ;
   CHAR *pServiceName                = NULL ;
   CHAR *pUsr                        = NULL ;
   CHAR *pPasswd                     = NULL ;
   // define a connetion handle; use to connect to database
   sdbConnectionHandle connection    = 0 ;
   // define a collection space handle
   sdbCSHandle collectionspace       = 0 ;
   // define a collection handle
   sdbCollectionHandle collection    = 0 ;
   // define a cursor handle for query
   sdbCursorHandle cursor            = 0 ;

   // define local variables
   // initialize them before use
   bson obj ;
   bson rule ;
   bson condition ;
   INT32 rc = SDB_OK ;

   // read argument
   pHostName    = (CHAR*)argv[1] ;
   pServiceName = (CHAR*)argv[2] ;
   pUsr         = (CHAR*)argv[3] ;
   pPasswd      = (CHAR*)argv[4] ;

   // verify syntax
   if ( 5 != argc )
   {
      displaySyntax ( (CHAR*)argv[0] ) ;
      exit ( 0 ) ;
   }

   // connect to database
   rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to connet to database, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }

   // create collection space
   rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME,
                                   SDB_PAGESIZE_4K, &collectionspace ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to create collection space, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }

   // create collection in a specified colletion space.
   rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to create collection, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }

   // insert records to the collection
   bson_init( &obj ) ;
   // insert a English record
   createEnglishRecord ( &obj  ) ;
   rc = sdbInsert ( collection, &obj ) ;
   if ( rc )
   {
      printf ( "Failed to insert record, rc = %d" OSS_NEWLINE, rc ) ;
   }
   bson_destroy ( &obj ) ;

   // query the records
   // the result is in the cursor handle
   rc = sdbQuery(collection, NULL, NULL,  NULL, NULL, 0, -1, &cursor ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }

   // update the record
   // let's set the rule and query condition first
   // here,we make the condition to be NULL
   // so all the records will be update
   bson_init( &rule ) ;
   bson_append_start_object ( &rule, "$set" ) ;
   bson_append_start_object ( &rule, "dev_id" ) ;
   bson_append_binary ( &rule, "id_rand", BSON_BIN_BINARY, "a", 1 ) ;
   bson_append_start_object ( &rule, "id_s" ) ;
   bson_append_int ( &rule, "year", 2005 ) ;
   bson_append_int ( &rule, "mon", 11 ) ;
   bson_append_int ( &rule, "day", 10 ) ;
   bson_append_int ( &rule, "num", 1024 ) ;
   bson_append_finish_object ( &rule ) ;
   //bson_append_binary ( &rule, "id_rand", BSON_BIN_BINARY, "a", 1 ) ;
   bson_append_finish_object ( &rule ) ;
   bson_append_finish_object ( &rule ) ;
   rc = bson_finish ( &rule ) ;
   CHECK_RC ( rc, "Failed to build bson" ) ;

   printf("The update rule is:") ;
   bson_print( &rule ) ;

   bson_init ( &condition ) ;
   bson_append_start_object ( &condition, "dev_id.id_s" ) ;
   bson_append_int ( &condition, "year", 2007 ) ;
   bson_append_int ( &condition, "mon", 11 ) ;
   bson_append_int ( &condition, "day", 10 ) ;
   bson_append_int ( &condition, "num", 1024 ) ;
   bson_append_finish_object ( &condition ) ;
   rc = bson_finish ( &condition ) ;
   CHECK_RC ( rc, "Failed to build bson" ) ;

   rc = sdbUpsert( collection, &rule, &condition, NULL ) ;
   if( rc!=SDB_OK )
   {
      printf("Failed to update the record, rc = %d" OSS_NEWLINE, rc ) ;
      goto error ;
   }
   bson_destroy(&rule);
   printf("Success to update!" OSS_NEWLINE ) ;

done:
   // drop collection space
   rc = sdbDropCollectionSpace( connection, COLLECTION_SPACE_NAME ) ;
   if ( rc != SDB_OK )
   {
      printf("Failed to drop the specified collection,\
              rc = %d" OSS_NEWLINE, rc ) ;
   }
Exemplo n.º 9
0
INT32 main ( INT32 argc, CHAR **argv )
{
   // initialize local variables
   CHAR *pHostName                   = NULL ;
   CHAR *pServiceName                = NULL ;
   CHAR *pUsr                        = NULL ;
   CHAR *pPasswd                     = NULL ;
   // define a connetion handle; use to connect to database
   sdbConnectionHandle connection    = 0 ;
   // define a collection space handle
   sdbCSHandle collectionspace       = 0 ;
   // define a collection handle
   sdbCollectionHandle collection    = 0 ;
   // define a cursor handle for query
   sdbCursorHandle cursor            = 0 ;

   // define local variables
   // initialize them before use
   bson obj ;
   bson rule ;
   INT32 rc = SDB_OK ;

   // read argument
   pHostName    = (CHAR*)argv[1] ;
   pServiceName = (CHAR*)argv[2] ;
   pUsr         = (CHAR*)argv[3] ;
   pPasswd      = (CHAR*)argv[4] ;

   // verify syntax
   if ( 5 != argc )
   {
      displaySyntax ( (CHAR*)argv[0] ) ;
      exit ( 0 ) ;
   }

   // connect to database
   rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ;
   CHECK_RC ( rc, "Failed to connet to database" ) ;

   // create collection space
   rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME,
                                   SDB_PAGESIZE_4K, &collectionspace ) ;
   CHECK_RC ( rc, "Failed to create collection space" ) ;

   // create collection in a specified colletion space.
   rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ;
   CHECK_RC ( rc, "Failed to create collection" ) ;

   // insert records to the collection
   bson_init ( &obj ) ;
   // build a English record
   createEnglishRecord ( &obj  ) ;

   // insert
   rc = sdbInsert ( collection, &obj ) ;
   CHECK_RC ( rc, "Failed to insert record" ) ;

   // query the records
   // the result is in the cursor handle
   rc = sdbQuery(collection, NULL, NULL,  NULL, NULL, 0, -1, &cursor ) ;
   CHECK_RC ( rc, "Failed to query" ) ;

   // update the record
   // let's set the rule and query condition first
   // here,we make the condition to be NULL
   // so all the records will be update
   bson_init ( &rule ) ;
   bson_append_start_object ( &rule, "$set" ) ;
   bson_append_int ( &rule, "age", 19 ) ;
   bson_append_finish_object ( &rule ) ;
   bson_finish ( &rule ) ;
   CHECK_RC ( rc, "Failed to build bson" ) ;

   printf ( "The update rule is:" ) ;
   bson_print( &rule ) ;

   // update
   rc = sdbUpdate ( collection, &rule, NULL, NULL ) ;
   CHECK_RC ( rc, "Failed to update the record" ) ;

   // free memory after finish using bson
   bson_destroy ( &rule ) ;
   printf ( "Success to update!" OSS_NEWLINE ) ;

   // drop the specified collection
   rc = sdbDropCollection ( collectionspace,COLLECTION_NAME ) ;
   CHECK_RC ( rc, "Failed to drop the specified collection" ) ;

   // drop the specified collection space
   rc = sdbDropCollectionSpace ( connection,COLLECTION_SPACE_NAME ) ;
   CHECK_RC ( rc, "Failed to drop the specified collection" ) ;

done:
   // disconnect the connection
   sdbDisconnect ( connection ) ;
   // release the local variables
   sdbReleaseCursor ( cursor ) ;
   sdbReleaseCollection ( collection ) ;
   sdbReleaseCS ( collectionspace ) ;
   sdbReleaseConnection ( connection ) ;
   return 0;
error:
   goto done ;
}