class ThinkingSphinx::PostgreSQLAdapter

Public Instance Methods

boolean(value) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 65
def boolean(value)
  value ? 'TRUE' : 'FALSE'
end
cast_to_datetime(clause) click to toggle source
# 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) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 46
def cast_to_int(clause)
  "#{clause}::INT8"
end
cast_to_string(clause) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 30
def cast_to_string(clause)
  clause
end
cast_to_unsigned(clause) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 42
def cast_to_unsigned(clause)
  clause
end
concatenate(clause, separator = ' ') click to toggle source
# 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 = '') click to toggle source
# 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) click to toggle source
# 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 = ' ') click to toggle source
# 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
setup() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 3
def setup
  create_array_accum_function
  create_crc32_function
end
sphinx_identifier() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 8
def sphinx_identifier
  "pgsql"
end
time_difference(diff) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 78
def time_difference(diff)
  "current_timestamp - interval '#{diff} seconds'"
end
utc_query_pre() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 82
def utc_query_pre
  "SET TIME ZONE 'UTC'"
end
utf8_query_pre() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 74
def utf8_query_pre
  nil
end

Private Instance Methods

create_array_accum_function() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 111
def create_array_accum_function
  if server_version >= 80311
    return
  elsif server_version > 80200
    execute <<-SQL
      CREATE AGGREGATE array_accum (anyelement)
      (
          sfunc = array_append,
          stype = anyarray,
          initcond = '{}'
      );
    SQL
  else
    execute <<-SQL
      CREATE AGGREGATE array_accum
      (
          basetype = anyelement,
          sfunc = array_append,
          stype = anyarray,
          initcond = '{}'
      );
    SQL
  end
end
create_crc32_function() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 136
def create_crc32_function
  execute "CREATE LANGUAGE 'plpgsql';"
  function = <<-SQL
    CREATE OR REPLACE FUNCTION crc32(word text)
    RETURNS bigint AS $$
      DECLARE tmp bigint;
      DECLARE i int;
      DECLARE j int;
      DECLARE byte_length int;
      DECLARE word_array bytea;
      BEGIN
        IF COALESCE(word, '') = '' THEN
          return 0;
        END IF;

        i = 0;
        tmp = 4294967295;
        byte_length = bit_length(word) / 8;
        word_array = decode(replace(word, E'\\\\', E'\\\\\\\\'), 'escape');
        LOOP
          tmp = (tmp # get_byte(word_array, i))::bigint;
          i = i + 1;
          j = 0;
          LOOP
            tmp = ((tmp >> 1) # (3988292384 * (tmp & 1)))::bigint;
            j = j + 1;
            IF j >= 8 THEN
              EXIT;
            END IF;
          END LOOP;
          IF i >= byte_length THEN
            EXIT;
          END IF;
        END LOOP;
        return (tmp # 4294967295);
      END
    $$ IMMUTABLE LANGUAGE plpgsql;
  SQL
  execute function, true
end
execute(command, output_error = false) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 88
def execute(command, output_error = false)
  if RUBY_PLATFORM == 'java'
    connection.transaction do
      execute_command command, output_error
    end
  else
    execute_command command, output_error
  end
end
execute_command(command, output_error = false) click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 98
def execute_command(command, output_error = false)
  connection.execute "begin"
  connection.execute "savepoint ts"
  begin
    connection.execute command
  rescue StandardError => err
    puts err if output_error
    connection.execute "rollback to savepoint ts"
  end
  connection.execute "release savepoint ts"
  connection.execute "commit"
end
server_version() click to toggle source
# File lib/thinking_sphinx/adapters/postgresql_adapter.rb, line 177
def server_version
  if RUBY_PLATFORM == 'java'
    (connection.raw_connection.connection.server_major_version * 10000) +
    (connection.raw_connection.connection.server_minor_version * 100)
  elsif connection.raw_connection.respond_to?(:server_version)
    connection.raw_connection.server_version
  else
    0
  end
end