Report abuse

[5:43pm] hasmanyjosh: nzkoz: nick is here. 
[5:43pm] hasmanyjosh: nzkoz: he's taking the kbd now
[5:43pm] nkallen: hi nzkoz
[5:43pm] nkallen: ok so i heard you're interested in the refactoring work i'd like to do to AR
[5:43pm] nkallen: basically I'm working on a prototype - I'll have something to show this weekend. I must warn that what I propose to do is maybe radical and would entail a fair amount of work
[5:44pm] nkallen: nzkoz: you there?
[5:45pm] nkallen: guess not -- but you'll prolly see this later. OK. So the long and the short of what I'm doing is to use a Relational Algebra to mediate between AR and the database. You can read about Relational Algebras on Wikipedia.
[5:45pm] nzkoz: yeah sorry
[5:45pm] nzkoz: doing the dishes
[5:45pm] • nzkoz did a maths degree
[5:45pm] nzkoz: that may be too radical, but an interesting experiment anyway
[5:45pm] nkallen: They have some compelling advantages, in my opinion, over having a sophisticated sql builder (a la something like Sequel, or SqlAlchemy, a Python ORM)
[5:45pm] nzkoz: I certainly want something ti mediate between the two
[5:46pm] nzkoz: yeah, I'd be interested to see the results
[5:46pm] nkallen: I think there are overwhelmingly compelling reasons to stop concatenating strings to generate SQL queries. There is massive duplication.
[5:46pm] nzkoz: yeah that much is absolutely a given
[5:46pm] nkallen: The :include / :join JoinDependency stuff is insanely complex.
[5:47pm] nkallen: I feel like a better foundation would really take AR to the next level without really changing its interface to the end user.
[5:47pm] nzkoz: some kind of api that you can pass a 'query' or similar around then call .to_sql or something would be the obvious choice
[5:47pm] nkallen: right.
[5:47pm] nzkoz: would also enable prepared statement spport for databases which need it
[5:47pm] nzkoz: oracle f.ex
[5:48pm] nzkoz: as this 'query' would be able to generate the statement and arguments
[5:48pm] nkallen: so the typical approach taken by the more sophisticated ORMs is to have a very sophisticated query builder.
[5:48pm] nkallen: SqlAlchemy is a really interesting example of this approach. It's very nice and would work well in AR I think.
[5:48pm] nzkoz: basically improving the query generation is what I'm interested in, but if you can do something with the same results but a different approach I'd look at that too
[5:49pm] nkallen: The problem is that you're essentially generating a select query. A Relational algebra, OTOH is working with data as opposed to queries. So the same "algebra" (joins, etc.) are used when you're doing an insert and a select
[5:49pm] nkallen: This obviates the need for among other things with_scope
[5:49pm] nzkoz: yeah,  with_scope is really only used for conditions = merge_conditions(my_conditions, some_other_conditions)
[5:49pm] nkallen: so I am building a couple things. First is a SqlBuilder. This is fairly straightforward.
[5:50pm] • nzkoz lets the man talk 
[5:50pm] nkallen: nzkoz: right, but there's no need to have condition merging logic in with_scope with a relational algebra, since that's essentially what the operators in the algebra do.
[5:50pm] nkallen: The second piece of this is a Relational Algebra that implements to_sql, basically building a datastructure with the builder.
[5:51pm] nkallen: I've already implemented most of both of these things. I've implemented, for example, has_many, has_many :through in a way that is fairly compelling:
[5:51pm] nkallen: I can implement has_many :through in one line of code such that it will work uniformly when the thru reflection is a belongs_to or a has_many, or the belongs_to is polymorphic.
[5:52pm] nkallen: This is something that is handled with specialcases in HMTAssociation
[5:52pm] nzkoz: yeah, sounds awesome
[5:52pm] nkallen: Furthermore, this logic is specified in one place meaning the giant case statment in JoinDependency disappears
[5:52pm] nkallen: What I am workin on now, and would like to show off when I am completed, is demonstrating how I can do :includes and :joins in 20 lines of code, rather than the 200+ in associations.rb
[5:53pm] nkallen: This includes eager loading.
[5:53pm] nzkoz: indeed this does sound compelling
[5:54pm] nkallen:  There are also some features that a relational algebra will make possible in AR that were not possible or were difficult before.
[5:54pm] nzkoz: I'll confess to being skeptical about anything that uses the phrase 'relational algebra' but that's just my dislike of Date's latest book
[5:54pm] nkallen: For example, acts_as_paranoid will be trivial.
[5:54pm] nkallen: no need to alias method chain find, etc. You can set the relation for an AR class rather than set_table
[5:54pm] nzkoz: yeah
[5:54pm] nkallen: It also makes HasFinder (a gem I wrote) basically trivial
[5:55pm] nzkoz: I'm completely sold on moving all our queries and the like to something like this
[5:55pm] nkallen: So anyway, the proof is in the pudding. remain skeptical until I show you the code which will be forthcoming.
[5:55pm] nzkoz: I suspect I'm biased having been on the dbdebunk clueless-person's list
[5:55pm] nzkoz: but yeah.  s/relation/table/ and it'll still work 
[5:56pm] nzkoz: Please don't kill me mr pascal
[5:56pm] nkallen: Bear in mind that I am proposing both a very formal mathematical foundation for AR which may be distasteful to some. Also, what this entails in terms of the number of lines to change in AR will be quite large.
[5:56pm] nzkoz: the lines changed don't bother me, they're mostly shitty lines
[5:56pm] nkallen: But, in the end, the math is quite simple. It's really intuitive it just has its own jargon
[5:56pm] nzkoz: exactly, and I'd probably suggest *not* using that jargon
[5:56pm] bitsweat: I remember shuffling those methods around
[5:57pm] nzkoz: just to keep it approachable
[5:57pm] nkallen: nzkoz: we can totally discuss that. I like making code simple / easy to understand.
[5:57pm] nzkoz: so yeah, whenever you have something to show feel free to grab me
[5:57pm] nkallen: So anyway. I'll have some interesting code to show very soon. Hopefully Sunday
[5:58pm] nzkoz: cool
[5:58pm] nkallen: OK so I'm going to go see a movie now. TTYL thanks for expressing interest. I really hope you'll like my stuff. I think rails will benefit from it.
[5:58pm] nzkoz: I'll make a point of being around when I can
[5:58pm] nzkoz: thanks for doing the work!