Skip to content

Instantly share code, notes, and snippets.

@Asher-
Created October 9, 2010 09:00
Show Gist options
  • Save Asher-/618041 to your computer and use it in GitHub Desktop.
Save Asher-/618041 to your computer and use it in GitHub Desktop.
/**************************
* retrieve_data_for_key *
* retrieve_for_key *
* retrieve *
**************************/
// database.retrieve( primary_key )
// database.retrieve( :index, secondary_key )
// database.retrieve( :index => secondary_key_in_index )
// database.retrieve( :index => [ secondary_keys_in_index, ... ] )
// database.retrieve( [ any args ] )
VALUE rb_RPDB_Database_retrieve( int argc,
VALUE* args,
VALUE rb_database ) {
VALUE rb_index = Qnil;
VALUE rb_key = Qnil;
VALUE rb_hash_descriptor_index_key_or_keys_array = Qnil;
VALUE rb_args_array = Qnil;
R_DefineAndParse( argc, args, rb_database,
//----------------------------------------------//
R_DescribeParameterSet(
R_ParameterSet( R_Parameter( R_MatchHash( R_Key( R_Type( R_SYMBOL | R_STRING ) ),
R_Data( R_Type( R_ANY ) ),
rb_hash_descriptor_index_key_or_keys_array ) ) ),
R_ListOrder( 3 ),
"{ :index => <key>, ... }, ...",
"{ 'index' => <key>, ... }, ...",
"{ :index => [ <keys> ], ... }, ...",
"{ 'index' => [ <keys> ], ... }, ..."
),
//----------------------------------------------//
R_DescribeParameterSet(
R_ParameterSet( R_Parameter( R_MatchType( R_SYMBOL | R_STRING,
rb_index ) ),
R_Parameter( R_MatchAny( rb_key ) ) ),
R_ListOrder( 2 ),
":index, <key, ...>",
"'index', <key, ...>"
),
//----------------------------------------------//
R_DescribeParameterSet(
R_ParameterSet( R_Parameter( R_MatchArray( rb_args_array ) ) ),
R_ListOrder( 4 ),
"[ <arg> ], ..."
),
//----------------------------------------------//
R_DescribeParameterSet(
R_ParameterSet( R_Parameter( R_MatchAny( rb_key ) ) ),
R_ListOrder( 1 ),
"<key>, ..."
)
//----------------------------------------------//
)
VALUE rb_return = Qnil;
if ( rb_hash_descriptor_index_key_or_keys_array != Qnil ) {
R_IterateHashDescriptor( rb_database,
rb_hash_descriptor_index_key_or_keys_array,
rb_return,
rb_RPDB_Database_internal_retrieveKeysForEachIndex );
}
else if ( rb_args_array != Qnil ) {
R_IterateArrayDescriptor( rb_database,
rb_args_array,
rb_return,
rb_RPDB_Database_retrieve );
}
// rb_index, rb_key and rb_key
else if ( rb_key != Qnil ) {
RPDB_Database* c_database = NULL;
RPDB_Record* c_record = NULL;
PRIMARY_OR_SECONDARY_DATABASE_FOR_INDEX( rb_database, c_database, rb_index );
rb_return = rb_ary_new();
VALUE rb_data = Qnil;
do {
PREPARE_RECORD_FROM_KEY_FOR_WRITE_RETRIEVE_DELETE( rb_database, c_database, c_record, rb_index, rb_key );
c_record = RPDB_Database_retrieveRecord( c_database,
c_record );
rb_data = RUBY_STRING_FOR_DATA_IN_RPDB_RECORD( c_record );
rb_ary_push( rb_return,
rb_data );
// remaining args are keys
} while ( R_Arg( rb_key ) );
rb_return = SIMPLIFIED_RUBY_ARRAY( rb_return );
}
return rb_return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment