Class: PGTrunk::QualifiedName
- Inherits:
-
Struct
- Object
- Struct
- PGTrunk::QualifiedName
- Includes:
- Comparable
- Defined in:
- lib/pg_trunk/core/qualified_name.rb
Overview
The qualified name of an object consists from schema (namespace) and name. The class contains several helper methods for ruby and sql snippets.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#schema ⇒ Object
Returns the value of attribute schema.
Class Method Summary collapse
-
.wrap(string) ⇒ Object
Build qualified name structure from a string.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer, NilClass
Make qualified names comparable by their full representation.
-
#arg_types ⇒ Object
Arg types from the function definition QualifiedName.wrap("foo.bar(a int, b int DEFAULT = 0) int").args # => %w[int int].
-
#args ⇒ Object
Args from the function definition QualifiedName.wrap("foo.bar(a int, b int DEFAULT = 0) int").args # => "a int, b int DEFAULT = 0".
-
#blank? ⇒ Boolean
If the qualified name is blank (not specified).
-
#current_schema ⇒ Object
Get the current schema.
-
#current_schema? ⇒ Boolean
If the schema is current (which is usually 'public') This schema would be ignored in +lean+.
-
#differs_from?(regex) ⇒ Boolean
If the name is present but not matches the regexp (used to check if it is not generated by some pattern).
-
#full ⇒ Object
The qualified name with unquoted parts (to be used in ruby snippets).
-
#lean ⇒ Object
Exclude current schema from a qualified name.
-
#match?(regex) ⇒ Boolean
If the name matches the regex.
-
#maybe_eq?(other) ⇒ Boolean
Checks if partially qualified
have the same schema/name as another function. -
#merge(**args) ⇒ Object
Build the name with a changed part (either a name or a schema).
-
#namespace ⇒ Object
Transform the namespace (to compare to oid fields in the database) QualifiedName.wrap("foo.bar").namespace # => "'foo'::regnamespace".
-
#quoted ⇒ Object
Quote the name (to compare to text fields in the database) QualifiedName.wrap("foo.concat(a int, b int) text").quoted # => "'concat'".
-
#returns ⇒ Object
Type of returned value of a routine QualifiedName.wrap("foo.concat(a int, b int) text").returns # => "text".
-
#routine ⇒ Object
Name of the routine for function definition QualifiedName.wrap("foo.bar(a int, b int) int").routine # => "bar".
-
#to_sql(with_args = nil) ⇒ Object
Quoted representation of the name for SQL snippets.
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name
7 8 9 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 7 def name @name end |
#schema ⇒ Object
Returns the value of attribute schema
7 8 9 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 7 def schema @schema end |
Class Method Details
.wrap(string) ⇒ Object
Build qualified name structure from a string
19 20 21 22 23 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 19 def self.wrap(string) schema = /^([^.(]+)[.]/.match(string).to_a[1] name = string.sub(/^[^.(]*[.]/, "") new(schema, name) end |
Instance Method Details
#<=>(other) ⇒ Integer, NilClass
Make qualified names comparable by their full representation
144 145 146 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 144 def <=>(other) sort_order <=> other.sort_order if other.is_a?(self.class) end |
#arg_types ⇒ Object
Arg types from the function definition QualifiedName.wrap("foo.bar(a int, b int DEFAULT = 0) int").args # => %w[int int]
117 118 119 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 117 def arg_types @arg_types ||= args.to_s.split(",").map { |a| a.strip.split(/\s+/).last } end |
#args ⇒ Object
Args from the function definition QualifiedName.wrap("foo.bar(a int, b int DEFAULT = 0) int").args # => "a int, b int DEFAULT = 0"
110 111 112 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 110 def args @args ||= name&.match(/\(([^)]+)/).to_a.last&.gsub(/\s+/, " ")&.strip end |
#blank? ⇒ Boolean
If the qualified name is blank (not specified)
41 42 43 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 41 def blank? routine.blank? end |
#current_schema ⇒ Object
Get the current schema
26 27 28 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 26 def current_schema @current_schema ||= PGTrunk.database.current_schema end |
#current_schema? ⇒ Boolean
If the schema is current (which is usually 'public') This schema would be ignored in +lean+
36 37 38 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 36 def current_schema? schema == current_schema end |
#differs_from?(regex) ⇒ Boolean
If the name is present but not matches the regexp (used to check if it is not generated by some pattern)
52 53 54 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 52 def differs_from?(regex) !routine.match?(regex) if present? end |
#full ⇒ Object
The qualified name with unquoted parts (to be used in ruby snippets)
QualifiedName.wrap("bar.foo").full # => "bar.foo"
QualifiedName.wrap("public.foo(int, int) int").full # => "foo(int, int) int"
85 86 87 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 85 def full @full ||= [schema, name].join(".") end |
#lean ⇒ Object
Exclude current schema from a qualified name
QualifiedName.wrap("public.foo").lean # => "foo"
QualifiedName.wrap("bar.baz").lean # => "bar.baz"
96 97 98 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 96 def lean @lean ||= [*(schema unless current_schema?), name].join(".") end |
#match?(regex) ⇒ Boolean
If the name matches the regex
46 47 48 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 46 def match?(regex) routine.match?(regex) end |
#maybe_eq?(other) ⇒ Boolean
Checks if partially qualified
150 151 152 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 150 def maybe_eq?(other) sort_order.first(2) == other.sort_order.first(2) end |
#merge(**args) ⇒ Object
Build the name with a changed part (either a name or a schema)
155 156 157 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 155 def merge(**args) self.class.new(args.fetch(:schema, schema), args.fetch(:name, name)) end |
#namespace ⇒ Object
Transform the namespace (to compare to oid fields in the database) QualifiedName.wrap("foo.bar").namespace # => "'foo'::regnamespace"
138 139 140 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 138 def namespace @namespace ||= "#{PGTrunk.database.quote(schema)}::regnamespace" end |
#quoted ⇒ Object
Quote the name (to compare to text fields in the database) QualifiedName.wrap("foo.concat(a int, b int) text").quoted # => "'concat'"
131 132 133 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 131 def quoted @quoted ||= PGTrunk.database.quote(routine) end |
#returns ⇒ Object
Type of returned value of a routine QualifiedName.wrap("foo.concat(a int, b int) text").returns # => "text"
124 125 126 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 124 def returns @returns ||= name&.match(/\)(.+)/).to_a.last&.strip end |
#routine ⇒ Object
Name of the routine for function definition QualifiedName.wrap("foo.bar(a int, b int) int").routine # => "bar"
103 104 105 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 103 def routine @routine ||= name[/^[^(]+/]&.strip end |
#to_sql(with_args = nil) ⇒ Object
Quoted representation of the name for SQL snippets
QualifiedName.wrap("foo.bar(a int, OUT b int) record").to_sql # => '"foo"."bar" (a int)'
qname = QualifiedName.wrap("public.foo") qname.to_sql # => '"foo"' qname.to_sql(true) # => '"foo"()'
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pg_trunk/core/qualified_name.rb', line 66 def to_sql(with_args = nil) @to_sql ||= begin str = [ *(schema unless current_schema?), routine, ].map(&:inspect).join(".") str = "#{str} (#{args})" if args.present? || with_args str end end |