Class: Purview::Databases::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::Connection, Mixins::Dialect, Mixins::Helpers, Mixins::Logger
Defined in:
lib/purview/databases/base.rb

Direct Known Subclasses

MSSQL, MySQL, PostgreSQL, SQLite

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Logger

#logger, #logger_opts, #logger_type, #with_context_logging

Methods included from Mixins::Helpers

#blank?, #coalesced, #filter_blank_values, #filter_nil_values, #present?, #timestamp, #with_timestamp, #zero?

Methods included from Mixins::Dialect

#dialect, #false_value, #null_value, #quoted, #sanitized, #true_value

Methods included from Mixins::Connection

#connect, #connection_opts, #with_new_connection, #with_new_or_existing_connection, #with_transaction

Constructor Details

#initialize(name, opts = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
# File 'lib/purview/databases/base.rb', line 6

def initialize(name, opts={})
  @name = name.to_sym
  @opts = opts
  @tables = Set.new.tap do |result|
    (default_tables + tables_opt).each do |table|
      table.database = self if result.add?(table)
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/purview/databases/base.rb', line 4

def name
  @name
end

#tablesObject (readonly)

Returns the value of attribute tables.



4
5
6
# File 'lib/purview/databases/base.rb', line 4

def tables
  @tables
end

Instance Method Details

#baseline_table(table, timestamp = Time.now) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/purview/databases/base.rb', line 16

def baseline_table(table, timestamp=Time.now)
  ensure_table_valid_for_database(table)
  raise Purview::Exceptions::CouldNotBaselineTable.new(table) \
    unless table_initialized?(table)
  table_name = table_name(table)
  with_context_logging("`baseline_table` for: #{table_name}") do
    starting_timestamp = timestamp
    with_table_locked(table, starting_timestamp) do
      loop do
        last_window = sync_table_without_lock(table, timestamp)
        break if last_window.max > starting_timestamp
      end
    end
  end
  table_name
end

#create_index(index, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/purview/databases/base.rb', line 33

def create_index(index, opts={})
  ensure_index_valid_for_database(index)
  table_opts = extract_table_opts(opts)
  table_name = table_name(index.table, table_opts)
  index_opts = extract_index_opts(opts)
  index_name = index_name(
    table_name,
    index,
    index_opts
  )
  with_context_logging("`create_index` for: #{index_name}") do
    with_new_or_existing_connection(opts) do |connection|
      connection.execute(
        create_index_sql(
          table_name,
          index_name,
          index,
          index_opts
        )
      )
    end
  end
  index_name
end

#create_table(table, opts = {}) ⇒ Object



58
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
# File 'lib/purview/databases/base.rb', line 58

def create_table(table, opts={})
  ensure_table_valid_for_database(table)
  (table)
  table_opts = extract_table_opts(opts)
  table_name = table_name(table, table_opts)
  with_context_logging("`create_table` for: #{table_name}") do
    with_new_or_existing_connection(opts) do |connection|
      connection.execute(
        send(
          create_table_sql_method_name(table, table_opts),
          table_name,
          table,
          table_opts
        )
      )
      if table_opts[:create_indices]
        table.indices.each do |index|
          create_index(
            index,
            :connection => connection,
            :table => { :name => table_name }
          )
        end
      end
    end
  end
  table_name
end

#disable_table(table, timestamp = Time.now) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/purview/databases/base.rb', line 87

def disable_table(table, timestamp=Time.now)
  ensure_table_valid_for_database(table)
  table_name = table_name(table)
  with_context_logging("`disable_table` for: #{table_name}") do
    with_new_connection do |connection|
      rows_affected = \
        connection.execute(disable_table_sql(table)).rows_affected
      raise Purview::Exceptions::CouldNotDisableTable.new(table) \
        if zero?(rows_affected)
    end
  end
  table_name
end

#drop_index(index, opts = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/purview/databases/base.rb', line 101

