class SqliteClient

Public Class Methods

new(filename, infer_column_types: false) click to toggle source
# File lib/sqlite2mysql/services/sqlite.rb, line 5
def initialize(filename, infer_column_types: false)
  @db = SQLite3::Database.new(filename)
  @infer = infer_column_types
end

Public Instance Methods

build_schema() click to toggle source
# File lib/sqlite2mysql/services/sqlite.rb, line 10
def build_schema
  schema = {}
  tables = @db.execute 'SELECT name FROM sqlite_master WHERE type="table"'

  tables.flatten.each do |t|
    schema[t] = column_formatter(t)
  end

  schema
end
column_formatter(table) click to toggle source
# File lib/sqlite2mysql/services/sqlite.rb, line 25
def column_formatter(table)
  columns = @db.execute("pragma table_info(#{table})")

  formatted_columns = []
  columns.each do |col|
    formatted_columns << { name:    col[1],
                           type:    type_getter(col[2], table, col[1]),
                           notnull: col[3],
                           default: col[4] }
  end
  formatted_columns
end
get_data(table) click to toggle source
# File lib/sqlite2mysql/services/sqlite.rb, line 21
def get_data(table)
  @db.execute("select * from #{table}")
end
select(column, table) click to toggle source
# File lib/sqlite2mysql/services/sqlite.rb, line 55
def select(column, table)
  @db.execute("SELECT #{column} FROM #{table}").flatten.first
end
type_getter(type, table, column) click to toggle source
# File lib/sqlite2mysql/services/sqlite.rb, line 38
def type_getter(type, table, column)
  if @infer
    samples = @db.execute("SELECT #{column} FROM #{table} WHERE #{column} IS NOT NULL AND #{column} != '' ORDER BY RANDOM() LIMIT 100").flatten
    type = TypeInferrer.new(samples, BoundFinder.new(self, table, column)).make_inference
    puts "Inferring type of #{column} as #{type}"
    return type
  else
    if type == '' || type.nil?
      return 'varchar(255)'
    elsif type.start_with?('float')
      return 'float'
    else
      return type
    end
  end
end