class Sequel::Postgres::PropertyGraph::Table

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.

Constants

Element

Internal struct for a single element (vertex or edge) in the graph pattern:

type

Either :vertex or :edge.

marker

Connector string to use for the element (empty for initial vertex).

label

Label restriction symbol or SQL::Identifier for the element, if any. Can be an array or set to match multiple labels.

var

Graph pattern variable symbol for the element, if any.

where

WHERE condition for the element, if any.

Attributes

columns[R]

A frozen array of the columns used in the COLUMNS clause (aliased as columns_used, as columns is used to modify the columns).

columns_used[R]

A frozen array of the columns used in the COLUMNS clause (aliased as columns_used, as columns is used to modify the columns).

elements[R]

A frozen array of Element instances, representing the vertices and edges in the graph pattern.

name[R]

The name of the property graph the table is querying.

Public Class Methods

create(graph_name, initial_vertex_label, initial_vertex_opts) click to toggle source

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.

    # File lib/sequel/adapters/shared/postgres.rb
682 def self.create(graph_name, initial_vertex_label, initial_vertex_opts)
683   vertex = Element.create(:vertex, "", initial_vertex_label, initial_vertex_opts)
684   new(graph_name, [vertex].freeze, [].freeze)
685 end
new(name, elements, columns) click to toggle source
    # File lib/sequel/adapters/shared/postgres.rb
687 def initialize(name, elements, columns)
688   @name = name
689   @elements = elements
690   @columns = columns
691   freeze
692 end

Public Instance Methods

add_columns(*cols) click to toggle source

Return a modified copy that adds the given columns to the existing list of columns for the graph table.

    # File lib/sequel/adapters/shared/postgres.rb
745 def add_columns(*cols)
746   columns(*@columns, *cols)
747 end
from(label, opts=OPTS) click to toggle source

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])
    # File lib/sequel/adapters/shared/postgres.rb
730 def from(label, opts=OPTS)
731   append_element('<-', label, opts)
732 end
sql_literal_append(ds, sql) click to toggle source

Append the SQL for the GRAPH_TABLE expression to the given SQL string. Requires graph table have at least one column set.

    # File lib/sequel/adapters/shared/postgres.rb
751 def sql_literal_append(ds, sql)
752   if @columns.empty?
753     raise Error, "cannot use graph_table in a query if it does not return any columns"
754   end
755   if @elements.last.type == :edge
756     raise Error, "cannot use graph_table in a query if the last element is an edge"
757   end
758 
759   sql << "GRAPH_TABLE ("
760   ds.literal_append(sql, @name)
761   sql << " MATCH "
762 
763   @elements.each do |element|
764     marker = element.marker
765     var = element.var
766     label = element.label
767     where = element.where
768     vertex = element.type == :vertex
769 
770     sql << marker
771     sql << (vertex ? '(' : '[')
772 
773     ds.literal_append(sql, var) if var
774     if label 
775       sql << (var ? " IS " : "IS ")
776       if label.is_a?(Array)
777         label_sep = ""
778         label.each do |l|
779           sql << label_sep
780           label_sep = "|" if label_sep.empty?
781           ds.literal_append(sql, l)
782         end
783       else
784         ds.literal_append(sql, label)
785       end
786     end
787 
788     if where
789       sql << ((var || label) ? " WHERE " : "WHERE ")
790       ds.literal_append(sql, where)
791     end
792 
793     sql << (vertex ? ')' : ']')
794   end
795 
796   sql << " COLUMNS "
797   ds.literal_append(sql, @columns)
798   sql << ")"
799 end
to(label, opts=OPTS) click to toggle source

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])
    # File lib/sequel/adapters/shared/postgres.rb
720 def to(label, opts=OPTS)
721   append_element('->', label, opts)
722 end

Private Instance Methods

append_element(marker, label, opts) click to toggle source

Internals of link, to, and from.

    # File lib/sequel/adapters/shared/postgres.rb
804 def append_element(marker, label, opts)
805   node_type = if opts[:vertex] 
806     :vertex
807   else
808     @elements.last.type == :vertex ? :edge : :vertex
809   end
810 
811   element = Element.create(node_type, marker, label, opts)
812   self.class.new(@name, (@elements.dup << element).freeze, @columns)
813 end