rdo-sqlite 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +55 -26
- data/ext/rdo_sqlite/driver.c +9 -1
- data/lib/rdo/sqlite/driver.rb +20 -0
- data/lib/rdo/sqlite/version.rb +1 -1
- data/spec/sqlite/driver_spec.rb +37 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -5,8 +5,8 @@ This is the SQLite3 driver for [RDO—Ruby Data Objects]
|
|
5
5
|
|
6
6
|
[](http://travis-ci.org/d11wtq/rdo-sqlite)
|
7
7
|
|
8
|
-
Refer to the RDO project
|
9
|
-
information.
|
8
|
+
Refer to the [RDO project README](https://github.com/d11wtq/rdo) for full
|
9
|
+
usage information.
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -27,7 +27,6 @@ And install with Bundler:
|
|
27
27
|
The registered URI schemes are sqlite: and sqlite3:
|
28
28
|
|
29
29
|
``` ruby
|
30
|
-
require "rdo"
|
31
30
|
require "rdo-sqlite"
|
32
31
|
|
33
32
|
# use an in-memory database :memory:
|
@@ -41,37 +40,68 @@ db = RDO.open("sqlite:some/path/to/your.db")
|
|
41
40
|
|
42
41
|
# use an absolute path to a database
|
43
42
|
db = RDO.open("sqlite:/absolute/path/to/your.db")
|
44
|
-
```
|
45
|
-
|
46
|
-
## Type casting and bind parameters
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
the only internal types it actually stores are:
|
52
|
-
|
53
|
-
- NULL, which converts to nil in Ruby
|
54
|
-
- TEXT, which converts to a UTF-8 encoded String in Ruby
|
55
|
-
- INTEGER, which converts to a Fixnum in Ruby
|
56
|
-
- REAL, which converts to a Float in Ruby
|
57
|
-
- BLOB, which converts to a binary String in Ruby
|
44
|
+
# open in read-only mode
|
45
|
+
db = RDO.open("sqlite:/path/to/your.db?mode=readonly")
|
46
|
+
```
|
58
47
|
|
59
|
-
|
60
|
-
|
61
|
-
|
48
|
+
## Type support
|
49
|
+
|
50
|
+
SQLite has extremely limited type support. In fact, it only supports five
|
51
|
+
types. It allows other types to be specified as column types, but they will
|
52
|
+
be one of the core five types. It also allows storing any value of any type
|
53
|
+
in any column, regardless of what the column type is. You can read about that
|
54
|
+
[here](http://www.sqlite.org/datatype3.html).
|
55
|
+
|
56
|
+
The five data types are mapped as below:
|
57
|
+
|
58
|
+
<table>
|
59
|
+
<thead>
|
60
|
+
<tr>
|
61
|
+
<th>SQLite Type</th>
|
62
|
+
<th>Ruby Type</th>
|
63
|
+
<th>Notes</th>
|
64
|
+
</tr>
|
65
|
+
</thead>
|
66
|
+
<tbody>
|
67
|
+
<tr>
|
68
|
+
<th>NULL</th>
|
69
|
+
<td>NilClass</td>
|
70
|
+
<td></td>
|
71
|
+
</tr>
|
72
|
+
<tr>
|
73
|
+
<th>TEXT</th>
|
74
|
+
<td>String</td>
|
75
|
+
<td>The encoding is always UTF-8</td>
|
76
|
+
</tr>
|
77
|
+
<tr>
|
78
|
+
<th>INTEGER</th>
|
79
|
+
<td>Fixnum</td>
|
80
|
+
<td></td>
|
81
|
+
</tr>
|
82
|
+
<tr>
|
83
|
+
<th>REAL</th>
|
84
|
+
<td>Float</td>
|
85
|
+
<td></td>
|
86
|
+
</tr>
|
87
|
+
<tr>
|
88
|
+
<th>BLOB</th>
|
89
|
+
<td>String</td>
|
90
|
+
<td>The encoding is always ASCII-8BIT/BINARY</td>
|
91
|
+
</tr>
|
92
|
+
</tbody>
|
93
|
+
</table>
|
62
94
|
|
63
95
|
### Boolean types
|
64
96
|
|
65
97
|
Because defining fields as BOOLEAN and storing integer 0 or 1 in them is
|
66
|
-
common, rdo-sqlite will convert boolean bind parameters to 0 or 1.
|
67
|
-
actually want to store the String 'true' or 'false', you will need to
|
68
|
-
convert it to a String first.
|
98
|
+
common, rdo-sqlite will convert boolean bind parameters to 0 or 1.
|
69
99
|
|
70
100
|
### Character encoding
|
71
101
|
|
72
102
|
SQLite does not allow the encoding of an existing database to be changed. It
|
73
|
-
only supports two encodings: UTF-8 and UTF-16. rdo-sqlite
|
74
|
-
assumes UTF-8 encoding.
|
103
|
+
only supports two encodings for new databases: UTF-8 and UTF-16. rdo-sqlite
|
104
|
+
currently just assumes UTF-8 encoding. Support for UTF-16 is planned.
|
75
105
|
|
76
106
|
## Contributing
|
77
107
|
|
@@ -79,8 +109,7 @@ If you find any bugs, please send a pull request if you think you can
|
|
79
109
|
fix it, or file in an issue in the issue tracker.
|
80
110
|
|
81
111
|
When sending pull requests, please use topic branches—don't send a pull
|
82
|
-
request from the master branch of your fork
|
83
|
-
unintentionally.
|
112
|
+
request from the master branch of your fork.
|
84
113
|
|
85
114
|
Contributors will be credited in this README.
|
86
115
|
|
data/ext/rdo_sqlite/driver.c
CHANGED
@@ -26,6 +26,14 @@ static VALUE rdo_sqlite_driver_allocate(VALUE klass) {
|
|
26
26
|
return Data_Wrap_Struct(klass, 0, rdo_sqlite_driver_free, driver);
|
27
27
|
}
|
28
28
|
|
29
|
+
/** Set the correct mode flags, based on the driver options */
|
30
|
+
static int rdo_sqlite_driver_mode(VALUE self) {
|
31
|
+
if (rb_funcall(self, rb_intern("readonly?"), 0) == Qtrue)
|
32
|
+
return SQLITE_OPEN_READONLY;
|
33
|
+
else
|
34
|
+
return SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
35
|
+
}
|
36
|
+
|
29
37
|
/** Opens a database */
|
30
38
|
static VALUE rdo_sqlite_driver_open(VALUE self) {
|
31
39
|
RDOSQLiteDriver * driver;
|
@@ -38,7 +46,7 @@ static VALUE rdo_sqlite_driver_open(VALUE self) {
|
|
38
46
|
if (sqlite3_open_v2(
|
39
47
|
RSTRING_PTR(rb_funcall(self, rb_intern("filename"), 0)),
|
40
48
|
&(driver->db),
|
41
|
-
|
49
|
+
rdo_sqlite_driver_mode(self), NULL) != SQLITE_OK) {
|
42
50
|
RDO_ERROR("SQLite3 database open failed: %s", sqlite3_errmsg(driver->db));
|
43
51
|
} else {
|
44
52
|
driver->is_open = 1;
|
data/lib/rdo/sqlite/driver.rb
CHANGED
@@ -9,10 +9,30 @@ module RDO
|
|
9
9
|
module SQLite
|
10
10
|
# Main Driver class to hook into sqlite3 API
|
11
11
|
class Driver < RDO::Driver
|
12
|
+
# Execute a single statement.
|
13
|
+
#
|
14
|
+
# This method delegates to #prepare, then executes.
|
15
|
+
#
|
16
|
+
# @param [String] stmt
|
17
|
+
# the statement to execute, with optional bind markers
|
18
|
+
#
|
19
|
+
# @param [Object...] *args
|
20
|
+
# bind parameters
|
21
|
+
#
|
22
|
+
# @return [RDO::Result]
|
23
|
+
# the result of executing the statement
|
12
24
|
def execute(stmt, *args)
|
13
25
|
prepare(stmt).execute(*args)
|
14
26
|
end
|
15
27
|
|
28
|
+
# Predicte check to see if this is a read-only database.
|
29
|
+
#
|
30
|
+
# @return [Boolean]
|
31
|
+
# true if ?mode=ro
|
32
|
+
def readonly?
|
33
|
+
%w[ro readonly].include?(options[:mode])
|
34
|
+
end
|
35
|
+
|
16
36
|
private
|
17
37
|
|
18
38
|
def filename
|
data/lib/rdo/sqlite/version.rb
CHANGED
data/spec/sqlite/driver_spec.rb
CHANGED
@@ -73,6 +73,14 @@ describe RDO::SQLite::Driver do
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
76
|
+
|
77
|
+
context "in read-only mode" do
|
78
|
+
let(:options) { "sqlite::memory:?mode=readonly" }
|
79
|
+
|
80
|
+
it "opens the database" do
|
81
|
+
db.should be_open
|
82
|
+
end
|
83
|
+
end
|
76
84
|
end
|
77
85
|
|
78
86
|
describe "#close" do
|
@@ -237,6 +245,35 @@ describe RDO::SQLite::Driver do
|
|
237
245
|
end
|
238
246
|
end
|
239
247
|
|
248
|
+
context "in readonly mode" do
|
249
|
+
let(:options) { "sqlite:./tmp/test.db?mode=readonly" }
|
250
|
+
|
251
|
+
before(:each) do
|
252
|
+
RDO.open("sqlite:./tmp/test.db?mode=rw") do |db|
|
253
|
+
db.execute("CREATE TABLE bob (id integer primary key)")
|
254
|
+
db.close
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
after(:each) do
|
259
|
+
File.delete("./tmp/test.db") rescue nil
|
260
|
+
end
|
261
|
+
|
262
|
+
context "on write" do
|
263
|
+
it "raises an RDO::Exception on write" do
|
264
|
+
expect {
|
265
|
+
db.execute("DROP TABLE bob")
|
266
|
+
}.to raise_error(RDO::Exception)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
context "on read" do
|
271
|
+
it "returns an RDO::Result" do
|
272
|
+
db.execute("SELECT * FROM bob").should be_a_kind_of(RDO::Result)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
240
277
|
describe "#prepare" do
|
241
278
|
before(:each) do
|
242
279
|
db.execute("CREATE TABLE test (id integer primary key, name text, age integer)")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdo-sqlite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdo
|