Permalink
Browse files

Merge pull request #11 from sionide21/master

Sqlite Support. There are some tests in this but I could not verify them.
  • Loading branch information...
2 parents aad5150 + 1df71db commit d8348d2da0e9b612757e1a1c58f76fada8609e9e @jlecour jlecour committed Jan 31, 2012
Showing with 64 additions and 5 deletions.
  1. +4 −1 .gitignore
  2. +0 −1 README.markdown
  3. +53 −0 lib/geokit-rails3/adapters/sqlite.rb
  4. +4 −0 test/database.yml
  5. +3 −3 test/tasks.rake
View
@@ -5,6 +5,9 @@ Gemfile.lock
# Test log files
test/*-debug.log
+# SQLite Test DB
+test/test.sqlite3
+
# SVN
.svn
@@ -31,4 +34,4 @@ doc
*.gem
# For MacOS:
-.DS_Store
+.DS_Store
View
@@ -604,7 +604,6 @@ Geokit plugin the migration would be as follows.
## Database Compatability
* Geokit works with MySQL (tested with version 5.0.41), PostgreSQL (tested with version 8.2.6) and Microsoft SQL Server (tested with 2000).
-* Geokit does *not* work with SQLite, as it lacks the necessary geometry functions.
* Geokit is known to *not* work with Postgres versions under 8.1 -- it uses the least() funciton.
@@ -0,0 +1,53 @@
+module Geokit
+ module Adapters
+ class SQLite < Abstract
+
+ def self.add_numeric(name)
+ @@connection.create_function name, 1, :numeric do |func, *args|
+ func.result = yield(*args)
+ end
+ end
+
+ def self.add_math(name)
+ add_numeric name do |*n|
+ Math.send name, *n
+ end
+ end
+
+ class << self
+ def load(klass)
+ @@connection = klass.connection.raw_connection
+ # Define the functions needed
+ add_math 'sqrt'
+ add_math 'cos'
+ add_math 'acos'
+ add_math 'sin'
+
+ add_numeric('pow') { |n, m| n**m }
+ add_numeric('radians') { |n| n * Math::PI / 180 }
+ add_numeric('least') { |*args| args.min }
+ end
+ end
+
+ def sphere_distance_sql(lat, lng, multiplier)
+ %|
+ (CASE WHEN #{qualified_lat_column_name} IS NULL OR #{qualified_lng_column_name} IS NULL THEN NULL ELSE
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
+ COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
+ SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
+ END)
+ |
+ end
+
+ def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
+ %|
+ (CASE WHEN #{qualified_lat_column_name} IS NULL OR #{qualified_lng_column_name} IS NULL THEN NULL ELSE
+ SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+
+ POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))
+ END)
+ |
+ end
+
+ end
+ end
+end
View
@@ -18,3 +18,7 @@ sqlserver:
mode: ODBC
dsn: geokit_rails_tests
username: tests
+
+sqlite:
+ adapter: sqlite3
+ database: test/test.sqlite3
View
@@ -18,7 +18,7 @@ Rake::TestTask.new(:test) do |test|
test.verbose = true
end
-%w(mysql postgresql sqlserver).each do |configuration|
+%w(mysql postgresql sqlserver sqlite).each do |configuration|
EnvTestTask.new("test_#{configuration}") do |t|
t.pattern = 'test/**/*_test.rb'
t.verbose = true
@@ -28,11 +28,11 @@ end
end
desc 'Test available databases.'
-task :test_databases => %w(test_mysql test_postgresql test_sqlserver)
+task :test_databases => %w(test_mysql test_postgresql test_sqlserver test_sqlite)
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
-end
+end

0 comments on commit d8348d2

Please sign in to comment.