Class: Oedipus::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/oedipus/query_builder.rb

Overview

Constructs SphinxQL queries from the internal Hash format.

Instance Method Summary collapse

Constructor Details

#initialize(index_name) ⇒ QueryBuilder

Initialize a new QueryBuilder for index_name.

Parameters:

  • index_name (Symbol)

    the name of the index being queried



17
18
19
# File 'lib/oedipus/query_builder.rb', line 17

def initialize(index_name)
  @index_name = index_name
end

Instance Method Details

#delete(id) ⇒ String

Build a SphinxQL query to delete the record identified by id.

Parameters:

  • id (Fixnum)

    the unique ID of the document to delete

Returns:

  • (String)

    the SphinxQL to delete the record



101
102
103
# File 'lib/oedipus/query_builder.rb', line 101

def delete(id)
  ["DELETE FROM #{@index_name} WHERE id = ?", id]
end

#insert(id, attributes) ⇒ String

Build a SphinxQL query to insert the record identified by id with the given attributes.

Parameters:

  • id (Fixnum)

    the unique ID of the document to insert

  • attributes (Hash)

    a Hash of attributes

Returns:

  • (String)

    the SphinxQL to insert the record



54
55
56
# File 'lib/oedipus/query_builder.rb', line 54

def insert(id, attributes)
  into("INSERT", id, attributes)
end

#replace(id, attributes) ⇒ String

Build a SphinxQL query to replace the record identified by id with the given attributes.

Parameters:

  • id (Fixnum)

    the unique ID of the document to replace

  • attributes (Hash)

    a Hash of attributes

Returns:

  • (String)

    the SphinxQL to replace the record



90
91
92
# File 'lib/oedipus/query_builder.rb', line 90

def replace(id, attributes)
  into("REPLACE", id, attributes)
end

#select(query, filters) ⇒ String

Build a SphinxQL query for the fulltext search query and filters in filters.

Parameters:

  • query (String)

    the fulltext query to execute (may be empty)

  • filters (Hash)

    additional attribute filters and other options

Returns:

  • (String)

    a SphinxQL query



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/oedipus/query_builder.rb', line 31

def select(query, filters)
  where, *bind_values = conditions(query, filters)
  [
    [
      from(filters),
      where,
      order_by(filters),
      limits(filters)
    ].join(" "),
    *bind_values
  ]
end

#update(id, attributes) ⇒ String

Build a SphinxQL query to update the record identified by id with the given attributes.

Parameters:

  • id (Fixnum)

    the unique ID of the document to update

  • attributes (Hash)

    a Hash of attributes

Returns:

  • (String)

    the SphinxQL to update the record



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/oedipus/query_builder.rb', line 68

def update(id, attributes)
  set_attrs, *bind_values = update_attributes(attributes)
  [
    [
      "UPDATE #{@index_name} SET",
      set_attrs,
      "WHERE id = ?"
    ].join(" "),
    *bind_values.push(id)
  ]
end