Sha256: f9d36c04da904248b453963a54c6b890a36a7e70d87933254bcf707c29764edf

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'sqlite3'
require 'time'

class SqliteClient
  def initialize(filename, infer_column_types: false)
    @db = SQLite3::Database.new(filename)
    @infer = infer_column_types
  end

  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

  def get_data(table)
    @db.execute("select * from #{table}")
  end

  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

  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

  def select(column, table)
    @db.execute("SELECT #{column} FROM #{table}").flatten.first
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sqlite2mysql-0.2.0 lib/sqlite2mysql/services/sqlite.rb