Class: Mysql2postgres::MysqlReader
- Inherits:
-
Object
- Object
- Mysql2postgres::MysqlReader
- Defined in:
- lib/mysql2postgres/mysql_reader.rb
Defined Under Namespace
Classes: Table
Instance Attribute Summary collapse
-
#mysql ⇒ Object
readonly
Returns the value of attribute mysql.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(options) ⇒ MysqlReader
constructor
A new instance of MysqlReader.
- #paginated_read(table, page_size) ⇒ Object
- #query(*args, &block) ⇒ Object
- #reconnect ⇒ Object
- #tables ⇒ Object
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() @host = [:mysql][:hostname] @user = [:mysql][:username] @passwd = [:mysql][:password] @db = [:mysql][:database] @port = if [:mysql][:port] [:mysql][:port] unless [:mysql][:port].to_s.empty? else 3306 end @sock = [:mysql][:socket] && ![:mysql][:socket].empty? ? [:mysql][:socket] : nil @flag = [:mysql][:flag] && ![:mysql][:flag].empty? ? [:mysql][:flag] : nil connect end |
Instance Attribute Details
#mysql ⇒ Object (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
#connect ⇒ Object
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..match?(/gone away/i) reconnect retry else puts "MySQL Query failed '#{args.inspect}' #{e.inspect}" puts e.backtrace[0, 5].join("\n") [] end end |
#reconnect ⇒ Object
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 |