Class: PGTrunk::QualifiedName

Inherits:
Struct
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



7
8
9
# File 'lib/pg_trunk/core/qualified_name.rb', line 7

def name
  @name
end

#schemaObject

Returns the value of attribute schema

Returns:

  • (Object)

    the current value of 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

Examples:

QualifiedName["bar"].to_h
# => { schema: "public", name: "bar" }

QualifiedName["foo.bar(a int, b int)"].to_h
# => { schema: "foo", name: "bar(a int, b int)" }


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

Returns:

  • (Integer, NilClass)


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_typesObject

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

#argsObject

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)

Returns:

  • (Boolean)


41
42
43
# File 'lib/pg_trunk/core/qualified_name.rb', line 41

def blank?
  routine.blank?
end

#current_schemaObject

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+

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


52
53
54
# File 'lib/pg_trunk/core/qualified_name.rb', line 52

def differs_from?(regex)
  !routine.match?(regex) if present?
end

#fullObject

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

#leanObject

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

Returns:

  • (Boolean)


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 have the same schema/name as another function

Returns:

  • (Boolean)


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

#namespaceObject

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

#quotedObject

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

#returnsObject

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

#routineObject

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