Skip to content

Commit

Permalink
JRuby fixes for test suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Aug 22, 2011
1 parent c29cd41 commit 232e861
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 75 deletions.
12 changes: 11 additions & 1 deletion Gemfile
Expand Up @@ -2,4 +2,14 @@ source :rubygems

gemspec

gem 'rcov', '0.9.8', :platform => :mri_18
gem 'rcov', '0.9.8', :platform => :mri_18

platforms :ruby do
gem 'mysql2', '~> 0.2.11'
gem 'pg', '0.9.0'
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter', '~> 1.1.3'
gem 'activerecord-jdbcpostgresql-adapter', '~> 1.1.3'
end
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -21,7 +21,7 @@ end
namespace :cucumber do
def add_task(name, description)
Cucumber::Rake::Task.new(name, description) do |t|
t.cucumber_opts = "--format pretty features/*.feature DATABASE=#{name}"
t.cucumber_opts = "--format pretty DATABASE=#{name}"
end
end

Expand Down
22 changes: 11 additions & 11 deletions lib/thinking_sphinx/adapters/abstract_adapter.rb
Expand Up @@ -3,12 +3,12 @@ class AbstractAdapter
def initialize(model)
@model = model
end

def setup
# Deliberately blank - subclasses should do something though. Well, if
# they need to.
end

def self.detect(model)
adapter = adapter_for_model model
case adapter
Expand All @@ -22,7 +22,7 @@ def self.detect(model)
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL, not #{adapter}"
end
end

def self.adapter_for_model(model)
case ThinkingSphinx.database_adapter
when String
Expand All @@ -35,7 +35,7 @@ def self.adapter_for_model(model)
ThinkingSphinx.database_adapter
end
end

def self.standard_adapter_for_model(model)
case model.connection.class.name
when "ActiveRecord::ConnectionAdapters::MysqlAdapter",
Expand All @@ -52,34 +52,34 @@ def self.standard_adapter_for_model(model)
when "jdbcpostgresql"
:postgresql
else
model.connection.config[:adapter]
model.connection.config[:adapter].to_sym
end
else
model.connection.class.name
end
end

def quote_with_table(column)
"#{@model.quoted_table_name}.#{@model.connection.quote_column_name(column)}"
end

def bigint_pattern
/bigint/i
end

def downcase(clause)
"LOWER(#{clause})"
end

def case(expression, pairs, default)
"CASE #{expression} " +
pairs.keys.inject('') { |string, key|
string + "WHEN '#{key}' THEN #{pairs[key]} "
} + "ELSE #{default} END"
end

protected

def connection
@connection ||= @model.connection
end
Expand Down
48 changes: 29 additions & 19 deletions lib/thinking_sphinx/adapters/postgresql_adapter.rb
Expand Up @@ -4,11 +4,11 @@ def setup
create_array_accum_function
create_crc32_function
end

def sphinx_identifier
"pgsql"
end

def concatenate(clause, separator = ' ')
if clause[/^COALESCE/]
clause.split('), ').join(") || '#{separator}' || ")
Expand All @@ -18,31 +18,31 @@ def concatenate(clause, separator = ' ')
}.join(" || '#{separator}' || ")
end
end

def group_concatenate(clause, separator = ' ')
"array_to_string(array_accum(COALESCE(#{clause}, '0')), '#{separator}')"
end

def cast_to_string(clause)
clause
end

def cast_to_datetime(clause)
if ThinkingSphinx::Configuration.instance.use_64_bit
"cast(extract(epoch from #{clause}) as bigint)"
else
"cast(extract(epoch from #{clause}) as int)"
end
end

def cast_to_unsigned(clause)
clause
end

def cast_to_int(clause)
"#{clause}::INT8"
end

def convert_nulls(clause, default = '')
default = case default
when String
Expand All @@ -54,34 +54,44 @@ def convert_nulls(clause, default = '')
else
default
end

"COALESCE(#{clause}, #{default})"
end

def boolean(value)
value ? 'TRUE' : 'FALSE'
end

def crc(clause, blank_to_null = false)
clause = "NULLIF(#{clause},'')" if blank_to_null
"crc32(#{clause})"
end

def utf8_query_pre
nil
end

def time_difference(diff)
"current_timestamp - interval '#{diff} seconds'"
end

def utc_query_pre
"SET TIME ZONE 'UTC'"
end

private

def execute(command, output_error = false)
if RUBY_PLATFORM == 'java'
connection.transaction do
execute_command command, output_error
end
else
execute_command command, output_error
end
end

def execute_command(command, output_error = false)
connection.execute "begin"
connection.execute "savepoint ts"
begin
Expand All @@ -93,7 +103,7 @@ def execute(command, output_error = false)
connection.execute "release savepoint ts"
connection.execute "commit"
end

def create_array_accum_function
if connection.raw_connection.respond_to?(:server_version) && connection.raw_connection.server_version > 80200
execute <<-SQL
Expand All @@ -116,7 +126,7 @@ def create_array_accum_function
SQL
end
end

def create_crc32_function
execute "CREATE LANGUAGE 'plpgsql';"
function = <<-SQL
Expand All @@ -131,7 +141,7 @@ def create_crc32_function
IF COALESCE(word, '') = '' THEN
return 0;
END IF;
i = 0;
tmp = 4294967295;
byte_length = bit_length(word) / 8;
Expand Down
2 changes: 1 addition & 1 deletion spec/thinking_sphinx/active_record_spec.rb
Expand Up @@ -54,7 +54,7 @@
lambda {
Alpha.define_indexes
}.should_not raise_error
end
end unless RUBY_PLATFORM == 'java'

it "should die noisily if there is a non-database error" do
ThinkingSphinx::Index::Builder.stub(:generate) { raise StandardError }
Expand Down

0 comments on commit 232e861

Please sign in to comment.