Class: Baza::Driver::Mysql::Database

Inherits:
Baza::Database show all
Defined in:
lib/baza/driver/mysql/database.rb

Constant Summary collapse

CREATE_ALLOWED_KEYS =
[:columns, :indexes, :temp, :return_sql].freeze

Instance Attribute Summary

Attributes inherited from Baza::Database

#db, #driver, #name, #name_was

Instance Method Summary collapse

Methods inherited from Baza::Database

#initialize, #table, #table_exists?, #tables, #to_param, #use

Methods included from Baza::DatabaseModelFunctionality

#model_name, #to_model

Constructor Details

This class inherits a constructor from Baza::Database

Instance Method Details

#create_table(name, data, args = nil) ⇒ Object

Creates a new table by the given name and data.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/baza/driver/mysql/database.rb', line 15

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]

  use { @db.query(sql) }
end

#dropObject



7
8
9
10
11
# File 'lib/baza/driver/mysql/database.rb', line 7

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

#save!Object



2
3
4
5
# File 'lib/baza/driver/mysql/database.rb', line 2

def save!
  rename(name) unless name.to_s == name_was
  self
end