Class: ActiveRecord::ConnectionAdapters::SpannerAdapter

Inherits:
AbstractAdapter
  • Object
show all
Extended by:
TypeMapBuilder
Includes:
ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements, ActiveRecord::ConnectionAdapters::Spanner::Quoting, ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements, TypeMapBuilder
Defined in:
lib/active_record/connection_adapters/spanner_adapter.rb

Defined Under Namespace

Modules: TypeMapBuilder

Constant Summary collapse

ADAPTER_NAME =
"spanner".freeze
NATIVE_DATABASE_TYPES =
{
  primary_key:  "INT64",
  parent_key:   "INT64",
  string:       { name: "STRING", limit: "MAX" },
  text:         { name: "STRING", limit: "MAX" },
  integer:      { name: "INT64" },
  bigint:       { name: "INT64" },
  float:        { name: "FLOAT64" },
  decimal:      { name: "NUMERIC" },
  numeric:      { name: "NUMERIC" },
  datetime:     { name: "TIMESTAMP" },
  time:         { name: "TIMESTAMP" },
  date:         { name: "DATE" },
  binary:       { name: "BYTES", limit: "MAX" },
  boolean:      { name: "BOOL" },
  json:         { name: "JSON" }
}.freeze
TYPE_MAP =
Type::TypeMap.new.tap { |m| initialize_type_map m }

Constants included from ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements

ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements::VERSION_6_0_3, ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements::VERSION_6_1_0, ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements::VERSION_7_2

Constants included from ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements

ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements::COMMENT_REGEX, ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements::RequestOptions, ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements::VERSION_7_1_0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::ConnectionAdapters::Spanner::SchemaStatements

#_add_foreign_key, #_remove_columns, #add_column, #add_foreign_key, #add_index, #add_reference, #assume_migrated_upto_version, #change_column, #change_column_default, #change_column_null, #check_constraints, #column_definitions, #create_join_table, #create_schema_dumper, #create_table, #current_database, #data_sources, #drop_table, #extract_schema_qualified_name, #fetch_type_metadata, #foreign_keys, #index_name_exists?, #indexes, #insert_versions_sql, #migration_context, #new_column_from_field, #primary_and_parent_keys, #primary_keys, #quoted_scope, #remove_column, #remove_columns, #remove_foreign_key, #remove_index, #rename_column, #rename_index, #rename_table, #schema_migration, #table_exists?, #type_to_sql

Methods included from ActiveRecord::ConnectionAdapters::Spanner::DatabaseStatements

#_has_pk_binding, #append_request_tag_from_query_logs, #append_request_tag_from_query_logs_with_format, #begin_db_transaction, #begin_isolated_db_transaction, #commit_db_transaction, #exec_mutation, #exec_query, #exec_update, #execute, #execute_ddl, #execute_query_or_dml, #internal_exec_query, #internal_execute, #query, #rollback_db_transaction, #sql_for_insert, #transaction, #transaction_isolation_levels, #truncate, #update, #write_query?

Methods included from ActiveRecord::ConnectionAdapters::Spanner::Quoting

#_type_cast, #quote_column_name, #quote_string, #quote_table_name, #quoted_binary

Constructor Details

#initialize(config_or_deprecated_connection, deprecated_logger = nil, deprecated_connection_options = nil, deprecated_config = nil) ⇒ SpannerAdapter

Returns a new instance of SpannerAdapter.



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

def initialize config_or_deprecated_connection, deprecated_logger = nil,
               deprecated_connection_options = nil, deprecated_config = nil
  if config_or_deprecated_connection.is_a? Hash
    @connection = ActiveRecordSpannerAdapter::Connection.new config_or_deprecated_connection
    @connection.connect!
    super config_or_deprecated_connection
    @raw_connection ||= @connection
  else
    @connection = config_or_deprecated_connection
    @connection_options = deprecated_connection_options
    super config_or_deprecated_connection, deprecated_logger, deprecated_config
    @raw_connection ||= config_or_deprecated_connection
  end
  # Spanner does not support unprepared statements
  @prepared_statements = true
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Database

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 101

def self.database_exists? config
  connection = ActiveRecordSpannerAdapter::Connection.new config
  connection.connect!
  true
