Class: Yaasql::Query

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
17
18
# File 'lib/yaasql/query.rb', line 11

def initialize(components)
  [:body, :name, :arguments].each do |param|
    raise ArgumentError.new("Query missing component #{param}") unless components[param]
  end
  @body = components[:body]
  @name = components[:name]
  @arguments = components[:arguments]
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



5
6
7
# File 'lib/yaasql/query.rb', line 5

def arguments
  @arguments
end

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/yaasql/query.rb', line 5

def body
  @body
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/yaasql/query.rb', line 5

def name
  @name
end

Class Method Details

.from_string(query) ⇒ Object



7
8
9
# File 'lib/yaasql/query.rb', line 7

def self.from_string(query)
  new(Reader.new.components(query))
end

Instance Method Details

#execute(connection, args = {}) ⇒ Object



29
30
31
32
# File 'lib/yaasql/query.rb', line 29

def execute(connection, args = {})
  params = self.arguments.map { |a| prepare(args.fetch(a)) }
  connection.exec_params(body, params).to_a
end

#prepare(argument) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/yaasql/query.rb', line 20

def prepare(argument)
  case argument
  when Array
    "{#{argument.join(",")}}"
  else
    argument
  end
end