sqlite3_extend_function 0.2.0 → 0.3.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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +15 -0
  3. data/.rubocop.yml +36 -0
  4. data/CHANGELOG.md +17 -0
  5. data/Gemfile +10 -0
  6. data/README.md +32 -3
  7. data/bin/console +15 -0
  8. data/bin/setup +8 -0
  9. data/lib/sqlite3_extend_function.rb +7 -9
  10. data/lib/sqlite3_extend_function/function.rb +20 -555
  11. data/lib/sqlite3_extend_function/functions.rb +17 -0
  12. data/lib/sqlite3_extend_function/functions/acos.rb +19 -0
  13. data/lib/sqlite3_extend_function/functions/ascii.rb +20 -0
  14. data/lib/sqlite3_extend_function/functions/asin.rb +19 -0
  15. data/lib/sqlite3_extend_function/functions/atan.rb +19 -0
  16. data/lib/sqlite3_extend_function/functions/atan2.rb +20 -0
  17. data/lib/sqlite3_extend_function/functions/bit_length.rb +21 -0
  18. data/lib/sqlite3_extend_function/functions/btrim.rb +21 -0
  19. data/lib/sqlite3_extend_function/functions/cbrt.rb +22 -0
  20. data/lib/sqlite3_extend_function/functions/ceil.rb +21 -0
  21. data/lib/sqlite3_extend_function/functions/char_length.rb +20 -0
  22. data/lib/sqlite3_extend_function/functions/chr.rb +20 -0
  23. data/lib/sqlite3_extend_function/functions/concat.rb +20 -0
  24. data/lib/sqlite3_extend_function/functions/concat_ws.rb +21 -0
  25. data/lib/sqlite3_extend_function/functions/cos.rb +19 -0
  26. data/lib/sqlite3_extend_function/functions/cot.rb +19 -0
  27. data/lib/sqlite3_extend_function/functions/decode.rb +29 -0
  28. data/lib/sqlite3_extend_function/functions/degrees.rb +19 -0
  29. data/lib/sqlite3_extend_function/functions/div.rb +20 -0
  30. data/lib/sqlite3_extend_function/functions/encode.rb +29 -0
  31. data/lib/sqlite3_extend_function/functions/exp.rb +22 -0
  32. data/lib/sqlite3_extend_function/functions/floor.rb +19 -0
  33. data/lib/sqlite3_extend_function/functions/format.rb +17 -0
  34. data/lib/sqlite3_extend_function/functions/initcap.rb +24 -0
  35. data/lib/sqlite3_extend_function/functions/ln.rb +22 -0
  36. data/lib/sqlite3_extend_function/functions/log.rb +24 -0
  37. data/lib/sqlite3_extend_function/functions/md5.rb +20 -0
  38. data/lib/sqlite3_extend_function/functions/mod.rb +21 -0
  39. data/lib/sqlite3_extend_function/functions/now.rb +15 -0
  40. data/lib/sqlite3_extend_function/functions/octet_length.rb +20 -0
  41. data/lib/sqlite3_extend_function/functions/pi.rb +15 -0
  42. data/lib/sqlite3_extend_function/functions/power.rb +23 -0
  43. data/lib/sqlite3_extend_function/functions/radians.rb +22 -0
  44. data/lib/sqlite3_extend_function/functions/sign.rb +22 -0
  45. data/lib/sqlite3_extend_function/functions/sin.rb +19 -0
  46. data/lib/sqlite3_extend_function/functions/sqrt.rb +22 -0
  47. data/lib/sqlite3_extend_function/functions/tan.rb +19 -0
  48. data/lib/sqlite3_extend_function/functions/trunc.rb +25 -0
  49. data/lib/sqlite3_extend_function/functions/width_bucket.rb +27 -0
  50. data/lib/sqlite3_extend_function/version.rb +1 -1
  51. data/sqlite3_extend_function.gemspec +25 -0
  52. metadata +52 -62
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Exp
6
+ module Exp
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.exp(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,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Floor
6
+ module Floor
7
+ class << self
8
+ # @param [Integer, Float] dp
9
+ # @return [Integer]
10
+ # @raise [SQLite3::SQLException]
11
+ def call(dp)
12
+ Float(dp).floor.to_i 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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Format
6
+ module Format
7
+ class << self
8
+ # @param [String] formartstr
9
+ # @param [Array<Object>] args
10
+ # @return [String]
11
+ def call(formartstr, *args)
12
+ formartstr % args
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Initcap
6
+ module Initcap
7
+ class << self
8
+ # @param [String] str
9
+ # @return [String]
10
+ # @raise [SQLite3::SQLException]
11
+ def call(str)
12
+ return if str.nil?
13
+
14
+ str.split(/([a-zA-Z0-9]*|[^a-zA-Z0-9]*)/).map.with_index do |s, i|
15
+ i.odd? ? s.downcase.capitalize : s
16
+ end.join
17
+ rescue StandardError
18
+ raise SQLite3::SQLException, 'No function matches the given name and argument types. ' \
19
+ 'You might need to add explicit type casts.'
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Ln
6
+ module Ln
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.log(Float(dp))
15
+ result.to_i == result ? result.to_i : result
16
+ rescue ArgumentError
17
+ raise SQLite3::SQLException, 'cannot take logarithm of a negative number'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Log
6
+ module Log
7
+ class << self
8
+ # @param [Integer, Float] y
9
+ # @param [Integer, Float, NilClass] x
10
+ # @return [Integer, Float]
11
+ # @raise [SQLite3::SQLException]
12
+ def call(y, x = nil)
13
+ return if y.nil?
14
+ return Math.log(x, y) unless x.nil?
15
+
16
+ result = Math.log10(Float(y))
17
+ result.to_i == result ? result.to_i : result
18
+ rescue ArgumentError
19
+ raise SQLite3::SQLException, 'invalid input syntax for type numeric'
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Md5
6
+ module Md5
7
+ class << self
8
+ # @param [String] str
9
+ # @return [String]
10
+ # @raise [SQLite3::SQLException]
11
+ def call(str)
12
+ Digest::MD5.hexdigest(str) unless str.nil?
13
+ rescue StandardError
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::Mod
6
+ module Mod
7
+ class << self
8
+ # @param [Integer, Float] y
9
+ # @param [Integer, Float] x
10
+ # @return [Integer]
11
+ # @raise [SQLite3::SQLException]
12
+ def call(y, x = nil)
13
+ Float(y).modulo(Float(x)).to_i
14
+ rescue ArgumentError
15
+ raise SQLite3::SQLException,
16
+ 'Could not choose a best candidate function. You might need to add explicit type casts.'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Now
6
+ module Now
7
+ class << self
8
+ # @return [String]
9
+ def call
10
+ Time.now.strftime('%Y-%m-%d %H:%M:%S.%6N%:z')
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::OctetLength
6
+ module OctetLength
7
+ class << self
8
+ # @param [String] str
9
+ # @return [Integer]
10
+ # @raise [SQLite3::SQLException]
11
+ def call(str)
12
+ str.bytesize
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,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Pi
6
+ module Pi
7
+ class << self
8
+ # @return [Float]
9
+ def call
10
+ Math::PI
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Power
6
+ module Power
7
+ class << self
8
+ # @param [Integer, Float] a
9
+ # @param [Integer, Float] b
10
+ # @return [Integer, Float]
11
+ # @raise [SQLite3::SQLException]
12
+ def call(a, b)
13
+ return if a.nil? || b.nil?
14
+
15
+ result = Float(a)**Float(b)
16
+ result.to_i == result ? result.to_i : result
17
+ rescue ArgumentError
18
+ raise SQLite3::SQLException, 'invalid input syntax for type double precision'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Radians
6
+ module Radians
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 = Float(dp) * Math::PI / 180
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,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Sign
6
+ module Sign
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
+ value = Float(dp)
15
+ (value.zero? && 0) || (value.positive? && 1) || -1
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,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Sin
6
+ module Sin
7
+ class << self
8
+ # @param [Integer, Float] x
9
+ # @return [Float]
10
+ # @raise [SQLite3::SQLException]
11
+ def call(x)
12
+ Math.sin(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,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Sqrt
6
+ module Sqrt
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.sqrt(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,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Tan
6
+ module Tan
7
+ class << self
8
+ # @param [Float, Integer] x
9
+ # @return [Float]
10
+ # @raise [SQLite3::SQLException]
11
+ def call(x)
12
+ 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,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::Trunc
6
+ module Trunc
7
+ class << self
8
+ # @param [Integer, Float] v
9
+ # @param [Integer] s
10
+ # @return [Integer, Float]
11
+ # @raise [SQLite3::SQLException]
12
+ def call(v, s = 0)
13
+ return if v.nil?
14
+ raise ArgumentError if s.is_a?(Integer)
15
+
16
+ dec = BigDecimal(s.zero? ? '1.0' : (1.0 / (10**s)).to_s)
17
+ result = (v / dec).to_i * dec
18
+ result.to_i == result ? result.to_i : result.to_f
19
+ rescue ArgumentError
20
+ raise SQLite3::SQLException, 'invalid input syntax for type double precision'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ module Functions
5
+ # SQLite3ExtendFunction::Functions::WidthBucket
6
+ module WidthBucket
7
+ class << self
8
+ # @param [Float, Integer] operand
9
+ # @param [Integer, Float] b1
10
+ # @param [Integer, Float] b2
11
+ # @param [Integer] count
12
+ # @return [Integer]
13
+ # @raise [SQLite3::SQLException]
14
+ def call(operand, b1, b2, count)
15
+ raise ArgumentError unless count.positive?
16
+
17
+ result = (Float(operand) / ((Float(b2) - Float(b1)) / Float(count))).ceil
18
+ result = count + 1 if result > count
19
+ result = 0 if result.negative?
20
+ result.to_i
21
+ rescue ArgumentError
22
+ raise SQLite3::SQLException, 'ArgumentError'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SQLite3ExtendFunction
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/sqlite3_extend_function/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sqlite3_extend_function'
7
+ spec.version = SQLite3ExtendFunction::VERSION
8
+ spec.authors = %w[shoma07]
9
+ spec.email = %w[23730734+shoma07@users.noreply.github.com]
10
+ spec.homepage = 'https://github.com/shoma07/sqlite3_extend_function'
11
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
12
+ spec.summary = 'Add some user-defined functions to SQL when using SQLie3.'
13
+ spec.description = 'Add some user-defined functions to SQL when using SQLie3.'
14
+ spec.license = 'MIT'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/v#{spec.version}/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ end