Class: ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter
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 a SET 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
-
#discard! ⇒ Object
-
#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
-
#native_database_types ⇒ Object
-
#reconnect ⇒ Object
-
#reload_type_map ⇒ Object
-
#return_value_after_insert?(column) ⇒ Boolean
-
#server_config ⇒ Object
-
#supports_json? ⇒ Boolean
-
#supports_statement_cache? ⇒ Boolean
-
#transaction(requires_new: nil, isolation: nil, joinable: true) ⇒ Object
-
#update_table_definition(table_name, base) ⇒ Object
-
#url(path = nil) ⇒ Object
-
#use_insert_returning? ⇒ Boolean
-
#valid_type?(type) ⇒ Boolean
#prepare_column_options
#affected_rows, #cacheable_query, #cast_result, #delete, #exec_delete, #exec_insert, #last_inserted_id, #perform_query, #raw_execute, #returning_column_values, #sar_for_insert, #select_all, #to_sar, #to_sar_and_binds, #to_sql, #update
#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
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
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
97
98
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 94
def active?
@lock.synchronize do
@raw_connection&.active?
end
end
|
#arel_visitor ⇒ Object
142
143
144
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 142
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)
@definitions = {}
end
|
#collector ⇒ Object
146
147
148
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 146
def collector
Arel::Collectors::Sunstone.new
end
|
Configures the encoding, verbosity, schema search path, and time zone of the connection. This is called by #connect and should not be called manually.
200
201
202
203
204
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 200
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.
102
103
104
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 102
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
|
#discard! ⇒ Object
121
122
123
124
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 121
def discard! super
@raw_connection = nil
end
|
#disconnect! ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 113
def disconnect!
@lock.synchronize do
super
@raw_connection&.disconnect!
@raw_connection = nil
end
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
193
194
195
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 193
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
160
161
162
163
164
165
166
167
168
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 160
def lookup_cast_type_from_column(column) 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
126
127
128
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 126
def native_database_types NATIVE_DATABASE_TYPES
end
|
#reconnect ⇒ Object
106
107
108
109
110
111
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 106
def reconnect
@lock.synchronize do
@raw_connection&.reconnect!
connect unless @raw_connection
end
end
|
#reload_type_map ⇒ Object
206
207
208
209
210
211
212
213
214
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 206
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
156
157
158
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 156
def return_value_after_insert?(column) column.auto_populated?
end
|
#server_config ⇒ Object
150
151
152
153
154
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 150
def server_config
with_raw_connection do |conn|
JSON.parse(conn.get("/configuration").body)
end
end
|
#supports_json? ⇒ Boolean
187
188
189
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 187
def supports_json?
true
end
|
#supports_statement_cache? ⇒ Boolean
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 170
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
end
|
#update_table_definition(table_name, base) ⇒ Object
138
139
140
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 138
def update_table_definition(table_name, base) 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
130
131
132
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 130
def use_insert_returning?
true
end
|
#valid_type?(type) ⇒ Boolean
134
135
136
|
# File 'lib/active_record/connection_adapters/sunstone_adapter.rb', line 134
def valid_type?(type)
!native_database_types[type].nil?
end
|