Class: Mysql2psql::MysqlReader
- Inherits:
-
Object
- Object
- Mysql2psql::MysqlReader
- Defined in:
- lib/mysql2psql/mysql_reader.rb
Defined Under Namespace
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
- #reconnect ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(options) ⇒ MysqlReader
Returns a new instance of MysqlReader.
161 162 163 164 165 166 167 |
# File 'lib/mysql2psql/mysql_reader.rb', line 161 def initialize() @host, @user, @passwd, @db, @port, @sock, @flag = .mysqlhostname('localhost'), .mysqlusername, .mysqlpassword, .mysqldatabase, .mysqlport, .mysqlsocket connect end |
Instance Attribute Details
#mysql ⇒ Object (readonly)
Returns the value of attribute mysql.
169 170 171 |
# File 'lib/mysql2psql/mysql_reader.rb', line 169 def mysql @mysql end |
Instance Method Details
#connect ⇒ Object
150 151 152 153 154 |
# File 'lib/mysql2psql/mysql_reader.rb', line 150 def connect @mysql = ::Mysql.connect(@host, @user, @passwd, @db, @port, @sock, @flag) @mysql.query("SET NAMES utf8") @mysql.query("SET SESSION query_cache_type = OFF") end |
#paginated_read(table, page_size) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mysql2psql/mysql_reader.rb', line 175 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.has_id? ? (i+1)*page_size : page_size) while row = statement.fetch counter += 1 yield(row, counter) end end counter end |
#reconnect ⇒ Object
156 157 158 159 |
# File 'lib/mysql2psql/mysql_reader.rb', line 156 def reconnect @mysql.close rescue false connect end |