Class: SyntaxTree::Database::AttrQuery
- Inherits:
-
Object
- Object
- SyntaxTree::Database::AttrQuery
- Defined in:
- lib/syntax_tree/database.rb
Overview
Query for the attributes of a node, optionally also filtering by type.
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #each(database, &block) ⇒ Object
-
#initialize(type, attrs) ⇒ AttrQuery
constructor
A new instance of AttrQuery.
Constructor Details
#initialize(type, attrs) ⇒ AttrQuery
Returns a new instance of AttrQuery.
102 103 104 105 |
# File 'lib/syntax_tree/database.rb', line 102 def initialize(type, attrs) @type = type @attrs = attrs end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
100 101 102 |
# File 'lib/syntax_tree/database.rb', line 100 def attrs @attrs end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
100 101 102 |
# File 'lib/syntax_tree/database.rb', line 100 def type @type end |
Instance Method Details
#each(database, &block) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/syntax_tree/database.rb', line 107 def each(database, &block) joins = [] binds = [] attrs.each do |name, query| ids = query.each(database).map { |row| row[0] } joins << <<~SQL JOIN edges AS #{name} ON #{name}.from_id = nodes.id AND #{name}.name = ? AND #{name}.to_id IN (#{(["?"] * ids.size).join(", ")}) SQL binds.push(name).concat(ids) end sql = +"SELECT nodes.* FROM nodes, edges #{joins.join(" ")}" if type sql << " WHERE nodes.type = ?" binds << type end sql << " GROUP BY nodes.id" database.execute(sql, binds).each(&block) end |