Class: ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter
- Includes:
- ActiveRecord::ConnectionAdapters::Sunstone::ColumnDumper, ActiveRecord::ConnectionAdapters::Sunstone::DatabaseStatements, ActiveRecord::ConnectionAdapters::Sunstone::Quoting, ActiveRecord::ConnectionAdapters::Sunstone::SchemaStatements
- Defined in:
- lib/active_record/connection_adapters/sunstone_adapter.rb
Overview
The SunstoneAPI adapter.
Options:
-
:host
- Defaults to a Unix-domain socket in /tmp. On machines without Unix-domain sockets, the default is to connect to localhost. -
:port
- Defaults to 5432. -
:username
- The API key to connect with -
:encoding
- An optional client encoding that is used in aSET client_encoding TO <encoding>
call on the connection.
Constant Summary collapse
- ADAPTER_NAME =
'Sunstone'.freeze
- VALID_SUNSTONE_CONN_PARAMS =
[:url, :host, :port, :api_key, :use_ssl, :user_agent, :ca_cert]
- NATIVE_DATABASE_TYPES =
{ string: { name: "string" }, number: { name: "number" }, json: { name: "json" }, boolean: { name: "boolean" } }
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #arel_visitor ⇒ Object
- #clear_cache!(new_connection: false) ⇒ Object
- #collector ⇒ Object
-
#configure_connection ⇒ Object
Configures the encoding, verbosity, schema search path, and time zone of the connection.
-
#connect ⇒ Object
Connects to a StandardAPI server and sets up the adapter depending on the connected server’s characteristics.
- #default_prepared_statements ⇒ Object
-
#delete(arel, name = nil, binds = []) ⇒ Object
Executes the delete statement and returns the number of rows affected.
-
#discard! ⇒ Object
:nodoc:.
- #disconnect! ⇒ Object
-
#initialize ⇒ SunstoneAPIAdapter
constructor
Initializes and connects a SunstoneAPI adapter.
-
#insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil) ⇒ Object
(also: #create)
Executes an INSERT query and returns a hash of the object and any updated relations.
-
#lookup_cast_type_from_column(column) ⇒ Object
:nodoc:.
-
#native_database_types ⇒ Object
:nodoc:.
- #reconnect ⇒ Object
- #reload_type_map ⇒ Object
-
#return_value_after_insert?(column) ⇒ Boolean
:nodoc:.
- #server_config ⇒ Object
- #supports_json? ⇒ Boolean
-
#supports_statement_cache? ⇒ Boolean
include Savepoints.
- #transaction(requires_new: nil, isolation: nil, joinable: true) ⇒ Object
-
#update_table_definition(table_name, base) ⇒ Object
:nodoc:.
- #url(path = nil) ⇒ Object
- #use_insert_returning? ⇒ Boolean
- #valid_type?(type) ⇒ Boolean
Methods included from ActiveRecord::ConnectionAdapters::Sunstone::ColumnDumper
Methods included from ActiveRecord::ConnectionAdapters::Sunstone::DatabaseStatements
#cacheable_query, #exec_insert, #internal_exec_query, #last_inserted_id, #returning_column_values, #sar_for_insert, #select_all, #to_sar, #to_sar_and_binds, #to_sql, #update
Methods included from ActiveRecord::ConnectionAdapters::Sunstone::SchemaStatements
#column_definitions, #column_name_for_operation, #columns, #columns_for_distinct, #definition, #distinct_relation_for_primary_key, #fetch_type_metadata, #limit_definition, #lookup_cast_type, #new_column, #primary_key, #table_exists?, #tables, #views
Constructor Details
#initialize ⇒ SunstoneAPIAdapter
Initializes and connects a SunstoneAPI adapter.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 68 def initialize(...) super conn_params = @config.compact if conn_params[:url] uri = URI.parse(conn_params.delete(:url)) conn_params[:api_key] ||= (uri.user ? CGI.unescape(uri.user) : nil) conn_params[:host] ||= uri.host conn_params[:port] ||= uri.port conn_params[:use_ssl] ||= (uri.scheme == 'https') end # Forward only valid config params to Sunstone::Connection conn_params.slice!(*VALID_SUNSTONE_CONN_PARAMS) @connection_parameters = conn_params @max_identifier_length = nil @type_map = nil @raw_connection = nil end |
Class Method Details
.new_client(conn_params) ⇒ Object
42 43 44 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 42 def new_client(conn_params) ::Sunstone::Connection.new(conn_params) end |
Instance Method Details
#active? ⇒ Boolean
94 95 96 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 94 def active? @raw_connection&.active? end |
#arel_visitor ⇒ Object
136 137 138 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 136 def arel_visitor Arel::Visitors::Sunstone.new end |
#clear_cache!(new_connection: false) ⇒ Object
62 63 64 65 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 62 def clear_cache!(new_connection: false) # TODO move @definitions to using @schema_cache @definitions = {} end |
#collector ⇒ Object
140 141 142 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 140 def collector Arel::Collectors::Sunstone.new end |
#configure_connection ⇒ Object
Configures the encoding, verbosity, schema search path, and time zone of the connection. This is called by #connect and should not be called manually.
205 206 207 208 209 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 205 def configure_connection super reload_type_map end |
#connect ⇒ Object
Connects to a StandardAPI server and sets up the adapter depending on the connected server’s characteristics.
194 195 196 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 194 def connect @raw_connection = self.class.new_client(@connection_parameters) end |
#default_prepared_statements ⇒ Object
58 59 60 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 58 def default_prepared_statements false end |
#delete(arel, name = nil, binds = []) ⇒ Object
Executes the delete statement and returns the number of rows affected.
115 116 117 118 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 115 def delete(arel, name = nil, binds = []) r = exec_delete(arel, name, binds) r.rows.first.to_i end |
#discard! ⇒ Object
:nodoc:
109 110 111 112 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 109 def discard! # :nodoc: super @raw_connection = nil end |
#disconnect! ⇒ Object
103 104 105 106 107 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 103 def disconnect! super @raw_connection&.disconnect! @raw_connection = nil end |
#insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil) ⇒ Object Also known as: create
Executes an INSERT query and returns a hash of the object and any updated relations. This is different from AR which returns an ID
187 188 189 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 187 def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil) exec_insert(arel, name, binds, pk, sequence_name, returning: returning) end |
#lookup_cast_type_from_column(column) ⇒ Object
:nodoc:
154 155 156 157 158 159 160 161 162 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 154 def lookup_cast_type_from_column(column) # :nodoc: verify! if type_map.nil? cast_type = @type_map.lookup(column.sql_type, { limit: column.limit, precision: column.precision, scale: column.scale }) column.array ? Sunstone::Type::Array.new(cast_type) : cast_type end |
#native_database_types ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 120 def native_database_types #:nodoc: NATIVE_DATABASE_TYPES end |
#reconnect ⇒ Object
98 99 100 101 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 98 def reconnect super @raw_connection&.reconnect! end |
#reload_type_map ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 211 def reload_type_map if @type_map type_map.clear else @type_map = Type::HashLookupTypeMap.new end initialize_type_map end |
#return_value_after_insert?(column) ⇒ Boolean
:nodoc:
150 151 152 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 150 def return_value_after_insert?(column) # :nodoc: column.auto_populated? end |
#server_config ⇒ Object
144 145 146 147 148 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 144 def server_config with_raw_connection do |conn| JSON.parse(conn.get("/configuration").body) end end |
#supports_json? ⇒ Boolean
181 182 183 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 181 def supports_json? true end |
#supports_statement_cache? ⇒ Boolean
include Savepoints
54 55 56 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 54 def supports_statement_cache? false end |
#transaction(requires_new: nil, isolation: nil, joinable: true) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 164 def transaction(requires_new: nil, isolation: nil, joinable: true) Thread.current[:sunstone_transaction_count] ||= 0 Thread.current[:sunstone_request_sent] = nil if Thread.current[:sunstone_transaction_count] == 0 Thread.current[:sunstone_transaction_count] += 1 begin yield ensure Thread.current[:sunstone_transaction_count] -= 1 if Thread.current[:sunstone_transaction_count] == 0 Thread.current[:sunstone_transaction_count] = nil Thread.current[:sunstone_request_sent] = nil end end rescue ActiveRecord::Rollback # rollbacks are silently swallowed end |
#update_table_definition(table_name, base) ⇒ Object
:nodoc:
132 133 134 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 132 def update_table_definition(table_name, base) #:nodoc: SunstoneAPI::Table.new(table_name, base) end |
#url(path = nil) ⇒ Object
90 91 92 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 90 def url(path=nil) "http#{@connection_parameters[:use_ssl] ? 's' : ''}://#{@connection_parameters[:host]}#{@connection_parameters[:port] != 80 ? (@connection_parameters[:port] == 443 && @connection_parameters[:use_ssl] ? '' : ":#{@connection_parameters[:port]}") : ''}#{path}" end |
#use_insert_returning? ⇒ Boolean
124 125 126 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 124 def use_insert_returning? true end |
#valid_type?(type) ⇒ Boolean
128 129 130 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 128 def valid_type?(type) !native_database_types[type].nil? end |