Class: Baza::Driver::Sqlite3::Tables
- Inherits:
-
Object
- Object
- Baza::Driver::Sqlite3::Tables
show all
- Defined in:
- lib/baza/drivers/sqlite3/tables.rb
Constant Summary
collapse
- CREATE_ALLOWED_KEYS =
[:indexes, :columns]
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/baza/drivers/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
#db ⇒ Object
Returns the value of attribute db.
2
3
4
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 2
def db
@db
end
|
#driver ⇒ Object
Returns the value of attribute driver.
2
3
4
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 2
def driver
@driver
end
|
Instance Method Details
#[](table_name) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 12
def [](table_name)
table_name = table_name.to_sym
if ret = @list.get(table_name)
return ret
end
self.list do |table_obj|
return table_obj if table_obj.name == table_name
end
raise Errno::ENOENT, "Table was not found: #{table_name}."
end
|
#add_to_list(table) ⇒ Object
69
70
71
72
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 69
def add_to_list(table)
raise "Already exists: '#{table.name}'." if @list.key?(table.name) && @list[table.name].__id__ != table.__id__
@list[table.name] = table
end
|
#create(name, data, args = nil) ⇒ Object
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
105
106
107
108
109
110
111
112
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 75
def create(name, data, args = nil)
data.each do |key, val|
raise "Invalid key: '#{key}' (#{key.class.name})." unless CREATE_ALLOWED_KEYS.include?(key)
end
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 && args[:return_sql]
ret = [sql]
else
@db.query(sql)
end
if data.key?(:indexes) && data[:indexes]
table_obj = self[name]
if args && args[:return_sql]
ret += table_obj.create_indexes(data[:indexes], return_sql: true)
else
table_obj.create_indexes(data[:indexes])
end
end
if args && args[:return_sql]
return ret
else
return nil
end
end
|
#exists_in_list?(table) ⇒ Boolean
60
61
62
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 60
def exists_in_list? table
@list.key?(table.name)
end
|
#list ⇒ Object
26
27
28
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
58
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 26
def list
ret = {} unless block_given?
@list_mutex.synchronize do
q_tables = @db.select("sqlite_master", {"type" => "table"}, {orderby: "name"}) do |d_tables|
next if d_tables[:name] == "sqlite_sequence"
tname = d_tables[:name].to_sym
obj = @list.get(tname)
unless obj
obj = Baza::Driver::Sqlite3::Table.new(
db: @db,
data: d_tables,
tables: self
)
@list[tname] = obj
end
if block_given?
yield(obj)
else
ret[tname] = obj
end
end
end
if block_given?
return nil
else
return ret
end
end
|
#remove_from_list(table) ⇒ Object
64
65
66
67
|
# File 'lib/baza/drivers/sqlite3/tables.rb', line 64
def remove_from_list(table)
raise "Table doesnt exist: '#{table.name}'." unless @list.key?(table.name)
@list.delete(table.name)
end
|