Author: phd
Date: Sat Sep 24 09:47:26 2011
New Revision: 4456
Log:
Strings are treated specially in Select to allow Select(['id, 'name'], where='value = 42').
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/docs/SQLBuilder.txt
SQLObject/trunk/docs/TODO.txt
SQLObject/trunk/sqlobject/sqlbuilder.py
SQLObject/trunk/sqlobject/tests/test_sqlbuilder.py
Modified: SQLObject/trunk/docs/News.txt
==============================================================================
--- SQLObject/trunk/docs/News.txt Tue Aug 30 09:15:11 2011 (r4455)
+++ SQLObject/trunk/docs/News.txt Sat Sep 24 09:47:26 2011 (r4456)
@@ -13,6 +13,9 @@
Features & Interface
--------------------
+* Strings are treated specially in Select to allow
+ Select(['id, 'name'], where='value = 42').
+
* A bug caused by psycopg2 recently added a new boolean not callable
autocommit attribute was fixed.
Modified: SQLObject/trunk/docs/SQLBuilder.txt
==============================================================================
--- SQLObject/trunk/docs/SQLBuilder.txt Tue Aug 30 09:15:11 2011 (r4455)
+++ SQLObject/trunk/docs/SQLBuilder.txt Sat Sep 24 09:47:26 2011 (r4456)
@@ -98,25 +98,23 @@
to execute the query and get back the results as a list of rows.
`items`:
- An SQLExpression or a sequence of SQLExpression's, represents the
- list of columns. If there are q-values SQLExpression's ``Select``
- derives a list of tables for SELECT query.
+ A string, an SQLExpression or a sequence of strings or
+ SQLExpression's, represents the list of columns. If there are
+ q-values SQLExpression's ``Select`` derives a list of tables for
+ SELECT query.
`where`:
- An SQLExpression, represents the ``WHERE`` clause.
+ A string or an SQLExpression, represents the ``WHERE`` clause.
`groupBy`:
- An SQLExpression, represents the ``GROUP BY`` clause.
+ A string or an SQLExpression, represents the ``GROUP BY`` clause.
`having`:
- An SQLExpression, represents the ``HAVING`` part of the ``GROUP BY``
- clause.
+ A string or an SQLExpression, represents the ``HAVING`` part of the
+ ``GROUP BY`` clause.
`orderBy`:
- An SQLExpression, represents the ``ORDER BY`` clause.
-
-`limit`:
- An SQLExpression, represents the ``LIMIT`` clause.
+ A string or an SQLExpression, represents the ``ORDER BY`` clause.
`join`:
A (list of) JOINs (``LEFT JOIN``, etc.)
@@ -125,8 +123,10 @@
A bool flag to turn on ``DISTINCT`` query.
`start`, `end`:
- Integers. Alternative ways to calculate ``LIMIT``. `limit`, if passed,
- overrides `end`.
+ Integers. The way to calculate ``OFFSET`` and ``LIMIT``.
+
+`limit`:
+ An integer. `limit`, if passed, overrides `end`.
`reversed`:
A bool flag to do ``ORDER BY`` in the reverse direction.
@@ -137,7 +137,7 @@
`staticTables`:
A sequence of strings or SQLExpression's that name tables for
``FROM``. This parameter must be used if `items` is a list of strings
- from which Select cannot derive a list of tables.
+ from which Select cannot derive the list of tables.
Insert
~~~~~~
Modified: SQLObject/trunk/docs/TODO.txt
==============================================================================
--- SQLObject/trunk/docs/TODO.txt Tue Aug 30 09:15:11 2011 (r4455)
+++ SQLObject/trunk/docs/TODO.txt Sat Sep 24 09:47:26 2011 (r4456)
@@ -1,8 +1,6 @@
TODO
----
-* Make strings special-cased in Select to allow Select(['name', 'value']).
-
* Allow to override ConsoleWriter/LogWriter classes and makeDebugWriter
function.
Modified: SQLObject/trunk/sqlobject/sqlbuilder.py
==============================================================================
--- SQLObject/trunk/sqlobject/sqlbuilder.py Tue Aug 30 09:15:11 2011 (r4455)
+++ SQLObject/trunk/sqlobject/sqlbuilder.py Sat Sep 24 09:47:26 2011 (r4456)
@@ -95,6 +95,12 @@
else:
return expr
+
+def _str_or_sqlrepr(expr, db):
+ if isinstance(expr, basestring):
+ return expr
+ return sqlrepr(expr, db)
+
########################################
## Expression generation
########################################
@@ -372,9 +378,7 @@
raise AttributeError
return self.FieldClass(self.tableName, attr)
def __sqlrepr__(self, db):
- if isinstance(self.tableName, str):
- return self.tableName
- return sqlrepr(self.tableName, db)
+ return _str_or_sqlrepr(self.tableName, db)
def execute(self, executor):
raise ValueError, "Tables don't have values"
@@ -616,11 +620,11 @@
if self.ops['distinct']:
select += " DISTINCT"
if self.ops['distinctOn'] is not NoDefault:
- select += " ON(%s)" % sqlrepr(self.ops['distinctOn'], db)
+ select += " ON(%s)" % _str_or_sqlrepr(self.ops['distinctOn'], db)
if not self.ops['lazyColumns']:
- select += " %s" % ", ".join([str(sqlrepr(v, db)) for v in self.ops['items']])
+ select += " %s" % ", ".join([str(_str_or_sqlrepr(v, db)) for v in self.ops['items']])
else:
- select += " %s" % sqlrepr(self.ops['items'][0], db)
+ select += " %s" % _str_or_sqlrepr(self.ops['items'][0], db)
join = []
join_str = ''
@@ -644,8 +648,9 @@
if isinstance(thing, SQLExpression):
tables.update(tablesUsedSet(thing, db))
for j in join:
- t1, t2 = sqlrepr(j.table1, db), sqlrepr(j.table2, db)
+ t1 = _str_or_sqlrepr(j.table1, db)
if t1 in tables: tables.remove(t1)
+ t2 = _str_or_sqlrepr(j.table2, db)
if t2 in tables: tables.remove(t2)
if tables:
select += " FROM %s" % ", ".join(tables)
@@ -664,14 +669,14 @@
select += join_str
if self.ops['clause'] is not NoDefault:
- select += " WHERE %s" % sqlrepr(self.ops['clause'], db)
+ select += " WHERE %s" % _str_or_sqlrepr(self.ops['clause'], db)
if self.ops['groupBy'] is not NoDefault:
- groupBy = sqlrepr(self.ops['groupBy'], db)
+ groupBy = _str_or_sqlrepr(self.ops['groupBy'], db)
if isinstance(self.ops['groupBy'], (list, tuple)):
groupBy = groupBy[1:-1] # Remove parens
select += " GROUP BY %s" % groupBy
if self.ops['having'] is not NoDefault:
- select += " HAVING %s" % sqlrepr(self.ops['having'], db)
+ select += " HAVING %s" % _str_or_sqlrepr(self.ops['having'], db)
if self.ops['orderBy'] is not NoDefault and self.ops['orderBy'] is not None:
orderBy = self.ops['orderBy']
if self.ops['reversed']:
@@ -679,9 +684,9 @@
else:
reverser = lambda x: x
if isinstance(orderBy, (list, tuple)):
- select += " ORDER BY %s" % ", ".join([sqlrepr(reverser(x), db) for x in orderBy])
+ select += " ORDER BY %s" % ", ".join([_str_or_sqlrepr(reverser(x), db) for x in orderBy])
else:
- select += " ORDER BY %s" % sqlrepr(reverser(orderBy), db)
+ select += " ORDER BY %s" % _str_or_sqlrepr(reverser(orderBy), db)
start, end = self.ops['start'], self.ops['end']
if self.ops['limit'] is not NoDefault:
end = start + self.ops['limit']
Modified: SQLObject/trunk/sqlobject/tests/test_sqlbuilder.py
==============================================================================
--- SQLObject/trunk/sqlobject/tests/test_sqlbuilder.py Tue Aug 30 09:15:11 2011 (r4455)
+++ SQLObject/trunk/sqlobject/tests/test_sqlbuilder.py Sat Sep 24 09:47:26 2011 (r4456)
@@ -33,3 +33,14 @@
assert sqlrepr(AND(1, 2, '3'), "sqlite") == \
sqlrepr(SQLOp("AND", 1, SQLOp("AND", 2, '3')), "sqlite") == \
"((1) AND ((2) AND ('3')))"
+
+def test_str_or_sqlrepr():
+ select = Select(['id', 'name'], staticTables=['employees'],
+ where='value>0', orderBy='id')
+ assert sqlrepr(select, 'sqlite') == \
+ 'SELECT id, name FROM employees WHERE value>0 ORDER BY id'
+
+ select = Select(['id', 'name'], staticTables=['employees'],
+ where='value>0', orderBy='id', lazyColumns=True)
+ assert sqlrepr(select, 'sqlite') == \
+ 'SELECT id FROM employees WHERE value>0 ORDER BY id'
|