Class: Lhm::Connection::DataMapperConnection

Inherits:
Object
  • Object
show all
Includes:
SqlHelper
Defined in:
lib/lhm/connection.rb

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #version_string

Constructor Details

#initialize(adapter) ⇒ DataMapperConnection

Returns a new instance of DataMapperConnection.



18
19
20
21
# File 'lib/lhm/connection.rb', line 18

def initialize(adapter)
  @adapter       = adapter
  @database_name = adapter.options['database'] || adapter.options['path'][1..-1]
end

Instance Method Details

#current_databaseObject



34
35
36
# File 'lib/lhm/connection.rb', line 34

def current_database
  @database_name
end

#destination_create(origin) ⇒ Object



61
62
63
64
65
66
# File 'lib/lhm/connection.rb', line 61

def destination_create(origin)
  original    = %{CREATE TABLE "#{ origin.name }"}
  replacement = %{CREATE TABLE "#{ origin.destination_name }"}

  sql(origin.ddl.gsub(original, replacement))
end

#execute(sql) ⇒ Object



68
69
70
# File 'lib/lhm/connection.rb', line 68

def execute(sql)
  @adapter.execute(sql)
end

#quote_value(value) ⇒ Object



81
82
83
# File 'lib/lhm/connection.rb', line 81

def quote_value(value)
  quoter.quote_value(value)
end

#quoterObject



85
86
87
# File 'lib/lhm/connection.rb', line 85

def quoter
  @quoter ||= Object.new.tap { |o| o.extend(DataObjects::Quoting) }
end

#select_all(sql) ⇒ Object



45
46
47
# File 'lib/lhm/connection.rb', line 45

def select_all(sql)
  @adapter.select(sql).to_a
end

#select_one(sql) ⇒ Object



49
50
51
# File 'lib/lhm/connection.rb', line 49

def select_one(sql)
  select_all(sql).first
end

#select_value(sql) ⇒ Object



57
58
59
# File 'lib/lhm/connection.rb', line 57

def select_value(sql)
  select_one(sql)
end

#select_values(sql) ⇒ Object



53
54
55
# File 'lib/lhm/connection.rb', line 53

def select_values(sql)
  select_all(sql)
end

#show_create(table_name) ⇒ Object



29
30
31
32
# File 'lib/lhm/connection.rb', line 29

def show_create(table_name)
  sql = "show create table `#{ table_name }`"
  select_one(sql).values.last
end

#sql(statements) ⇒ Object



23
24
25
26
27
# File 'lib/lhm/connection.rb', line 23

def sql(statements)
  [statements].flatten.each do |statement|
    execute(tagged(statement))
  end
end

#table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
# File 'lib/lhm/connection.rb', line 72

def table_exists?(table_name)
  !!select_one(%Q{
    select *
      from information_schema.tables
     where table_schema = '#{ @database_name }'
       and table_name = '#{ table_name }'
  })
end

#update(statements) ⇒ Object



38
39
40
41
42
43
# File 'lib/lhm/connection.rb', line 38

def update(statements)
  [statements].flatten.inject(0) do |memo, statement|
    result = @adapter.execute(tagged(statement))
    memo  += result.affected_rows
  end
end