sqlite_ext 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53a1d3805872b3cf9967f73e10eceef144881de8
4
- data.tar.gz: 7b8f3047b28c6d4ef317a312c6599923a283cee4
3
+ metadata.gz: cc98b5174c0640ce74feba4c553d0061600bffef
4
+ data.tar.gz: 5d617dd109ed311c43c8b066f6d74d3505da4568
5
5
  SHA512:
6
- metadata.gz: b527f7514fe8d6bedf7280f1ff18ec153eead448d297b8d96c5d1fb44edc04a434f4d626e32bc0df4d8a7b46bda773e2f82990646107a5b270bf6d4c24f1755d
7
- data.tar.gz: 7de2cc1620f33e8193d3cae6340ba45551dbda54b01ca91562a9c69f20c8a997336b76e59c53aa3dfbe9ed70c1950038b1508fdbe0c459583365c0e08ff09109
6
+ metadata.gz: 9a918310e21666283174b41a6b87f85fe42052e052bf6774fb8ee7d3fb57f85314bf6f7d9b36811517e30003c7930994dfce55992fd4159cb8a74b15b07c076b
7
+ data.tar.gz: e57a95e894c8be15190ba915ee9810d534f0b64754c23f252225e01d649cf1d79235ff1c7c1162ced5298608c30bffd5c17b2aa2fb05bb935bb0137310550f7a
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ### 0.3.0 - 2016-06-23
2
+ * Removals
3
+ * Removed support for a block argument to `SqliteExt.register_function`.
4
+ It now accepts a `Proc` passed as a standard argument instead.
5
+ * Enhancements
6
+ * Functions registered using `SqliteExt.register_function` automatically
7
+ propagate `NULL` when `NULL` is passed for one or more of the required
8
+ parameters of the `Proc` that performs the function.
data/README.md CHANGED
@@ -37,12 +37,20 @@ Or install it yourself as:
37
37
 
38
38
  ## Usage
39
39
 
40
- SqliteExt.register_function('sqrt'){ |x| Math.sqrt(x) }
41
-
40
+ SqliteExt.register_function(
41
+ 'sqrt',
42
+ ->(x){ Math.sqrt(x) }
43
+ )
44
+
42
45
  SQLite3::Database.new 'data.db' do |db|
43
- puts db.execute("SELECT sqrt(25)")[0][0]
46
+ puts db.execute(
47
+ "SELECT sqrt(25), COALESCE(sqrt(NULL), -1)"
48
+ ).first
44
49
  end
45
- # Output: 5.0
50
+
51
+ # == Output ==
52
+ # 5.0
53
+ # -1
46
54
 
47
55
  ## Development
48
56
 
@@ -1,15 +1,24 @@
1
1
  module SqliteExt
2
+
3
+ # This module is prepended to `SQLite3::Database` to inject new
4
+ # initialization behavior.
2
5
  module InitInjection
6
+
7
+ # Adds functions registered with SqliteExt to each new
8
+ # instance before it is returned from `.new` or `.open`or is
9
+ # passed to the given block.
3
10
  def initialize(file, *other_init_args)
4
11
  if block_given?
5
- super file, *other_init_args do |db|
12
+ super file, *other_init_args do
6
13
  SqliteExt.enhance_db_session self
7
- yield db
14
+ yield self
8
15
  end
9
16
  else
10
17
  super
11
18
  SqliteExt.enhance_db_session self
12
19
  end
13
20
  end
21
+
14
22
  end
23
+
15
24
  end
@@ -1,3 +1,3 @@
1
1
  module SqliteExt
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/sqlite_ext.rb CHANGED
@@ -16,22 +16,56 @@ 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
19
+ # Registers a Ruby `Proc` to be used as a function in SQL
20
+ # code executed through subsequent new instances of
21
21
  # `SQLite3::Database`.
22
22
  #
23
+ # When `NULL` value is passed 1 or more of the `Proc`'s
24
+ # required parameters, it will be "propagated", meaning that
25
+ # the specified `Proc` will not be invoked and `NULL` will be
26
+ # returned from the call in SQL.
27
+ #
28
+ # When non-`NULL` values are passed to all of the `Proc`'s
29
+ # required parameters and `NULL` is passed to any or all of
30
+ # the `Proc`'s optional parameters, they will NOT be
31
+ # propagated, and the `Proc` WILL be invoked. Values passed
32
+ # as `NULL` in SQL will be forwarded to the `Proc` as Ruby
33
+ # `nil`s.
34
+ #
35
+ # Whenever the `Proc` is called and returns `nil`, that will
36
+ # result in `NULL` being returned from the function call in
37
+ # SQL.
38
+ #
23
39
  # Example:
24
40
  #
25
- # SqliteExt.register_function('sqrt'){ |x| Math.sqrt(x) }
41
+ # SqliteExt.register_function(
42
+ # 'sqrt',
43
+ # ->(x){ Math.sqrt(x) }
44
+ # )
26
45
  #
27
46
  # SQLite3::Database.new 'data.db' do |db|
28
- # puts db.execute("SELECT sqrt(25)")[0][0]
47
+ # puts db.execute(
48
+ # "SELECT sqrt(25), COALESCE(sqrt(NULL), -1)"
49
+ # ).first
29
50
  # end
30
- # # Output: 5.0
31
51
  #
32
- def register_function(name, &block)
33
- register_create_function name, block.arity do |fn,*args|
34
- fn.result = block.call(*args)
52
+ # # == Output ==
53
+ # # 5.0
54
+ # # -1
55
+ #
56
+ def register_function(name, prok)
57
+ minimum_arity =
58
+ prok.
59
+ parameters.select{ |(kind,_)| kind == :req }.
60
+ count
61
+
62
+ register_create_function name, minimum_arity do |fn,*args|
63
+ fn.result =
64
+ if args[0...minimum_arity].any?{ |a| a.nil? }
65
+ nil
66
+ else
67
+ prok.call(*args)
68
+ end
35
69
  end
36
70
  end
37
71
 
@@ -88,7 +122,7 @@ module SqliteExt
88
122
  # `SQLite3::Database`.
89
123
  #
90
124
  # This is normally called automatically for each new
91
- # instance, but can also be used to add the functions to
125
+ # instance, but it can also be used to add the functions to
92
126
  # an instance that was created before the functions were
93
127
  # registered.
94
128
  def enhance_db_session(db)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlite_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Jorgensen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-23 00:00:00.000000000 Z
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -76,6 +76,7 @@ files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
78
  - ".travis.yml"
79
+ - CHANGELOG.md
79
80
  - CODE_OF_CONDUCT.md
80
81
  - Gemfile
81
82
  - LICENSE.txt
@@ -83,8 +84,6 @@ files:
83
84
  - Rakefile
84
85
  - bin/console
85
86
  - bin/setup
86
- - data.db
87
- - foo.db
88
87
  - lib/sqlite_ext.rb
89
88
  - lib/sqlite_ext/init_injection.rb
90
89
  - lib/sqlite_ext/version.rb
data/data.db DELETED
File without changes
data/foo.db DELETED
File without changes