From: Jamey Cribbs Date: 2006-03-05T10:34:45+09:00 Subject: Re: Kirbybase Hacks NilClass Jamey Cribbs wrote: > angus wrote: > >> But doesn't NULL always propagate in "database semantics"? I mean, if >> any >> subexpression is NULL, the whole expression becomes NULL. It's the >> expected >> behaviour (but maybe not to the users of KirbyBase). In SQL, >> normally, one >> uses NVL(a, b) (with the meaning 'a.nil? ? b : a') to guard >> subexpressions >> against NULL. >> >> > I'm not so sure about that. Here's a link to a page talking about how > the developer of SQLite decided how to treat NULL: > > http://www.sqlite.org/nulls.html > > If I read it correctly, he says that all of the major SQL dbms' treat > "null OR true" as true. Maybe I'm reading this page wrong, but that > seems to go against what you are saying. > > I'm definitely not a database expert, so anyone with knowledge on > this, please chime in. > > In the meantime, I'm going to start taking a look at adding a Null > class (or some such construct) to KirbyBase, to see if I can do away > with the NilClass#method_missing issue. Here's another follow-up to Angus comment about NULL values in OR expressions that, to me, is evidence that SQL treats the expression "null OR true" as true. This excerpt is from: http://databases.about.com/cs/sql/a/aa042803a.htm "The OR operand treats NULL values in a similar fashion. If the other operand is TRUE, the result of the OR is TRUE (because the real value of the NULL operand doesn�t matter.) On the other hand, if the other operand is either FALSE or NULL, the result of the OR operation is NULL." Here's a link to a Microsoft page talking about NULL. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_qd_02_8pwy.asp It says, in the second table, that the result of applying an OR operator to two Boolean operands, one being NULL, and the other being TRUE, is TRUE. Although, to be honest, I don't really care how SQL does it. One reason I wrote KirbyBase is because I don't like using SQL in my programs and the thought of being able to express my query in Ruby code was very attractive. I want KirbyBase to follow Matz Principle of Least Surprise motto. To me, if I have a query that says, "nil > 300 or 800 < 900", I think that query is true. That, to me follows POLS, but of course, everyone has different ideas of what is Surprising. :-) I know James said in an earlier email that the proper Ruby expression, in order to make sure the query didn't throw an exception is: #select { |r| r.speed and r.speed > 300 } But, even if that is "proper" Ruby code, I don't want the user to have to do that for *every* freakin query just because one of the field values *might* be nil. What if you were testing for three or four conditions? To cover your ass, you would have to write a query like this: #select { |r| (r.speed and r.speed > 300) or (r.range and r.range < 900) or (r.service_date and r.service_date < Date.today) } That's just ugly. You should be able to just write: #select { |r| r.speed > 300 or r.range < 900 or r.service_date < Date.today } To me, that is POLS. Maybe you don't agree with me. I guess that means that this is my contribution to "Opinionated Software". :-) Anyway, I hear ya, James. You want me to quit messin' around with NilClass. Let me see what I can whip up with a Null class or maybe I can subclass Nil (I'll call it KBNil, or something) and override the #method_missing method in the subclass. Jamey