On May 11, 2013, at 11:22 AM, Dan Farmer <dfarme...@gmail.com> wrote:

> 
> On Sat, May 11, 2013 at 6:37 AM, Michael Bayer <mike...@zzzcomputing.com> 
> wrote:
> Building a custom FROM clause though, a little tricky.   Feel free to send 
> along a whole working example.
> 
> Ok, thanks for the info. The Pivot example is something I've been working on 
> at work, so I can't send that one along. But I've got a few more to do that I 
> haven't started yet (silly copyright stuff) so maybe I'll give another one of 
> those a try this weekend and see if I can figure it out (if you don't mind me 
> sending a few more questions along in the process).
> 
> One question I had right away is the sequence of events between actually 
> querying the database and building the Columns for .c etc. I think I'm going 
> to try to implement UNPIVOT since it's fairly related and shouldn't give me 
> work issues. 
> http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=UNPIVOTData
>  
> 
> So I'm going to have something like
> 
> class Unpivot(FromClause):
>   def __init__(self):
>     self. (blah blah... set all of my parameters)
>   def _populate_column_collection(self):
>     # Make list of Columns, each one being _make_proxy(name)?
> 
> @compiles(Unpivot)
> def compile(element, compiler, **kw):
>   # Generate the actual SQL expression
> 
> Is there anything that needs to be done to actually tie those things together 
> so that the query actually gets run when I use Unpivot or is that somehow 
> "automagic?"
> 
> Thanks for pointing me in the right direction,

The ".c" collection on any FromClause typically initializes itself when you 
first access ".c" in Python, which results in the calling of 
_populate_column_collection().   If you don't actually call upon ".c", or try 
to select() from your FromClause, it's never called, even if you execute the 
FromClause.   The ".c" collection represents the columns "exported" by the 
FromClause that can be selected from - such as if you have this:

        select([table])

that compiles to something like:

        select a, b from table

The .c. collection of this select is like this:

        sel = select([table])
        sel2 = select([sel.c.a, sel.c.b])

sel2 compiles to this:

        select a, b from (select a, b from table)

the ".c" collection is dealing with the "a, b" on the *outside*.  The "a, b" on 
the *inside* comes from "table.c.a, table.c.b". 

so there's really not much that needs to tie together the .c. collection of a 
FromClause versus how it's turned into a string.   ".c." is there to serve the 
string-ification needs of an *enclosing* query around your FromClause.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to