sqlite3_extend_function 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +15 -0
- data/.rubocop.yml +36 -0
- data/CHANGELOG.md +17 -0
- data/Gemfile +10 -0
- data/README.md +32 -3
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/sqlite3_extend_function.rb +7 -9
- data/lib/sqlite3_extend_function/function.rb +20 -555
- data/lib/sqlite3_extend_function/functions.rb +17 -0
- data/lib/sqlite3_extend_function/functions/acos.rb +19 -0
- data/lib/sqlite3_extend_function/functions/ascii.rb +20 -0
- data/lib/sqlite3_extend_function/functions/asin.rb +19 -0
- data/lib/sqlite3_extend_function/functions/atan.rb +19 -0
- data/lib/sqlite3_extend_function/functions/atan2.rb +20 -0
- data/lib/sqlite3_extend_function/functions/bit_length.rb +21 -0
- data/lib/sqlite3_extend_function/functions/btrim.rb +21 -0
- data/lib/sqlite3_extend_function/functions/cbrt.rb +22 -0
- data/lib/sqlite3_extend_function/functions/ceil.rb +21 -0
- data/lib/sqlite3_extend_function/functions/char_length.rb +20 -0
- data/lib/sqlite3_extend_function/functions/chr.rb +20 -0
- data/lib/sqlite3_extend_function/functions/concat.rb +20 -0
- data/lib/sqlite3_extend_function/functions/concat_ws.rb +21 -0
- data/lib/sqlite3_extend_function/functions/cos.rb +19 -0
- data/lib/sqlite3_extend_function/functions/cot.rb +19 -0
- data/lib/sqlite3_extend_function/functions/decode.rb +29 -0
- data/lib/sqlite3_extend_function/functions/degrees.rb +19 -0
- data/lib/sqlite3_extend_function/functions/div.rb +20 -0
- data/lib/sqlite3_extend_function/functions/encode.rb +29 -0
- data/lib/sqlite3_extend_function/functions/exp.rb +22 -0
- data/lib/sqlite3_extend_function/functions/floor.rb +19 -0
- data/lib/sqlite3_extend_function/functions/format.rb +17 -0
- data/lib/sqlite3_extend_function/functions/initcap.rb +24 -0
- data/lib/sqlite3_extend_function/functions/ln.rb +22 -0
- data/lib/sqlite3_extend_function/functions/log.rb +24 -0
- data/lib/sqlite3_extend_function/functions/md5.rb +20 -0
- data/lib/sqlite3_extend_function/functions/mod.rb +21 -0
- data/lib/sqlite3_extend_function/functions/now.rb +15 -0
- data/lib/sqlite3_extend_function/functions/octet_length.rb +20 -0
- data/lib/sqlite3_extend_function/functions/pi.rb +15 -0
- data/lib/sqlite3_extend_function/functions/power.rb +23 -0
- data/lib/sqlite3_extend_function/functions/radians.rb +22 -0
- data/lib/sqlite3_extend_function/functions/sign.rb +22 -0
- data/lib/sqlite3_extend_function/functions/sin.rb +19 -0
- data/lib/sqlite3_extend_function/functions/sqrt.rb +22 -0
- data/lib/sqlite3_extend_function/functions/tan.rb +19 -0
- data/lib/sqlite3_extend_function/functions/trunc.rb +25 -0
- data/lib/sqlite3_extend_function/functions/width_bucket.rb +27 -0
- data/lib/sqlite3_extend_function/version.rb +1 -1
- data/sqlite3_extend_function.gemspec +25 -0
- metadata +52 -62
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Dir["#{__dir__}/functions/**/*.rb"].sort.each { |f| require f }
|
4
|
+
|
5
|
+
module SQLite3ExtendFunction
|
6
|
+
# SQLite3ExtendFunction::Functions
|
7
|
+
module Functions
|
8
|
+
class << self
|
9
|
+
# @return [void]
|
10
|
+
def each
|
11
|
+
constants.each do |const|
|
12
|
+
yield SQLite3ExtendFunction::Function.new(const_get(const))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Acos
|
6
|
+
module Acos
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] x
|
9
|
+
# @return [Float]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(x)
|
12
|
+
Math.acos(Float(x))
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: #{x}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Ascii
|
6
|
+
module Ascii
|
7
|
+
class << self
|
8
|
+
# @param [String] str
|
9
|
+
# @return [Integer]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(str)
|
12
|
+
str.is_a?(String) ? str.ord : (raise ArgumentError)
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
15
|
+
'You might need to add explicit type casts.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Asin
|
6
|
+
module Asin
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] x
|
9
|
+
# @return [Float]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(x)
|
12
|
+
Math.asin(Float(x))
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: #{x}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Atan
|
6
|
+
module Atan
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] x
|
9
|
+
# @return [Float]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(x)
|
12
|
+
Math.atan(Float(x))
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: #{x}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Atan2
|
6
|
+
module Atan2
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] y
|
9
|
+
# @param [Integer, Float] x
|
10
|
+
# @return [Float]
|
11
|
+
# @raise [SQLite3::SQLException]
|
12
|
+
def call(y, x)
|
13
|
+
Math.atan2(Float(y), Float(x))
|
14
|
+
rescue ArgumentError
|
15
|
+
raise SQLite3::SQLException, 'invalid input syntax for type double precision'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::BitLength
|
6
|
+
module BitLength
|
7
|
+
class << self
|
8
|
+
# @param [String] str
|
9
|
+
# @return [Integer]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(str)
|
12
|
+
str.bytesize * 8
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException,
|
15
|
+
'No function matches the given name and argument types. ' \
|
16
|
+
'You might need to add explicit type casts.'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Btrim
|
6
|
+
module Btrim
|
7
|
+
class << self
|
8
|
+
# @param [String] text
|
9
|
+
# @param [String] chars
|
10
|
+
# @return [String]
|
11
|
+
# @raise [SQLite3::SQLException]
|
12
|
+
def call(text, chrs = '')
|
13
|
+
text.sub(/\A[#{chrs}]*/, '').sub(/[#{chrs}]*\z/, '')
|
14
|
+
rescue ArgumentError
|
15
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
16
|
+
'You might need to add explicit type casts.'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Cbrt
|
6
|
+
module Cbrt
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] dp
|
9
|
+
# @return [Integer, Float]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(dp)
|
12
|
+
return if dp.nil?
|
13
|
+
|
14
|
+
result = Math.cbrt(Float(dp))
|
15
|
+
result.to_i == result ? result.to_i : result
|
16
|
+
rescue ArgumentError
|
17
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: \"#{dp}\""
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Ceil
|
6
|
+
module Ceil
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] dp
|
9
|
+
# @return [Integer]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(dp)
|
12
|
+
return if dp.nil?
|
13
|
+
|
14
|
+
Float(dp).ceil.to_i
|
15
|
+
rescue ArgumentError
|
16
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: \"#{dp}\""
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::CharLength
|
6
|
+
module CharLength
|
7
|
+
class << self
|
8
|
+
# @param [String] str
|
9
|
+
# @return [Integer]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(str)
|
12
|
+
str.length
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
15
|
+
'You might need to add explicit type casts.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Chr
|
6
|
+
module Chr
|
7
|
+
class << self
|
8
|
+
# @param [Integer] int
|
9
|
+
# @return [String]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(int)
|
12
|
+
int.chr
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
15
|
+
'You might need to add explicit type casts.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Concat
|
6
|
+
module Concat
|
7
|
+
class << self
|
8
|
+
# @param [Array<String>] args
|
9
|
+
# @return [String]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(*args)
|
12
|
+
args.compact.join
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
15
|
+
'You might need to add explicit type casts.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::ConcatWs
|
6
|
+
module ConcatWs
|
7
|
+
class << self
|
8
|
+
# @param [String] sep
|
9
|
+
# @param [Array<String>] args
|
10
|
+
# @return [String]
|
11
|
+
# @raise [SQLite3::SQLException]
|
12
|
+
def call(sep, *args)
|
13
|
+
args.compact.join(sep)
|
14
|
+
rescue ArgumentError
|
15
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
16
|
+
'You might need to add explicit type casts.'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Cos
|
6
|
+
module Cos
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] x
|
9
|
+
# @return [Float]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(x)
|
12
|
+
Math.cos(Float(x))
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: #{x}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Cot
|
6
|
+
module Cot
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] x
|
9
|
+
# @return [Integer]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(x)
|
12
|
+
1 / Math.tan(Float(x))
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: #{x}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Decode
|
6
|
+
module Decode
|
7
|
+
class << self
|
8
|
+
# @todo implement escape
|
9
|
+
#
|
10
|
+
# @param [String] str
|
11
|
+
# @param [String] format
|
12
|
+
# @return [String]
|
13
|
+
# @raise [SQLite3::SQLException]
|
14
|
+
def call(str, format)
|
15
|
+
return if str.nil?
|
16
|
+
raise ArgumentError unless str.is_a?(String)
|
17
|
+
|
18
|
+
return Base64.decode64(str) if format == 'base64'
|
19
|
+
return str.scan(/.{2}/).map { |s| s.hex.chr }.join if format == 'hex'
|
20
|
+
|
21
|
+
raise ArgumentError
|
22
|
+
rescue ArgumentError
|
23
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
24
|
+
'You might need to add explicit type casts.'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Degrees
|
6
|
+
module Degrees
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] dp
|
9
|
+
# @return [Float]
|
10
|
+
# @raise [SQLite3::SQLException]
|
11
|
+
def call(dp)
|
12
|
+
Float(dp) * 180 / Math::PI unless dp.nil?
|
13
|
+
rescue ArgumentError
|
14
|
+
raise SQLite3::SQLException, "invalid input syntax for type double precision: \"#{dp}\""
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Div
|
6
|
+
module Div
|
7
|
+
class << self
|
8
|
+
# @param [Integer, Float] y
|
9
|
+
# @param [Integer, Float] x
|
10
|
+
# @return [Integer, Float]
|
11
|
+
# @raise [SQLite3::SQLException]
|
12
|
+
def call(y, x)
|
13
|
+
Float(y).div(Float(x)) unless y.nil? || x.nil?
|
14
|
+
rescue ArgumentError
|
15
|
+
raise SQLite3::SQLException, 'invalid input syntax for type numeric'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQLite3ExtendFunction
|
4
|
+
module Functions
|
5
|
+
# SQLite3ExtendFunction::Functions::Encode
|
6
|
+
module Encode
|
7
|
+
class << self
|
8
|
+
# @todo implement escape
|
9
|
+
#
|
10
|
+
# @param [String] data
|
11
|
+
# @param [String] format
|
12
|
+
# @return [String, NilClass]
|
13
|
+
# @raise [SQLite3::SQLException]
|
14
|
+
def call(data, format)
|
15
|
+
return if data.nil?
|
16
|
+
raise ArgumentError unless data.is_a?(String)
|
17
|
+
|
18
|
+
return Base64.encode64(data) if format == 'base64'
|
19
|
+
return data.unpack('C*').map { |c| c.to_s(16) }.join if format == 'hex'
|
20
|
+
|
21
|
+
raise ArgumentError
|
22
|
+
rescue ArgumentError
|
23
|
+
raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
|
24
|
+
'You might need to add explicit type casts.'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|