hecks-adapters-sql-database 0.1.16.rc → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cli/cli.rb +3 -0
- data/lib/cli/generate_domain_migrations.rb +29 -0
- data/lib/cli/generate_sql_database.rb +31 -0
- data/lib/cli/migration_builder.rb +47 -0
- data/lib/cli/templates/migration.rb.tt +14 -0
- data/lib/cli/templates/repository.rb.tt +24 -0
- data/lib/cli/templates/sql_database/Rakefile +15 -0
- data/lib/column.rb +62 -63
- data/lib/commands/create.rb +46 -44
- data/lib/commands/create/add_to_join_tables.rb +26 -26
- data/lib/commands/create/find_or_create_references.rb +46 -39
- data/lib/commands/delete.rb +13 -14
- data/lib/commands/read.rb +22 -23
- data/lib/commands/read/fetch_references.rb +40 -40
- data/lib/commands/update.rb +28 -29
- data/lib/commands/update/create_new_value.rb +33 -30
- data/lib/commands/update/delete_references.rb +30 -31
- data/lib/commands/update/link_to_references.rb +33 -33
- data/lib/commands/update/update_values.rb +42 -43
- data/lib/hecks-adapters-sql-database.rb +11 -10
- data/lib/join_table.rb +14 -15
- data/lib/repository.rb +23 -28
- data/lib/schema.rb +13 -14
- data/lib/schema_factory.rb +39 -40
- data/lib/table.rb +29 -30
- metadata +18 -11
@@ -1,50 +1,57 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
module HecksAdapters
|
2
|
+
class SQLDatabase
|
3
|
+
module Commands
|
4
|
+
class Create
|
5
|
+
# Find and update, or create Reference Rows
|
6
|
+
class FindOrCreateReferences
|
7
|
+
attr_reader :reference_ids
|
8
|
+
|
9
|
+
def initialize(head:, attributes:)
|
10
|
+
@head = head
|
11
|
+
@attributes = attributes
|
12
|
+
@reference_ids = {}
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
find_or_create_reference
|
17
|
-
find_or_create_references
|
18
|
-
self
|
19
|
-
end
|
15
|
+
def call
|
20
16
|
|
21
|
-
|
17
|
+
find_or_create_reference
|
18
|
+
find_or_create_references
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
next if reference.list?
|
26
|
-
attributes = @attributes.delete(reference.name.to_sym)
|
27
|
-
column = Column.factory(reference)
|
28
|
-
result = DB[column.to_table_name].first(attributes)
|
20
|
+
self
|
21
|
+
end
|
29
22
|
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
private
|
24
|
+
|
25
|
+
def find_or_create_reference
|
26
|
+
@head.references.each do |reference|
|
27
|
+
next if reference.list?
|
28
|
+
attributes = @attributes.delete(reference.name.to_sym)
|
29
|
+
column = Column.factory(reference)
|
30
|
+
result = DB[column.to_table_name].first(attributes)
|
31
|
+
|
32
|
+
@reference_ids[reference.name] = result[:id] and return if result
|
33
|
+
id = SecureRandom.uuid
|
34
|
+
with_id = attributes.merge!(id: id)
|
35
|
+
DB[column.to_table_name].insert(with_id)
|
36
|
+
@reference_ids[reference.name] = id
|
33
37
|
end
|
38
|
+
end
|
34
39
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
def find_or_create_references
|
41
|
+
@head.references.each do |reference|
|
42
|
+
next unless reference.list?
|
43
|
+
attributes = @attributes.delete(reference.name.to_sym)
|
44
|
+
attributes.each do |attributes|
|
45
|
+
@reference_ids[reference.name] = [] unless @reference_ids[reference.name]
|
41
46
|
|
42
|
-
|
43
|
-
|
47
|
+
column = Column.factory(reference)
|
48
|
+
result = DB[column.to_table_name].first(attributes)
|
44
49
|
|
45
|
-
|
46
|
-
|
47
|
-
|
50
|
+
@reference_ids[reference.name] << result[:id] and return if result
|
51
|
+
id = SecureRandom.uuid
|
52
|
+
with_id = attributes.merge!(id: id)
|
53
|
+
DB[column.to_table_name].insert(with_id)
|
54
|
+
@reference_ids[reference.name] << id
|
48
55
|
end
|
49
56
|
end
|
50
57
|
end
|
data/lib/commands/delete.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module HecksAdapters
|
2
|
+
class SQLDatabase
|
3
|
+
module Commands
|
4
|
+
# Delete a resource
|
5
|
+
class Delete
|
6
|
+
def initialize(id:, head:)
|
7
|
+
@id = id
|
8
|
+
@head = head
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
11
|
+
def call
|
12
|
+
table = Table.factory([@head]).first
|
13
|
+
DB[table.name.to_sym].where(id: @id).delete
|
14
|
+
self
|
16
15
|
end
|
17
16
|
end
|
18
17
|
end
|
data/lib/commands/read.rb
CHANGED
@@ -1,31 +1,30 @@
|
|
1
1
|
require_relative 'read/fetch_references'
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
module HecksAdapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
# Read a resource
|
6
|
+
class Read
|
7
|
+
attr_reader :head, :entity, :table, :id
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
def initialize(id:, head:, entity_class:)
|
10
|
+
@head = head
|
11
|
+
@table = Table.factory([@head]).first
|
12
|
+
@id = id
|
13
|
+
@entity_class = entity_class
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
def call
|
17
|
+
fetch_entity
|
18
|
+
return if @entity.nil?
|
19
|
+
@entity_class.new(
|
20
|
+
@entity.merge(FetchReferences.new(self).call.reference_map)
|
21
|
+
)
|
22
|
+
end
|
23
23
|
|
24
|
-
|
24
|
+
private
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
end
|
26
|
+
def fetch_entity
|
27
|
+
@entity = DB[@table.name.to_sym].first(id: @id)
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
@@ -1,52 +1,52 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
module HecksAdapters
|
2
|
+
class SQLDatabase
|
3
|
+
module Commands
|
4
|
+
class Read
|
5
|
+
# Look up any references so we can fill in relationship data on the
|
6
|
+
# object
|
7
|
+
class FetchReferences
|
8
|
+
attr_reader :reference_map
|
9
|
+
def initialize(read_command)
|
10
|
+
@head = read_command.head
|
11
|
+
@reference_map = {}
|
12
|
+
@entity = read_command.entity
|
13
|
+
@table = read_command.table
|
14
|
+
@id = read_command.id
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
self
|
17
|
+
def call
|
18
|
+
@head.references.each do |reference|
|
19
|
+
fetch_reference(reference)
|
20
|
+
fetch_references(reference)
|
22
21
|
end
|
22
|
+
self
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
+
private
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def fetch_reference(reference)
|
28
|
+
return if reference.list?
|
29
|
+
column = Column.factory(reference)
|
30
|
+
value = DB[column.to_table_name].first(id: @entity[column.to_foreign_key])
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
@entity.delete(column.to_foreign_key)
|
33
|
+
value.delete(:id)
|
34
|
+
@reference_map[column.name.to_sym] = value
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
def fetch_references(reference)
|
38
|
+
return unless reference.list?
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
where_clause = {}
|
41
|
+
where_clause[@table.to_foreign_key.to_sym] = @id
|
42
|
+
column = Column.factory(reference)
|
42
43
|
|
43
|
-
|
44
|
+
@reference_map[column.name.to_sym] ||= []
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
46
|
+
DB[JoinTable.new(@table, reference).name.to_sym].where(where_clause).map do |record|
|
47
|
+
value_object = DB[column.to_table_name].first(id: record[column.to_foreign_key])
|
48
|
+
value_object.delete(:id)
|
49
|
+
@reference_map[column.name.to_sym] << value_object
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
data/lib/commands/update.rb
CHANGED
@@ -3,42 +3,41 @@ require_relative 'update/link_to_references'
|
|
3
3
|
require_relative 'update/create_new_value'
|
4
4
|
require_relative 'update/delete_references'
|
5
5
|
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
module HecksAdapters
|
7
|
+
class SQLDatabase
|
8
|
+
module Commands
|
9
|
+
# Upate a resource
|
10
|
+
class Update
|
11
|
+
attr_reader :id
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
def initialize(attributes:, head:, id:)
|
14
|
+
@attributes = attributes.clone
|
15
|
+
@references = head.references
|
16
|
+
@head_table = Table.factory([head]).first
|
17
|
+
@id = id
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
self
|
20
|
+
def call
|
21
|
+
DB.transaction do
|
22
|
+
update_references
|
23
|
+
fetch_record
|
24
|
+
update_record
|
27
25
|
end
|
26
|
+
self
|
27
|
+
end
|
28
28
|
|
29
|
-
|
29
|
+
private
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
def update_references
|
32
|
+
UpdateValues.new(@references, @attributes, @head_table, @id).call
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
def update_record
|
36
|
+
@record.update(@attributes)
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
39
|
+
def fetch_record
|
40
|
+
@record = DB[@head_table.name.to_sym].where(id: @id)
|
42
41
|
end
|
43
42
|
end
|
44
43
|
end
|
@@ -1,39 +1,42 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module HecksAdapters
|
2
|
+
class SQLDatabase
|
3
|
+
module Commands
|
4
|
+
class Update
|
5
|
+
# Create any new values
|
6
|
+
class CreateNewValue
|
7
|
+
attr_reader :reference_ids
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
def initialize(reference:, attributes:, reference_ids:)
|
10
|
+
@reference = reference
|
11
|
+
@attributes = attributes
|
12
|
+
@reference_ids = {}
|
13
|
+
@column = Column.factory(reference)
|
14
|
+
@column_name = @column.name.to_sym
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
def call
|
18
|
+
create_values
|
19
|
+
create_value
|
20
|
+
self
|
21
|
+
end
|
22
22
|
|
23
|
-
|
23
|
+
private
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
def create_values
|
26
|
+
return unless @reference.list?
|
27
|
+
@attributes[@column_name].each do |value|
|
28
|
+
@reference_ids[@column_name] ||= []
|
29
|
+
id = SecureRandom.uuid
|
30
|
+
DB[@column.to_table_name].insert(value.merge(id: id))
|
31
|
+
@reference_ids[@column_name] << id
|
31
32
|
end
|
33
|
+
end
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
def create_value
|
36
|
+
return if @reference.list?
|
37
|
+
id = SecureRandom.uuid
|
38
|
+
DB[@column.to_table_name].insert(@attributes[@column.name.to_sym].merge(id: id))
|
39
|
+
@reference_ids[@reference.name] = id
|
37
40
|
end
|
38
41
|
end
|
39
42
|
end
|
@@ -1,40 +1,39 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module HecksAdapters
|
2
|
+
class SQLDatabase
|
3
|
+
module Commands
|
4
|
+
class Update
|
5
|
+
# Delete references to old values
|
6
|
+
class DeleteReferences
|
7
|
+
attr_reader :attributes
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
def initialize(table:, attributes:, reference:)
|
10
|
+
@table = table
|
11
|
+
@attributes = attributes
|
12
|
+
@reference = reference
|
13
|
+
@where_clause = {}
|
14
|
+
@dataset = DB[JoinTable.new(@table, @reference).name.to_sym]
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
def call
|
18
|
+
# build_where_clause
|
19
|
+
delete_references
|
20
|
+
remove_references_from_attributes
|
21
|
+
self
|
22
|
+
end
|
23
23
|
|
24
|
-
|
24
|
+
private
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
def build_where_clause
|
27
|
+
DB[JoinTable.new(@table, @reference).name.to_sym]
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def remove_references_from_attributes
|
31
|
+
@attributes.delete(@reference.name.to_sym)
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
34
|
+
def delete_references
|
35
|
+
return unless @reference.list?
|
36
|
+
@dataset.where(@where_clause).delete
|
38
37
|
end
|
39
38
|
end
|
40
39
|
end
|