コード例 #1
0
ファイル: update_use_id.c プロジェクト: 247687009/SequoiaDB
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 ;
}
コード例 #2
0
int test_bson_generic( void ) {

   bson_iterator it, it2, it3;
   bson_oid_t oid;
   bson_timestamp_t ts;
   bson_timestamp_t ts_result;
   bson b[1];
   bson copy[1];
   bson scope[1];

   ts.i = 1;
   ts.t = 2;

   bson_init( b );
   bson_append_double( b, "d", 3.14 );
   bson_append_string( b, "s", "hello" );
   bson_append_string_n( b, "s_n", "goodbye cruel world", 7 );

   {
       bson_append_start_object( b, "o" );
       bson_append_start_array( b, "a" );
       bson_append_binary( b, "0", 8, "w\0rld", 5 );
       bson_append_finish_object( b );
       bson_append_finish_object( b );
   }

   bson_append_undefined( b, "u" );

   bson_oid_from_string( &oid, "010203040506070809101112" );
   ASSERT( !memcmp( oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) );
   bson_append_oid( b, "oid", &oid );

   bson_append_bool( b, "b", 1 );
   bson_append_date( b, "date", 0x0102030405060708 );
   bson_append_null( b, "n" );
   bson_append_regex( b, "r", "^asdf", "imx" );
   /* no dbref test (deprecated) */
   bson_append_code( b, "c", "function(){}" );
   bson_append_code_n( b, "c_n", "function(){}garbage", 12 );
   bson_append_symbol( b, "symbol", "symbol" );
   bson_append_symbol_n( b, "symbol_n", "symbol and garbage", 6 );

   {
       bson_init( scope );
       bson_append_int( scope, "i", 123 );
       bson_finish( scope );

       bson_append_code_w_scope( b, "cws", "function(){return i}", scope );
       bson_destroy( scope );
   }

   bson_append_timestamp( b, "timestamp", &ts );
   bson_append_long( b, "l", 0x1122334455667788 );

   /* Ensure that we can't copy a non-finished object. */
   ASSERT( bson_copy( copy, b ) == BSON_ERROR );

   bson_finish( b );

   ASSERT( b->err == BSON_VALID );

   /* Test append after finish. */
   ASSERT( bson_append_string( b, "foo", "bar" ) == BSON_ERROR );
   ASSERT( b->err & BSON_ALREADY_FINISHED );

   ASSERT( bson_copy( copy, b ) == BSON_OK );

   ASSERT( 1 == copy->finished );
   ASSERT( 0 == copy->err );

   bson_destroy( copy );

   bson_print( b );

   bson_iterator_init( &it, b );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
   ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) );
   ASSERT( bson_iterator_double( &it ) == 3.14 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_STRING );
   ASSERT( bson_iterator_type( &it ) == BSON_STRING );
   ASSERT( !strcmp( bson_iterator_key( &it ), "s" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "hello" ) );
   ASSERT( strcmp( bson_iterator_string( &it ), "" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_STRING );
   ASSERT( bson_iterator_type( &it ) == BSON_STRING );
   ASSERT( !strcmp( bson_iterator_key( &it ), "s_n" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "goodbye" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_OBJECT );
   ASSERT( bson_iterator_type( &it ) == BSON_OBJECT );
   ASSERT( !strcmp( bson_iterator_key( &it ), "o" ) );
   bson_iterator_subiterator( &it, &it2 );

   ASSERT( bson_iterator_more( &it2 ) );
   ASSERT( bson_iterator_next( &it2 ) == BSON_ARRAY );
   ASSERT( bson_iterator_type( &it2 ) == BSON_ARRAY );
   ASSERT( !strcmp( bson_iterator_key( &it2 ), "a" ) );
   bson_iterator_subiterator( &it2, &it3 );

   ASSERT( bson_iterator_more( &it3 ) );
   ASSERT( bson_iterator_next( &it3 ) == BSON_BINDATA );
   ASSERT( bson_iterator_type( &it3 ) == BSON_BINDATA );
   ASSERT( !strcmp( bson_iterator_key( &it3 ), "0" ) );
   ASSERT( bson_iterator_bin_type( &it3 ) == 8 );
   ASSERT( bson_iterator_bin_len( &it3 ) == 5 );
   ASSERT( !memcmp( bson_iterator_bin_data( &it3 ), "w\0rld", 5 ) );

   ASSERT( bson_iterator_more( &it3 ) );
   ASSERT( bson_iterator_next( &it3 ) == BSON_EOO );
   ASSERT( bson_iterator_type( &it3 ) == BSON_EOO );
   ASSERT( !bson_iterator_more( &it3 ) );

   ASSERT( bson_iterator_more( &it2 ) );
   ASSERT( bson_iterator_next( &it2 ) == BSON_EOO );
   ASSERT( bson_iterator_type( &it2 ) == BSON_EOO );
   ASSERT( !bson_iterator_more( &it2 ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_UNDEFINED );
   ASSERT( bson_iterator_type( &it ) == BSON_UNDEFINED );
   ASSERT( !strcmp( bson_iterator_key( &it ), "u" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_OID );
   ASSERT( bson_iterator_type( &it ) == BSON_OID );
   ASSERT( !strcmp( bson_iterator_key( &it ), "oid" ) );
   ASSERT( !memcmp( bson_iterator_oid( &it )->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) );
   ASSERT( bson_iterator_oid( &it )->ints[0] == oid.ints[0] );
   ASSERT( bson_iterator_oid( &it )->ints[1] == oid.ints[1] );
   ASSERT( bson_iterator_oid( &it )->ints[2] == oid.ints[2] );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_BOOL );
   ASSERT( bson_iterator_type( &it ) == BSON_BOOL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "b" ) );
   ASSERT( bson_iterator_bool( &it ) == 1 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_DATE );
   ASSERT( bson_iterator_type( &it ) == BSON_DATE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "date" ) );
   ASSERT( bson_iterator_date( &it ) == 0x0102030405060708 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_NULL );
   ASSERT( bson_iterator_type( &it ) == BSON_NULL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "n" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_REGEX );
   ASSERT( bson_iterator_type( &it ) == BSON_REGEX );
   ASSERT( !strcmp( bson_iterator_key( &it ), "r" ) );
   ASSERT( !strcmp( bson_iterator_regex( &it ), "^asdf" ) );
   ASSERT( !strcmp( bson_iterator_regex_opts( &it ), "imx" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_CODE );
   ASSERT( bson_iterator_type( &it ) == BSON_CODE );
   ASSERT( !strcmp( bson_iterator_code(&it), "function(){}") );
   ASSERT( !strcmp( bson_iterator_key( &it ), "c" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_CODE );
   ASSERT( bson_iterator_type( &it ) == BSON_CODE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "c_n" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "" ) );
   ASSERT( !strcmp( bson_iterator_code( &it ), "function(){}" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL );
   ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "symbol" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL );
   ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "symbol_n" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_CODEWSCOPE );
   ASSERT( bson_iterator_type( &it ) == BSON_CODEWSCOPE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "cws" ) );
   ASSERT( !strcmp( bson_iterator_code( &it ), "function(){return i}" ) );

   {
       bson scope;
       bson_iterator_code_scope( &it, &scope );
       bson_iterator_init( &it2, &scope );

       ASSERT( bson_iterator_more( &it2 ) );
       ASSERT( bson_iterator_next( &it2 ) == BSON_INT );
       ASSERT( bson_iterator_type( &it2 ) == BSON_INT );
       ASSERT( !strcmp( bson_iterator_key( &it2 ), "i" ) );
       ASSERT( bson_iterator_int( &it2 ) == 123 );

       ASSERT( bson_iterator_more( &it2 ) );
       ASSERT( bson_iterator_next( &it2 ) == BSON_EOO );
       ASSERT( bson_iterator_type( &it2 ) == BSON_EOO );
       ASSERT( !bson_iterator_more( &it2 ) );
   }

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_TIMESTAMP );
   ASSERT( bson_iterator_type( &it ) == BSON_TIMESTAMP );
   ASSERT( !strcmp( bson_iterator_key( &it ), "timestamp" ) );
   ts_result = bson_iterator_timestamp( &it );
   ASSERT( ts_result.i == 1 );
   ASSERT( ts_result.t == 2 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_LONG );
   ASSERT( bson_iterator_type( &it ) == BSON_LONG );
   ASSERT( !strcmp( bson_iterator_key( &it ), "l" ) );
   ASSERT( bson_iterator_long( &it ) == 0x1122334455667788 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_EOO );
   ASSERT( bson_iterator_type( &it ) == BSON_EOO );
   ASSERT( !bson_iterator_more( &it ) );

   bson_destroy( b );

   {
       bson bsrc[1];
       bson_init( bsrc );
       bson_append_double( bsrc, "d", 3.14 );
       bson_finish( bsrc );
       ASSERT( bsrc->err == BSON_VALID );
       bson_init( b );
       bson_append_double( b, "", 3.14 ); /* test empty name (in general) */
       bson_iterator_init( &it, bsrc );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE );
       bson_append_element( b, "d", &it );
       bson_append_element( b, 0, &it ); /* test null */
       bson_append_element( b, "", &it ); /* test empty name */
       bson_finish( b );
       ASSERT( b->err == BSON_VALID );
       /* bson_print( b ); */
       bson_iterator_init( &it, b );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_EOO );
       ASSERT( !bson_iterator_more( &it ) );
       bson_destroy( bsrc );
       bson_destroy( b );
   }

   return 0;
}