Class: Mysql2psql::MysqlReader

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

Defined Under Namespace

Classes: Field, Table

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @host, @user, @passwd, @db, @port, @sock, @flag = 
    options.mysqlhostname('localhost'), options.mysqlusername, 
    options.mysqlpassword, options.mysqldatabase, 
    options.mysqlport, options.mysqlsocket
  connect
end

Instance Attribute Details

#mysqlObject (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

#connectObject



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

#reconnectObject



156
157
158
159
# File 'lib/mysql2psql/mysql_reader.rb', line 156

def reconnect
  @mysql.close rescue false
  connect
end

#tablesObject



171
172
173
# File 'lib/mysql2psql/mysql_reader.rb', line 171

def tables
  @tables ||= @mysql.list_tables.map {|table| Table.new(self, table)}
end