Class: ActiveRecord::ConnectionAdapters::DataWorldAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::DataWorldAdapter
- Defined in:
- lib/active_record/connection_adapters/data_world_adapter.rb
Instance Method Summary collapse
-
#adapter_name ⇒ Object
:nodoc:.
-
#columns(table_name, name = nil) ⇒ Object
:nodoc:.
- #exec_query(sql, name = nil, binds = [], prepare: false) ⇒ Object
-
#execute(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================.
- #requires_reloading? ⇒ Boolean
-
#sql_bind(sql, binds) ⇒ Object
do our best job binding the variables into the query.
-
#supports_count_distinct? ⇒ Boolean
:nodoc:.
-
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ========================================.
- #try_json(string) ⇒ Object
- #views ⇒ Object
Instance Method Details
#adapter_name ⇒ Object
:nodoc:
18 19 20 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 18 def adapter_name #:nodoc: 'Data.World' end |
#columns(table_name, name = nil) ⇒ Object
:nodoc:
102 103 104 105 106 107 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 102 def columns(table_name, name = nil) #:nodoc: table_structure(table_name).map do |field| = SqlTypeMetadata.new(sql_type: field['columnDatatype']) ActiveRecord::ConnectionAdapters::Column.new(field['columnName'], nil, ) end end |
#exec_query(sql, name = nil, binds = [], prepare: false) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 53 def exec_query(sql, name = nil, binds = [], prepare: false) if preventing_writes? && write_query?(sql) raise ActiveRecord::ReadOnlyError, "Data.World is read-only, query not accepted: #{sql}" end type_casted_binds = type_casted_binds(binds) log(sql, name, binds, type_casted_binds) do raw = execute(sql_bind(sql, binds)) cols = raw.dig(0, 'fields')&.map { |h| h['name'] } records = raw[1..-1].map(&:values) ActiveRecord::Result.new(cols, records) end end |
#execute(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 33 def execute(sql, name = nil) #:nodoc: url = URI("https://api.data.world/v0/sql/#{@config[:owner]}/#{@config[:id]}") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(url) request["content-type"] = 'application/json' request["authorization"] = "Bearer #{@config[:auth_token]}" request.body = { query: sql, includeTableSchema: true }.to_json response = http.request(request) try_json response.read_body end |
#requires_reloading? ⇒ Boolean
22 23 24 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 22 def requires_reloading? true end |
#sql_bind(sql, binds) ⇒ Object
do our best job binding the variables into the query
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 70 def sql_bind(sql, binds) new_sql = sql.clone binds.each do |attribute| value = if attribute.value.is_a?(String) "'#{attribute.value}'" else attribute.value.to_s end new_sql = new_sql.sub('?', value) end new_sql end |
#supports_count_distinct? ⇒ Boolean
:nodoc:
26 27 28 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 26 def supports_count_distinct? #:nodoc: true end |
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ========================================
93 94 95 96 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 93 def tables(name = nil) #:nodoc: sql = "SELECT tableName FROM Tables" execute(sql, name).map { |row| row[0] } end |
#try_json(string) ⇒ Object
84 85 86 87 88 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 84 def try_json(string) JSON.parse(string) rescue raise string end |
#views ⇒ Object
98 99 100 |
# File 'lib/active_record/connection_adapters/data_world_adapter.rb', line 98 def views [] end |