lib/conceptql/nodes/define.rb in conceptql-0.0.5 vs lib/conceptql/nodes/define.rb in conceptql-0.0.6
- old
+ new
@@ -11,10 +11,14 @@
# Also, sometimes a particular piece of a concept is used in many places,
# so it makes sense to write that piece out once, store it as a "variable"
# and then insert that variable into the concept as needed.
# run the query once and subsequent calls
class Define < Node
+ def initialize(*args)
+ super
+ tree.defined[table_name] = self
+ end
# Create a temporary table and store the stream of results in that table.
# This "caches" the results so we only have to execute stream's query
# once.
#
# The logic here is that if something is assigned to a variable, chances
@@ -37,38 +41,25 @@
# Perhaps in the future we can find a way around this.
#
# Also, things will blow up if you try to use a variable that hasn't been
# defined yet.
def query(db)
- table_name = namify(arguments.first)
- stash_types(db, table_name, types)
db.create_table!(table_name, temp: true, as: stream.evaluate(db))
- db[db.send(:create_table_as_sql, table_name, stream.evaluate(db).sql, temp: true)]
+ db.from(table_name)
end
def types
stream.types
end
+ def sql(db)
+ db[db.send(:create_table_as_sql, table_name, stream.evaluate(db).sql, temp: true)].sql
+ end
+
private
- # TODO: Fix this. This is shameful.
- # In order to remember which types a table is storing, we need to preserve
- # the type information outside of the "define" statement because the "recall"
- # statement most likely doesn't have a reference to this node (that's the
- # whole point).
- #
- # There is only one object shared between all the nodes: the database
- # connection. We'll do something terrible and piggyback the type
- # information about this variable on the database connection so that
- # the "recall" node can pull that information back out.
- #
- # Ugh.
- def stash_types(db, name, types)
- def db.types
- @types ||= {}
- end
- db.types[name] = types
+ def table_name
+ @table_name ||= namify(arguments.first)
end
end
end
end