Class: Rigrate::Mysql

Inherits:
Driver
  • Object
show all
Defined in:
lib/rigrate/interface/mysql.rb

Instance Attribute Summary collapse

Attributes inherited from Driver

#db

Instance Method Summary collapse

Methods inherited from Driver

#connect, #extract_conn_param, #extract_tbl_from_sql, #method_missing, #to_native_row

Constructor Details

#initialize(uri) ⇒ Mysql

Returns a new instance of Mysql.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rigrate/interface/mysql.rb', line 9

def initialize(uri)
  default_opts = {
    host: '127.0.0.1',
    user: nil,
    passwd: nil,
    db: nil,
    port: 3306,
    socket: nil,
    flag: 0
  }

  extract_from_db_path(uri).each do |k, v|
    default_opts[k.to_sym] = v if default_opts.keys.include? k.to_sym
  end

  @db = ::Mysql.connect(*default_opts.values)
  @transaction_active = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rigrate::Driver

Instance Attribute Details

#transaction_activeObject

Returns the value of attribute transaction_active.



7
8
9
# File 'lib/rigrate/interface/mysql.rb', line 7

def transaction_active
  @transaction_active
end

Instance Method Details

#commitObject



136
137
138
139
140
# File 'lib/rigrate/interface/mysql.rb', line 136

def commit
  @db.commit
  @db.autocommit true
  @transaction_active = false
end

#extract_from_db_path(uri) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rigrate/interface/mysql.rb', line 113

def extract_from_db_path(uri)
  uri = URI.parse(uri)
  args = {}

  args[:host] = uri.host if uri.host
  args[:user] = uri.user if uri.user
  args[:passwd] = uri.password if uri.password
  args[:port] = uri.port if uri.port
  args[:scheme] = uri.scheme if uri.scheme
  args[:db] = uri.path.sub('/','') if uri.path.size > 1

  args
end

#format_sql_args(args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rigrate/interface/mysql.rb', line 95

def format_sql_args(args)
  args.map do |arg|
    if String === arg
      "'#{arg}'"
    elsif DateTime === arg
      arg.strptime('%Y-%m-%d %H:%M:%S')
    else
      arg
    end
  end
end

#get_field_type(num) ⇒ Object



107
108
109
110
111
# File 'lib/rigrate/interface/mysql.rb', line 107

def get_field_type(num)
  ::Mysql::Field.constants.select do |cons|
    cons if ::Mysql::Field.const_get(cons) == num
  end
end

#primary_key(tbl_name) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rigrate/interface/mysql.rb', line 69

def primary_key(tbl_name)
  tbl_name = tbl_name.to_s

  db.list_fields(tbl_name).fetch_fields.select do |field|
    field.is_pri_key?
  end.map(&:name)
end

#rollbackObject



142
143
144
145
146
# File 'lib/rigrate/interface/mysql.rb', line 142

def rollback
  @db.rollback
  @db.autocommit true
  @transaction_active = false
end

#save(resultset) ⇒ Object



46
47
48
49
50
# File 'lib/rigrate/interface/mysql.rb', line 46

def save(resultset)
  resultset.db = self

  resultset.save!
end

#select(sql, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rigrate/interface/mysql.rb', line 28

def select(sql, *args)
  target_tbl_name = extract_tbl_from_sql(sql)

  ResultSet.new.tap do |rs|
    stm = @db.prepare(sql)
    rs.db = self
    rs.target_tbl_name = target_tbl_name
    rs.column_info = statement_fields(stm)
    result = stm.execute(*args)
    rs.rows = []
    while row = result.fetch
      new_row = Row.new(to_rb_row(row))
      yield new_row if block_given?
      rs.rows << new_row
    end
  end
end

#statement_fields(stm) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/rigrate/interface/mysql.rb', line 77

def statement_fields(stm)
  cols = []

  stm..fetch_fields.each do |field|
    cols << Column.new(field.name, get_field_type(field.type))
  end
end

#to_rb_row(mysql_row) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/rigrate/interface/mysql.rb', line 85

def to_rb_row(mysql_row)
  mysql_row.map do |field|
    if ::Mysql::Time === field
      field.to_s
    else
      field
    end
  end
end

#transactionObject



131
132
133
134
# File 'lib/rigrate/interface/mysql.rb', line 131

def transaction
  @db.autocommit false
  @transaction_active = true
end

#transaction_active?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rigrate/interface/mysql.rb', line 127

def transaction_active?
  @transaction_active
end

#update(sql, *args) ⇒ Object Also known as: insert, delete



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rigrate/interface/mysql.rb', line 52

def update(sql, *args)
  begin
    stm = @db.prepare sql
    args.each do |row|
      if Rigrate::Row === row
        row = row.data
      end
      stm.execute(*row)
    end
  rescue Exception => e
    Rigrate.logger.error("execute SQL [#{sql}] ARGS [#{args.size}] -> #{e.backtrace.join('\n')}")
    raise DriverError.new("execute error #{e.message}")
  end
end