lib/piglet/relation/relation.rb in piglet-0.1.2 vs lib/piglet/relation/relation.rb in piglet-0.2.0

- old
+ new

@@ -137,19 +137,34 @@ # x.union(y, z) # => UNION x, y, z def union(*relations) Union.new(*([self] + relations)) end + def field(name) + type = schema.field_type(name) rescue nil + Field::Reference.new(name, self, :type => type) + end + + def schema + if @sources.nil? + raise Piglet::Schema::SchemaError, 'Could not determine the schema since there was no source relation and this relation does not define its own schema' + elsif @sources.size > 1 + raise Piglet::Schema::SchemaError, 'Could not determine the schema since there were more than one source relation' + else + @sources.first.schema + end + end + def method_missing(name, *args) if name.to_s =~ /^\w+$/ && args.empty? - Field::Reference.new(name, self) + field(name) else super end end def [](n) - Field::Reference.new("\$#{n}", self) + field("\$#{n}") end def hash self.alias.hash end \ No newline at end of file