sqlite_ext 0.1.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/README.md +2 -12
- data/lib/sqlite_ext/version.rb +1 -1
- data/lib/sqlite_ext.rb +35 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ef79118c67623c9f8ea24483b8546c8ce61a8d9
|
4
|
+
data.tar.gz: 82e046090cdf94052b499694feb657f13251d280
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 644c88efc1b5b4803ca26c0a355485e426c4fc6a9e1711599c886e04b5a8f5c3ac88a63ebeadbc566b35e74c4da543ac194bcd971bbe61deab2f9497dc488493
|
7
|
+
data.tar.gz: d20b161052dd7baa1773d7a451d5a4b8ab7f85f2a95d93772f685710ce7738d6d92d8d799bc030a997ee237a0b05b7d1f0a9bb503a12711bc9d04bfe6090d5af
|
data/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# SqliteExt
|
2
2
|
|
3
|
-
Note that the work on this gem is still in the early stages, so
|
4
|
-
the text below represents what it is intended to do. The gem does
|
5
|
-
not yet accomplish all of that as of yet.
|
6
|
-
|
7
3
|
Provides a convenient way of writing functions in Ruby that can
|
8
4
|
be called from with in SQLite queries through the SQLite3 gem.
|
9
5
|
|
@@ -41,14 +37,8 @@ Or install it yourself as:
|
|
41
37
|
|
42
38
|
## Usage
|
43
39
|
|
44
|
-
SqliteExt.
|
45
|
-
|
46
|
-
case x
|
47
|
-
when nil then nil
|
48
|
-
else Math.sqrt(x)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
40
|
+
SqliteExt.register_function('sqrt', 1){ |x| Math.sqrt(x) }
|
41
|
+
|
52
42
|
SQLite3::Database.new 'data.db' do |db|
|
53
43
|
puts db.execute("SELECT sqrt(25)")[0][0]
|
54
44
|
end
|
data/lib/sqlite_ext/version.rb
CHANGED
data/lib/sqlite_ext.rb
CHANGED
@@ -16,30 +16,54 @@ module SqliteExt
|
|
16
16
|
|
17
17
|
class << self
|
18
18
|
|
19
|
+
# Registers a block of ruby code to be used as a function in
|
20
|
+
# SQL executed through subsequent new instances of
|
21
|
+
# `SQLite3::Database`.
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
#
|
25
|
+
# SqliteExt.register_function('sqrt', 1){ |x| Math.sqrt(x) }
|
26
|
+
#
|
27
|
+
# SQLite3::Database.new 'data.db' do |db|
|
28
|
+
# puts db.execute("SELECT sqrt(25)")[0][0]
|
29
|
+
# end
|
30
|
+
# # Output: 5.0
|
31
|
+
#
|
32
|
+
def register_function(name, &block)
|
33
|
+
register_create_function name, block.arity do |fn,*args|
|
34
|
+
fn.result = block.call(*args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
19
38
|
# Registers a #create_function call to be invoked on every
|
20
39
|
# new instance of `SQLite3::Database` immidately after it is
|
21
40
|
# instantiated and before it is returned from the call to
|
22
41
|
# `.new` and before the invocation of a block that is passed
|
23
42
|
# to `.new`.
|
24
43
|
#
|
44
|
+
# The parameters passed to `#register_create_function` are
|
45
|
+
# exactly the same as those that would be passed to
|
46
|
+
# `SQLite3::Database#create_function`.
|
47
|
+
#
|
25
48
|
# Note that this only affects instances of
|
26
49
|
# `SQLite3::Database` that are subsequently created and has
|
27
50
|
# no effect on previously created instances.
|
28
51
|
#
|
29
52
|
# Example:
|
30
53
|
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
54
|
+
# SqliteExt.register_create_function 'sqrt', 1 do |fn,x|
|
55
|
+
# fn.result =
|
56
|
+
# case x
|
57
|
+
# when nil then nil
|
58
|
+
# else Math.sqrt(x)
|
59
|
+
# end
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# SQLite3::Database.new 'data.db' do |db|
|
63
|
+
# puts db.execute("SELECT sqrt(25)")[0][0]
|
64
|
+
# end
|
65
|
+
# # Output: 5.0
|
38
66
|
#
|
39
|
-
# SQLite3::Database.new 'data.db' do |db|
|
40
|
-
# puts db.execute("SELECT sqrt(25)")[0][0]
|
41
|
-
# end
|
42
|
-
# # Output: 5.0
|
43
67
|
def register_create_function(name, arity, *other_args, &block)
|
44
68
|
name = "#{name}"
|
45
69
|
registered_function_creations[name] = [
|