[SQL-CVS] r207 - in trunk/SQLObject: docs sqlobject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2004-08-30 21:11:38
|
Author: ianb Date: 2004-08-30 13:01:33 -0400 (Mon, 30 Aug 2004) New Revision: 207 Modified: trunk/SQLObject/docs/News.txt trunk/SQLObject/sqlobject/dbconnection.py trunk/SQLObject/sqlobject/main.py Log: New classmethod createTableSQL() =20 Modified: trunk/SQLObject/docs/News.txt =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/SQLObject/docs/News.txt 2004-08-25 20:30:11 UTC (rev 206) +++ trunk/SQLObject/docs/News.txt 2004-08-30 17:01:33 UTC (rev 207) @@ -56,6 +56,10 @@ This is a temporary interface; a more general specifier for primary keys will be added later. =20 +* New classmethod ``createTableSQL()`` method for SQLObject classes, + which returns the SQL that can be used to create the table. Analog + to ``createTable()``. + Bugs ---- =20 Modified: trunk/SQLObject/sqlobject/dbconnection.py =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/SQLObject/sqlobject/dbconnection.py 2004-08-25 20:30:11 UTC (re= v 206) +++ trunk/SQLObject/sqlobject/dbconnection.py 2004-08-30 17:01:33 UTC (re= v 207) @@ -313,20 +313,26 @@ return q =20 def _SO_createJoinTable(self, join): - self.query('CREATE TABLE %s (\n%s %s,\n%s %s\n)' % - (join.intermediateTable, - join.joinColumn, - self.joinSQLType(join), - join.otherColumn, - self.joinSQLType(join))) + self.query(self._SO_createJoinTableSQL(join)) =20 + def _SO_createJoinTableSQL(self, join): + return ('CREATE TABLE %s (\n%s %s,\n%s %s\n)' % + (join.intermediateTable, + join.joinColumn, + self.joinSQLType(join), + join.otherColumn, + self.joinSQLType(join))) + def _SO_dropJoinTable(self, join): self.query("DROP TABLE %s" % join.intermediateTable) =20 def createTable(self, soClass): - self.query('CREATE TABLE %s (\n%s\n)' % \ - (soClass._table, self.createColumns(soClass))) + self.query(self.createTableSQL(soClass)) =20 + def createTableSQL(self, soClass): + return ('CREATE TABLE %s (\n%s\n)' %=20 + (soClass._table, self.createColumns(soClass))) + def createColumns(self, soClass): columnDefs =3D [self.createIDColumn(soClass)] \ + [self.createColumn(soClass, col) Modified: trunk/SQLObject/sqlobject/main.py =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/SQLObject/sqlobject/main.py 2004-08-25 20:30:11 UTC (rev 206) +++ trunk/SQLObject/sqlobject/main.py 2004-08-30 17:01:33 UTC (rev 207) @@ -975,25 +975,41 @@ cls.createJoinTables(ifNotExists=3DifNotExists) createTable =3D classmethod(createTable) =20 + def createTableSQL(cls, createJoinTables=3DTrue): + sql =3D cls._connection.createTableSQL(cls) + if createJoinTables: + sql +=3D '\n' + cls.createJoinTablesSQL() + return sql + createTableSQL =3D classmethod(createTableSQL) + def createJoinTables(cls, ifNotExists=3DFalse): + for join in cls._getJoinsToCreate(): + if ifNotExists and \ + cls._connection.tableExists(join.intermediateTable): + continue + cls._connection._SO_createJoinTable(join) + createJoinTables =3D classmethod(createJoinTables) + + def createJoinTablesSQL(cls): + sql =3D [] + for join in cls._getJoinsToCreate(): + sql.append(cls._connection._SO_createJoinTableSQL(join)) + return '\n'.join(sql) + createJoinTablesSQL =3D classmethod(createJoinTablesSQL) + + def _getJoinsToCreate(cls): + joins =3D [] for join in cls._SO_joinList: if not join: continue if not join.hasIntermediateTable(): continue - # This join will show up twice, in each of the - # classes, but we only create the table once. We - # arbitrarily create it while we're creating the - # alphabetically earlier class. if join.soClass.__name__ > join.otherClass.__name__: continue - if ifNotExists and \ - cls._connection.tableExists(join.intermediateTable): - continue - cls._connection._SO_createJoinTable(join) + joins.append(join) + return joins + _getJoinsToCreate =3D classmethod(_getJoinsToCreate) =20 - createJoinTables =3D classmethod(createJoinTables) - def dropJoinTables(cls, ifExists=3DFalse): for join in cls._SO_joinList: if not join: |