Class: KnjDB_mysql::Tables

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

Overview

This class handels various MySQL-table-specific behaviour.

Defined Under Namespace

Classes: Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Tables

Constructor. This should not be called manually.



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

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

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

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

Yields the tables of the current database.



40
41
42
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 40

def list
  @list
end

Instance Method Details

#[](table_name) ⇒ Object

Returns a table by the given table-name.

Raises:

  • (Errno::ENOENT)


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

def [](table_name)
  table_name = table_name.to_s
  
  begin
    return @list[table_name]
  rescue Wref::Recycled
    #ignore.
  end
  
  self.list(:name => table_name) do |table_obj|
    return table_obj if table_obj.name == table_name
  end
  
  raise Errno::ENOENT, "Table was not found: #{table_name}."
end

#cleanObject

Cleans the wref-map.



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

def clean
  @list.clean
end

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

Creates a new table by the given name and data.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb', line 74

def create(name, data, args = nil)
  raise "No columns was given for '#{name}'." if !data["columns"] or data["columns"].empty?
  
  sql = "CREATE"
  sql << " TEMPORARY" if data["temp"]
  sql << " 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
  
  if data["indexes"] and !data["indexes"].empty?
    sql << ", "
    sql << KnjDB_mysql::Tables::Table.create_indexes(data["indexes"], {
      :db => @db,
      :return_sql => true,
      :create => false,
      :on_table => false,
      :table_name => name
    })
  end
  
  sql << ")"
  
  return [sql] if args and args[:return_sql]
  @db.query(sql)
end