def drop_index(index, opts={})
  ensure_index_valid_for_database(index)
  table_opts = extract_table_opts(opts)
  table_name = table_name(index.table, table_opts)
  index_opts = extract_index_opts(opts)
  index_name = index_name(
    table_name,
    index,
    index_opts
  )
  with_context_logging("`drop_index` for: #{index_name}") do
    with_new_or_existing_connection(opts) do |connection|
      connection.execute(
        drop_index_sql(
          table_name,
          index_name,
          index,
          index_opts
        )
      )
    end
  end
  index_name
end

#drop_table(table, opts = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/purview/databases/base.rb', line 126

def drop_table(table, opts={})
  ensure_table_valid_for_database(table)
  (table)
  table_opts = extract_table_opts(opts)
  table_name = table_name(table, table_opts)
  with_context_logging("`drop_table` for: #{table_name}") do
    with_new_connection do |connection|
      connection.execute(
        drop_table_sql(
          table_name,
          table,
          table_opts
        )
      )
    end
  end
  table_name
end

#enable_table(table, timestamp = Time.now) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/purview/databases/base.rb', line 145

def enable_table(table, timestamp=Time.now)
  ensure_table_valid_for_database(table)
  table_name = table_name(table)
  with_context_logging("`enable_table` for: #{table_name}") do
    with_new_connection do |connection|
      rows_affected = \
        connection.execute(enable_table_sql(table, timestamp)).rows_affected
      raise Purview::Exceptions::CouldNotEnableTable.new(table) \
        if zero?(rows_affected)
    end
  end
  table_name
end

#initialize_table(table, timestamp = Time.now) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/purview/databases/base.rb', line 159

def initialize_table(table, timestamp=Time.now)
  ensure_table_valid_for_database(table)
  table_name = table_name(table)
  with_context_logging("`initialize_table` for: #{table_name}") do
    with_new_connection do |connection|
      rows_affected = \
        connection.execute(initialize_table_sql(table, timestamp)).rows_affected
      raise Purview::Exceptions::CouldNotInitializeTable.new(table) \
        if zero?(rows_affected)
    end
  end
  table_name
end

#lock_table(table, timestamp = Time.now) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/purview/databases/base.rb', line 173

def lock_table(table, timestamp=Time.now)
  ensure_table_valid_for_database(table)
  table_name = table_name(table)
  with_context_logging("`lock_table` for: #{table_name}") do
    with_new_connection do |connection|
      rows_affected = \
        connection.execute(lock_table_sql(table, timestamp)).rows_affected
      raise Purview::Exceptions::CouldNotLockTable.new(table) \
        if zero?(rows_affected)
    end
  end
  table_name
end

#syncObject



187
188
189
190
191
192
193
194
195
# File 'lib/purview/databases/base.rb', line 187

def sync
  with_context_logging('`sync`') do
    with_timestamp do |timestamp|
      with_next_table(timestamp) do |table|
        sync_table_with_lock(table, timestamp)
      end
    end
  end
end

#sync_table(table, timestamp = Time.now) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/purview/databases/base.rb', line 197

def sync_table(table, timestamp=Time.now)
  ensure_table_valid_for_database(table)
  raise Purview::Exceptions::CouldNotSyncTable.new(table) \
    unless table_initialized?(table) && table_enabled?(table)
  table_name = table_name(table)
  with_context_logging("`sync_table` for: #{table_name}") do
    sync_table_with_lock(table, timestamp)
  end
  table_name
end

#table_metadata(table) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/purview/databases/base.rb', line 208

def (table)
  ensure_table_valid_for_database(table)
   = nil
  table_name = table_name(table)
  with_context_logging("`table_metadata` for: #{table_name}") do
    with_new_connection do |connection|
       = Purview::Structs::TableMetadata.new(
        .columns.reduce({}) do |memo, column|
          memo[column.name] = (
            connection,
            table,
            column
          )
          memo
        end
      )
    end
  end
  
end

#unlock_table(table) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/purview/databases/base.rb', line 229

def unlock_table(table)
  ensure_table_valid_for_database(table)
  table_name = table_name(table)
  with_context_logging("`unlock_table` for: #{table_name}") do
    with_new_connection do |connection|
      rows_affected = \
        connection.execute(unlock_table_sql(table)).rows_affected
      raise Purview::Exceptions::CouldNotUnlockTable.new(table) \
        if zero?(rows_affected)
    end
  end
  table_name
end