Class: Mysql2postgres::MysqlReader

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql2postgres/mysql_reader.rb

Defined Under Namespace

Classes: Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MysqlReader

Returns a new instance of MysqlReader.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/mysql2postgres/mysql_reader.rb', line 169

def initialize(options)
  @host = options[:mysql][:hostname]
  @user = options[:mysql][:username]
  @passwd = options[:mysql][:password]
  @db = options[:mysql][:database]
  @port = if options[:mysql][:port]
            options[:mysql][:port] unless options[:mysql][:port].to_s.empty?
          else
            3306
          end
  @sock = options[:mysql][:socket] && !options[:mysql][:socket].empty? ? options[:mysql][:socket] : nil
  @flag = options[:mysql][:flag] && !options[:mysql][:flag].empty? ? options[:mysql][:flag] : nil

  connect
end

Instance Attribute Details

#mysqlObject (readonly)

Returns the value of attribute mysql.



167
168
169
# File 'lib/mysql2postgres/mysql_reader.rb', line 167

def mysql
  @mysql
end

Instance Method Details

#connectObject



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mysql2postgres/mysql_reader.rb', line 185

def connect
  @mysql = ::Mysql.connect @host, @user, @passwd, @db, @port, @sock
  # utf8_unicode_ci :: https://rubydoc.info/gems/ruby-mysql/Mysql/Charset
  @mysql.charset = ::Mysql::Charset.by_number 192
  @mysql.query 'SET NAMES utf8'

  var_info = @mysql.query "SHOW VARIABLES LIKE 'query_cache_type'"
  return if var_info.nil? || var_info.first.nil? || var_info.first[1] == 'OFF'

  @mysql.query 'SET SESSION query_cache_type = OFF'
end

#paginated_read(table, page_size) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mysql2postgres/mysql_reader.rb', line 222

def paginated_read(table, page_size)
  count = table.count_for_pager
  return if count < 1

  statement = @mysql.prepare table.query_for_pager
  counter = 0
  0.upto (count + page_size) / page_size do |i|
    statement.execute(i * page_size, table.id? ? (i + 1) * page_size : page_size)
    while (row = statement.fetch)
      counter += 1
      yield row, counter
    end
  end
  counter
end

#query(*args, &block) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mysql2postgres/mysql_reader.rb', line 205

def query(*args, &block)
  mysql.query(*args, &block)
rescue Mysql::Error => e
  if e.message.match?(/gone away/i)
    reconnect
    retry
  else
    puts "MySQL Query failed '#{args.inspect}' #{e.inspect}"
    puts e.backtrace[0, 5].join("\n")
    []
  end
end

#reconnectObject



197
198
199
200
201
202
203
# File 'lib/mysql2postgres/mysql_reader.rb', line 197

def reconnect
  @mysql.close
rescue StandardError
  warn 'could not close previous mysql connection'
ensure
  connect
end

#tablesObject



218
219
220
# File 'lib/mysql2postgres/mysql_reader.rb', line 218

def tables
  @tables ||= @mysql.query('SHOW TABLES').map { |row| Table.new self, row.first }
end