Sha256: 7aa7b2f14ef1c1a11a73d3ae2d5fbd8c13608fb6ef19aca4f49c8e2e96cd34fe

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

class Baza::Driver::Mysql::Database < Baza::Database
  def save!
    rename(name) unless name.to_s == name_was
    self
  end

  def drop
    sql = "DROP DATABASE `#{@db.escape_database(name)}`"
    @db.query(sql)
    self
  end

  CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql].freeze
  # Creates a new table by the given name and data.
  def create_table(name, data, args = nil)
    raise "No columns was given for '#{name}'." if !data[:columns] || data[:columns].empty?

    sql = "CREATE"
    sql << " TEMPORARY" if data[:temp]
    sql << " TABLE #{db.sep_table}#{@db.escape_table(name)}#{db.sep_table} ("

    first = true
    data[:columns].each do |col_data|
      sql << ", " unless first
      first = false if first
      col_data.delete(:after) if col_data[:after]
      sql << @db.columns.data_sql(col_data)
    end

    if data[:indexes] && !data[:indexes].empty?
      sql << ", "
      sql << Baza::Driver::Mysql::Table.create_indexes(
        data[:indexes],
        db: @db,
        return_sql: true,
        create: false,
        on_table: false,
        table_name: name
      )
    end

    sql << ")"

    # return [sql] if args && args[:return_sql]

    sql = Baza::Driver::Mysql::Sql::CreateTable.new(
      columns: data.fetch(:columns),
      indexes: data[:indexes],
      name: name,
      temporary: data[:temp]
    ).sql

    return sql if args && args[:return_sql]

    use do
      sql.each do |sql_i|
        @db.query(sql_i)
      end
    end
  end

private

  def rename(new_name)
    new_name = new_name.to_s
    @db.databases.create(name: new_name)

    tables.each do |table|
      @db.query("ALTER TABLE `#{@db.escape_database(name_was)}`.`#{@db.escape_table(table.name)}` RENAME `#{@db.escape_database(name)}`.`#{@db.escape_table(table.name)}`")
    end

    @db.query("DROP DATABASE `#{@db.escape_database(name_was)}`")

    @name = new_name
    @name_was = new_name
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
baza-0.0.35 lib/baza/driver/mysql/database.rb
baza-0.0.34 lib/baza/driver/mysql/database.rb