Module: Ronin::DB::CLI::URIMethods Private

Included in:
Commands::Add, DatabaseOptions
Defined in:
lib/ronin/db/cli/uri_methods.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Mixin which adds methods for parsing database URIs (ex: sqlite3:path/to/db.sqlite3).

Constant Summary collapse

ADAPTER_ALIASES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Common database adapter names and their ActiveRecord equivalents.

{
  'sqlite'   => 'sqlite3',
  'mysql'    => 'mysql2',
  'pg'       => 'postgresql',
  'psql'     => 'postgresql',
  'postgres' => 'postgresql'
}

Instance Method Summary collapse

Instance Method Details

#normalize_adapter(adapter) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalizes a database adapter name.

Parameters:

  • adapter (String)

    The adapter name to normalize.

Returns:

  • (String)

    The normalized adapter.



50
51
52
# File 'lib/ronin/db/cli/uri_methods.rb', line 50

def normalize_adapter(adapter)
  ADAPTER_ALIASES.fetch(adapter,adapter)
end

#normalize_sqlite3_path(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalizes a sqlite3 database path.

Parameters:

  • path (String)

    The path to the sqlite3 database.

Returns:

  • (String)

    The expanded path or ":memory:" if the path was ":memory:".



63
64
65
66
67
# File 'lib/ronin/db/cli/uri_methods.rb', line 63

def normalize_sqlite3_path(path)
  if path == ':memory:' then path
  else                       File.expand_path(path)
  end
end

#parse_uri(uri) ⇒ Hash{Symbol => String,Integer,nil}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses a database URI.

Parameters:

  • uri (String)

    The database URI to parse.

Returns:

  • (Hash{Symbol => String,Integer,nil})

    The database configuration Hash.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ronin/db/cli/uri_methods.rb', line 78

def parse_uri(uri)
  if (match = uri.match(/\Asqlite(?:3)?:(.+)\z/))
    {
      adapter: 'sqlite3',
      database: normalize_sqlite3_path(match[1])
    }
  else
    uri     = URI.parse(uri)
    adapter = normalize_adapter(uri.scheme)

    hash = {adapter: adapter}

    hash[:host]     = uri.host if uri.host
    hash[:port]     = uri.port if uri.port
    hash[:username] = uri.user if uri.user
    hash[:password] = uri.password if uri.password
    hash[:database] = uri.path[1..]

    return hash
  end
end