Method: Libsql::Client#execute

Defined in:
lib/libsql.rb

#execute(query_string, args = nil) ⇒ Object

Raises:

  • (StandardError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/libsql.rb', line 16

def execute(query_string, args = nil)
  argument_builder = case args
  in Array
    PositionalArgBuilder.new(*args)
  in Hash
    NamedArgBuilder.new(**args)
  in NilClass
    EmptyBuilder.new
  else
    raise NotImplementedError
  end

  if argument_builder.empty?
    body = {
      requests: [
        { type: :execute, stmt: { sql: query_string } },
        { type: :close }
      ]
    }
  else
    body = {
      requests: [
        { type: :execute, stmt: { sql: query_string, argument_builder.key => argument_builder.build } },
        { type: :close }
      ]
    }
  end

  response = http_client.post(STMT_ENDPOINT, body)

  raise StandardError, response.results.first["error"]["message"] if response.failure?

  QueryResult.new(response)
end