Class: Arc::DataStores::AbstractDataStore
Constant Summary
Constants included
from Casting
Casting::CAST_METHODS, Casting::TYPES
Instance Method Summary
collapse
Methods included from Casting
#cast
Methods included from Quoting
#quote, #quote_column, #quote_table
#columns_hash, #connection, #connection_pool, #primary_key, #quote_hash, #spec, #table_exists?, #visitor, #with_connection
Instance Method Details
#[](table) ⇒ Object
Also known as:
columns
13
14
15
|
# File 'lib/arc/data_stores/abstract/store.rb', line 13
def [] table
schema[table]
end
|
#create(stmt) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/arc/data_stores/abstract/store.rb', line 30
def create stmt
case stmt
when String
execute stmt
when Arel::InsertManager
table = stmt.instance_variable_get(:@ast).relation
execute stmt.to_sql
projections = schema[table.name.to_sym].column_names.map{ |c| table[c.to_sym] }
read(
table.project(*projections).where(
table.primary_key.eq(
Arel.sql(last_insert_rowid(table.name, table.primary_key.name).to_s)
)
)
)[0]
end
end
|
#destroy(stmt) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/arc/data_stores/abstract/store.rb', line 65
def destroy stmt
case stmt
when String
execute stmt
when Arel::DeleteManager
execute stmt.to_sql
end
end
|
#execute(query) ⇒ Object
78
79
80
81
82
|
# File 'lib/arc/data_stores/abstract/store.rb', line 78
def execute query
raise NotImplementedError
end
|
#last_insert_rowid(table_name, pk_name) ⇒ Object
74
75
76
|
# File 'lib/arc/data_stores/abstract/store.rb', line 74
def last_insert_rowid table_name, pk_name
'last_insert_rowid()'
end
|
#read(query) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/arc/data_stores/abstract/store.rb', line 21
def read query
case query
when String
execute(query).symbolize_keys!
when Arel::SelectManager
result_for query
end
end
|
#result_for(query) ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/arc/data_stores/abstract/store.rb', line 109
def result_for query
mappings = type_mappings_for query
rows = read(query.to_sql)
return Array.new(rows.size) do |index|
Hash.new do |hash, key|
hash[key] = cast rows[index][key], mappings[key]
end
end
end
|
#schema ⇒ Object
17
18
19
|
# File 'lib/arc/data_stores/abstract/store.rb', line 17
def schema
raise NotImplementedError
end
|
#type_mappings_for(query) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/arc/data_stores/abstract/store.rb', line 86
def type_mappings_for query
type_mappings = {}
query.instance_variable_get(:@ctx).projections.each do |projection|
is_alias = projection.respond_to?(:right)
relation = is_alias ? projection.left.relation : projection.relation
root_projection = is_alias ? projection.left : projection
relation_is_alias = root_projection.relation.respond_to?(:left)
root_relation = relation_is_alias ? relation.left : relation
table_name = root_relation.name
result_column_name = is_alias ? projection.right.to_sym : projection.name
table_column_name = root_projection.name
table = Arel::Table.engine[table_name.to_sym]
column = table[table_column_name.to_sym]
type_mappings[result_column_name] = column.type.to_sym
end
type_mappings
end
|
#update(stmt, id = nil) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/arc/data_stores/abstract/store.rb', line 48
def update stmt, id=nil
case stmt
when String
execute stmt
when Arel::UpdateManager
execute stmt.to_sql
if id
table = stmt.instance_variable_get(:@ast).relation
projections = schema[table.name.to_sym].column_names.map{ |c| table[c.to_sym] }
read(
table.project(*projections)
.where(table.primary_key.eq id)
)[0]
end
end
end
|