hecks-adapters-sql-database 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/column.rb +78 -0
- data/lib/commands/commands.rb +4 -0
- data/lib/commands/create.rb +59 -0
- data/lib/commands/create/add_to_join_tables.rb +39 -0
- data/lib/commands/create/find_or_create_references.rb +55 -0
- data/lib/commands/delete.rb +20 -0
- data/lib/commands/read.rb +33 -0
- data/lib/commands/read/fetch_references.rb +56 -0
- data/lib/commands/update.rb +46 -0
- data/lib/commands/update/create_new_value.rb +42 -0
- data/lib/commands/update/delete_references.rb +43 -0
- data/lib/commands/update/link_to_references.rb +44 -0
- data/lib/commands/update/update_values.rb +55 -0
- data/lib/hecks-adapters-sql-database.rb +28 -0
- data/lib/join_table.rb +22 -0
- data/lib/repository.rb +38 -0
- data/lib/schema.rb +25 -0
- data/lib/schema_factory.rb +57 -0
- data/lib/table.rb +41 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5dd924418dc3e3cbc0550d67a7b833789e634f9c
|
4
|
+
data.tar.gz: 44855f256c24dda6ecad8fbe3baafff10c61909b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48f92c5faa521559100658cd02e3f74523a375fd96957ef439331ebceb7a40b3b6e62dfa8c4d9f74cd345cbf0d5c9f32556dcf6c6ae67ded41572ea6777e6b0a
|
7
|
+
data.tar.gz: 2bc7bd4b7ee26f68b8a17f6e3ab3870188b68e6221729e3f7c2558e890d10dbb0a76ab91042e13229cdd9548e975de98f0e75febb4e571944317d30faec06e54
|
data/lib/column.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
class Column
|
5
|
+
attr_reader :referenced_object
|
6
|
+
TYPE_MAP = {
|
7
|
+
'Currency' => "BigDecimal",
|
8
|
+
'String' => 'String',
|
9
|
+
'Integer' => 'Integer'
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize(name:, referenced_object: nil, table_name: nil, type:, is_list: false)
|
13
|
+
@name = name
|
14
|
+
@referenced_object = referenced_object
|
15
|
+
@type = type
|
16
|
+
@table_name = table_name
|
17
|
+
@is_list = is_list
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.factory(attribute)
|
21
|
+
new(
|
22
|
+
name: attribute.name,
|
23
|
+
referenced_object: attribute.referenced_object,
|
24
|
+
type: attribute.type,
|
25
|
+
table_name: attribute.object_name,
|
26
|
+
is_list: attribute.list?
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_foreign_key
|
31
|
+
(type.downcase + '_id').to_sym
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_table_name
|
35
|
+
name.downcase.pluralize.to_sym
|
36
|
+
end
|
37
|
+
|
38
|
+
def list?
|
39
|
+
@is_list
|
40
|
+
end
|
41
|
+
|
42
|
+
def reference?
|
43
|
+
@referenced_object
|
44
|
+
end
|
45
|
+
|
46
|
+
def referenced_table
|
47
|
+
return unless @referenced_object
|
48
|
+
@referenced_object.pluralize.underscore
|
49
|
+
end
|
50
|
+
|
51
|
+
def name
|
52
|
+
@name
|
53
|
+
end
|
54
|
+
|
55
|
+
def copy(new_attributes={})
|
56
|
+
self.class.new(
|
57
|
+
{
|
58
|
+
name: self.name,
|
59
|
+
referenced_object: self.referenced_object,
|
60
|
+
type: self.type,
|
61
|
+
is_list: self.list?
|
62
|
+
}.merge(new_attributes)
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def ==(other)
|
67
|
+
return false if name != other.name
|
68
|
+
return false if referenced_table != other.referenced_table
|
69
|
+
true
|
70
|
+
end
|
71
|
+
|
72
|
+
def type
|
73
|
+
TYPE_MAP[@type] || @type
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require_relative 'create/find_or_create_references'
|
2
|
+
require_relative 'create/add_to_join_tables'
|
3
|
+
|
4
|
+
module Hecks
|
5
|
+
module Adapters
|
6
|
+
class SQLDatabase
|
7
|
+
module Commands
|
8
|
+
class Create
|
9
|
+
attr_reader :id
|
10
|
+
|
11
|
+
def initialize(attributes:, head:)
|
12
|
+
@attributes = attributes.clone
|
13
|
+
@reference_ids = {}
|
14
|
+
@head = head
|
15
|
+
@references = @head.references
|
16
|
+
@table = Table.factory([@head]).first
|
17
|
+
end
|
18
|
+
|
19
|
+
def call
|
20
|
+
DB.transaction do
|
21
|
+
find_or_create_references
|
22
|
+
create
|
23
|
+
add_to_join_tables
|
24
|
+
end
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def find_or_create_references
|
31
|
+
@reference_ids =
|
32
|
+
FindOrCreateReferences.new(
|
33
|
+
head: @head,
|
34
|
+
attributes: @attributes
|
35
|
+
).call.reference_ids
|
36
|
+
end
|
37
|
+
|
38
|
+
def create
|
39
|
+
graph = @references.map do |reference|
|
40
|
+
next if reference.list?
|
41
|
+
column = Column.factory(reference)
|
42
|
+
[column.to_foreign_key, @reference_ids[reference.name]]
|
43
|
+
end.compact.to_h
|
44
|
+
|
45
|
+
@id = DB[@table.name.to_sym].insert(@attributes.merge(graph))
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_to_join_tables
|
49
|
+
AddToJoinTables.new(
|
50
|
+
head: @head,
|
51
|
+
id: @id,
|
52
|
+
reference_ids: @reference_ids
|
53
|
+
).call
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Create
|
6
|
+
class AddToJoinTables
|
7
|
+
attr_reader :reference_ids
|
8
|
+
def initialize(head:, reference_ids:, id:)
|
9
|
+
@head = head
|
10
|
+
@table = Table.factory([@head]).first
|
11
|
+
@reference_ids = reference_ids
|
12
|
+
@id = id
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
@head.references.each do |reference|
|
17
|
+
column = Column.factory(reference)
|
18
|
+
join_table = JoinTable.new(@table, column)
|
19
|
+
|
20
|
+
next unless reference.list?
|
21
|
+
|
22
|
+
@reference_ids[reference.name.downcase].each do |id|
|
23
|
+
DB[join_table.name.to_sym].insert(record(column, id))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def record(column, id)
|
32
|
+
[[@table.to_foreign_key, @id], [(column.to_foreign_key).to_sym, id]].to_h
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Create
|
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
|
+
|
15
|
+
def call
|
16
|
+
find_or_create_reference
|
17
|
+
find_or_create_references
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def find_or_create_reference
|
24
|
+
@head.references.each do |reference|
|
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)
|
29
|
+
|
30
|
+
@reference_ids[reference.name] = result[:id] and return if result
|
31
|
+
@reference_ids[reference.name] = DB[column.as_table_name].insert(attributes)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_or_create_references
|
36
|
+
@head.references.each do |reference|
|
37
|
+
next unless reference.list?
|
38
|
+
attributes = @attributes.delete(reference.name.to_sym)
|
39
|
+
attributes.each do |attributes|
|
40
|
+
@reference_ids[reference.name] = [] unless @reference_ids[reference.name]
|
41
|
+
|
42
|
+
column = Column.factory(reference)
|
43
|
+
result = DB[column.to_table_name].first(attributes)
|
44
|
+
|
45
|
+
@reference_ids[reference.name] << result[:id] and return if result
|
46
|
+
@reference_ids[reference.name] << DB[column.to_table_name].insert(attributes)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Delete
|
6
|
+
def initialize(id:, head:)
|
7
|
+
@id = id
|
8
|
+
@head = head
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
table = Table.factory([@head]).first
|
13
|
+
DB[table.name.to_sym].where(id: @id).delete
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'read/fetch_references'
|
2
|
+
module Hecks
|
3
|
+
module Adapters
|
4
|
+
class SQLDatabase
|
5
|
+
module Commands
|
6
|
+
class Read
|
7
|
+
attr_reader :head, :entity, :table, :id
|
8
|
+
|
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
|
+
|
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
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def fetch_entity
|
27
|
+
@entity = DB[@table.name.to_sym].first(id: @id)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Read
|
6
|
+
class FetchReferences
|
7
|
+
attr_reader :reference_map
|
8
|
+
def initialize(read_command)
|
9
|
+
@head = read_command.head
|
10
|
+
@reference_map = {}
|
11
|
+
@entity = read_command.entity
|
12
|
+
@table = read_command.table
|
13
|
+
@id = read_command.id
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
@head.references.each do |reference|
|
18
|
+
fetch_reference(reference)
|
19
|
+
fetch_references(reference)
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def fetch_reference(reference)
|
27
|
+
return if reference.list?
|
28
|
+
column = Column.factory(reference)
|
29
|
+
value = DB[column.to_table_name].first(id: @entity[column.to_foreign_key])
|
30
|
+
|
31
|
+
@entity.delete(column.to_foreign_key)
|
32
|
+
value.delete(:id)
|
33
|
+
@reference_map[column.name.to_sym] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def fetch_references(reference)
|
37
|
+
return unless reference.list?
|
38
|
+
|
39
|
+
where_clause = {}
|
40
|
+
where_clause[@table.to_foreign_key.to_sym] = @id
|
41
|
+
column = Column.factory(reference)
|
42
|
+
|
43
|
+
@reference_map[column.name.to_sym] ||= []
|
44
|
+
|
45
|
+
DB[JoinTable.new(@table, reference).name.to_sym].where(where_clause).map do |record|
|
46
|
+
value_object = DB[column.to_table_name].first(id: record[column.to_foreign_key])
|
47
|
+
value_object.delete(:id)
|
48
|
+
@reference_map[column.name.to_sym] << value_object
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'update/update_values'
|
2
|
+
require_relative 'update/link_to_references'
|
3
|
+
require_relative 'update/create_new_value'
|
4
|
+
require_relative 'update/delete_references'
|
5
|
+
|
6
|
+
module Hecks
|
7
|
+
module Adapters
|
8
|
+
class SQLDatabase
|
9
|
+
module Commands
|
10
|
+
class Update
|
11
|
+
attr_reader :id
|
12
|
+
|
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
|
+
|
20
|
+
def call
|
21
|
+
DB.transaction do
|
22
|
+
update_references
|
23
|
+
fetch_record
|
24
|
+
update_record
|
25
|
+
end
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def update_references
|
32
|
+
UpdateValues.new(@references, @attributes, @head_table, @id).call
|
33
|
+
end
|
34
|
+
|
35
|
+
def update_record
|
36
|
+
@record.update(@attributes)
|
37
|
+
end
|
38
|
+
|
39
|
+
def fetch_record
|
40
|
+
@record = DB[@head_table.name.to_sym].where(id: @id)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Update
|
6
|
+
class CreateNewValue
|
7
|
+
attr_reader :reference_ids
|
8
|
+
|
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
|
+
|
17
|
+
def call
|
18
|
+
create_values
|
19
|
+
create_value
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def create_values
|
26
|
+
return unless @reference.list?
|
27
|
+
@attributes[@column_name].each do |value|
|
28
|
+
@reference_ids[@column_name] ||= []
|
29
|
+
@reference_ids[@column_name] << DB[@column.to_table_name].insert(value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_value
|
34
|
+
return if @reference.list?
|
35
|
+
@reference_ids[@reference.name] = DB[@column.to_table_name].insert(@attributes[@column.name.to_sym])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Update
|
6
|
+
class DeleteReferences
|
7
|
+
attr_reader :attributes
|
8
|
+
|
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
|
+
|
17
|
+
def call
|
18
|
+
# build_where_clause
|
19
|
+
delete_references
|
20
|
+
remove_references_from_attributes
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def build_where_clause
|
27
|
+
DB[JoinTable.new(@table, @reference).name.to_sym]
|
28
|
+
end
|
29
|
+
|
30
|
+
def remove_references_from_attributes
|
31
|
+
@attributes.delete(@reference.name.to_sym)
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete_references
|
35
|
+
return unless @reference.list?
|
36
|
+
@dataset.where(@where_clause).delete
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Update
|
6
|
+
class LinkToReferences
|
7
|
+
attr_reader :reference_ids
|
8
|
+
def initialize(reference:, table:, reference_ids:, attributes:, id:)
|
9
|
+
@reference = reference
|
10
|
+
@reference_ids = reference_ids
|
11
|
+
@table = table
|
12
|
+
@attributes = attributes
|
13
|
+
@column = Column.factory(@reference)
|
14
|
+
@record = {}
|
15
|
+
@id = id
|
16
|
+
end
|
17
|
+
|
18
|
+
def call
|
19
|
+
make_linking_records
|
20
|
+
make_foreign_keys
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def make_linking_records
|
27
|
+
return unless @reference.list?
|
28
|
+
@reference_ids[@reference.name.to_sym].each do |value|
|
29
|
+
@record[@column.to_foreign_key] = value
|
30
|
+
@record[@table.to_foreign_key] = @id
|
31
|
+
DB[JoinTable.new(@table, @reference).name.to_sym].insert(@record)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def make_foreign_keys
|
36
|
+
return if @reference.list?
|
37
|
+
@attributes[@column.to_foreign_key] = @reference_ids[@reference.name]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
module Commands
|
5
|
+
class Update
|
6
|
+
class UpdateValues
|
7
|
+
def initialize(references, attributes, table, id)
|
8
|
+
@references = references
|
9
|
+
@attributes = attributes
|
10
|
+
@reference_ids = {}
|
11
|
+
@table = table
|
12
|
+
@id = id
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
@references.each do |reference|
|
17
|
+
create_new_value(reference)
|
18
|
+
delete_old_references(reference)
|
19
|
+
link_to_new_values(reference)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def create_new_value(reference)
|
26
|
+
@reference_ids = CreateNewValue.new(
|
27
|
+
reference: reference,
|
28
|
+
attributes: @attributes,
|
29
|
+
reference_ids: @reference_ids
|
30
|
+
).call.reference_ids
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete_old_references(reference)
|
34
|
+
@attributes = DeleteReferences.new(
|
35
|
+
reference: reference,
|
36
|
+
table: @table,
|
37
|
+
attributes: @attributes
|
38
|
+
).call.attributes
|
39
|
+
end
|
40
|
+
|
41
|
+
def link_to_new_values(reference)
|
42
|
+
LinkToReferences.new(
|
43
|
+
reference: reference,
|
44
|
+
table: @table,
|
45
|
+
reference_ids: @reference_ids,
|
46
|
+
attributes: @attributes,
|
47
|
+
id: @id
|
48
|
+
).call
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'hecks-application'
|
3
|
+
|
4
|
+
require_relative 'column'
|
5
|
+
require_relative 'join_table'
|
6
|
+
require_relative 'schema_factory'
|
7
|
+
require_relative 'schema'
|
8
|
+
require_relative 'table'
|
9
|
+
require_relative 'repository'
|
10
|
+
require_relative 'commands/commands'
|
11
|
+
|
12
|
+
if ENV["DATABASE_URL"]
|
13
|
+
DB = Sequel.connect(ENV["DATABASE_URL"])
|
14
|
+
end
|
15
|
+
|
16
|
+
module Hecks
|
17
|
+
module Adapters
|
18
|
+
class SQLDatabase
|
19
|
+
def initialize(domain:)
|
20
|
+
@domain = domain
|
21
|
+
end
|
22
|
+
|
23
|
+
def [](module_name)
|
24
|
+
Repository.new(module_name: module_name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/join_table.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
class JoinTable
|
5
|
+
def initialize(table, column)
|
6
|
+
@table = table
|
7
|
+
@column = column
|
8
|
+
end
|
9
|
+
|
10
|
+
def name
|
11
|
+
"#{@table.name}_#{@column.name}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def columns
|
15
|
+
[@table.name, @column.name].map do |name|
|
16
|
+
Column.new(name: name.singularize + '_id', type: 'Integer')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/repository.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
class Repository
|
5
|
+
def initialize(module_name:)
|
6
|
+
@module_name = module_name
|
7
|
+
@head = DOMAIN[module_name].head
|
8
|
+
end
|
9
|
+
|
10
|
+
def create attributes
|
11
|
+
Commands::Create.new(attributes: attributes, head: @head).call
|
12
|
+
end
|
13
|
+
|
14
|
+
def update id, attributes
|
15
|
+
Commands::Update.new(id: id, attributes: attributes, head: @head).call
|
16
|
+
end
|
17
|
+
|
18
|
+
def read id
|
19
|
+
Commands::Read.new(
|
20
|
+
id: id,
|
21
|
+
head: @head,
|
22
|
+
entity_class: entity_class
|
23
|
+
).call
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete id
|
27
|
+
Commands::Delete.new(id: id, head: @head).call
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def entity_class
|
33
|
+
DOMAIN.name.camelcase.constantize::Domain.const_get(@module_name).head
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/schema.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'schema_factory'
|
2
|
+
require_relative 'table'
|
3
|
+
require_relative 'join_table'
|
4
|
+
require_relative 'column'
|
5
|
+
|
6
|
+
module Hecks
|
7
|
+
module Adapters
|
8
|
+
class SQLDatabase
|
9
|
+
class Schema
|
10
|
+
attr_reader :tables
|
11
|
+
def self.factory(domain_spec)
|
12
|
+
SchemaFactory.new(domain_spec).build
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(tables)
|
16
|
+
@tables = tables
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_h
|
20
|
+
tables.map { |table| [table.name.to_sym, table] }.to_h
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
class SchemaFactory
|
5
|
+
def initialize(domain_spec)
|
6
|
+
@domain_spec = domain_spec
|
7
|
+
@tables = Table.factory(domain_objects)
|
8
|
+
@join_tables = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def build
|
12
|
+
@tables.each do |table|
|
13
|
+
swap_domain_references(table)
|
14
|
+
build_join_tables(table)
|
15
|
+
remove_domain_columns(table)
|
16
|
+
end
|
17
|
+
|
18
|
+
Schema.new(@tables + @join_tables)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def domain_objects
|
24
|
+
@domain_spec.domain_modules.values.flat_map(&:objects)
|
25
|
+
end
|
26
|
+
|
27
|
+
def swap_domain_references(table)
|
28
|
+
table.foreign_key_columns.each do |column|
|
29
|
+
add_db_reference(column, table)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_join_tables(table)
|
34
|
+
table.join_table_columns.each do |column|
|
35
|
+
@join_tables << JoinTable.new(table, column)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_domain_columns(table)
|
40
|
+
(table.foreign_key_columns + table.join_table_columns).each do |column|
|
41
|
+
table.columns = table.columns.select do |table_column|
|
42
|
+
table_column != column
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_db_reference(column, table)
|
48
|
+
table.columns << foreign_key_column(column.referenced_object)
|
49
|
+
end
|
50
|
+
|
51
|
+
def foreign_key_column(column_name, is_list = false)
|
52
|
+
Column.new(name: column_name + '_id', type: 'Integer', is_list: is_list)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/table.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Hecks
|
2
|
+
module Adapters
|
3
|
+
class SQLDatabase
|
4
|
+
class Table
|
5
|
+
attr_accessor :columns
|
6
|
+
|
7
|
+
def self.factory(domain_objects)
|
8
|
+
r = domain_objects.map do |domain_object|
|
9
|
+
new(
|
10
|
+
name: domain_object.name,
|
11
|
+
columns: domain_object.attributes.map do |attribute|
|
12
|
+
Column.factory(attribute)
|
13
|
+
end
|
14
|
+
)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(name:, columns:)
|
19
|
+
@name = name
|
20
|
+
@columns = columns
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_foreign_key
|
24
|
+
(name.singularize + '_id').to_sym
|
25
|
+
end
|
26
|
+
|
27
|
+
def join_table_columns
|
28
|
+
columns.select(&:list?)
|
29
|
+
end
|
30
|
+
|
31
|
+
def foreign_key_columns
|
32
|
+
columns.select(&:reference?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def name
|
36
|
+
@name.pluralize.underscore
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hecks-adapters-sql-database
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Young
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Make the Domain the center of your programming world
|
14
|
+
email: chris@example.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/column.rb
|
20
|
+
- lib/commands/commands.rb
|
21
|
+
- lib/commands/create.rb
|
22
|
+
- lib/commands/create/add_to_join_tables.rb
|
23
|
+
- lib/commands/create/find_or_create_references.rb
|
24
|
+
- lib/commands/delete.rb
|
25
|
+
- lib/commands/read.rb
|
26
|
+
- lib/commands/read/fetch_references.rb
|
27
|
+
- lib/commands/update.rb
|
28
|
+
- lib/commands/update/create_new_value.rb
|
29
|
+
- lib/commands/update/delete_references.rb
|
30
|
+
- lib/commands/update/link_to_references.rb
|
31
|
+
- lib/commands/update/update_values.rb
|
32
|
+
- lib/hecks-adapters-sql-database.rb
|
33
|
+
- lib/join_table.rb
|
34
|
+
- lib/repository.rb
|
35
|
+
- lib/schema.rb
|
36
|
+
- lib/schema_factory.rb
|
37
|
+
- lib/table.rb
|
38
|
+
homepage: https://github.com/chrisyoung/hecks-domain
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.6.10
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: DDD and Hexagonal Code Generators
|
62
|
+
test_files: []
|