Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ source 'https://rubygems.org'
gemspec

gem 'logger'
gem 'mysql2', '~> 0.5.0', :platform => :ruby
gem 'pg', '~> 0.18.4', :platform => :ruby
gem 'mysql2', '~> 0.5.0', :platform => :ruby
gem 'pg', '~> 1.6.3', :platform => :ruby
gem 'trilogy', '~> 2.12.0', :platform => :ruby

gem 'activerecord', '< 7' if RUBY_VERSION.to_f <= 2.4

Expand Down
2 changes: 1 addition & 1 deletion bin/testmatrix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "json"

RUBIES = %w[ 3.0 3.1 3.2 3.3 3.4 4.0 ]
DATABASES = %w[ mysql2 postgresql ]
DATABASES = %w[ mysql2 postgresql trilogy ]
SPHINX_ENGINES = {
"sphinx" => %w[ 2.2.11 3.4.1 ],
"manticore" => %w[ 4.0.2 6.0.0 ]
Expand Down
6 changes: 5 additions & 1 deletion lib/thinking_sphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
require 'jdbc/mysql'
Jdbc::MySQL.load_driver
else
require 'mysql2'
begin
require 'trilogy'
rescue LoadError => e
require 'mysql2'
end
end

require 'riddle'
Expand Down
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/active_record/database_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def adapter_for(model)
def adapter_type_for(model)
class_name = model.connection.class.name
case class_name.split('::').last
when 'MysqlAdapter', 'Mysql2Adapter'
when 'MysqlAdapter', 'Mysql2Adapter', 'TrilogyAdapter'
:mysql
when 'PostgreSQLAdapter'
:postgresql
Expand Down
17 changes: 14 additions & 3 deletions lib/thinking_sphinx/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ def self.clear
end

def self.connection_class
return ThinkingSphinx::Connection::JRuby if RUBY_PLATFORM == 'java'
return @connection_class if @connection_class

ThinkingSphinx::Connection::MRI
if RUBY_PLATFORM == 'java'
ThinkingSphinx::Connection::JRuby
elsif defined?(::Trilogy)
ThinkingSphinx::Connection::Trilogy
else
ThinkingSphinx::Connection::Mysql2
end
end

def self.connection_class=(klass)
@connection_class = klass
end

def self.pool
Expand Down Expand Up @@ -70,4 +80,5 @@ def self.persistent=(persist)

require 'thinking_sphinx/connection/client'
require 'thinking_sphinx/connection/jruby'
require 'thinking_sphinx/connection/mri'
require 'thinking_sphinx/connection/mysql2'
require 'thinking_sphinx/connection/trilogy'
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

class ThinkingSphinx::Connection::MRI < ThinkingSphinx::Connection::Client
class ThinkingSphinx::Connection::Mysql2 < ThinkingSphinx::Connection::Client
def base_error
Mysql2::Error
::Mysql2::Error
end

private

attr_reader :options

def client
@client ||= Mysql2::Client.new({
:flags => Mysql2::Client::MULTI_STATEMENTS,
@client ||= ::Mysql2::Client.new({
:flags => ::Mysql2::Client::MULTI_STATEMENTS,
:connect_timeout => 5
}.merge(options))
rescue base_error => error
Expand Down
26 changes: 26 additions & 0 deletions lib/thinking_sphinx/connection/trilogy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# lib/trilogy.rb

class ThinkingSphinx::Connection::Trilogy < ThinkingSphinx::Connection::Client
def base_error
::Trilogy::Error
end

private

attr_reader :options

def client
@client ||= Trilogy.new({
:multi_statement => true,
:connect_timeout => 5
}.merge(options))
rescue base_error => error
raise ThinkingSphinx::SphinxError.new_from_mysql error
end

def results_for(statements)
results = [client.query(statements)]
results << client.next_result while client.more_results_exist?
results.map(&:each_hash)
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Bundler.require :default, :development

root = File.expand_path File.dirname(__FILE__)
require "#{root}/support/connection"
require "#{root}/support/multi_schema"
require "#{root}/support/json_column"
require "#{root}/support/mysql"
Expand Down
15 changes: 15 additions & 0 deletions spec/support/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

case ENV["DATABASE"]
when "trilogy"
ThinkingSphinx::Connection.connection_class = ThinkingSphinx::Connection::Trilogy
when "jruby"
ThinkingSphinx::Connection.connection_class = ThinkingSphinx::Connection::JRuby
when "mysql2"
ThinkingSphinx::Connection.connection_class = ThinkingSphinx::Connection::Mysql2
else
# rely on the default connection_class lookup
end



7 changes: 7 additions & 0 deletions spec/thinking_sphinx/active_record/database_adapters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
adapter_type_for(model)).to eq(:mysql)
end

it "translates a Trilogy adapter" do
allow(klass).to receive_messages(:name => 'ActiveRecord::ConnectionAdapters::TrilogyAdapter')

expect(ThinkingSphinx::ActiveRecord::DatabaseAdapters.
adapter_type_for(model)).to eq(:mysql)
end

it "translates a normal PostgreSQL adapter" do
allow(klass).to receive_messages(:name => 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe ThinkingSphinx::Connection::MRI do
require 'spec_helper'

RSpec.describe ThinkingSphinx::Connection::Mysql2 do
subject { described_class.new :host => "127.0.0.1", :port => 9306 }

let(:client) { double :client, :query => "result", :next_result => false }
Expand Down
52 changes: 52 additions & 0 deletions spec/thinking_sphinx/connection/trilogy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ThinkingSphinx::Connection::Trilogy do
subject { described_class.new :host => "127.0.0.1", :port => 9306 }

let(:result) { double :result, :each_hash => [{ "foo" => "bar" }] }
let(:client) { double :client, :query => result, :more_results_exist? => false }

before :each do
allow(Trilogy).to receive(:new).and_return(client)
end

after :each do
ThinkingSphinx::Configuration.reset
end

describe "#execute" do
it "sends the query to the client" do
subject.execute "SELECT QUERY"

expect(client).to have_received(:query).with("SELECT QUERY")
end

it "returns a result" do
expect(subject.execute("SELECT QUERY")).to eq(["foo" => "bar"])
end

context "with long queries" do
let(:maximum) { (2 ** 23) - 5 }
let(:query) { String.new "SELECT * FROM book_core WHERE MATCH('')" }
let(:difference) { maximum - query.length }

it 'does not allow overly long queries' do
expect {
subject.execute(query.insert(-3, 'a' * (difference + 5)))
}.to raise_error(ThinkingSphinx::QueryLengthError)
end

it 'does not allow queries longer than specified in the settings' do
ThinkingSphinx::Configuration.reset

write_configuration('maximum_statement_length' => maximum - 5)

expect {
subject.execute(query.insert(-3, 'a' * (difference)))
}.to raise_error(ThinkingSphinx::QueryLengthError)
end
end
end
end if RUBY_PLATFORM != 'java'