Class: Oedipus::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/oedipus/connection.rb,
lib/oedipus/connection/pool.rb

Overview

Provides an interface for talking to SphinxQL.

Currently this class wraps a native mysql extension.

Defined Under Namespace

Classes: Pool

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Instantiate a new Connection to a SphinxQL host.

The connection will be established on initialization.

The underlying implementation uses a thread-safe connection pool.

Parameters:

  • server (String)

    a ‘hostname:port’ string

  • options (Hash)

    a Hash containing :host and :port



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oedipus/connection.rb', line 26

def initialize(options)
  options = options.kind_of?(String) ?
    Hash[ [:host, :port].zip(options.split(":")) ] :
    options

  @pool = Pool.new(
    host: options[:host],
    port: options[:port].to_i,
    size: options.fetch(:pool_size, 8),
    ttl:  60
  )

  assert_valid_pool
end

Instance Method Details

#[](index_name) ⇒ Index

Acess a specific index for querying.

Parameters:

  • index_name (String)

    the name of an existing index in Sphinx

Returns:

  • (Index)

    an index that can be queried



48
49
50
# File 'lib/oedipus/connection.rb', line 48

def [](index_name)
  Index.new(index_name, self)
end

#execute(sql, *bind_values) ⇒ Fixnum

Execute a non-read query.

Note that SphinxQL does not support prepared statements.

Parameters:

  • sql (String)

    a SphinxQL query, such as INSERT or REPLACE

  • bind_values (Object...)

    values to be substituted in place of ‘?’ in the query

Returns:

  • (Fixnum)

    the number of affected rows



99
100
101
# File 'lib/oedipus/connection.rb', line 99

def execute(sql, *bind_values)
  @pool.acquire { |conn| conn.execute(sql, *bind_values) }
end

#multi_query(sql, *bind_values) ⇒ Array

Execute one or more queries in a batch.

Queries should be separated by semicolons. Results are returned in a 2-dimensional array.

Note that SphinxQL does not support prepared statements.

Parameters:

  • sql (String)

    one or more SphinxQL statements, separated by semicolons

  • bind_values (Object...)

    values to be substituted in place of ‘?’ in the query

Returns:

  • (Array)

    an array of arrays, containing the returned records



67
68
69
# File 'lib/oedipus/connection.rb', line 67

def multi_query(sql, *bind_values)
  @pool.acquire { |conn| conn.query(sql, *bind_values) }
end

#query(sql, *bind_values) ⇒ Array

Execute a single read query.

Note that SphinxQL does not support prepared statements.

Parameters:

  • sql (String)

    a single SphinxQL statement

  • bind_values (Object...)

    values to be substituted in place of ‘?’ in the query

Returns:

  • (Array)

    an array of Hashes containing the matched records



83
84
85
# File 'lib/oedipus/connection.rb', line 83

def query(sql, *bind_values)
  @pool.acquire { |conn| conn.query(sql, *bind_values).first }
end