Module: Snowflake::Client

Defined in:
lib/snowflake_rb.rb

Class Method Summary collapse

Class Method Details

.close(db_pointer) ⇒ Object

Parameters:

  • db_pointer (Pointer)

    the pointer which ‘connect` returned.



58
59
60
# File 'lib/snowflake_rb.rb', line 58

def close(db_pointer)
  ::Snowflake::Binding.close(db_pointer)
end

.column_count(query_object) ⇒ Object

Parameters:

  • query_object (Pointer)

    the pointer which ‘fetch` returned.



142
143
144
# File 'lib/snowflake_rb.rb', line 142

def column_count(query_object)
  ::Snowflake::Binding.query_column_count(query_object)
end

.column_names(query_object, field_count = nil) ⇒ List<String>

Returns the column values in order.

Parameters:

  • query_object (Pointer)

    the pointer which ‘fetch` returned.

Returns:

  • (List<String>)

    the column values in order



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/snowflake_rb.rb', line 124

def column_names(query_object, field_count = nil)
  raw_row = ::Snowflake::Binding.query_columns(query_object)
  return nil if raw_row.nil? || raw_row == FFI::Pointer::NULL

  raw_row.get_array_of_pointer(0, field_count).map do |cstr|
    if cstr == FFI::Pointer::NULL || cstr.nil?
      nil
    else
      str = cstr.read_string
      LibC.free(cstr)
      str
    end
  end
ensure
  LibC.free(raw_row) if raw_row
end

.configDry::Configurable::Config

Returns configuration for the gem.

Returns:

  • (Dry::Configurable::Config)

    configuration for the gem.



147
148
149
# File 'lib/snowflake_rb.rb', line 147

def config
  SnowflakeRB::Configuration.config
end

.connect(account = nil, warehouse = nil, database = nil, schema = nil, user = nil, password = nil, role = nil, port = nil) ⇒ Object

Parameters:

  • account (String) (defaults to: nil)

    should include everything in the db url ahead of ‘snowflakecomputing.com’

  • port (Integer) (defaults to: nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/snowflake_rb.rb', line 35

def connect(
   = nil,
  warehouse = nil,
  database = nil,
  schema = nil,
  user = nil,
  password = nil,
  role = nil,
  port = nil
)
  ::Snowflake::Binding.connect(
     || Snowflake::configuration.,
    warehouse || Snowflake::configuration.warehouse,
    database || Snowflake::configuration.database,
    schema || Snowflake::configuration.schema,
    user || Snowflake::configuration.user,
    password || Snowflake::configuration.password,
    role || Snowflake::configuration.role,
    port || Snowflake::configuration.port
  )
end

.exec(db_pointer, statement) ⇒ Object

Parameters:

  • db_pointer (Pointer)

    the pointer which ‘connect` returned.

  • statement (String)

    an executable query which should return number of rows affected



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

def exec(db_pointer, statement)
  count = ::Snowflake::Binding.exec(db_pointer, statement) # returns -1 for error
  count >= 0 ? count : nil
end

.fetch(db_pointer, query) ⇒ Object

Parameters:

  • db_pointer (Pointer)

    the pointer which ‘connect` returned.

  • query (String)

    a select query to run.



95
96
97
# File 'lib/snowflake_rb.rb', line 95

def fetch(db_pointer, query)
  ::Snowflake::Binding.fetch(db_pointer, query)
end

.get_next_row(query_object, field_count) ⇒ List<String>

Returns the column values in order.

Parameters:

  • query_object (Pointer)

    the pointer which ‘fetch` returned. Go will gc this object when the query is done; so, don’t expect to reference it after the call which returned ‘nil`

  • field_count (Integer)

    column count: it will seg fault if you provide a number greater than the actual number. Using code should use wrap this in something like

Returns:

  • (List<String>)

    the column values in order



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/snowflake_rb.rb', line 105

def get_next_row(query_object, field_count)
  raw_row = ::Snowflake::Binding.next_row(query_object)
  return nil if raw_row.nil? || raw_row == FFI::Pointer::NULL

  raw_row.get_array_of_pointer(0, field_count).map do |cstr|
    if cstr == FFI::Pointer::NULL || cstr.nil?
      nil
    else
      str = cstr.read_string
      LibC.free(cstr)
      str
    end
  end
ensure
  LibC.free(raw_row) if raw_row
end

.select(db_pointer, sql, field_count: nil) ⇒ Object

Send a query and then yield each row as an array of strings to the given block

Parameters:

  • db_pointer (Pointer)

    the pointer which ‘connect` returned.

  • query (String)

    a select query to run.

Returns:

  • error_string



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/snowflake_rb.rb', line 75

def select(db_pointer, sql, field_count: nil)
  return 'db_pointer not initialized' unless db_pointer
  return to_enum(__method__, db_pointer, sql) unless block_given?

  query_pointer = fetch(db_pointer, sql)
  return 'query_pointer is missing' if query_pointer.nil? || query_pointer == FFI::Pointer::NULL

  field_count ||= column_count(query_pointer)
  loop do
    row = get_next_row(query_pointer, field_count)
    return 'row not present' unless row

    yield row
  end
  nil
end