Commit d2e34303d929866de2a06c7a10d68ca54806c11f
1 parent
78ebbb22
Exists in
master
and in
28 other branches
ActionItem261: extended the sqlite database with functions sqrt, acos, asin and radians
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1643 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
43 additions
and
0 deletions
Show diff stats
lib/sqlite_extension.rb
... | ... | @@ -13,5 +13,28 @@ if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i |
13 | 13 | database.create_function('pow', 2, :numeric) do |func, base, exponent| |
14 | 14 | func.set_result(base.to_f ** exponent.to_f) |
15 | 15 | end |
16 | + | |
17 | + database.create_function('asin', 1, :numeric) do |func, value| | |
18 | + func.set_result(Math.asin(value)) | |
19 | + end | |
20 | + | |
21 | + database.create_function('acos', 1, :numeric) do |func, value| | |
22 | + func.set_result(Math.acos(value)) | |
23 | + end | |
24 | + | |
25 | + database.create_function('radians', 1, :numeric) do |func, value| | |
26 | + func.set_result(value.to_f * Math::PI / 180.0) | |
27 | + end | |
28 | + | |
29 | + database.create_function('sqrt', 1, :numeric) do |func, value| | |
30 | + func.set_result(Math.sqrt(value)) | |
31 | + end | |
16 | 32 | |
33 | +# database.create_function('dist', 5, :numeric) do |func, lat1, long1, lat2, long2, radius| | |
34 | +# lat2, long2 = [lat2, long2].map{|l|l * Math::PI/180.0} | |
35 | +# func.set_result = radius * Math.acos([1, | |
36 | +# Math.cos(lat1.to_f) * Math.cos(long1.to_f) * Math.cos(lat2.to_f) * Math.cos(long2.to_f) + | |
37 | +# Math.cos(lat1.to_f) * Math.sin(long1.to_f) * Math.cos(lat2.to_f) * Math.sin(long2.to_f) + | |
38 | +# Math.sin(lat1.to_f) * Math.sin(lat2.to_f)].min) | |
39 | +# end | |
17 | 40 | end | ... | ... |
test/unit/sqlite_extension_test.rb
... | ... | @@ -16,4 +16,24 @@ class SQliteExtensionTest < Test::Unit::TestCase |
16 | 16 | assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001 |
17 | 17 | end |
18 | 18 | |
19 | + should 'have arcsine function' do | |
20 | + assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select asin(1) as asin').first['asin'], 0.0001 | |
21 | + end | |
22 | + | |
23 | + should 'have arccosine function' do | |
24 | + assert_in_delta Math::PI, ActiveRecord::Base.connection.execute('select acos(-1.0) as acos').first['acos'], 0.0001 | |
25 | + end | |
26 | + | |
27 | + should 'have radians function' do | |
28 | + assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select radians(90) as rad').first['rad'], 0.0001 | |
29 | + end | |
30 | + | |
31 | + should 'have square root function' do | |
32 | + assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001 | |
33 | + end | |
34 | + | |
35 | +# should 'have a distance function' do | |
36 | +# 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 | |
37 | +# end | |
38 | + | |
19 | 39 | end | ... | ... |