Class: RDO::SQLite::Driver
- Inherits:
-
Driver
- Object
- Driver
- RDO::SQLite::Driver
- Defined in:
- lib/rdo/sqlite/driver.rb
Overview
Main Driver class to hook into sqlite3 API
Instance Method Summary collapse
-
#close ⇒ Object
Close the database and free memory.
-
#execute(stmt, *args) ⇒ RDO::Result
Execute a single statement.
-
#open ⇒ Object
Opens a database.
-
#open? ⇒ Boolean
Checks if the database file is open.
-
#prepare(cmd) ⇒ Object
Create a new prepared statement for cmd.
-
#quote(str) ⇒ Object
Quote a string literal for interpolation into a statement.
-
#readonly? ⇒ Boolean
Predicte check to see if this is a read-only database.
Instance Method Details
#close ⇒ Object
Close the database and free memory
66 67 68 69 70 71 72 73 74 75 |
# File 'ext/rdo_sqlite/driver.c', line 66
static VALUE rdo_sqlite_driver_close(VALUE self) {
RDOSQLiteDriver * driver;
Data_Get_Struct(self, RDOSQLiteDriver, driver);
sqlite3_close(driver->db);
driver->db = NULL;
driver->is_open = 0;
return Qtrue;
}
|
#execute(stmt, *args) ⇒ RDO::Result
Execute a single statement.
This method delegates to #prepare, then executes.
24 25 26 |
# File 'lib/rdo/sqlite/driver.rb', line 24 def execute(stmt, *args) prepare(stmt).execute(*args) end |
#open ⇒ Object
Opens a database
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'ext/rdo_sqlite/driver.c', line 38
static VALUE rdo_sqlite_driver_open(VALUE self) {
RDOSQLiteDriver * driver;
Data_Get_Struct(self, RDOSQLiteDriver, driver);
if (driver->is_open) {
return Qtrue;
}
if (sqlite3_open_v2(
RSTRING_PTR(rb_funcall(self, rb_intern("filename"), 0)),
&(driver->db),
rdo_sqlite_driver_mode(self), NULL) != SQLITE_OK) {
RDO_ERROR("SQLite3 database open failed: %s", sqlite3_errmsg(driver->db));
} else {
driver->is_open = 1;
}
return Qtrue;
}
|
#open? ⇒ Boolean
Checks if the database file is open
59 60 61 62 63 |
# File 'ext/rdo_sqlite/driver.c', line 59
static VALUE rdo_sqlite_driver_open_p(VALUE self) {
RDOSQLiteDriver * driver;
Data_Get_Struct(self, RDOSQLiteDriver, driver);
return driver->is_open ? Qtrue : Qfalse;
}
|
#prepare(cmd) ⇒ Object
Create a new prepared statement for cmd
78 79 80 |
# File 'ext/rdo_sqlite/driver.c', line 78
static VALUE rdo_sqlite_driver_prepare(VALUE self, VALUE cmd) {
return rdo_sqlite_statement_executor_new(self, cmd);
}
|
#quote(str) ⇒ Object
Quote a string literal for interpolation into a statement
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'ext/rdo_sqlite/driver.c', line 83
static VALUE rdo_sqlite_driver_quote(VALUE self, VALUE str) {
Check_Type(str, T_STRING);
char * raw = RSTRING_PTR(str);
unsigned long len = RSTRING_LEN(str);
char * buf = malloc(sizeof(char) * len * 2);
char * b = buf;
char * s = raw;
// not using sqlite3_mprintf() due to \0 check & performance
for (; (unsigned long) (s - raw) < len; ++s, ++b) {
switch (*s) {
case '\0':
free(buf);
rb_raise(rb_eArgError,
"Cannot #quote binary data. Use #prepare, #execute or a hex X'AABB' literal.");
break;
case '\'':
*(b++) = *s;
default:
*b = *s;
}
}
VALUE quoted = rb_str_new(buf, b - buf);
free(buf);
return quoted;
}
|
#readonly? ⇒ Boolean
Predicte check to see if this is a read-only database.
32 33 34 |
# File 'lib/rdo/sqlite/driver.rb', line 32 def readonly? %w[ro readonly].include?([:mode]) end |