masamune 0.13.8 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/lib/masamune.rb +8 -5
  3. data/lib/masamune/actions.rb +1 -13
  4. data/lib/masamune/actions/data_flow.rb +2 -1
  5. data/lib/masamune/actions/date_parse.rb +0 -1
  6. data/lib/masamune/actions/elastic_mapreduce.rb +0 -2
  7. data/lib/masamune/actions/filesystem.rb +0 -2
  8. data/lib/masamune/actions/hive.rb +0 -2
  9. data/lib/masamune/actions/invoke_parallel.rb +2 -1
  10. data/lib/masamune/actions/postgres.rb +0 -1
  11. data/lib/masamune/actions/s3cmd.rb +2 -0
  12. data/lib/masamune/actions/transform.rb +0 -2
  13. data/lib/masamune/after_initialize_callbacks.rb +0 -2
  14. data/lib/masamune/commands.rb +1 -11
  15. data/lib/masamune/commands/postgres.rb +1 -0
  16. data/lib/masamune/commands/postgres_admin.rb +2 -0
  17. data/lib/masamune/configuration.rb +2 -0
  18. data/lib/masamune/data_plan/engine.rb +2 -0
  19. data/lib/masamune/filesystem.rb +2 -0
  20. data/lib/masamune/helpers.rb +1 -1
  21. data/lib/masamune/last_element.rb +0 -2
  22. data/lib/masamune/schema/dimension.rb +1 -3
  23. data/lib/masamune/schema/store.rb +2 -0
  24. data/lib/masamune/schema/table.rb +2 -0
  25. data/lib/masamune/template.rb +4 -1
  26. data/lib/masamune/thor.rb +1 -1
  27. data/lib/masamune/transform.rb +1 -21
  28. data/lib/masamune/transform/bulk_upsert.rb +1 -22
  29. data/lib/masamune/transform/common.rb +27 -0
  30. data/lib/masamune/transform/common/denormalize_table.rb +90 -0
  31. data/lib/masamune/transform/deduplicate_dimension.rb +1 -41
  32. data/lib/masamune/transform/define_table.rb +1 -113
  33. data/lib/masamune/transform/denormalize_table.rb +1 -50
  34. data/lib/masamune/transform/hive.rb +27 -0
  35. data/lib/masamune/transform/{define_schema.hql.erb → hive/define_schema.hql.erb} +0 -0
  36. data/lib/masamune/transform/{define_table.hql.erb → hive/define_table.hql.erb} +0 -0
  37. data/lib/masamune/transform/hive/define_table.rb +46 -0
  38. data/lib/masamune/transform/{denormalize_table.hql.erb → hive/denormalize_table.hql.erb} +0 -0
  39. data/lib/masamune/transform/hive/denormalize_table.rb +27 -0
  40. data/lib/masamune/transform/insert_reference_values.rb +1 -30
  41. data/lib/masamune/transform/operator.rb +36 -37
  42. data/lib/masamune/transform/postgres.rb +27 -0
  43. data/lib/masamune/transform/{bulk_upsert.psql.erb → postgres/bulk_upsert.psql.erb} +0 -0
  44. data/lib/masamune/transform/postgres/bulk_upsert.rb +62 -0
  45. data/lib/masamune/transform/{deduplicate_dimension.psql.erb → postgres/deduplicate_dimension.psql.erb} +1 -7
  46. data/lib/masamune/transform/postgres/deduplicate_dimension.rb +79 -0
  47. data/lib/masamune/transform/{define_foreign_key.psql.erb → postgres/define_foreign_key.psql.erb} +0 -0
  48. data/lib/masamune/transform/{define_index.psql.erb → postgres/define_index.psql.erb} +0 -0
  49. data/lib/masamune/transform/{define_inheritance.psql.erb → postgres/define_inheritance.psql.erb} +0 -0
  50. data/lib/masamune/transform/{define_schema.psql.erb → postgres/define_schema.psql.erb} +0 -0
  51. data/lib/masamune/transform/{define_table.psql.erb → postgres/define_table.psql.erb} +0 -0
  52. data/lib/masamune/transform/postgres/define_table.rb +142 -0
  53. data/lib/masamune/transform/{define_unique.psql.erb → postgres/define_unique.psql.erb} +0 -0
  54. data/lib/masamune/transform/{denormalize_table.psql.erb → postgres/denormalize_table.psql.erb} +0 -0
  55. data/lib/masamune/transform/postgres/denormalize_table.rb +27 -0
  56. data/lib/masamune/transform/{insert_reference_values.psql.erb → postgres/insert_reference_values.psql.erb} +1 -1
  57. data/lib/masamune/transform/postgres/insert_reference_values.rb +69 -0
  58. data/lib/masamune/transform/{relabel_dimension.psql.erb → postgres/relabel_dimension.psql.erb} +4 -1
  59. data/lib/masamune/transform/postgres/relabel_dimension.rb +45 -0
  60. data/lib/masamune/transform/{replace_table.psql.erb → postgres/replace_table.psql.erb} +0 -0
  61. data/lib/masamune/transform/{rollup_fact.psql.erb → postgres/rollup_fact.psql.erb} +0 -0
  62. data/lib/masamune/transform/postgres/rollup_fact.rb +123 -0
  63. data/lib/masamune/transform/{snapshot_dimension.psql.erb → postgres/snapshot_dimension.psql.erb} +3 -10
  64. data/lib/masamune/transform/postgres/snapshot_dimension.rb +83 -0
  65. data/lib/masamune/transform/{stage_dimension.psql.erb → postgres/stage_dimension.psql.erb} +0 -0
  66. data/lib/masamune/transform/postgres/stage_dimension.rb +90 -0
  67. data/lib/masamune/transform/{stage_fact.psql.erb → postgres/stage_fact.psql.erb} +0 -0
  68. data/lib/masamune/transform/postgres/stage_fact.rb +134 -0
  69. data/lib/masamune/transform/relabel_dimension.rb +1 -9
  70. data/lib/masamune/transform/rollup_fact.rb +1 -86
  71. data/lib/masamune/transform/snapshot_dimension.rb +1 -44
  72. data/lib/masamune/transform/stage_dimension.rb +1 -53
  73. data/lib/masamune/transform/stage_fact.rb +1 -96
  74. data/lib/masamune/version.rb +1 -1
  75. data/spec/masamune/template_spec.rb +1 -1
  76. data/spec/masamune/transform/bulk_upsert.dimension_spec.rb +1 -3
  77. data/spec/masamune/transform/deduplicate_dimension_spec.rb +1 -7
  78. data/spec/masamune/transform/define_table.dimension_spec.rb +0 -14
  79. data/spec/masamune/transform/denormalize_table_spec.rb +34 -0
  80. data/spec/masamune/transform/relabel_dimension_spec.rb +6 -1
  81. data/spec/masamune/transform/snapshot_dimension_spec.rb +3 -10
  82. metadata +37 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4d4d75d505e70be3e5c119ccb677ad9e9846a85
