Class: Sip::DBBase

Inherits:
Object
  • Object
show all
Defined in:
lib/sip/databases/dbbase.rb

Direct Known Subclasses

MySQLSipper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, sipper) ⇒ DBBase

Returns a new instance of DBBase.



14
15
16
17
# File 'lib/sip/databases/dbbase.rb', line 14

def initialize(args, sipper)
  @args = args
  @sipper = sipper
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/sip/databases/dbbase.rb', line 3

def args
  @args
end

Class Method Details

.make_interface(type, args, sipper) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/sip/databases/dbbase.rb', line 5

def self.make_interface(type, args, sipper)
  if type == 'mysql'
    require 'sip/databases/mysql'
    MySQLSipper.new args, sipper
  else
    raise UnsupportedDatabaseType, "DB type #{type.to_s} not supported."
  end
end

Instance Method Details

#columns(table) ⇒ Object



37
38
39
40
41
# File 'lib/sip/databases/dbbase.rb', line 37

def columns(table)
  query("DESCRIBE #{table}").map { |col|
    col.slice(0,2)
  }
end

#generate_command(tableconf, first = nil, last = nil) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/sip/databases/dbbase.rb', line 47

def generate_command(tableconf, first=nil, last=nil)
  select = "SELECT #{columns(tableconf['tablename']).map { |k,v| k }.join(',')} FROM #{tableconf['tablename']}"
  wheres = []
  wheres << "#{tableconf['incremental_index']} >= #{first}" if not first.nil?
  wheres << "#{tableconf['incremental_index']} <= #{last}" if not last.nil?
  select += " WHERE #{wheres.join(" AND ")}" if wheres.length > 0
  cmd_line_execute_string select
end

#get_column_max(tablename, field) ⇒ Object



27
28
29
# File 'lib/sip/databases/dbbase.rb', line 27

def get_column_max(tablename, field)
  query("SELECT max(#{field}) FROM #{tablename}").first.first.to_i
end

#hive_columns(table) ⇒ Object



31
32
33
34
35
# File 'lib/sip/databases/dbbase.rb', line 31

def hive_columns(table)
  columns(table).map { |name, type|
    [name, convert_to_hive_type(type)]
  }
end

#order_column_list(table, cols) ⇒ Object



43
44
45
# File 'lib/sip/databases/dbbase.rb', line 43

def order_column_list(table, cols)
  columns(table).map { |k,v| k }.select { |c| cols.include? c }
end

#rowcount(table) ⇒ Object



23
24
25
# File 'lib/sip/databases/dbbase.rb', line 23

def rowcount(table)
  query('SELECT count(1) FROM #{table}').first.first.to_i
end

#tablesObject



19
20
21
# File 'lib/sip/databases/dbbase.rb', line 19

def tables
  query('SHOW tables')
end