Class: KnjDB_mysql::Tables

Inherits:
Object show all
Defined in:
lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb

Defined Under Namespace

Classes: Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Tables

Returns a new instance of Tables.



7
8
9
10
11
12
13
14
15
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 7

def initialize(args)
  @args = args
  @db = @args[:db]
  @driver = @args[:driver]
  @subtype = @db.opts[:subtype]
  @list_mutex = Mutex.new
  @list = Knj::Wref_map.new
  @list_should_be_reloaded = true
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 4

def db
  @db
end

#driverObject (readonly)

Returns the value of attribute driver.



4
5
6
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 4

def driver
  @driver
end

#list(args = {}) ⇒ Object (readonly)

Returns the value of attribute list.



4
5
6
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 4

def list
  @list
end

#list_should_be_reloadedObject

Returns the value of attribute list_should_be_reloaded.



5
6
7
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 5

def list_should_be_reloaded
  @list_should_be_reloaded
end

Instance Method Details

#[](table_name) ⇒ Object

Returns a table by the given table-name.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 22

def [](table_name)
  table_name = table_name.to_s
  
  begin
    return @list[table_name]
  rescue WeakRef::RefError
    #ignore.
  end
  
  self.list do |table_obj|
    return table_obj if table_obj.name == table_name
  end
  
  raise Knj::Errors::NotFound.new("Table was not found: #{table_name}.")
end

#cleanObject



17
18
19
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 17

def clean
  @list.clean
end

#create(name, data) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 70

def create(name, data)
  raise "No columns was given for '#{name}'." if !data["columns"] or data["columns"].empty?
  
  sql = "CREATE TABLE `#{name}` ("
  
  first = true
  data["columns"].each do |col_data|
    sql << ", " if !first
    first = false if first
    col_data.delete("after") if col_data["after"]
    sql << @db.cols.data_sql(col_data)
  end
  
  sql << ")"
  
  @db.query(sql)
  @list_should_be_reloaded = true
  
  if data["indexes"]
    table_obj = self[name]
    table_obj.create_indexes(data["indexes"])
  end
end