[SQL-CVS] r2332 - in SQLObject/branches/sqlbuilder-views/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2007-02-20 17:45:45
|
Author: luke Date: 2007-02-20 10:45:05 -0700 (Tue, 20 Feb 2007) New Revision: 2332 Modified: SQLObject/branches/sqlbuilder-views/sqlobject/sqlbuilder.py SQLObject/branches/sqlbuilder-views/sqlobject/tests/test_sqlbuilder_importproxy.py SQLObject/branches/sqlbuilder-views/sqlobject/views.py Log: Cleaner ImportProxy, ViewSQLObjectMeta start Modified: SQLObject/branches/sqlbuilder-views/sqlobject/sqlbuilder.py =================================================================== --- SQLObject/branches/sqlbuilder-views/sqlobject/sqlbuilder.py 2007-02-20 17:17:27 UTC (rev 2331) +++ SQLObject/branches/sqlbuilder-views/sqlobject/sqlbuilder.py 2007-02-20 17:45:05 UTC (rev 2332) @@ -1139,7 +1139,7 @@ def tablesUsedImmediate(self): return [str(self.tableName)] -class ImportProxy(SQLObjectTable): +class ImportProxy(SQLExpression): '''Class to be used in column definitions that rely on other tables that might not yet be in a classregistry. ''' @@ -1153,7 +1153,7 @@ def __getattr__(self, attr): if self.soClass is None: return _Delay(self, attr) - return SQLObjectTable.__getattr__(self, attr) + return getattr(self.soClass.q, attr) class _Delay(SQLExpression): def __init__(self, proxy, attr): @@ -1163,11 +1163,8 @@ def __sqlrepr__(self, db): if self.proxy.soClass is None: return '_DELAYED_' + self.attr - return str(self._resolve()) + return sqlrepr(self._resolve(), db) - def __add__(self, other): - return str(self) + other - def _resolve(self): return getattr(self.proxy, self.attr) Modified: SQLObject/branches/sqlbuilder-views/sqlobject/tests/test_sqlbuilder_importproxy.py =================================================================== --- SQLObject/branches/sqlbuilder-views/sqlobject/tests/test_sqlbuilder_importproxy.py 2007-02-20 17:17:27 UTC (rev 2331) +++ SQLObject/branches/sqlbuilder-views/sqlobject/tests/test_sqlbuilder_importproxy.py 2007-02-20 17:45:05 UTC (rev 2332) @@ -1,5 +1,6 @@ from sqlobject import * from sqlobject.tests.dbtest import * +from sqlobject.views import * from sqlobject.sqlbuilder import ImportProxy def testSimple(): @@ -9,4 +10,30 @@ class NotYetImported(SQLObject): name = StringCol(dbName='a_name') - assert str(x) == 'not_yet_imported.a_name' \ No newline at end of file + y = nyi.q.name + + assert str(x) == 'not_yet_imported.a_name' + assert str(y) == 'not_yet_imported.a_name' + +def testAddition(): + nyi = ImportProxy('NotYetImported2') + x = nyi.q.name+nyi.q.name + + class NotYetImported2(SQLObject): + name = StringCol(dbName='a_name') + + assert str(x) == '((not_yet_imported2.a_name) + (not_yet_imported2.a_name))' + +def testOnView(): + nyi = ImportProxy('NotYetImportedV') + x = nyi.q.name + + class NotYetImported3(SQLObject): + name = StringCol(dbName='a_name') + + class NotYetImportedV(ViewSQLObject): + class sqlmeta: + idName = NotYetImported3.q.id + name = StringCol(dbName=NotYetImported3.q.name) + + assert str(x) == 'not_yet_imported_v.name' \ No newline at end of file Modified: SQLObject/branches/sqlbuilder-views/sqlobject/views.py =================================================================== --- SQLObject/branches/sqlbuilder-views/sqlobject/views.py 2007-02-20 17:17:27 UTC (rev 2331) +++ SQLObject/branches/sqlbuilder-views/sqlobject/views.py 2007-02-20 17:45:05 UTC (rev 2332) @@ -4,7 +4,7 @@ #from SQLObject import SQLObject #from sqlobject.col import Col, KeyCol from sqlbuilder import * -from main import SQLObject +from main import SQLObject, sqlmeta import types, threading @@ -57,6 +57,10 @@ else: return self.FieldClass(alias, self.tableName, column.name, attr) +class ViewSQLObjectMeta(sqlmeta): + pass + + class ViewSQLObject(SQLObject): '''A SQLObject class that derives all it's values from other SQLObject classes. Columns on subclasses should use SQLBuilder constructs for dbName, @@ -66,6 +70,10 @@ alias as an optional alternate name (as table is typically used for SQLObjects) See test_views.py for simple examples. ''' + + class sqlmeta(ViewSQLObjectMeta): + pass + def __classinit__(cls, new_attrs): SQLObject.__classinit__(cls, new_attrs) # like is_base @@ -81,7 +89,7 @@ else: columns.append(ColumnAS(col.dbName, n)) - clause = cls.sqlmeta.clause + clause = getattr(cls.sqlmeta, 'clause', NoDefault) select = Select(columns, distinct=True, distinctOn=cls.sqlmeta.idName, |