4
- data.tar.gz: 2518fa319f34c708c94b363473b9e0ab4cf73b75
3
+ metadata.gz: feca9f373a42ecd946e9682b08213c3357ce7437
4
+ data.tar.gz: 51cf1a4d353fb1f607299937e60c66437213d35c
5
5
  SHA512:
6
- metadata.gz: 43e4b96852c3cacaadf6a82169eefd892d962329f151ced5923369281340cf0a2b49e1edadc0de59cb1080f89b2c91ebfa61378e6bcae602ccd7af361ccb793a
7
- data.tar.gz: e5aabba71b0721d37537f59d0744ebb02a66e73dcc060a98704bf8247b9ec229c3db8a3a338a4603c2b57e780a97cec663da5ecb653a67ff09fc6acd577ecf13
6
+ metadata.gz: 2e1d3c79fbe068bb79cb653cb746845dbaf7c33bc4525727cfa569b40407578b970418ee701ecb4ed0ed15004fb48d183c14398c03e6267fa8f5429d0184d0cf
7
+ data.tar.gz: f62cd40539a9ac2036ee312e9abf9055792463f3340faaed9857bd2dfb16d06cf3fe12e32ba41bc1b3676b23e512641abcaf07c929fe3e0e70bc5b4240d002ea
data/lib/masamune.rb CHANGED
@@ -20,26 +20,29 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require 'active_support/concern'
24
+
23
25
  module Masamune
24
26
  require 'masamune/environment'
25
27
  require 'masamune/has_environment'
26
28
  require 'masamune/io'
27
29
  require 'masamune/template'
28
- require 'masamune/commands'
29
30
  require 'masamune/last_element'
30
- require 'masamune/actions'
31
- require 'masamune/helpers'
32
31
  require 'masamune/configuration'
