Class: Mongery::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, schema, mapped_properties, custom_operators) ⇒ Query

Returns a new instance of Query.



53
54
55
56
57
58
59
# File 'lib/mongery.rb', line 53

def initialize(table, schema, mapped_properties, custom_operators)
  @table = table
  @schema = schema
  @mapped_properties = mapped_properties
  @custom_operators = custom_operators
  @condition = nil
end

Instance Attribute Details

#custom_operatorsObject (readonly)

Returns the value of attribute custom_operators.



51
52
53
# File 'lib/mongery.rb', line 51

def custom_operators
  @custom_operators
end

#mapped_propertiesObject (readonly)

Returns the value of attribute mapped_properties.



51
52
53
# File 'lib/mongery.rb', line 51

def mapped_properties
  @mapped_properties
end

#schemaObject (readonly)

Returns the value of attribute schema.



51
52
53
# File 'lib/mongery.rb', line 51

def schema
  @schema
end

#tableObject (readonly)

Returns the value of attribute table.



51
52
53
# File 'lib/mongery.rb', line 51

def table
  @table
end

Instance Method Details

#arelObject



66
67
68
# File 'lib/mongery.rb', line 66

def arel
  @arel ||= build_arel
end

#countObject



123
124
125
126
127
# File 'lib/mongery.rb', line 123

def count
  table.project('COUNT(*)').tap do |t|
    t.where(condition) if condition
  end
end

#deleteObject



144
145
146
147
148
149
# File 'lib/mongery.rb', line 144

def delete
  Arel::DeleteManager.new(table.engine).tap do |manager|
    manager.from(table)
    manager.where(condition) if condition
  end
end

#index(col) ⇒ Object



109
110
111
# File 'lib/mongery.rb', line 109

def index(col)
  Arel.sql(%Q[CREATE INDEX "#{table.name}_#{col}_idx" ON "#{table.name}" ((#{sql_json_exp(col)}))])
end

#insert(args) ⇒ Object



129
130
131
132
133
134
# File 'lib/mongery.rb', line 129

def insert(args)
  Arel::InsertManager.new(table.engine).tap do |manager|
    manager.into(table)
    manager.insert([[table[:id], args['_id']], [table[:data], args.to_json], *mapped_values(args)])
  end
end

#limit(number) ⇒ Object



78
79
80
81
# File 'lib/mongery.rb', line 78

def limit(number)
  arel.take(number)
  self
end

#skip(number) ⇒ Object



83
84
85
86
# File 'lib/mongery.rb', line 83

def skip(number)
  arel.skip(number)
  self
end

#sort(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mongery.rb', line 88

def sort(params)
  params.each do |col, val|
    order = val > 0 ? :asc : :desc
    case col.to_s
    when "_id"
      arel.order(table[:id].send(order))
    else
      arel.order(sql_json_path(col).send(order))
    end
  end
  self
end

#sql_json_exp(col) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/mongery.rb', line 101

def sql_json_exp(col)
  if schema && numeric?(col)
    sql_json_path(col) + "::numeric"
  else
    sql_json_path(col)
  end
end

#to_arelObject



70
71
72
# File 'lib/mongery.rb', line 70

def to_arel
  arel
end

#to_sqlObject



74
75
76
# File 'lib/mongery.rb', line 74

def to_sql
  to_arel.to_sql
end

#update(args) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/mongery.rb', line 136

def update(args)
  Arel::UpdateManager.new(table.engine).tap do |manager|
    manager.table(table)
    manager.set([[table[:data], args.to_json], *mapped_values(args)])
    manager.where(condition) if condition
  end
end

#where(args) ⇒ Object



61
62
63
64
# File 'lib/mongery.rb', line 61

def where(args)
  @where = args
  self
end