Class: UnQLite::Database

Inherits:
Object
  • Object
show all
Defined in:
ext/unqlite/unqlite_database.c

Instance Method Summary collapse

Constructor Details

#initialize(rb_string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'ext/unqlite/unqlite_database.c', line 22

static VALUE initialize(VALUE self, VALUE rb_string)
{
  void *c_string;
  int rc;
  unqliteRubyPtr ctx;

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

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

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

  // Open database
  // TODO: Accept others open mode (read-only + mmap, etc. Check http://unqlite.org/c_api/unqlite_open.html)
  rc = unqlite_open(&ctx->pDb, c_string, UNQLITE_OPEN_CREATE);

  // Check if any exception should be raised
  CHECK(ctx->pDb, rc);

  return self;
}

Instance Method Details

#begin_transactionObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/unqlite/unqlite_database.c', line 130

static VALUE unqlite_database_begin_transaction(VALUE self)
{
  int rc;
  unqliteRubyPtr ctx;

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

  // Begin write-transaction manually
  rc = unqlite_begin(ctx->pDb);

  // Check for errors
  CHECK(ctx->pDb, rc);

  return Qtrue;
}

#closeObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/unqlite/unqlite_database.c', line 48

static VALUE unqlite_database_close(VALUE self)
{
  int rc;
  unqliteRubyPtr ctx;

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

  // Close database
  rc = unqlite_close(ctx->pDb);

  // Check for errors
  CHECK(ctx->pDb, rc);

  return Qtrue;
}

#commitObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/unqlite/unqlite_database.c', line 147

static VALUE unqlite_database_commit(VALUE self)
{
  int rc;
  unqliteRubyPtr ctx;

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

  // Commit transaction
  rc = unqlite_commit(ctx->pDb);

  // Check for errors
  CHECK(ctx->pDb, rc);

  return Qtrue;
}

#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);
}

#rollbackObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/unqlite/unqlite_database.c', line 164

static VALUE unqlite_database_rollback(VALUE self)
{
  int rc;
  unqliteRubyPtr ctx;

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

  // Rollback transaction
  rc = unqlite_rollback(ctx->pDb);

  // Check for errors
  CHECK(ctx->pDb, rc);

  return Qtrue;
}

#store(key, value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/unqlite/unqlite_database.c', line 66

static VALUE unqlite_database_store(VALUE self, VALUE key, VALUE value)
{
  void *c_key;
  void *c_value;
  int rc;
  unqliteRubyPtr ctx;

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

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

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

  c_value = calloc(RSTRING_LEN(value), sizeof(char));
  memcpy(c_value, StringValuePtr(value), RSTRING_LEN(value));

  // Store it
  rc = unqlite_kv_store(ctx->pDb, c_key, -1, c_value, sizeof(c_value));

  // Check for errors
  CHECK(ctx->pDb, rc);

  return Qtrue;
}