33
- require 'masamune/data_plan'
34
32
  require 'masamune/thor'
35
33
  require 'masamune/thor_loader'
36
34
  require 'masamune/filesystem'
37
35
  require 'masamune/cached_filesystem'
38
36
  require 'masamune/method_logger'
39
37
  require 'masamune/after_initialize_callbacks'
38
+ require 'masamune/topological_hash'
39
+
40
+ require 'masamune/data_plan'
41
+ require 'masamune/commands'
42
+ require 'masamune/actions'
43
+ require 'masamune/helpers'
40
44
  require 'masamune/schema'
41
45
  require 'masamune/transform'
42
- require 'masamune/topological_hash'
43
46
 
44
47
  extend self
45
48
  extend Masamune::HasEnvironment
@@ -22,18 +22,6 @@
22
22
 
23
23
  module Masamune
24
24
  module Actions
25
- require 'masamune/actions/hive'
26
- require 'masamune/actions/s3cmd'
27
- require 'masamune/actions/hadoop_streaming'
28
- require 'masamune/actions/hadoop_filesystem'
29
- require 'masamune/actions/postgres'
30
- require 'masamune/actions/postgres_admin'
31
- require 'masamune/actions/filesystem'
32
- require 'masamune/actions/date_parse'
33
- require 'masamune/actions/data_flow'
34
- require 'masamune/actions/elastic_mapreduce'
35
- require 'masamune/actions/execute'
36
- require 'masamune/actions/transform'
37
- require 'masamune/actions/invoke_parallel'
25
+ Dir["#{File.dirname(__FILE__)}/actions/*.rb"].each { |f| require f }
38
26
  end
39
27
  end
@@ -21,7 +21,8 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'chronic'
24
- require 'active_support/concern'
24
+
25
+ require 'masamune/actions/date_parse'
25
26
 
26
27
  module Masamune::Actions
27
28
  module DataFlow
@@ -21,7 +21,6 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'chronic'
24
- require 'active_support/concern'
25
24
 
26
25
  module Masamune::Actions
27
26
  module DateParse
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
-
25
23
  module Masamune::Actions
26
24
  module ElasticMapreduce
27
25
  extend ActiveSupport::Concern
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
-
25
23
  module Masamune::Actions
26
24
  module Filesystem
27
25
  extend ActiveSupport::Concern
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
-
25
23
  require 'masamune/transform/define_schema'
26
24
 
27
25
  module Masamune::Actions
@@ -20,9 +20,10 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
23
  require 'parallel'
25
24
 
25
+ require 'masamune/actions/execute'
26
+
26
27
  module Masamune::Actions
27
28
  module InvokeParallel
28
29
  extend ActiveSupport::Concern
@@ -20,7 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
23
  require 'masamune/transform/define_schema'
25
24
  require 'masamune/actions/postgres_admin'
26
25
 
@@ -20,6 +20,8 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require 'masamune/commands/s3cmd'
24
+
23
25
  module Masamune::Actions
24
26
  module S3Cmd
25
27
  include Masamune::Commands::S3Cmd::ClassMethods
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
-
25
23
  require 'masamune/actions/postgres'
26
24
 
27
25
  require 'masamune/transform/load_dimension'
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
-
25
23
  module Masamune
26
24
  module AfterInitializeCallbacks
27
25
  extend ActiveSupport::Concern
@@ -22,16 +22,6 @@
22
22
 
23
23
  module Masamune
24
24
  module Commands
25
- require 'masamune/commands/hive'
26
- require 'masamune/commands/s3cmd'
27
- require 'masamune/commands/hadoop_streaming'
28
- require 'masamune/commands/hadoop_filesystem'
29
- require 'masamune/commands/elastic_mapreduce'
30
- require 'masamune/commands/postgres_common'
31
- require 'masamune/commands/postgres'
32
- require 'masamune/commands/postgres_admin'
33
- require 'masamune/commands/interactive'
34
- require 'masamune/commands/shell'
35
- require 'masamune/commands/retry_with_backoff'
25
+ Dir["#{File.dirname(__FILE__)}/commands/*.rb"].each { |f| require f }
36
26
  end
37
27
  end
@@ -23,6 +23,7 @@
23
23
  require 'delegate'
24
24
 
25
25
  require 'masamune/string_format'
26
+ require 'masamune/commands/postgres_common'
26
27
 
27
28
  module Masamune::Commands
28
29
  class Postgres < SimpleDelegator
