sqlite3_extend_function 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +79 -0
- data/Rakefile +23 -0
- data/lib/sqlite3_extend_function.rb +27 -0
- data/lib/sqlite3_extend_function/function.rb +565 -0
- data/lib/sqlite3_extend_function/version.rb +5 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b370727ad3e7fe4e4dbce17de3372fb15fff6c2193f2400563c1a7a1e9e6fc88
|
4
|
+
data.tar.gz: d66b7180e476b6154ff201b82424df92f0e28572643e7de869ddd842a9707159
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b52dc19acdd7720c481acdc73ba8390aaad68f79181f0d9ce4f0fff87bd6c4b512e01bd2faa7141461b6efc4b9909a3b306ff0544c3b233c8b833c5183750940
|
7
|
+
data.tar.gz: d2f10a203913bbdcf6bf7d1c85e48b0be609cf158ae08209dbe6016561e28653350092f2d7b487a486c05680133b52c847b60073a5b3dd89550518ec26cf633c
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 shoma07
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# SQLite3ExtendFunction
|
2
|
+
|
3
|
+
Add some user-defined function to SQL when using SQLie3.
|
4
|
+
|
5
|
+
## Functions
|
6
|
+
|
7
|
+
The following PostgreSQL functions are supported.
|
8
|
+
|
9
|
+
- acos
|
10
|
+
- ascii
|
11
|
+
- asin
|
12
|
+
- atan
|
13
|
+
- atan2
|
14
|
+
- bit\_length
|
15
|
+
- btrim
|
16
|
+
- cbrt
|
17
|
+
- ceil
|
18
|
+
- char\_length
|
19
|
+
- chr
|
20
|
+
- concat
|
21
|
+
- concat\_ws
|
22
|
+
- cos
|
23
|
+
- cot
|
24
|
+
- decode
|
25
|
+
- degrees
|
26
|
+
- div
|
27
|
+
- encode
|
28
|
+
- exp
|
29
|
+
- floor
|
30
|
+
- format
|
31
|
+
- initcap
|
32
|
+
- ln
|
33
|
+
- log
|
34
|
+
- md5
|
35
|
+
- mod
|
36
|
+
- now
|
37
|
+
- octet\_length
|
38
|
+
- pi
|
39
|
+
- power
|
40
|
+
- radians
|
41
|
+
- sign
|
42
|
+
- sin
|
43
|
+
- sqrt
|
44
|
+
- tan
|
45
|
+
- trunc
|
46
|
+
- width\_bucket
|
47
|
+
|
48
|
+
See [PostgreSQL Document](https://www.postgresql.org/docs/12/functions.html)
|
49
|
+
|
50
|
+
```
|
51
|
+
# check
|
52
|
+
db = SQLite3::Database.new(':memory:')
|
53
|
+
|
54
|
+
db.execute("select ceil(0.1)")[0][0]
|
55
|
+
=> 1
|
56
|
+
|
57
|
+
db.execute("select floor(1.1)")[0][0]
|
58
|
+
=> 1
|
59
|
+
```
|
60
|
+
|
61
|
+
## Installation
|
62
|
+
Add this line to your application's Gemfile:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
gem 'sqlite3_extend_function', '~> 0.2'
|
66
|
+
|
67
|
+
# master branch
|
68
|
+
gem 'sqlite3_extend_function',
|
69
|
+
git: 'https://github.com/shoma07/sqlite3_extend_function.git',
|
70
|
+
branch: 'master'
|
71
|
+
```
|
72
|
+
|
73
|
+
And then execute:
|
74
|
+
```bash
|
75
|
+
$ bundle
|
76
|
+
```
|
77
|
+
|
78
|
+
## License
|
79
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'SQLite3ExtendFunction'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rspec/core/rake_task'
|
20
|
+
RSpec::Core::RakeTask.new(:spec)
|
21
|
+
task default: :spec
|
22
|
+
|
23
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sqlite3'
|
4
|
+
require 'sqlite3_extend_function/version'
|
5
|
+
require 'sqlite3_extend_function/function'
|
6
|
+
|
7
|
+
# SQLite3ExtendFunction
|
8
|
+
module SQLite3ExtendFunction
|
9
|
+
def self.included(base)
|
10
|
+
base.class_eval do
|
11
|
+
original_method = instance_method(:initialize)
|
12
|
+
define_method(:initialize) do |*args, &block|
|
13
|
+
result = original_method.bind(self).call(*args)
|
14
|
+
SQLite3ExtendFunction::Function.singleton_methods.each do |func|
|
15
|
+
lmd = Function.send(func)
|
16
|
+
create_function(func.to_s, (lmd.parameters.size - 1), &lmd)
|
17
|
+
end
|
18
|
+
# frozen_string_literal: true
|
19
|
+
# SQLite3ExtendFunction
|
20
|
+
block&.call(self)
|
21
|
+
result
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
SQLite3::Database.include(SQLite3ExtendFunction)
|
@@ -0,0 +1,565 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'base64'
|
5
|
+
require 'digest/md5'
|
6
|
+
module SQLite3ExtendFunction
|
7
|
+
# SQLite3ExtendFunction::Function
|
8
|
+
module Function
|
9
|
+
class << self
|
10
|
+
# @return [Proc]
|
11
|
+
def ceil
|
12
|
+
lambda do |func, dp|
|
13
|
+
return if dp.nil?
|
14
|
+
|
15
|
+
begin
|
16
|
+
func.result = Kernel.Float(dp).ceil.to_i
|
17
|
+
rescue ArgumentError
|
18
|
+
raise SQLite3::SQLException,
|
19
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Proc]
|
25
|
+
def floor
|
26
|
+
lambda do |func, dp|
|
27
|
+
return if dp.nil?
|
28
|
+
|
29
|
+
begin
|
30
|
+
func.result = Kernel.Float(dp).floor.to_i
|
31
|
+
rescue ArgumentError
|
32
|
+
raise SQLite3::SQLException,
|
33
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# cube root
|
39
|
+
# @return [Proc]
|
40
|
+
def cbrt
|
41
|
+
lambda do |func, dp|
|
42
|
+
return if dp.nil?
|
43
|
+
|
44
|
+
begin
|
45
|
+
result = Math.cbrt(Kernel.Float(dp))
|
46
|
+
func.result = result.to_i == result ? result.to_i : result
|
47
|
+
rescue ArgumentError
|
48
|
+
raise SQLite3::SQLException,
|
49
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# radians to degrees
|
55
|
+
# @return [Proc]
|
56
|
+
def degrees
|
57
|
+
lambda do |func, dp|
|
58
|
+
return if dp.nil?
|
59
|
+
|
60
|
+
begin
|
61
|
+
func.result = Kernel.Float(dp) * 180 / Math::PI
|
62
|
+
rescue ArgumentError
|
63
|
+
raise SQLite3::SQLException,
|
64
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# integer quotient of y/x
|
70
|
+
# @return [Proc]
|
71
|
+
def div
|
72
|
+
lambda do |func, y, x|
|
73
|
+
return if y.nil? || x.nil?
|
74
|
+
|
75
|
+
begin
|
76
|
+
func.result = Kernel.Float(y).div(Kernel.Float(x))
|
77
|
+
rescue ArgumentError
|
78
|
+
raise SQLite3::SQLException,
|
79
|
+
'invalid input syntax for type numeric'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# exponential
|
85
|
+
# @return [Proc]
|
86
|
+
def exp
|
87
|
+
lambda do |func, dp|
|
88
|
+
return if dp.nil?
|
89
|
+
|
90
|
+
begin
|
91
|
+
result = Math.exp(Kernel.Float(dp))
|
92
|
+
func.result = result.to_i == result ? result.to_i : result
|
93
|
+
rescue ArgumentError
|
94
|
+
raise SQLite3::SQLException,
|
95
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# natural logarithm
|
101
|
+
# @return [Proc]
|
102
|
+
def ln
|
103
|
+
lambda do |func, dp|
|
104
|
+
return if dp.nil?
|
105
|
+
|
106
|
+
begin
|
107
|
+
result = Math.log(Kernel.Float(dp))
|
108
|
+
func.result = result.to_i == result ? result.to_i : result
|
109
|
+
rescue ArgumentError
|
110
|
+
raise SQLite3::SQLException,
|
111
|
+
'cannot take logarithm of a negative number'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# base 10 logarithm
|
117
|
+
# @return [Proc]
|
118
|
+
def log
|
119
|
+
lambda do |func, y, x = nil|
|
120
|
+
if x.nil?
|
121
|
+
return if y.nil?
|
122
|
+
|
123
|
+
result = Math.log10(Kernel.Float(y))
|
124
|
+
func.result = result.to_i == result ? result.to_i : result
|
125
|
+
else
|
126
|
+
return if y.nil? || x.nil?
|
127
|
+
|
128
|
+
func.result = Math.log(x, y)
|
129
|
+
end
|
130
|
+
rescue ArgumentError
|
131
|
+
raise SQLite3::SQLException,
|
132
|
+
'invalid input syntax for type numeric'
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# remainder of y/x
|
137
|
+
# @return [Proc]
|
138
|
+
def mod
|
139
|
+
lambda do |func, y, x|
|
140
|
+
func.result = Kernel.Float(y).modulo(Kernel.Float(x)).to_i
|
141
|
+
rescue ArgumentError
|
142
|
+
raise SQLite3::SQLException,
|
143
|
+
'Could not choose a best candidate function. You might need to add explicit type casts.'
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# "π" constant
|
148
|
+
# @return [Proc]
|
149
|
+
def pi
|
150
|
+
lambda do |func|
|
151
|
+
func.result = Math::PI
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# a raised to the power of b
|
156
|
+
# @return [Proc]
|
157
|
+
def power
|
158
|
+
lambda do |func, a, b|
|
159
|
+
return if a.nil? || b.nil?
|
160
|
+
|
161
|
+
begin
|
162
|
+
result = Kernel.Float(a)**Kernel.Float(b)
|
163
|
+
func.result = result.to_i == result ? result.to_i : result
|
164
|
+
rescue ArgumentError
|
165
|
+
raise SQLite3::SQLException,
|
166
|
+
'invalid input syntax for type double precision'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# degrees to radians
|
172
|
+
# @return [Proc]
|
173
|
+
def radians
|
174
|
+
lambda do |func, dp|
|
175
|
+
return if dp.nil?
|
176
|
+
|
177
|
+
begin
|
178
|
+
result = Kernel.Float(dp) * Math::PI / 180
|
179
|
+
func.result = result.to_i == result ? result.to_i : result
|
180
|
+
rescue ArgumentError
|
181
|
+
raise SQLite3::SQLException,
|
182
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# sign of the argument (-1, 0, +1)
|
188
|
+
# @return [Proc]
|
189
|
+
def sign
|
190
|
+
lambda do |func, dp|
|
191
|
+
return if dp.nil?
|
192
|
+
|
193
|
+
begin
|
194
|
+
value = Kernel.Float(dp)
|
195
|
+
func.result = if value.zero?
|
196
|
+
0
|
197
|
+
elsif value.positive?
|
198
|
+
1
|
199
|
+
else
|
200
|
+
-1
|
201
|
+
end
|
202
|
+
rescue ArgumentError
|
203
|
+
raise SQLite3::SQLException,
|
204
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# square root
|
210
|
+
# @return [Proc]
|
211
|
+
def sqrt
|
212
|
+
lambda do |func, dp|
|
213
|
+
return if dp.nil?
|
214
|
+
|
215
|
+
begin
|
216
|
+
result = Math.sqrt(Kernel.Float(dp))
|
217
|
+
func.result = result.to_i == result ? result.to_i : result
|
218
|
+
rescue ArgumentError
|
219
|
+
raise SQLite3::SQLException,
|
220
|
+
"invalid input syntax for type double precision: \"#{dp}\""
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# truncate toward zero
|
226
|
+
# @return [Proc]
|
227
|
+
def trunc
|
228
|
+
lambda do |func, dp, s = 0|
|
229
|
+
return if dp.nil?
|
230
|
+
|
231
|
+
begin
|
232
|
+
ss = BigDecimal(Kernel.Float(s).to_s)
|
233
|
+
dec = if ss.zero?
|
234
|
+
BigDecimal('1.0')
|
235
|
+
else
|
236
|
+
BigDecimal((1.0 / (10**ss).to_f).to_s)
|
237
|
+
end
|
238
|
+
result = (BigDecimal(Kernel.Float(dp).to_s) / dec).to_i * dec
|
239
|
+
func.result = result.to_i == result ? result.to_i : result.to_f
|
240
|
+
rescue ArgumentError
|
241
|
+
raise SQLite3::SQLException,
|
242
|
+
'invalid input syntax for type double precision'
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# return the bucket number to which operand would be assigned input
|
248
|
+
# a histogram having count equal-width buckets spanning the range b1 to b2;
|
249
|
+
# returns 0 or count+1 for an input outside the range
|
250
|
+
# @return [Proc]
|
251
|
+
def width_bucket
|
252
|
+
lambda do |func, operand, b1, b2, count|
|
253
|
+
begin
|
254
|
+
operand = Kernel.Float(operand)
|
255
|
+
b1 = Kernel.Float(b1)
|
256
|
+
b2 = Kernel.Float(b2)
|
257
|
+
count = Kernel.Float(count)
|
258
|
+
|
259
|
+
raise ArgumentError unless count.positive?
|
260
|
+
result = (operand / ((b2 - b1) / count)).ceil
|
261
|
+
result = count + 1 if result > count
|
262
|
+
result = 0 if result.negative?
|
263
|
+
func.result = result.to_i
|
264
|
+
rescue ArgumentError
|
265
|
+
raise SQLite3::SQLException,
|
266
|
+
'ArgumentError'
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
# @return [Proc]
|
272
|
+
def acos
|
273
|
+
lambda do |func, x|
|
274
|
+
begin
|
275
|
+
func.result = Math.acos(Kernel.Float(x))
|
276
|
+
rescue ArgumentError
|
277
|
+
raise SQLite3::SQLException,
|
278
|
+
"invalid input syntax for type double precision: #{x}"
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
# @return [Proc]
|
284
|
+
def asin
|
285
|
+
lambda do |func, x|
|
286
|
+
begin
|
287
|
+
func.result = Math.asin(Kernel.Float(x))
|
288
|
+
rescue ArgumentError
|
289
|
+
raise SQLite3::SQLException,
|
290
|
+
"invalid input syntax for type double precision: #{x}"
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
# @return [Proc]
|
296
|
+
def atan
|
297
|
+
lambda do |func, x|
|
298
|
+
begin
|
299
|
+
func.result = Math.atan(Kernel.Float(x))
|
300
|
+
rescue ArgumentError
|
301
|
+
raise SQLite3::SQLException,
|
302
|
+
"invalid input syntax for type double precision: #{x}"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
# @return [Proc]
|
308
|
+
def atan2
|
309
|
+
lambda do |func, y, x|
|
310
|
+
begin
|
311
|
+
func.result = Math.atan2(Kernel.Float(y), Kernel.Float(x))
|
312
|
+
rescue ArgumentError
|
313
|
+
raise SQLite3::SQLException,
|
314
|
+
"invalid input syntax for type double precision"
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
# @return [Proc]
|
320
|
+
def cos
|
321
|
+
lambda do |func, x|
|
322
|
+
begin
|
323
|
+
func.result = Math.cos(Kernel.Float(x))
|
324
|
+
rescue ArgumentError
|
325
|
+
raise SQLite3::SQLException,
|
326
|
+
"invalid input syntax for type double precision: #{x}"
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
# @return [Proc]
|
332
|
+
def cot
|
333
|
+
lambda do |func, x|
|
334
|
+
begin
|
335
|
+
func.result = 1 / Math.tan(Kernel.Float(x))
|
336
|
+
rescue ArgumentError
|
337
|
+
raise SQLite3::SQLException,
|
338
|
+
"invalid input syntax for type double precision: #{x}"
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
# @return [Proc]
|
344
|
+
def sin
|
345
|
+
lambda do |func, x|
|
346
|
+
begin
|
347
|
+
func.result = Math.sin(Kernel.Float(x))
|
348
|
+
rescue ArgumentError
|
349
|
+
raise SQLite3::SQLException,
|
350
|
+
"invalid input syntax for type double precision: #{x}"
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
# @return [Proc]
|
356
|
+
def tan
|
357
|
+
lambda do |func, x|
|
358
|
+
begin
|
359
|
+
func.result = Math.tan(Kernel.Float(x))
|
360
|
+
rescue ArgumentError
|
361
|
+
raise SQLite3::SQLException,
|
362
|
+
"invalid input syntax for type double precision: #{x}"
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
# Number of bits in string
|
368
|
+
# @return [Proc]
|
369
|
+
def bit_length
|
370
|
+
lambda do |func, str|
|
371
|
+
begin
|
372
|
+
func.result = str.bytesize * 8
|
373
|
+
rescue ArgumentError
|
374
|
+
raise SQLite3::SQLException,
|
375
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
# Number of characters in string
|
381
|
+
# @return [Proc]
|
382
|
+
def char_length
|
383
|
+
lambda do |func, str|
|
384
|
+
begin
|
385
|
+
func.result = str.length
|
386
|
+
rescue ArgumentError
|
387
|
+
raise SQLite3::SQLException,
|
388
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
389
|
+
end
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
# octet_length
|
394
|
+
# @return [Proc]
|
395
|
+
def octet_length
|
396
|
+
lambda do |func, str|
|
397
|
+
begin
|
398
|
+
func.result = str.bytesize
|
399
|
+
rescue ArgumentError
|
400
|
+
raise SQLite3::SQLException,
|
401
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
# @return [Proc]
|
407
|
+
def ascii
|
408
|
+
lambda do |func, str|
|
409
|
+
begin
|
410
|
+
raise ArgumentError if str.is_a? String
|
411
|
+
|
412
|
+
func.result = str.ord
|
413
|
+
rescue ArgumentError
|
414
|
+
raise SQLite3::SQLException,
|
415
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
# @return [Proc]
|
421
|
+
def btrim
|
422
|
+
lambda do |func, text, chrs = ''|
|
423
|
+
begin
|
424
|
+
func.result = text.sub(/\A[#{chrs}]*/, '').sub(/[#{chrs}]*\z/, '')
|
425
|
+
rescue ArgumentError
|
426
|
+
raise SQLite3::SQLException,
|
427
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
428
|
+
end
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
# @return [Proc]
|
433
|
+
def chr
|
434
|
+
lambda do |func, int|
|
435
|
+
begin
|
436
|
+
func.result = int.chr
|
437
|
+
rescue ArgumentError
|
438
|
+
raise SQLite3::SQLException,
|
439
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
440
|
+
end
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
# @return [Proc]
|
445
|
+
def concat
|
446
|
+
lambda do |func, *args|
|
447
|
+
begin
|
448
|
+
func.result = args.compact.join
|
449
|
+
rescue ArgumentError
|
450
|
+
raise SQLite3::SQLException,
|
451
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
# @return [Proc]
|
457
|
+
def concat_ws
|
458
|
+
lambda do |func, sep, *args|
|
459
|
+
begin
|
460
|
+
func.result = args.compact.join(sep)
|
461
|
+
rescue ArgumentError
|
462
|
+
raise SQLite3::SQLException,
|
463
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
464
|
+
end
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
# @return [Proc]
|
469
|
+
def decode
|
470
|
+
lambda do |func, str, format|
|
471
|
+
return if data.nil?
|
472
|
+
|
473
|
+
begin
|
474
|
+
raise ArgumentError if data.is_a? String
|
475
|
+
|
476
|
+
func.result = case format
|
477
|
+
when 'base64'
|
478
|
+
Base64.decode64(str)
|
479
|
+
when 'hex'
|
480
|
+
str.scan(/.{2}/).map(&:hex).map(&:chr).join
|
481
|
+
when 'escape'
|
482
|
+
raise ArgumentError
|
483
|
+
else
|
484
|
+
raise ArgumentError
|
485
|
+
end
|
486
|
+
rescue ArgumentError
|
487
|
+
raise SQLite3::SQLException,
|
488
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
489
|
+
end
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
# @return [Proc]
|
494
|
+
def encode
|
495
|
+
lambda do |func, data, format|
|
496
|
+
return if data.nil?
|
497
|
+
|
498
|
+
begin
|
499
|
+
raise ArgumentError if data.is_a? String
|
500
|
+
|
501
|
+
func.result = case format
|
502
|
+
when 'base64'
|
503
|
+
Base64.encode64(data)
|
504
|
+
when 'hex'
|
505
|
+
data.unpack("C*").map { |c| c.to_s(16) }.join
|
506
|
+
when 'escape'
|
507
|
+
raise ArgumentError
|
508
|
+
else
|
509
|
+
raise ArgumentError
|
510
|
+
end
|
511
|
+
rescue ArgumentError
|
512
|
+
raise SQLite3::SQLException,
|
513
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
514
|
+
end
|
515
|
+
end
|
516
|
+
end
|
517
|
+
|
518
|
+
# @return [Proc]
|
519
|
+
def format
|
520
|
+
lambda do |func, formartstr, *args|
|
521
|
+
func.result = formartstr % args
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
# @return [Proc]
|
526
|
+
def initcap
|
527
|
+
lambda do |func, str|
|
528
|
+
return if str.nil?
|
529
|
+
|
530
|
+
begin
|
531
|
+
regexp = /([a-zA-Z0-9]*|[^a-zA-Z0-9]*)/
|
532
|
+
func.result = str.split(regexp).map.with_index { |s, i|
|
533
|
+
i.odd? ? s.downcase.capitalize : s
|
534
|
+
}.join
|
535
|
+
rescue
|
536
|
+
raise SQLite3::SQLException,
|
537
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
538
|
+
end
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
# @return [Proc]
|
543
|
+
def md5
|
544
|
+
lambda do |func, str|
|
545
|
+
return if str.nil?
|
546
|
+
|
547
|
+
begin
|
548
|
+
func.result = Digest::MD5.hexdigest(str)
|
549
|
+
rescue
|
550
|
+
raise SQLite3::SQLException,
|
551
|
+
"No function matches the given name and argument types. You might need to add explicit type casts."
|
552
|
+
end
|
553
|
+
end
|
554
|
+
end
|
555
|
+
|
556
|
+
# Current date and time (start of current transaction)
|
557
|
+
# @return [Proc]
|
558
|
+
def now
|
559
|
+
lambda do |func|
|
560
|
+
func.result = Time.now.strftime('%Y-%m-%d %H:%M:%S.%6N%:z')
|
561
|
+
end
|
562
|
+
end
|
563
|
+
end
|
564
|
+
end
|
565
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sqlite3_extend_function
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- shoma07
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sqlite3
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Add some user-defined functions to SQL when using SQLie3.
|
70
|
+
email:
|
71
|
+
- 23730734+shoma07@users.noreply.github.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- lib/sqlite3_extend_function.rb
|
80
|
+
- lib/sqlite3_extend_function/function.rb
|
81
|
+
- lib/sqlite3_extend_function/version.rb
|
82
|
+
homepage: https://github.com/shoma07/sqlite3_extend_function
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata:
|
86
|
+
homepage_uri: https://github.com/shoma07/sqlite3_extend_function
|
87
|
+
source_code_uri: https://github.com/shoma07/sqlite3_extend_function
|
88
|
+
changelog_uri: https://github.com/shoma07/sqlite3_extend_function/blob/v0.2.0/CHANGELOG.md
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '2'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.0.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Add some user-defined functions to SQL when using SQLie3.
|
108
|
+
test_files: []
|