Class: KnjDB_sqlite3::Tables

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_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.



4
5
6
7
8
9
10
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 4

def initialize(args)
  @args = args
  @db = @args[:db]
  
  @list_mutex = Mutex.new
  @list = Wref_map.new
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 2

def db
  @db
end

#driverObject (readonly)

Returns the value of attribute driver.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 2

def driver
  @driver
end

Instance Method Details

#[](table_name) ⇒ Object

Raises:

  • (Errno::ENOENT)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 12

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

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



59
60
61
62
63
64
65
66
67
68
69
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/sqlite3/knjdb_sqlite3_tables.rb', line 59

def create(name, data, args = nil)
  sql = "CREATE TABLE `#{name}` ("
  
  first = true
  data["columns"].each do |col_data|
    sql << ", " if !first
    first = false if first
    sql << @db.cols.data_sql(col_data)
  end
  
  sql << ")"
  
  if args and args[:return_sql]
    ret = [sql]
  else
    @db.query(sql)
  end
  
  if data.key?("indexes") and data["indexes"]
    table_obj = self[name]
    
    if args and args[:return_sql]
      ret += table_obj.create_indexes(data["indexes"], :return_sql => true)
    else
      table_obj.create_indexes(data["indexes"])
    end
  end
  
  if args and args[:return_sql]
    return ret
  else
    return nil
  end
end

#listObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 29

def list
  ret = {} unless block_given?
  
  @list_mutex.synchronize do
    q_tables = @db.select("sqlite_master", {"type" => "table"}, {"orderby" => "name"}) do |d_tables|
      obj = @list.get!(d_tables[:name])
      
      if !obj
        obj = KnjDB_sqlite3::Tables::Table.new(
          :db => @db,
          :data => d_tables
        )
        @list[d_tables[:name]] = obj
      end
      
      if block_given?
        yield(obj)
      else
        ret[d_tables[:name]] = obj
      end
    end
  end
  
  if block_given?
    return nil
  else
    return ret
  end
end