Class: Extralite::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/extralite.rb,
ext/extralite/database.c

Overview

An SQLite database

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object

Initializes a new SQLite database with the given path.


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/extralite/database.c', line 69

VALUE Database_initialize(VALUE self, VALUE path) {
  int rc;
  Database_t *db;
  GetDatabase(self, db);

  rc = sqlite3_open(StringValueCStr(path), &db->sqlite3_db);
  if (rc) {
    sqlite3_close(db->sqlite3_db);
    rb_raise(cError, "%s", sqlite3_errmsg(db->sqlite3_db));
  }

  rc = sqlite3_enable_load_extension(db->sqlite3_db, 1);
  if (rc) {
    sqlite3_close(db->sqlite3_db);
    rb_raise(cError, "%s", sqlite3_errmsg(db->sqlite3_db));
  }

  return Qnil;
}

Instance Method Details

#changesInteger

Returns the number of changes made to the database by the last operation.

Returns:

  • (Integer)

283
284
285
286
287
288
# File 'ext/extralite/database.c', line 283

VALUE Database_changes(VALUE self) {
  Database_t *db;
  GetOpenDatabase(self, db);

  return INT2NUM(sqlite3_changes(db->sqlite3_db));
}

#closeObject

Closes the database.


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/extralite/database.c', line 94

VALUE Database_close(VALUE self) {
  int rc;
  Database_t *db;
  GetDatabase(self, db);

  rc = sqlite3_close(db->sqlite3_db);
  if (rc) {
    rb_raise(cError, "%s", sqlite3_errmsg(db->sqlite3_db));
  }

  db->sqlite3_db = 0;
  return self;
}

#closed?Boolean

Returns true if the database is closed.

Returns:

  • (Boolean)

Returns:

  • (bool)

    is database closed


115
116
117
118
119
120
# File 'ext/extralite/database.c', line 115

VALUE Database_closed_p(VALUE self) {
  Database_t *db;
  GetDatabase(self, db);

  return db->sqlite3_db ? Qfalse : Qtrue;
}

#filenameString

Returns the database filename.

Returns:

  • (String)

295
296
297
298
299
300
301
302
303
304
305
# File 'ext/extralite/database.c', line 295

VALUE Database_filename(int argc, VALUE *argv, VALUE self) {
  const char *db_name;
  const char *filename;
  Database_t *db;
  GetOpenDatabase(self, db);

  rb_check_arity(argc, 0, 1);
  db_name = (argc == 1) ? StringValueCStr(argv[0]) : "main";
  filename = sqlite3_db_filename(db->sqlite3_db, db_name);
  return filename ? rb_str_new_cstr(filename) : Qnil;
}

#last_insert_rowidInteger

Returns the rowid of the last inserted row.

Returns:

  • (Integer)

271
272
273
274
275
276
# File 'ext/extralite/database.c', line 271

VALUE Database_last_insert_rowid(VALUE self) {
  Database_t *db;
  GetOpenDatabase(self, db);

  return INT2NUM(sqlite3_last_insert_rowid(db->sqlite3_db));
}

#load_extension(path) ⇒ Object

Loads an extension with the given path.


324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/extralite/database.c', line 324

VALUE Database_load_extension(VALUE self, VALUE path) {
  Database_t *db;
  GetOpenDatabase(self, db);
  char *err_msg;

  int rc = sqlite3_load_extension(db->sqlite3_db, RSTRING_PTR(path), 0, &err_msg);
  if (rc != SQLITE_OK) {
    VALUE error = rb_exc_new2(cError, err_msg);
    sqlite3_free(err_msg);
    rb_exc_raise(error);
  }

  return self;
}

#prepare(sql) ⇒ Extralite::PreparedStatement

Creates a prepared statement with the given SQL query.


344
345
346
# File 'ext/extralite/database.c', line 344

VALUE Database_prepare(VALUE self, VALUE sql) {
  return rb_funcall(cPreparedStatement, ID_NEW, 2, self, sql);
}

#query(sql, *parameters, &block) ⇒ Array #query_hash(sql, *parameters, &block) ⇒ Array

Runs a query returning rows as hashes (with symbol keys). If a block is given, it will be called for each row. Otherwise, an array containing all rows is returned.

Query parameters to be bound to placeholders in the query can be specified as a list of values or as a hash mapping parameter names to values. When parameters are given as a least, the query should specify parameters using `?`:

db.query('select * from foo where x = ?', 42)

Named placeholders are specified using `:`. The placeholder values are specified using a hash, where keys are either strings are symbols. String keys can include or omit the `:` prefix. The following are equivalent:

db.query('select * from foo where x = :bar', bar: 42)
db.query('select * from foo where x = :bar', 'bar' => 42)
db.query('select * from foo where x = :bar', ':bar' => 42)

Overloads:

  • #query(sql, *parameters, &block) ⇒ Array

    Returns:

    • (Array)
  • #query_hash(sql, *parameters, &block) ⇒ Array

    Returns:

    • (Array)

164
165
166
# File 'ext/extralite/database.c', line 164

VALUE Database_query_hash(int argc, VALUE *argv, VALUE self) {
  return Database_perform_query(argc, argv, self, safe_query_hash);
}

#query_ary(sql, *parameters, &block) ⇒ Array

Runs a query returning rows as arrays. If a block is given, it will be called for each row. Otherwise, an array containing all rows is returned.

