Class: Sequel::Postgres::PropertyGraph::Table
- Includes:
- SQL::AliasMethods
- Defined in:
- lib/sequel/adapters/shared/postgres.rb
Overview
Represents a GRAPH_TABLE expression, used to query a property graph via graph pattern matching. This is used in place of a table name expression or dataset in a SELECT query. These are created by calling #graph_table on the related Database object.
Table uses a method chaining design, similar to Dataset, where methods return modified frozen copies of the object.
Instance Attribute Summary collapse
-
#columns(*cols) ⇒ Object
readonly
Return a modifies copy that uses the given columns.
-
#elements ⇒ Object
readonly
A frozen array of Element instances, representing the vertices and edges in the graph pattern.
-
#name ⇒ Object
readonly
The name of the property graph the table is querying.
Class Method Summary collapse
-
.create(graph_name, initial_vertex_label, initial_vertex_opts) ⇒ Object
Create a new Table with the given
graph_name, withinitial_vertex_labelandinitial_vertex_optsbeing used to create the initial vertex.
Instance Method Summary collapse
-
#add_columns(*cols) ⇒ Object
Return a modified copy that adds the given columns to the existing list of columns for the graph table.
-
#columns_used ⇒ Object
A frozen array of the columns used in the COLUMNS clause (aliased as columns_used, as #columns is used to modify the columns).
-
#from(label, opts = OPTS) ⇒ Object
Similar to #link, but uses a directed link from the new element to the previous element (<- in the graph pattern).
-
#initialize(name, elements, columns) ⇒ Table
constructor
A new instance of Table.
-
#link(label, opts = OPTS) ⇒ Object
Return a modified copy with an element added using a bidirectional link (- in the graph pattern).
-
#sql_literal_append(ds, sql) ⇒ Object
Append the SQL for the GRAPH_TABLE expression to the given SQL string.
-
#to(label, opts = OPTS) ⇒ Object
Similar to #link, but uses a directed link from the previous element to the new element (-> in the graph pattern).
Methods included from SQL::AliasMethods
Constructor Details
#initialize(name, elements, columns) ⇒ Table
Returns a new instance of Table.
687 688 689 690 691 692 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 687 def initialize(name, elements, columns) @name = name @elements = elements @columns = columns freeze end |
Instance Attribute Details
#columns(*cols) ⇒ Object (readonly)
Return a modifies copy that uses the given columns. A graph table must have a least one column set before it is used in a query.
DB.graph_table(:gn, :v).columns(:a, Sequel[:b].as(:c))
# GRAPH_TABLE (gn MATCH (IS v) COLUMNS (a, b AS c))
676 677 678 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 676 def columns @columns end |
#elements ⇒ Object (readonly)
A frozen array of Element instances, representing the vertices and edges in the graph pattern.
672 673 674 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 672 def elements @elements end |
#name ⇒ Object (readonly)
The name of the property graph the table is querying.
668 669 670 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 668 def name @name end |
Class Method Details
.create(graph_name, initial_vertex_label, initial_vertex_opts) ⇒ Object
Create a new Table with the given graph_name, with initial_vertex_label
and initial_vertex_opts being used to create the initial vertex.
See Table#link for which options are supported for the initial vertex.
682 683 684 685 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 682 def self.create(graph_name, initial_vertex_label, initial_vertex_opts) vertex = Element.create(:vertex, "", initial_vertex_label, initial_vertex_opts) new(graph_name, [vertex].freeze, [].freeze) end |
Instance Method Details
#add_columns(*cols) ⇒ Object
Return a modified copy that adds the given columns to the existing list of columns for the graph table.
745 746 747 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 745 def add_columns(*cols) columns(*@columns, *cols) end |
#columns_used ⇒ Object
A frozen array of the columns used in the COLUMNS clause (aliased as columns_used, as #columns is used to modify the columns).
677 678 679 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 677 def columns @columns end |
#from(label, opts = OPTS) ⇒ Object
Similar to #link, but uses a directed link from the new element to the previous element (<- in the graph pattern). Accepts same arguments and options as #link.
DB.graph_table(:gn, :v).from(:e)
# GRAPH_TABLE (gn MATCH (IS v)<-[IS e])
730 731 732 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 730 def from(label, opts=OPTS) append_element('<-', label, opts) end |
#link(label, opts = OPTS) ⇒ Object
Return a modified copy with an element added using a bidirectional link
(- in the graph pattern).
label specifies the label restriction for the element. This can be
nil for no label restriction, or an array or set to restrict to the
given labels.
Options supported:
:var :: Specifies a graph pattern variable name for the element,
usable in the WHERE or COLUMNS clauses.
:vertex :: Specifies that the element being linked to is a vertex.
This allows for direct vertex<->vertex linking, instead of
the default vertex<->edge<->vertex linking.
:where :: An expression to use for the WHERE clause for the element.
DB.graph_table(:gn, :v).link(:e)
# GRAPH_TABLE (gn MATCH (IS v)-[IS e])
710 711 712 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 710 def link(label, opts=OPTS) append_element('-', label, opts) end |
#sql_literal_append(ds, sql) ⇒ Object
Append the SQL for the GRAPH_TABLE expression to the given SQL string. Requires graph table have at least one column set.
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 751 def sql_literal_append(ds, sql) if @columns.empty? raise Error, "cannot use graph_table in a query if it does not return any columns" end if @elements.last.type == :edge raise Error, "cannot use graph_table in a query if the last element is an edge" end sql << "GRAPH_TABLE (" ds.literal_append(sql, @name) sql << " MATCH " @elements.each do |element| marker = element.marker var = element.var label = element.label where = element.where vertex = element.type == :vertex sql << marker sql << (vertex ? '(' : '[') ds.literal_append(sql, var) if var if label sql << (var ? " IS " : "IS ") if label.is_a?(Array) label_sep = "" label.each do |l| sql << label_sep label_sep = "|" if label_sep.empty? ds.literal_append(sql, l) end else ds.literal_append(sql, label) end end if where sql << ((var || label) ? " WHERE " : "WHERE ") ds.literal_append(sql, where) end sql << (vertex ? ')' : ']') end sql << " COLUMNS " ds.literal_append(sql, @columns) sql << ")" end |
#to(label, opts = OPTS) ⇒ Object
Similar to #link, but uses a directed link from the previous element to the new element (-> in the graph pattern). Accepts same arguments and options as #link.
DB.graph_table(:gn, :v).to(:e)
# GRAPH_TABLE (gn MATCH (IS v)->[IS e])
720 721 722 |
# File 'lib/sequel/adapters/shared/postgres.rb', line 720 def to(label, opts=OPTS) append_element('->', label, opts) end |