diff --git a/lib/sqlite_extension.rb b/lib/sqlite_extension.rb index 5e746c0..743475a 100644 --- a/lib/sqlite_extension.rb +++ b/lib/sqlite_extension.rb @@ -13,5 +13,28 @@ if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i database.create_function('pow', 2, :numeric) do |func, base, exponent| func.set_result(base.to_f ** exponent.to_f) end + + database.create_function('asin', 1, :numeric) do |func, value| + func.set_result(Math.asin(value)) + end + + database.create_function('acos', 1, :numeric) do |func, value| + func.set_result(Math.acos(value)) + end + + database.create_function('radians', 1, :numeric) do |func, value| + func.set_result(value.to_f * Math::PI / 180.0) + end + + database.create_function('sqrt', 1, :numeric) do |func, value| + func.set_result(Math.sqrt(value)) + end +# database.create_function('dist', 5, :numeric) do |func, lat1, long1, lat2, long2, radius| +# lat2, long2 = [lat2, long2].map{|l|l * Math::PI/180.0} +# func.set_result = radius * Math.acos([1, +# Math.cos(lat1.to_f) * Math.cos(long1.to_f) * Math.cos(lat2.to_f) * Math.cos(long2.to_f) + +# Math.cos(lat1.to_f) * Math.sin(long1.to_f) * Math.cos(lat2.to_f) * Math.sin(long2.to_f) + +# Math.sin(lat1.to_f) * Math.sin(lat2.to_f)].min) +# end end diff --git a/test/unit/sqlite_extension_test.rb b/test/unit/sqlite_extension_test.rb index d481331..958067a 100644 --- a/test/unit/sqlite_extension_test.rb +++ b/test/unit/sqlite_extension_test.rb @@ -16,4 +16,24 @@ class SQliteExtensionTest < Test::Unit::TestCase assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001 end + should 'have arcsine function' do + assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select asin(1) as asin').first['asin'], 0.0001 + end + + should 'have arccosine function' do + assert_in_delta Math::PI, ActiveRecord::Base.connection.execute('select acos(-1.0) as acos').first['acos'], 0.0001 + end + + should 'have radians function' do + assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select radians(90) as rad').first['rad'], 0.0001 + end + + should 'have square root function' do + assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001 + end + +# should 'have a distance function' do +# assert_in_delta 2.28402, ActiveRecord::Base.connection.execute('select dist(32.918593, -96.958444, 32.895155, -96.958444, 3963.19) as dist').first['dist'], 0.0001 +# end + end -- libgit2 0.21.2