Class: Seaquel::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/seaquel/ast/node.rb

Overview

A general purpose node containing a node type and a list of arguments. The node stores a link to its parent or nil if no such parent exists. This class can be visited.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
# File 'lib/seaquel/ast/node.rb', line 11

def initialize *args
  if args.first.kind_of?(Symbol)
    @type, *@args = args
  else
    @parent, @type, *@args = args
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/seaquel/ast/node.rb', line 9

def args
  @args
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/seaquel/ast/node.rb', line 8

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/seaquel/ast/node.rb', line 7

def type
  @type
end

Instance Method Details

#fields(*list) ⇒ Object

INSERT INTO … (FIELDS) VALUES (…)



71
72
73
# File 'lib/seaquel/ast/node.rb', line 71

def fields *list
  node(:fields, list)
end

#from(*tables) ⇒ Object



28
29
30
# File 'lib/seaquel/ast/node.rb', line 28

def from *tables
  node(:from, *tables)
end

#group_by(*fields) ⇒ Object



57
58
59
# File 'lib/seaquel/ast/node.rb', line 57

def group_by *fields
  node(:group_by, fields)
end

#having(*exps) ⇒ Object



54
55
56
# File 'lib/seaquel/ast/node.rb', line 54

def having *exps
  node(:having, exps)
end

#inspectObject



112
113
114
# File 'lib/seaquel/ast/node.rb', line 112

def inspect
  [type, args, parent].inspect
end

#into(table) ⇒ Object



91
92
93
# File 'lib/seaquel/ast/node.rb', line 91

def into table
  node(:into, table)
end

#join(*tables) ⇒ Object



40
41
42
# File 'lib/seaquel/ast/node.rb', line 40

def join *tables
  node(:join, tables)
end

#limit(n) ⇒ Object



47
48
49
# File 'lib/seaquel/ast/node.rb', line 47

def limit n
  node(:limit, n)
end

#node(*args) ⇒ Object



95
96
97
# File 'lib/seaquel/ast/node.rb', line 95

def node *args
  self.class.new(self, *args)
end

#offset(n) ⇒ Object



50
51
52
# File 'lib/seaquel/ast/node.rb', line 50

def offset n
  node(:offset, n)
end

#on(*exps) ⇒ Object



43
44
45
# File 'lib/seaquel/ast/node.rb', line 43

def on *exps
  node(:on, exps)
end

#order_by(*list) ⇒ Node

Replaces previous ORDER BY specification with this one.

Parameters:

Returns:

  • (Node)

    node for chaining



66
67
68
# File 'lib/seaquel/ast/node.rb', line 66

def order_by *list
  node(:order_by, list)
end

#project(*fields) ⇒ Node

Replaces previous projection list with this one.

Parameters:

Returns:

  • (Node)

    node for chaining



24
25
26
# File 'lib/seaquel/ast/node.rb', line 24

def project *fields
  node(:project, fields)
end

#set(*assign_list) ⇒ Object



36
37
38
# File 'lib/seaquel/ast/node.rb', line 36

def set *assign_list
  node(:set, assign_list)
end

#to_sqlObject



108
109
110
# File 'lib/seaquel/ast/node.rb', line 108

def to_sql
  ::Seaquel::Generator.new(self).compact_sql
end

#values(*list) ⇒ AST::Node

Adds a values list to an INSERT statement. You have to pass all the columns at once; repeating this method call will create an INSERT statement that inserts multiple rows at once (a Postgres extension).

Example:

include Seaquel
insert.into(table('foo')).values(1, 2)
# => INSERT INTO "foo" VALUES (1, 2)

Parameters:

Returns:



87
88
89
# File 'lib/seaquel/ast/node.rb', line 87

def values *list
  node(:values, list)
end

#visit(visitor) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/seaquel/ast/node.rb', line 99

def visit visitor
  method_name = "visit_#{type}"
  if visitor.respond_to?(:visit_node)
    visitor.visit_node(self)
  else
    visitor.send(method_name, parent, *args)
  end
end

#where(*exps) ⇒ Object



32
33
34
# File 'lib/seaquel/ast/node.rb', line 32

def where *exps
  node(:where, JoinOp.new(:and, exps))
end