/
view.rb
54 lines (48 loc) · 1.45 KB
/
view.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Scenic
# The in-memory representation of a view definition.
#
# **This object is used internally by adapters and the schema dumper and is
# not intended to be used by application code. It is documented here for
# use by adapter gems.**
#
# @api extension
class View
# The name of the view
# @return [String]
attr_reader :name
# The SQL schema for the query that defines the view
# @return [String]
#
# @example
# "SELECT name, email FROM users UNION SELECT name, email FROM contacts"
attr_reader :definition
# True if the view is materialized
# @return [Boolean]
attr_reader :materialized
# Returns a new instance of View.
#
# @param name [String] The name of the view.
# @param definition [String] The SQL for the query that defines the view.
# @param materialized [String] `true` if the view is materialized.
def initialize(name:, definition:, materialized:)
@name = name
@definition = definition
@materialized = materialized
end
# @api private
def ==(other)
name == other.name &&
definition == other.definition &&
materialized == other.materialized
end
# @api private
def to_schema
materialized_option = materialized ? "materialized: true, " : ""
<<-DEFINITION
create_view #{name.inspect}, #{materialized_option}sql_definition: <<-\SQL
#{definition.indent(2)}
SQL
DEFINITION
end
end
end