Class: Hyperion::Sql::Datastore

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion/sql/datastore.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_url, db_strategy, query_executor_strategy, query_builder_strategy) ⇒ Datastore

Returns a new instance of Datastore.



11
12
13
14
15
16
# File 'lib/hyperion/sql/datastore.rb', line 11

def initialize(connection_url, db_strategy, query_executor_strategy, query_builder_strategy)
  @connection_url = connection_url
  @db_strategy = db_strategy
  @query_executor = QueryExecutor.new(query_executor_strategy)
  @query_builder = QueryBuilder.new(query_builder_strategy)
end

Instance Method Details

#count(query) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/hyperion/sql/datastore.rb', line 60

def count(query)
  with_connection do
    sql_query = query_builder.build_count(query)
    results = query_executor.execute_query(sql_query)
    db_strategy.process_count_result(results[0])
  end
end

#delete(query) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/hyperion/sql/datastore.rb', line 52

def delete(query)
  with_connection do
    sql_query = query_builder.build_delete(query)
    query_executor.execute_mutation(sql_query)
    nil
  end
end

#delete_by_key(key) ⇒ Object



46
47
48
49
50
# File 'lib/hyperion/sql/datastore.rb', line 46

def delete_by_key(key)
  with_connection do
    delete(query_from_key(key))
  end
end

#find(query) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/hyperion/sql/datastore.rb', line 38

def find(query)
  with_connection do
    sql_query = query_builder.build_select(query)
    results = query_executor.execute_query(sql_query)
    results.map { |record| record_from_db(record, query.kind) }
  end
end

#find_by_key(key) ⇒ Object



32
33
34
35
36
# File 'lib/hyperion/sql/datastore.rb', line 32

def find_by_key(key)
  with_connection do
    find(query_from_key(key)).first
  end
end

#pack_key(kind, key) ⇒ Object



68
69
70
71
72
73
# File 'lib/hyperion/sql/datastore.rb', line 68

def pack_key(kind, key)
  if key
    table, id = Key.decompose_key(key)
    id
  end
end

#save(records) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hyperion/sql/datastore.rb', line 18

def save(records)
  with_connection do
    records.map do |record|
      if Hyperion.new?(record)
        execute_save_query(query_builder.build_insert(record), record)
      elsif non_empty_record?(record)
        execute_save_query(query_builder.build_update(record), record)
      else
        record
      end
    end
  end
end

#unpack_key(kind, key) ⇒ Object



75
76
77
78
79
# File 'lib/hyperion/sql/datastore.rb', line 75

def unpack_key(kind, key)
  if key
    Key.compose_key(kind, key)
  end
end