class Sequel::Postgres::PropertyGraph::Table

  1. lib/sequel/adapters/shared/postgres.rb
Superclass: Object

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.

Methods

Public Class

  1. create
  2. new

Public Instance

  1. add_columns
  2. columns
  3. elements
  4. from
  5. link
  6. name
  7. sql_literal_append
  8. to

Included modules

  1. SQL::AliasMethods

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)

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.

[show source]
    # 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)
[show 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)

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

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

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])
[show source]
    # 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)

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

[show source]
    # 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)

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