Class: Purview::Databases::Base

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

Direct Known Subclasses

MySQL, PostgreSQL

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?, #zero?

Methods included from Mixins::Connection

#connect, #connection_opts, #with_new_connection

Constructor Details

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

Returns a new instance of Base.



6
7
8
9
# File 'lib/purview/databases/base.rb', line 6

def initialize(name, opts={})
  @name = name
  @opts = opts
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

Instance Method Details

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/purview/databases/base.rb', line 11

def create_table(table, opts={})
  (table)
  table_opts = extract_table_options(opts)
  table_name = table_name(table, table_opts)
  with_context_logging("`create_table` for: #{table_name}") do
    with_new_connection do |connection|
      connection.execute(
        create_table_sql(
          table_name,
          table,
          table_opts
        )
      )
      if table_opts[:create_indices]
        table.indexed_columns.each do |columns|
          create_index(
            connection,
            table,
            columns,
            :table => { :name => table_name }
          )
        end
      end
    end
    table_name
  end
end

#create_temporary_table(connection, table, opts = {}) ⇒ Object



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/purview/databases/base.rb', line 39

def create_temporary_table(connection, table, opts={})
  table_opts = extract_table_options(opts)
  table_name = table_name(table, table_opts)
  with_context_logging("`create_temporary_table` for: #{table_name}") do
    connection.execute(
      create_temporary_table_sql(
        table_name,
        table,
        table_opts
      )
    )
    if table_opts[:create_indices]
      table.indexed_columns.each do |columns|
        create_index(
          connection,
          table,
          columns,
          :table => { :name => table_name }
        )
      end
    end
    table_name
  end
end

#disable_table(table) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/purview/databases/base.rb', line 64

def disable_table(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::CouldNotDisable.new(table) \
        if zero?(rows_affected)
    end
    table_name
  end
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/purview/databases/base.rb', line 77

def drop_table(table, opts={})
  (table)
  table_opts = extract_table_options(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
    table_name
  end
end

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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/purview/databases/base.rb', line 95

def enable_table(table, timestamp=Time.now.utc)
  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::CouldNotEnable.new(table) \
        if zero?(rows_affected)
    end
    table_name
  end
end

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



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/purview/databases/base.rb', line 108

def initialize_table(table, starting_timestamp=Time.now.utc)
  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, starting_timestamp)).rows_affected
      raise Purview::Exceptions::CouldNotInitialize.new(table) \
        if zero?(rows_affected)
    end
    table_name
  end
end

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



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/purview/databases/base.rb', line 121

def lock_table(table, timestamp=Time.now.utc)
  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::CouldNotLock.new(table) \
        if zero?(rows_affected)
    end
    table_name
  end
end

#syncObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/purview/databases/base.rb', line 134

def sync
  with_context_logging('`sync`') do
    with_new_connection do |connection|
      with_transaction(connection) do |timestamp|
        with_next_table(connection, timestamp) do |table|
          with_next_window(
            connection,
            table,
            timestamp
          ) do |window|
            with_table_locked(table, timestamp) do
              table.sync(connection, window)
            end
          end
        end
      end
    end
  end
end

#unlock_table(table) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/purview/databases/base.rb', line 154

def unlock_table(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::CouldNotUnlock.new(table) \
        if zero?(rows_affected)
    end
    table_name
  end
end