Thread: [SQLObject] Can SQLBuilder do this?
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Markus G. <m.g...@gm...> - 2007-08-20 18:41:47
|
Hi again, can I use SQLBuilder in some way to perform the query below, so that I 1) do not have to write SQL directly and 2) can avoid the manual conversion to a date object? for row in connection.queryAll( 'SELECT DISTINCT date(entry.begin) FROM %s WHERE %s' % ( ', '.join( query.tablesUsed() ), connection.sqlrepr( query ) ) ): entryDate = row[ 0 ] # MySQLdb returns datetime.date objects. # pysqlite returns unicode objects of the form u'2007-08-19'. if not isinstance( entryDate, datetime.date ): entryDate = datetime.datetime.strptime( entryDate, '%Y-%m-%d' ).date() # do something with entryDate here Kind regards, Markus |
From: Oleg B. <ph...@ph...> - 2007-08-20 18:52:50
|
On Mon, Aug 20, 2007 at 08:41:42PM +0200, Markus Gritsch wrote: > can I use SQLBuilder in some way to perform the query below, so that I > 1) do not have to write SQL directly and 2) can avoid the manual > conversion to a date object? 1 contradicts 2 because SQLBuilder doesn't perform any conversion. Column objects perform conversions. So, either SQLObject with conversions or SQLBuilder with raw data, > for row in connection.queryAll( 'SELECT DISTINCT date(entry.begin) SELECT DISTINCT in SQLBuilder is implemented in the trunk by Luke Opperman. Wait until it will be released as SQLObject 0.10, or use the code from the trunk, it is stable enough to be useable. from sqlobject.sqlbuilder import Select, func for row in conn.queryAll(conn.sqlrepr( Select(func.date(Entry.q.begin), distinct=True) )) Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Markus G. <m.g...@gm...> - 2007-08-20 19:02:50
|
2007/8/20, Oleg Broytmann <ph...@ph...>: > 1 contradicts 2 because SQLBuilder doesn't perform any conversion. > Column objects perform conversions. So, either SQLObject with conversions > or SQLBuilder with raw data, I see. > > for row in connection.queryAll( 'SELECT DISTINCT date(entry.begin) > > SELECT DISTINCT in SQLBuilder is implemented in the trunk by Luke > Opperman. Wait until it will be released as SQLObject 0.10, or use the code > from the trunk, it is stable enough to be useable. > > from sqlobject.sqlbuilder import Select, func > for row in conn.queryAll(conn.sqlrepr( > Select(func.date(Entry.q.begin), distinct=True) > )) Ok. And is there a way to put the 'query' variable, which is of __class__ sqlobject.sqlbuilder.SQLOp, from my original code into the SQLBuilder version? The query is already constructed elsewhere and passed around in my program. Kind regards, Markus |
From: Oleg B. <ph...@ph...> - 2007-08-20 19:18:12
|
On Mon, Aug 20, 2007 at 09:02:46PM +0200, Markus Gritsch wrote: > > from sqlobject.sqlbuilder import Select, func > > for row in conn.queryAll(conn.sqlrepr( > > Select(func.date(Entry.q.begin), distinct=True) > > )) > > Ok. And is there a way to put the 'query' variable, which is of > __class__ sqlobject.sqlbuilder.SQLOp, from my original code into the > SQLBuilder version? Try Select(func.date(Entry.q.begin), clause=query, distinct=True) Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Markus G. <m.g...@gm...> - 2007-08-20 19:21:04
|
2007/8/20, Oleg Broytmann <ph...@ph...>: > On Mon, Aug 20, 2007 at 09:02:46PM +0200, Markus Gritsch wrote: > > Ok. And is there a way to put the 'query' variable, which is of > > __class__ sqlobject.sqlbuilder.SQLOp, from my original code into the > > SQLBuilder version? > > Try > > Select(func.date(Entry.q.begin), clause=query, distinct=True) Thank you again, Oleg. I will have a look into it. Kind regards, Markus |