Class: Mongify::Database::SqlConnection

Inherits:
BaseConnection show all
Defined in:
lib/mongify/database/sql_connection.rb

Overview

Sql connection configuration

Basic format should look something like this:

sql_connection do
  adapter   "mysql"
  host      "localhost"
  username  "root"
  password  "passw0rd"
  database  "my_database"
end

Possible attributes:

adapter
host
database
username
password
port
encoding
socket

Constant Summary collapse

REQUIRED_FIELDS =

List of required fields to bulid a valid sql connection

%w{host adapter database}

Constants inherited from BaseConnection

BaseConnection::AVAILABLE_FIELDS

Instance Method Summary collapse

Methods inherited from BaseConnection

#method_missing, #respond_to?, #to_hash

Constructor Details

#initialize(options = nil) ⇒ SqlConnection

Returns a new instance of SqlConnection.



32
33
34
35
# File 'lib/mongify/database/sql_connection.rb', line 32

def initialize(options=nil)
  @prefixed_db = false
  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongify::Database::BaseConnection

Instance Method Details

#columns_for(table_name) ⇒ Object

Returns all the columns for a given table



73
74
75
# File 'lib/mongify/database/sql_connection.rb', line 73

def columns_for(table_name)
  self.connection.columns(table_name)
end

#connectionObject

Returns the active_record connection



61
62
63
64
# File 'lib/mongify/database/sql_connection.rb', line 61

def connection
  return nil unless has_connection?
  ActiveRecord::Base.connection
end

#has_connection?Boolean

Returns true or false depending if the connction actually talks to the database server.

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/mongify/database/sql_connection.rb', line 54

def has_connection?
  setup_connection_adapter
  connection.send(:connect) if ActiveRecord::Base.connection.respond_to?(:connect)
  true
end

#select_rows(table_name) ⇒ Object

Returns an array with hash values of all the records in a given table



78
79
80
# File 'lib/mongify/database/sql_connection.rb', line 78

def select_rows(table_name)
  self.connection.select_all("SELECT * FROM #{table_name}")
end

#setup_connection_adapterObject

Setups up an active_record connection



38
39
40
# File 'lib/mongify/database/sql_connection.rb', line 38

def setup_connection_adapter
  ActiveRecord::Base.establish_connection(self.to_hash)
end

#tablesObject

Returns all the tables in the database server



67
68
69
70
# File 'lib/mongify/database/sql_connection.rb', line 67

def tables
  return nil unless has_connection?
  self.connection.tables
end

#valid?Boolean

Returns true or false depending if the record is valid

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/mongify/database/sql_connection.rb', line 43

def valid?
  return false unless @adapter
  if sqlite_adapter?
    return true if @database
  else
    return super
  end
  false
end