@@ -22,6 +22,8 @@
22
22
 
23
23
  require 'delegate'
24
24
 
25
+ require 'masamune/commands/postgres_common'
26
+
25
27
  module Masamune::Commands
26
28
  class PostgresAdmin < SimpleDelegator
27
29
  include Masamune::Commands::PostgresCommon
@@ -27,6 +27,8 @@ require 'pp'
27
27
 
28
28
  require 'active_support/core_ext/hash'
29
29
 
30
+ require 'masamune/has_environment'
31
+
30
32
  class Masamune::Configuration
31
33
  extend Forwardable
32
34
  include Masamune::HasEnvironment
@@ -23,6 +23,8 @@
23
23
  require 'active_support'
24
24
  require 'active_support/core_ext/numeric/time'
25
25
 
26
+ require 'masamune/has_environment'
27
+
26
28
  class Masamune::DataPlan::Engine
27
29
  MAX_DEPTH = 10
28
30
 
@@ -21,6 +21,8 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'masamune/has_environment'
24
+ require 'masamune/actions/s3cmd'
25
+ require 'masamune/actions/hadoop_filesystem'
24
26
 
25
27
  module Masamune
26
28
  class Filesystem
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Masamune
24
24
  module Helpers
25
- require 'masamune/helpers/postgres'
25
+ Dir["#{File.dirname(__FILE__)}/helpers/*.rb"].each { |f| require f }
26
26
  end
27
27
  end
@@ -20,8 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'active_support/concern'
24
-
25
23
  module Masamune
26
24
  module LastElement
27
25
  extend ActiveSupport::Concern
@@ -62,7 +62,7 @@ module Masamune::Schema
62
62
  when :two
63
63
  [:start_at, :end_at, :version, :last_modified_at]
64
64
  when :four
65
- [:parent_id, :record_id, :start_at, :end_at, :version, :last_modified_at]
65
+ [:start_at, :end_at, :version, :last_modified_at]
66
66
  when :ledger
67
67
  [:source_kind, :source_uuid, :start_at, :last_modified_at, :delta]
68
68
  else
@@ -103,8 +103,6 @@ module Masamune::Schema
103
103
  when :four
104
104
  children << ledger_table
105
105
  # FIXME derive type from from parent
106
- initialize_column! id: 'parent_id', type: :integer, null: true, reference: TableReference.new(ledger_table)
107
- initialize_column! id: 'record_id', type: :integer, null: true, reference: TableReference.new(ledger_table)
108
106
  initialize_column! id: 'start_at', type: :timestamp, default: 'TO_TIMESTAMP(0)', index: [:start_at, :natural], unique: :natural
109
107
  initialize_column! id: 'end_at', type: :timestamp, null: true, index: :end_at
110
108
  initialize_column! id: 'version', type: :integer, default: 1, null: true
@@ -22,6 +22,8 @@
22
22
 
23
23
  require 'active_support/core_ext/hash'
24
24
 
25
+ require 'masamune/has_environment'
26
+
25
27
  module Masamune::Schema
26
28
  class Store
27
29
  include Masamune::HasEnvironment
@@ -20,6 +20,8 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require 'masamune/last_element'
24
+
23
25
  module Masamune::Schema
24
26
  class Table
25
27
  include Masamune::LastElement
@@ -42,7 +42,10 @@ module Masamune
42
42
  return partial_file if Pathname.new(partial_file).absolute?
43
43
  @paths.each do |path|
44
44
  file = File.expand_path(File.join(path, partial_file))
45
- return file if File.exists?(file) && File.file?(file)
45
+ if File.exists?(file) && File.file?(file)
46
+ @paths << File.dirname(file)
47
+ return file
48
+ end
46
49
  end
47
50
  raise IOError, "File not found: #{partial_file}"
48
51
  end
data/lib/masamune/thor.rb CHANGED
@@ -22,8 +22,8 @@
22
22
 
23
23
  require 'date'
24
24
  require 'thor'
25
- require 'active_support/concern'
26
25
 
26
+ require 'masamune/has_environment'
27
27
  require 'masamune/after_initialize_callbacks'
28
28
 
29
29
  module Masamune
@@ -22,26 +22,6 @@
22
22
 
23
23
  module Masamune
24
24
  module Transform
