Sha256: f46dfd6c75d1c1e23169aee710ac967b141b36d361432037063cb80d08695bc1

Contents?: true

Size: 1.52 KB

Versions: 34

Compression:

Stored size: 1.52 KB

Contents

module RequestLogAnalyzer::Database::Connection

  def self.from_string(string)
    hash = {}
    if string =~ /^(?:\w+=(?:[^;])*;)*\w+=(?:[^;])*$/
      string.scan(/(\w+)=([^;]*);?/) { |variable, value| hash[variable.to_sym] = value }
    elsif string =~ /^(\w+)\:\/\/(?:(?:([^:]+)(?:\:([^:]+))?\@)?([\w\.-]+)\/)?([\w\:\-\.\/]+)$/
      hash[:adapter], hash[:username], hash[:password], hash[:host], hash[:database] = $1, $2, $3, $4, $5
      hash.delete_if { |k, v| v.nil? }
    end
    return hash.empty? ? nil : hash
  end

  def connect(connection_identifier)
    if connection_identifier.kind_of?(Hash)
      RequestLogAnalyzer::Database::Base.establish_connection(connection_identifier)
    elsif connection_identifier == ':memory:'
      RequestLogAnalyzer::Database::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
    elsif connection_hash = RequestLogAnalyzer::Database::Connection.from_string(connection_identifier)
      RequestLogAnalyzer::Database::Base.establish_connection(connection_hash)
    elsif connection_identifier.kind_of?(String) # Normal SQLite 3 database file
      RequestLogAnalyzer::Database::Base.establish_connection(:adapter => 'sqlite3', :database => connection_identifier)
    elsif connection_identifier.nil?
      nil
    else
      raise "Cannot connect with this connection_identifier: #{connection_identifier.inspect}"
    end
  end

  def disconnect
    RequestLogAnalyzer::Database::Base.remove_connection
  end

  def connection
    RequestLogAnalyzer::Database::Base.connection
  end

end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
request-log-analyzer-1.12.0 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.11.1 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.11.0 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.10.1 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.10.0 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.10 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.9 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.8 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.7 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.6 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.5 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.4 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.3 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.2 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.1 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.9.0 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.8.1 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.8.0 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.7.0 lib/request_log_analyzer/database/connection.rb
request-log-analyzer-1.6.4 lib/request_log_analyzer/database/connection.rb