Class: WCC::Contentful::Store::PostgresStore::Query

Inherits:
Base::Query
  • Object
show all
Defined in:
lib/wcc/contentful/store/postgres_store.rb

Instance Method Summary collapse

Methods inherited from Base::Query

#apply

Constructor Details

#initialize(conn, statement = nil, params = nil) ⇒ Query



51
52
53
54
55
56
# File 'lib/wcc/contentful/store/postgres_store.rb', line 51

def initialize(conn, statement = nil, params = nil)
  @conn = conn
  @statement = statement ||
    "WHERE data->'sys'->>'id' IS NOT NULL"
  @params = params || []
end

Instance Method Details

#countObject



74
75
76
77
78
79
# File 'lib/wcc/contentful/store/postgres_store.rb', line 74

def count
  return @count if @count
  statement = 'SELECT count(*) FROM contentful_raw ' + @statement
  result = @conn.exec(statement, @params)
  @count = result.getvalue(0, 0).to_i
end

#eq(field, expected, context = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wcc/contentful/store/postgres_store.rb', line 58

def eq(field, expected, context = nil)
  locale = context[:locale] if context.present?
  locale ||= 'en-US'

  params = @params.dup

  statement = @statement + " AND data->'fields'->$#{push_param(field, params)}" \
    "->$#{push_param(locale, params)} ? $#{push_param(expected, params)}"

  Query.new(
    @conn,
    statement,
    params
  )
end

#firstObject



81
82
83
84
85
86
# File 'lib/wcc/contentful/store/postgres_store.rb', line 81

def first
  return @first if @first
  statement = 'SELECT * FROM contentful_raw ' + @statement + ' LIMIT 1'
  result = @conn.exec(statement, @params)
  JSON.parse(result.getvalue(0, 1))
end

#mapObject



88
89
90
91
92
# File 'lib/wcc/contentful/store/postgres_store.rb', line 88

def map
  arr = []
  resolve.each { |row| arr << yield(JSON.parse(row['data'])) }
  arr
end

#resultObject



94
95
96
97
98
# File 'lib/wcc/contentful/store/postgres_store.rb', line 94

def result
  arr = []
  resolve.each { |row| arr << JSON.parse(row['data']) }
  arr
end