25
- require 'masamune/transform/operator'
26
-
27
- require 'masamune/transform/define_table'
28
- require 'masamune/transform/define_schema'
29
- require 'masamune/transform/denormalize_table'
30
-
31
- require 'masamune/transform/bulk_upsert'
32
- require 'masamune/transform/insert_reference_values'
33
-
34
- require 'masamune/transform/stage_dimension'
35
- require 'masamune/transform/stage_fact'
36
-
37
- require 'masamune/transform/load_dimension'
38
- require 'masamune/transform/load_fact'
39
-
40
- require 'masamune/transform/snapshot_dimension'
41
- require 'masamune/transform/deduplicate_dimension'
42
- require 'masamune/transform/consolidate_dimension'
43
- require 'masamune/transform/relabel_dimension'
44
-
45
- require 'masamune/transform/rollup_fact'
25
+ Dir["#{File.dirname(__FILE__)}/transform/*.rb"].each { |f| require f }
46
26
  end
47
27
  end
@@ -25,28 +25,7 @@ module Masamune::Transform
25
25
  extend ActiveSupport::Concern
26
26
 
27
27
  def bulk_upsert(source, target)
28
- Operator.new(__method__, source: source, target: target, presenters: { postgres: Postgres})
29
- end
30
-
31
- private
32
-
33
- class Postgres < SimpleDelegator
34
- include Masamune::LastElement
35
-
36
- def update_columns
37
- columns.values.reject { |column| reserved_column_ids.include?(column.id) || column.surrogate_key || column.natural_key || column.unique.any? || column.auto_reference || column.ignore }
38
- end
39
- method_with_last_element :update_columns
40
-
41
- def insert_columns
42
- columns.values.reject { |column| column.surrogate_key || column.auto_reference || column.ignore }
43
- end
44
- method_with_last_element :insert_columns
45
-
46
- def unique_columns
47
- columns.values.select { |column| column.unique.any? && !column.null }
48
- end
49
- method_with_last_element :unique_columns
28
+ Operator.new(__method__, source: source, target: target)
50
29
  end
51
30
  end
52
31
  end
@@ -0,0 +1,27 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ module Masamune::Transform
24
+ module Common
25
+ Dir["#{File.dirname(__FILE__)}/common/*.rb"].each { |f| require f }
26
+ end
27
+ end
@@ -0,0 +1,90 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'masamune/last_element'
24
+
25
+ module Masamune::Transform::Common
26
+ class DenormalizeTable
27
+ def initialize(options = {})
28
+ @target = options[:target]
29
+ @columns = options[:columns]
30
+ @order_by = options[:order_by]
31
+ end
32
+
33
+ def locals
34
+ { target: target, columns: @columns, order_by: @order_by }
35
+ end
36
+
37
+ def target
38
+ TargetPresenter.new(@target)
39
+ end
40
+
41
+ private
42
+
43
+ class TargetPresenter < SimpleDelegator
44
+ include Masamune::LastElement
45
+
46
+ def select_columns(column_names)
47
+ column_names.map do |column_name|
48
+ next unless column = dereference_column_name(column_name)
49
+ if column.reference
50
+ if column.reference.implicit || column.reference.degenerate
51
+ "#{column.name} AS #{column.name}"
52
+ else
53
+ "#{column.foreign_key_name} AS #{column.name}"
54
+ end
55
+ else
56
+ column.qualified_name
57
+ end
58
+ end.compact
59
+ end
60
+ method_with_last_element :select_columns
61
+
62
+ def join_alias(reference)
63
+ reference.label ? "#{reference.name} AS #{[reference.label, reference.name].compact.join('_')}" : reference.name
64
+ end
65
+
66
+ def join_conditions(column_names)
67
+ {}.tap do |conditions|
68
+ column_names.each do |column_name|
69
+ next unless column = dereference_column_name(column_name)
70
+ next unless column.reference
71
+ next if column.reference.degenerate
72
+ adjacent_reference = references[column.reference.id]
73
+ next unless adjacent_reference
74
+ adjacent_column = columns[adjacent_reference.foreign_key_name]
75
+ next unless adjacent_column
76
+ conditions[join_alias(column.reference)] = "#{column.reference.surrogate_key.qualified_name(column.reference.label)} = #{adjacent_column.qualified_name}"
77
+ end
78
+ end
79
+ end
80
+
81
+ def order_by_columns(column_names)
82
+ column_names.map do |column_name|
83
+ next unless column = dereference_column_name(column_name)
84
+ column.name
85
+ end.compact
86
+ end
87
+ method_with_last_element :order_by_columns
88
+ end
89
+ end
90
+ end