Class: ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter
- Includes:
- Sunstone::ColumnDumper, Sunstone::DatabaseStatements, 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.
- #load_type_map ⇒ Object
-
#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
Constructor Details
#initialize ⇒ SunstoneAPIAdapter
Initializes and connects a SunstoneAPI adapter.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 80 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
54 55 56 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 54 def new_client(conn_params) ::Sunstone::Connection.new(conn_params) end |
Instance Method Details
#active? ⇒ Boolean
106 107 108 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 106 def active? @raw_connection&.active? end |
#arel_visitor ⇒ Object
153 154 155 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 153 def arel_visitor Arel::Visitors::Sunstone.new end |
#clear_cache!(new_connection: false) ⇒ Object
74 75 76 77 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 74 def clear_cache!(new_connection: false) # TODO move @definitions to using @schema_cache @definitions = {} end |
#collector ⇒ Object
157 158 159 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 157 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.
221 222 223 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 221 def configure_connection reload_type_map end |
#connect ⇒ Object
Connects to a StandardAPI server and sets up the adapter depending on the connected server’s characteristics.
210 211 212 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 210 def connect @raw_connection = self.class.new_client(@connection_parameters) end |
#default_prepared_statements ⇒ Object
70 71 72 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 70 def default_prepared_statements false end |
#delete(arel, name = nil, binds = []) ⇒ Object
Executes the delete statement and returns the number of rows affected.
132 133 134 135 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 132 def delete(arel, name = nil, binds = []) r = exec_delete(arel, name, binds) r.rows.first.to_i end |
#discard! ⇒ Object
:nodoc:
126 127 128 129 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 126 def discard! # :nodoc: super @raw_connection = nil end |
#disconnect! ⇒ Object
120 121 122 123 124 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 120 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
203 204 205 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 203 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 |
#load_type_map ⇒ Object
110 111 112 113 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 110 def load_type_map @type_map = Type::HashLookupTypeMap.new initialize_type_map(@type_map) end |
#lookup_cast_type_from_column(column) ⇒ Object
:nodoc:
171 172 173 174 175 176 177 178 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 171 def lookup_cast_type_from_column(column) # :nodoc: 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:
137 138 139 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 137 def native_database_types #:nodoc: NATIVE_DATABASE_TYPES end |
#reconnect ⇒ Object
115 116 117 118 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 115 def reconnect super @raw_connection&.reconnect! end |
#reload_type_map ⇒ Object
225 226 227 228 229 230 231 232 233 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 225 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:
167 168 169 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 167 def return_value_after_insert?(column) # :nodoc: column.auto_populated? end |
#server_config ⇒ Object
161 162 163 164 165 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 161 def server_config with_raw_connection do |conn| JSON.parse(conn.get("/configuration").body) end end |
#supports_json? ⇒ Boolean
197 198 199 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 197 def supports_json? true end |
#supports_statement_cache? ⇒ Boolean
include Savepoints
66 67 68 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 66 def supports_statement_cache? false end |
#transaction(requires_new: nil, isolation: nil, joinable: true) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 180 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:
149 150 151 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 149 def update_table_definition(table_name, base) #:nodoc: SunstoneAPI::Table.new(table_name, base) end |
#url(path = nil) ⇒ Object
102 103 104 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 102 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
141 142 143 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 141 def use_insert_returning? true end |
#valid_type?(type) ⇒ Boolean
145 146 147 |
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 145 def valid_type?(type) !native_database_types[type].nil? end |