Method: UnQLite::Database#fetch

Defined in:
ext/unqlite/unqlite_database.c

#fetch(collection_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/unqlite/unqlite_database.c', line 96

static VALUE unqlite_database_fetch(VALUE self, VALUE collection_name)
{
  void *c_collection_name;
  void *fetched_data;
  unqlite_int64 n_bytes;
  int rc;
  unqliteRubyPtr ctx;

  // Ensure the given argument is a ruby string
  Check_Type(collection_name, T_STRING);

  // Get class context
  Data_Get_Struct(self, unqliteRuby, ctx);

  // Transform Ruby string into C string
  c_collection_name = calloc(RSTRING_LEN(collection_name), sizeof(char));
  memcpy(c_collection_name, StringValuePtr(collection_name), RSTRING_LEN(collection_name));

  // Extract the data size, check for errors and return if any
  rc = unqlite_kv_fetch(ctx->pDb, c_collection_name, -1, NULL, &n_bytes);
  CHECK(ctx->pDb, rc);
  if( rc != UNQLITE_OK ) { return Qnil; }

  // Data is empty
  fetched_data = (char *)malloc(n_bytes);
  if( fetched_data == NULL ) { return rb_str_new2(""); }

  // Now, fetch the data
  rc = unqlite_kv_fetch(ctx->pDb, c_collection_name, -1, fetched_data, &n_bytes);
  CHECK(ctx->pDb, rc);

  return rb_str_new2((char *)fetched_data);
}