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
batch_size

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, BaseConnection::STRING_FIELDS

Instance Method Summary collapse

Methods inherited from BaseConnection

#method_missing, #respond_to?, #to_hash

Constructor Details

#initialize(options = {}) ⇒ SqlConnection

Returns a new instance of SqlConnection.



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

def initialize(options={})
  options['batch_size'] ||= 10000
  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



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

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

#connectionObject

Returns the active_record connection



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

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

#count(table_name, where = nil) ⇒ Object



111
112
113
114
115
# File 'lib/mongify/database/sql_connection.rb', line 111

def count(table_name, where = nil)
  q = "SELECT COUNT(*) FROM #{table_name}"
  q = "#{q} WHERE #{where}" if where
  self.connection.select_value(q).to_i
end

#execute(query) ⇒ Object



117
118
119
# File 'lib/mongify/database/sql_connection.rb', line 117

def execute(query)
  self.connection.execute(query)
end

#has_connection?Boolean

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

Returns:

  • (Boolean)


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

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

#select_by_query(query) ⇒ Object

Returns an array with hash values of the records in a given table specified by a query



107
108
109
# File 'lib/mongify/database/sql_connection.rb', line 107

def select_by_query(query)
  self.connection.select_all(query)
end

#select_paged_rows(table_name, batch_size, page) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mongify/database/sql_connection.rb', line 91

def select_paged_rows(table_name, batch_size, page)
  if adapter == "sqlserver"
    offset = (page - 1) * batch_size
    return connection.select_all(
      "SELECT * FROM
                  (
                      SELECT TOP #{offset+batch_size} *, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rnum
                      FROM #{table_name}
                  ) #{table_name}
                  WHERE rnum > #{offset}"
    )
  end
  connection.select_all("SELECT * FROM #{table_name} LIMIT #{batch_size} OFFSET #{(page - 1) * batch_size}")
end

#select_rows(table_name, &block) ⇒ Object

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



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mongify/database/sql_connection.rb', line 79

def select_rows(table_name, &block)
  return self.connection.select_all("SELECT * FROM #{table_name}") unless block_given?

  row_count = count(table_name);
  pages = (row_count.to_f/batch_size).ceil
  (1..pages).each do |page|
    rows = select_paged_rows(table_name, batch_size, page)
    yield rows, page, pages
  end

end

#setup_connection_adapterObject

Setups up an active_record connection



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

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

#tablesObject

Returns all the tables in the database server



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

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)


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

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