Class: Sequel::SchemaSharding::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/schema-sharding/connection_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

Returns a new instance of ConnectionManager.



8
9
10
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 8

def initialize
  @connections = {}
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



6
7
8
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 6

def connections
  @connections
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
15
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 12

def [](name)
  config = db_config_for(name)
  @connections[name.to_s] ||= Sequel.postgres(master_config_for(config).merge!(replica_hash_for(config)))
end

#default_dataset_for(table_name) ⇒ Object



34
35
36
37
38
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 34

def default_dataset_for(table_name)
  shard_number = config.logical_shard_configs(table_name).keys.first
  shard_name = config.logical_shard_configs(table_name)[shard_number]
  self[shard_name][:"#{schema_for(table_name, shard_number)}__#{table_name}"]
end

#disconnectObject



21
22
23
24
25
26
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 21

def disconnect
  @connections.each_value do |conn|
    conn.disconnect
  end
  @connections = {}
end

#master(name) ⇒ Object



17
18
19
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 17

def master(name)
  @connections["master_#{name}"] ||= Sequel.postgres(master_config_for(db_config_for(name)))
end

#schema_for(table_name, shard_number) ⇒ Object



28
29
30
31
32
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 28

def schema_for(table_name, shard_number)
  number_of_shards = config.number_of_shards(table_name)
  pattern = config.schema_name(table_name)
  sprintf pattern, shard_number
end