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

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

Constant Summary

Constants inherited from Base::Query

Base::Query::OPERATORS

Instance Method Summary collapse

Methods inherited from Base::Query

#apply, #apply_operator

Constructor Details

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

Returns a new instance of Query.



57
58
59
60
61
62
63
64
# File 'lib/wcc/contentful/store/postgres_store.rb', line 57

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

Instance Method Details

#countObject



84
85
86
87
88
89
90
# File 'lib/wcc/contentful/store/postgres_store.rb', line 84

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wcc/contentful/store/postgres_store.rb', line 66

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(
    @store,
    @conn,
    statement,
    params,
    @options
  )
end

#firstObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wcc/contentful/store/postgres_store.rb', line 92

def first
  return @first if @first

  statement = 'SELECT * FROM contentful_raw ' + @statement + ' LIMIT 1'
  result = @conn.exec(statement, @params)
  return if result.num_tuples == 0

  resolve_includes(
    JSON.parse(result.getvalue(0, 1)),
    @options[:include]
  )
end

#mapObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/wcc/contentful/store/postgres_store.rb', line 105

def map
  arr = []
  resolve.each do |row|
    arr << yield(
    resolve_includes(
      JSON.parse(row['data']),
      @options[:include]
      )
    )
  end
  arr
end

#resultObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/wcc/contentful/store/postgres_store.rb', line 118

def result
  arr = []
  resolve.each do |row|
    arr <<
      resolve_includes(
        JSON.parse(row['data']),
        @options[:include]
       )
  end
  arr
end