Class: ThinkingSphinx::PostgreSQLAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/thinking_sphinx/adapters/postgresql_adapter.rb

Instance Method Summary collapse

Methods inherited from AbstractAdapter

adapter_for_model, #bigint_pattern, #case, detect, #downcase, #initialize, #quote_with_table, standard_adapter_for_model

Constructor Details

This class inherits a constructor from ThinkingSphinx::AbstractAdapter

Instance Method Details

#boolean(value) ⇒ Object



65
66
67
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 65

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

#cast_to_datetime(clause) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 34

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

#cast_to_int(clause) ⇒ Object



46
47
48
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 46

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

#cast_to_string(clause) ⇒ Object



30
31
32
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 30

def cast_to_string(clause)
  clause
end

#cast_to_unsigned(clause) ⇒ Object



42
43
44
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 42

def cast_to_unsigned(clause)
  clause
end

#concatenate(clause, separator = ' ') ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 12

def concatenate(clause, separator = ' ')
  if clause[/^COALESCE/]
    clause.split('), ').join(") || '#{separator}' || ")
  else
    clause.split(', ').collect { |field|
      "CAST(COALESCE(#{field}::varchar, '') as varchar)"
    }.join(" || '#{separator}' || ")
  end
end

#convert_nulls(clause, default = '') ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 50

def convert_nulls(clause, default = '')
  default = case default
  when String
    "'#{default}'"
  when NilClass
    'NULL'
  when Fixnum
    "#{default}::bigint"
  else
    default
  end

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

#crc(clause, blank_to_null = false) ⇒ Object



69
70
71
72
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 69

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

#group_concatenate(clause, separator = ' ') ⇒ Object



22
23
24
25
26
27
28
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 22

def group_concatenate(clause, separator = ' ')
  if server_version >= 80400
    "array_to_string(array_agg(COALESCE(#{clause}, '0')), '#{separator}')"
  else
    "array_to_string(array_accum(COALESCE(#{clause}, '0')), '#{separator}')"
  end
end

#setupObject



3
4
5
6
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 3

def setup
  create_array_accum_function
  create_crc32_function
end

#sphinx_identifierObject



8
9
10
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 8

def sphinx_identifier
  "pgsql"
end

#time_difference(diff) ⇒ Object



78
79
80
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 78

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

#utc_query_preObject



82
83
84
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 82

def utc_query_pre
  "SET TIME ZONE 'UTC'"
end

#utf8_query_preObject



74
75
76
# File 'lib/thinking_sphinx/adapters/postgresql_adapter.rb', line 74

def utf8_query_pre
  nil
end