Class: RtSphinx::Connection
- Inherits:
-
Object
- Object
- RtSphinx::Connection
- Includes:
- Singleton
- Defined in:
- lib/rt_sphinx/connection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #execute_sql(sql, mode) ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rt_sphinx/connection.rb', line 16 def initialize @pool = ConnectionPool.new(:size => 5, :timeout => 5) do if RUBY_PLATFORM =~ /java/ require 'jdbc/mysql' Jdbc::MySQL.load_driver(:require) if Jdbc::MySQL.respond_to?(:load_driver) url = "jdbc:mysql://127.0.0.1:9327?characterEncoding=utf8&maxAllowedPacket=512000" conn = java.sql.DriverManager.getConnection(url, "", "") else require 'mysql2' Mysql2::Client.new( :host => '127.0.0.1', :port => 9327, :encoding => 'utf8' ) end end end |
Class Method Details
.instance ⇒ Object
12 13 14 |
# File 'lib/rt_sphinx/connection.rb', line 12 def self.instance @@instance ||= new end |
Instance Method Details
#execute_sql(sql, mode) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rt_sphinx/connection.rb', line 34 def execute_sql(sql, mode) res = {} @pool.with do |connection| if RUBY_PLATFORM =~ /java/ st = connection.create_statement res = case mode when 'select' resultset_to_hash(st.execute_query(sql)) when 'insert' st.execute_update(sql) end st.close else res = connection.query(sql) end end res end |