rescue ActiveRecord::NoDatabaseError
  false
end

Instance Method Details

#active?Boolean

Connection management

Returns:

  • (Boolean)


111
112
113
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 111

def active?
  @connection.active?
end

#arel_visitorObject



203
204
205
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 203

def arel_visitor
  Arel::Visitors::Spanner.new self
end

#build_insert_sql(insert) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 207

def build_insert_sql insert
  if current_spanner_transaction&.isolation == :buffered_mutations
    raise "ActiveRecordSpannerAdapter does not support insert_sql with buffered_mutations transaction."
  end

  values_list, = insert.values_list
  prefix = "INSERT"
  if insert.update_duplicates?
    prefix += " OR UPDATE"
  elsif insert.skip_duplicates?
    prefix += " OR IGNORE"
  end
  "#{prefix} #{insert.into} #{values_list}"
end

#current_spanner_transactionObject



133
134
135
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 133

def current_spanner_transaction
  @connection.current_transaction
end

#disconnect!Object



115
116
117
118
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 115

def disconnect!
  super
  @connection.disconnect!
end

#log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, *args) ⇒ Object

Overwrite the standard log method to be able to translate exceptions.



287
288
289
290
291
292
293
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 287

def log sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, *args
  super
rescue ActiveRecord::StatementInvalid
  raise
rescue StandardError => e
  raise translate_exception_class(e, sql, binds)
end

#max_identifier_lengthObject



91
92
93
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 91

def max_identifier_length
  128
end

#native_database_typesObject



95
96
97
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 95

def native_database_types
  NATIVE_DATABASE_TYPES
end

#next_sequence_value(_sequence_name) ⇒ Object

Generate next sequence number for primary key



195
196
197
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 195

def next_sequence_value _sequence_name
  SecureRandom.uuid.gsub("-", "").hex & 0x7FFFFFFFFFFFFFFF
end

#prefetch_primary_key?(_table_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 182

def prefetch_primary_key? _table_name = nil
  true
end

#reset!Object Also known as: reconnect!



120
121
122
123
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 120

def reset!
  super
  @connection.reset!
end

#return_value_after_insert?(column) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 199

def return_value_after_insert? column
  column.auto_incremented_by_db? || column.primary_key?
end

#spanner_schema_cacheObject



126
127
128
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 126

def spanner_schema_cache
  @spanner_schema_cache ||= SpannerSchemaCache.new self
end

#supports_bulk_alter?Boolean

Supported features

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 139

def supports_bulk_alter?
  true
end

#supports_check_constraints?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 186

def supports_check_constraints?
  true
end

#supports_common_table_expressions?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 143

def supports_common_table_expressions?
  true
end

#supports_explain?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 147

def supports_explain?
  false
end

#supports_foreign_keys?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 151

def supports_foreign_keys?
  true
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 155

def supports_index_sort_order?
  true
end

#supports_insert_on_conflict?Boolean Also known as: supports_insert_on_duplicate_skip?, supports_insert_on_duplicate_update?, supports_insert_conflict_target?

Returns:

  • (Boolean)


159
160
161
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 159

def supports_insert_on_conflict?
  true
end

#supports_insert_returning?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 166

def supports_insert_returning?
  true
end

#supports_multi_insert?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 170

def supports_multi_insert?
  true
end

#supports_optimizer_hints?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 174

def supports_optimizer_hints?
  true
end

#supports_primary_key?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 178

def supports_primary_key?
  true
end

#supports_virtual_columns?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 190

def supports_virtual_columns?
  true
end

#transform(sql) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 278

def transform sql
  if ActiveRecord::VERSION::MAJOR >= 7
    transform_query sql
  else
    sql
  end
end

#translate_exception(exception, message:, sql:, binds:) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/active_record/connection_adapters/spanner_adapter.rb', line 295

def translate_exception exception, message:, sql:, binds:
  if exception.is_a? Google::Cloud::FailedPreconditionError
    case exception.message
    when /.*does not specify a non-null value for these NOT NULL columns.*/,
         /.*must not be NULL.*/
      NotNullViolation.new message, sql: sql, binds: binds
    else
      super
    end
  else
    super
  end
end