Query parameters to be bound to placeholders in the query can be specified as a list of values or as a hash mapping parameter names to values. When parameters are given as a least, the query should specify parameters using `?`:

db.query_ary('select * from foo where x = ?', 42)

Named placeholders are specified using `:`. The placeholder values are specified using a hash, where keys are either strings are symbols. String keys can include or omit the `:` prefix. The following are equivalent:

db.query_ary('select * from foo where x = :bar', bar: 42)
db.query_ary('select * from foo where x = :bar', 'bar' => 42)
db.query_ary('select * from foo where x = :bar', ':bar' => 42)

Returns:

  • (Array)

189
190
191
# File 'ext/extralite/database.c', line 189

VALUE Database_query_ary(int argc, VALUE *argv, VALUE self) {
  return Database_perform_query(argc, argv, self, safe_query_ary);
}

#query(sql, *parameters, &block) ⇒ Array #query_hash(sql, *parameters, &block) ⇒ Array

Runs a query returning rows as hashes (with symbol keys). If a block is given, it will be called for each row. Otherwise, an array containing all rows is returned.

Query parameters to be bound to placeholders in the query can be specified as a list of values or as a hash mapping parameter names to values. When parameters are given as a least, the query should specify parameters using `?`:

db.query('select * from foo where x = ?', 42)

Named placeholders are specified using `:`. The placeholder values are specified using a hash, where keys are either strings are symbols. String keys can include or omit the `:` prefix. The following are equivalent:

db.query('select * from foo where x = :bar', bar: 42)
db.query('select * from foo where x = :bar', 'bar' => 42)
db.query('select * from foo where x = :bar', ':bar' => 42)

Overloads:

  • #query(sql, *parameters, &block) ⇒ Array

    Returns:

    • (Array)
  • #query_hash(sql, *parameters, &block) ⇒ Array

    Returns:

    • (Array)

164
165
166
# File 'ext/extralite/database.c', line 164

VALUE Database_query_hash(int argc, VALUE *argv, VALUE self) {
  return Database_perform_query(argc, argv, self, safe_query_hash);
}

#query_single_column(sql, *parameters, &block) ⇒ Array

Runs a query returning single column values. If a block is given, it will be called for each value. Otherwise, an array containing all values is returned.

Query parameters to be bound to placeholders in the query can be specified as a list of values or as a hash mapping parameter names to values. When parameters are given as a least, the query should specify parameters using `?`:

db.query_single_column('select x from foo where x = ?', 42)

Named placeholders are specified using `:`. The placeholder values are specified using a hash, where keys are either strings are symbols. String keys can include or omit the `:` prefix. The following are equivalent:

db.query_single_column('select x from foo where x = :bar', bar: 42)
db.query_single_column('select x from foo where x = :bar', 'bar' => 42)
db.query_single_column('select x from foo where x = :bar', ':bar' => 42)

Returns:

  • (Array)

238
239
240
# File 'ext/extralite/database.c', line 238

VALUE Database_query_single_column(int argc, VALUE *argv, VALUE self) {
  return Database_perform_query(argc, argv, self, safe_query_single_column);
}

#query_single_row(sql, *parameters) ⇒ Object

Runs a query returning a single row as a hash.

Query parameters to be bound to placeholders in the query can be specified as a list of values or as a hash mapping parameter names to values. When parameters are given as a least, the query should specify parameters using `?`:

db.query_single_row('select * from foo where x = ?', 42)

Named placeholders are specified using `:`. The placeholder values are specified using a hash, where keys are either strings are symbols. String keys can include or omit the `:` prefix. The following are equivalent:

db.query_single_row('select * from foo where x = :bar', bar: 42)
db.query_single_row('select * from foo where x = :bar', 'bar' => 42)
db.query_single_row('select * from foo where x = :bar', ':bar' => 42)

213
214
215
# File 'ext/extralite/database.c', line 213

VALUE Database_query_single_row(int argc, VALUE *argv, VALUE self) {
  return Database_perform_query(argc, argv, self, safe_query_single_row);
}

#query_single_value(sql, *parameters) ⇒ Object

Runs a query returning a single value from the first row.

Query parameters to be bound to placeholders in the query can be specified as a list of values or as a hash mapping parameter names to values. When parameters are given as a least, the query should specify parameters using `?`:

db.query_single_value('select x from foo where x = ?', 42)

Named placeholders are specified using `:`. The placeholder values are specified using a hash, where keys are either strings are symbols. String keys can include or omit the `:` prefix. The following are equivalent:

db.query_single_value('select x from foo where x = :bar', bar: 42)
db.query_single_value('select x from foo where x = :bar', 'bar' => 42)
db.query_single_value('select x from foo where x = :bar', ':bar' => 42)

262
263
264
# File 'ext/extralite/database.c', line 262

VALUE Database_query_single_value(int argc, VALUE *argv, VALUE self) {
  return Database_perform_query(argc, argv, self, safe_query_single_value);
}

#transaction_active?Boolean

Returns true if a transaction is currently in progress.

Returns:

  • (Boolean)

312
313
314
315
316
317
# File 'ext/extralite/database.c', line 312

VALUE Database_transaction_active_p(VALUE self) {
  Database_t *db;
  GetOpenDatabase(self, db);

  return sqlite3_get_autocommit(db->sqlite3_db) ? Qfalse : Qtrue;
}