Class: Baza::Driver::Sqlite3::Tables

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/driver/sqlite3/tables.rb

Direct Known Subclasses

Baza::Driver::Sqlite3Java::Tables

Constant Summary collapse

CREATE_ALLOWED_KEYS =
[:indexes, :columns].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Tables

Returns a new instance of Tables.



6
7
8
9
10
11
12
# File 'lib/baza/driver/sqlite3/tables.rb', line 6

def initialize(args)
  @args = args
  @db = @args[:db]

  @list_mutex = Monitor.new
  @list = Wref::Map.new
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/baza/driver/sqlite3/tables.rb', line 4

def db
  @db
end

#driverObject (readonly)

Returns the value of attribute driver.



4
5
6
# File 'lib/baza/driver/sqlite3/tables.rb', line 4

def driver
  @driver
end

Instance Method Details

#[](table_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baza/driver/sqlite3/tables.rb', line 14

def [](table_name)
  table_name = table_name.to_s

  ret = @list.get(table_name)
  return ret if ret

  list(name: table_name) do |table|
    return table if table.name == table_name
  end

  raise Baza::Errors::TableNotFound, "Table was not found: #{table_name}."
end

#add_to_list(table) ⇒ Object



73
74
75
76
# File 'lib/baza/driver/sqlite3/tables.rb', line 73

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



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
113
114
115
116
117
118
# File 'lib/baza/driver/sqlite3/tables.rb', line 79

def create(name, data, args = nil)
  data.each_key do |key|
    raise "Invalid key: '#{key}' (#{key.class.name})." unless CREATE_ALLOWED_KEYS.include?(key)
  end

  raise "No columns given" if data.fetch(:columns).empty?

  sql = "CREATE TABLE `#{name}` ("

  first = true
  data.fetch(:columns).each do |col_data|
    sql << ", " unless first
    first = false if first
    sql << @db.columns.data_sql(col_data)
  end

  sql << ")"

  if args && args[:return_sql]
    ret = [sql]
  else
    @db.query(sql)
  end

  if data[:indexes]
    table_obj = self[name]

    if args && args[:return_sql]
      ret += table_obj.create_indexes(data.fetch(:indexes), return_sql: true)
    else
      table_obj.create_indexes(data.fetch(:indexes))
    end
  end

  if args && args[:return_sql]
    return ret
  else
    return nil
  end
end

#exists_in_list?(table) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/baza/driver/sqlite3/tables.rb', line 64

def exists_in_list?(table)
  @list.key?(table.name)
end

#list(args = {}) ⇒ Object



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
59
60
61
62
# File 'lib/baza/driver/sqlite3/tables.rb', line 27

def list(args = {})
  ret = [] unless block_given?

  @list_mutex.synchronize do
    tables_args = {type: "table"}
    tables_args[:name] = args.fetch(:name) if args[:name]

    @db.select("sqlite_master", tables_args, orderby: "name") do |d_tables|
      table_name = d_tables.fetch(:name)
      next if table_name == "sqlite_sequence"

      obj = @list.get(table_name)

      unless obj
        obj = Baza::Driver::Sqlite3::Table.new(
          db: @db,
          data: d_tables,
          tables: self
        )
        @list[table_name] = obj
      end

      if block_given?
        yield obj
      else
        ret << obj
      end
    end
  end

  if block_given?
    nil
  else
    ret
  end
end

#remove_from_list(table) ⇒ Object



68
69
70
71
# File 'lib/baza/driver/sqlite3/tables.rb', line 68

def remove_from_list(table)
  raise "Table doesnt exist: '#{table.name}'." unless @list.key?(table.name)
  @list.delete(table.name)
end