Class: ActiveRecord::ConnectionAdapters::SimpleDBAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/simpledb_adapter/adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'SimpleDB'.freeze
NIL_REPRESENTATION =
"Aws::Nil".freeze
@@collections =
{}
@@ccn =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, aws_key, aws_secret, domain_name, connection_parameters, config) ⇒ SimpleDBAdapter

Returns a new instance of SimpleDBAdapter.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 91

def initialize(connection, logger, aws_key, aws_secret, domain_name, connection_parameters, config)
  super(connection, logger)
  @config = config
  @batch_started = false
  @domain_name = domain_name
  @connection_parameters = [
      aws_key,
      aws_secret,
      connection_parameters.merge(:nil_representation => nil_representation)
  ]
  connect
end

Instance Attribute Details

#batch_startedObject (readonly)

Returns the value of attribute batch_started.



89
90
91
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 89

def batch_started
  @batch_started
end

#domain_nameObject (readonly)



88
89
90
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 88

def domain_name
  @domain_name
end

Class Method Details

.set_collection_columns(table_name, columns_definition) ⇒ Object



7
8
9
10
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 7

def self.set_collection_columns table_name, columns_definition
  @@collections[table_name] = columns_definition
  @@ccn[table_name] = columns_definition.collection_column_name
end

Instance Method Details

#adapter_nameObject



22
23
24
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 22

def adapter_name
  ADAPTER_NAME
end

#begin_batchObject

BATCHES ==========

Raises:

  • (ActiveRecord::ActiveRecordError)


54
55
56
57
58
59
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 54

def begin_batch
  raise ActiveRecord::ActiveRecordError.new("Batch already started. Finish it before start new batch") \
    if batch_started

  @batch_started = true
end

#clear_batch(type = nil) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 78

def clear_batch type = nil
  if type.nil?
    batch_pool.clear
    @batch_started = false
  else
    batch_pool[type].clear
  end
end

#collection_column_name(table_name) ⇒ Object



16
17
18
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 16

def collection_column_name table_name
  @@ccn[table_name]
end

#columns(table_name, name = nil) ⇒ Object



112
113
114
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 112

def columns table_name, name = nil
  @@collections[table_name].columns
end

#columns_definition(table_name) ⇒ Object



12
13
14
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 12

def columns_definition table_name
  @@collections[table_name]
end

#commit_batch(type = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 61

def commit_batch type = nil
  count = batch_pool.inject(0) {|sum, (key, value)| sum += value.count }
  clear_batch and return unless count

  log({:count => count }.inspect, "SimpleDB Batch Operation") do
    pool = batch_pool[:update] || []
    @connection.batch_put_attributes(domain_name, pool) \
      if pool.any? && (type.nil? || type == :update)

    pool = batch_pool[:delete] || []
    @connection.batch_delete_attributes(domain_name, pool) \
      if pool.any? && (type.nil? || type == :delete)

    clear_batch type
  end
end

#connectObject



104
105
106
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 104

def connect
  @connection = Aws::SdbInterface.new *@connection_parameters
end

#create_domain(domain_name) ⇒ Object



220
221
222
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 220

def create_domain domain_name
  @connection.create_domain domain_name
end

#delete_domain(domain_name) ⇒ Object



224
225
226
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 224

def delete_domain domain_name
  @connection.delete_domain domain_name
end

#delete_sql(sql, name = nil) ⇒ Object

Executes the delete statement and returns the number of rows affected.



216
217
218
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 216

def delete_sql(sql, name = nil)
  update_sql(sql, name)
end

#execute(sql, name = nil, skip_logging = false) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 120

def execute sql, name = nil, skip_logging = false
  log_title = "SimpleDB (#{sql[:action]})"
  log_title += " *BATCHED*" if batch_started
  log sql.inspect, log_title do
    case sql[:action]
    when :insert
      item_name = get_id sql[:attrs]
      item_name = sql[:attrs][:id] = generate_id unless item_name
      if batch_started
        add_to_batch :update, item_name, sql[:attrs], true
      else
        @connection.put_attributes domain_name, item_name, sql[:attrs], true
      end
      @last_insert_id = item_name
    when :update
      item_name = get_id sql[:wheres], true
      if batch_started
        add_to_batch :update, item_name, sql[:attrs], true
      else
        @connection.put_attributes domain_name, item_name, sql[:attrs], true, sql[:wheres]
      end
    when :delete
      item_name = get_id sql[:wheres], true
      if batch_started
        add_to_batch :delete, item_name
      else
        @connection.delete_attributes domain_name, item_name, nil, sql[:wheres]
      end
    else
      raise "Unsupported action: #{sql[:action].inspect}"
    end
  end
end

#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object Also known as: create



154
155
156
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 154

def insert_sql sql, name = nil, pk = nil, id_value = nil, sequence_name = nil
  super || @last_insert_id
end

#list_domainsObject



228
229
230
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 228

def list_domains
  @connection.list_domains[:domains]
end

#nil_representationObject



28
29
30
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 28

def nil_representation
  NIL_REPRESENTATION
end

#primary_key(_) ⇒ Object



116
117
118
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 116

def primary_key _
  'id'
end

#quote(value, column = nil) ⇒ Object

QUOTING =====================


37
38
39
40
41
42
43
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 37

def quote(value, column = nil)
  if value.nil?
    quote(nil_representation, column)
  else
    super
  end
end

#quote_column_name(column_name) ⇒ Object



45
46
47
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 45

def quote_column_name(column_name)
  "`#{column_name}`"
end

#quote_table_name(table_name) ⇒ Object



49
50
51
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 49

def quote_table_name(table_name)
  table_name
end

#select(sql, name = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 159

def select sql, name = nil
  log sql, "SimpleDB" do
    result = []
    response = nil
    if sql.offset
      first_query = sql.gsub(/LIMIT\s+\d+/, "LIMIT #{sql.offset}")
      first_query.gsub!(/SELECT(.+?)FROM/, "SELECT COUNT(*) FROM")
      log first_query, "SimpleDB (offset partial)" do
        response = @connection.select(first_query, nil, false)
      end
      response = @connection.select(sql, response[:next_token], false)
    else
      response = @connection.select(sql, nil, true)
    end
    collection_name = get_collection_column_and_name(sql)
    collection_column_name = collection_column_name(collection_name)
    columns = columns_definition(collection_name)

    response[:items].each do |item|
      item.each do |id, attrs|
        ritem = {}
        ritem['id'] = id unless id == 'Domain' && attrs['Count'] # unless count(*) result
        attrs.each {|k, vs|
          # skip collection column (detect collection)
          if k != collection_column_name
            column = columns[k]
            if column.present?
              ritem[column.name] = column.unconvert(vs.first)
            else
              ritem[k] = vs.first
            end
          end
        }
        result << ritem
      end
    end
    result
  end
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)


33
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 33

def supports_count_distinct?; false; end

#tablesObject



108
109
110
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 108

def tables
  @@collections.keys
end

#translate_exception(exception, message) ⇒ Object



199
200
201
202
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 199

def translate_exception(exception, message)
  clear_batch
  raise exception
end

#update_sql(sql, name = nil) ⇒ Object

Executes the update statement and returns the number of rows affected.



204
205
206
207
208
209
210
211
212
213
# File 'lib/active_record/connection_adapters/simpledb_adapter/adapter.rb', line 204

def update_sql(sql, name = nil)
  begin
    execute(sql, name)
    1
  rescue Aws::AwsError => ex
    #if not a conflict state this raise
    raise ex if ex.http_code.to_i != 409
    0
  end
end