Update of /cvsroot/sqlobject/SQLObject/SQLObject
In directory sc8-pr-cvs1:/tmp/cvs-serv16872/SQLObject
Modified Files:
DBConnection.py SQLObject.py
Log Message:
Moved all SQL to DBConnection
Index: DBConnection.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/DBConnection.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** DBConnection.py 2 Apr 2003 18:19:26 -0000 1.10
--- DBConnection.py 6 Apr 2003 07:56:27 -0000 1.11
***************
*** 190,211 ****
return q
! def createJoinSQL(self, soClass):
! result = []
! for join in soClass._joins:
! 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.callingClass > join.otherClass:
! continue
! result.append('CREATE TABLE %s (\n%s %s,\n%s %s\n)'
! % (join.intermediateTable,
! join.joinColumn,
! self.joinSQLType(join),
! join.otherColumn,
! self.joinSQLType(join)))
! return result
def createTable(self, soClass):
--- 190,203 ----
return q
! 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)))
!
! def _SO_dropJoinTable(self, join):
! self.query("DROP TABLE %s" % join.intermediateTable)
def createTable(self, soClass):
***************
*** 231,234 ****
--- 223,292 ----
# that.
self.query("DELETE FROM %s" % tableName)
+
+ # The _SO_* series of methods are sorts of "friend" methods
+ # with SQLObject. They grab values from the SQLObject instances
+ # or classes freely, but keep the SQLObject class from accessing
+ # the database directly. This way no SQL is actually created
+ # in SQLObject.
+
+ def _SO_update(self, so, values):
+ self.query("UPDATE %s SET %s WHERE %s = %s" %
+ (so._table,
+ ", ".join(["%s = %s" % (dbName, SQLBuilder.sqlRepr(value))
+ for dbName, value in values]),
+ so._idName,
+ SQLBuilder.sqlRepr(so.id)))
+
+ def _SO_selectOne(self, so, columnNames):
+ return self.queryOne("SELECT %s FROM %s WHERE %s = %s" %
+ (", ".join(columnNames),
+ so._table,
+ so._idName,
+ SQLBuilder.sqlRepr(so.id)))
+
+ def _SO_selectOneAlt(self, cls, columnNames, column, value):
+ return self.queryOne("SELECT %s FROM %s WHERE %s = %s" %
+ (", ".join(columnNames),
+ cls._table,
+ column,
+ SQLBuilder.sqlRepr(value)))
+
+ def _SO_delete(self, so):
+ self.query("DELETE FROM %s WHERE %s = %s" %
+ (so._table,
+ so._idName,
+ SQLBuilder.sqlRepr(so.id)))
+
+ def _SO_selectJoin(self, soClass, column, value):
+ return self.queryAll("SELECT %s FROM %s WHERE %s = %s" %
+ (soClass._idName,
+ soClass._table,
+ column,
+ SQLBuilder.sqlRepr(value)))
+
+ def _SO_intermediateJoin(self, table, getColumn, joinColumn, value):
+ return self.queryAll("SELECT %s FROM %s WHERE %s = %s" %
+ (getColumn,
+ table,
+ joinColumn,
+ SQLBuilder.sqlRepr(value)))
+
+ def _SO_intermediateDelete(self, table, firstColumn, firstValue,
+ secondColumn, secondValue):
+ self.query("DELETE FROM %s WHERE %s = %s AND %s = %s" %
+ (table,
+ firstColumn,
+ SQLBuilder.sqlRepr(firstValue),
+ secondColumn,
+ SQLBuilder.sqlRepr(secondValue)))
+
+ def _SO_intermediateInsert(self, table, firstColumn, firstValue,
+ secondColumn, secondValue):
+ self.query("INSERT INTO %s (%s, %s) VALUES (%s, %s)" %
+ (table,
+ firstColumn,
+ secondColumn,
+ SQLBuilder.sqlRepr(firstValue),
+ SQLBuilder.sqlRepr(secondValue)))
class Transaction(object):
Index: SQLObject.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/SQLObject.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** SQLObject.py 31 Mar 2003 05:51:28 -0000 1.12
--- SQLObject.py 6 Apr 2003 07:56:27 -0000 1.13
***************
*** 505,509 ****
# And again...
! func = eval('lambda self, obj: self._SO_joinList[%i].add(self, obj)' % (len(dict['_SO_joinList'])-1))
setattr(cls, '_SO_add' + join.addRemovePrefix, func)
if not hasattr(cls, 'add' + appendMeth):
--- 505,509 ----
# And again...
! func = eval('lambda self, obj: self._SO_joinList[%i].add(self, obj)' % (len(cls._SO_joinList)-1))
setattr(cls, '_SO_add' + join.addRemovePrefix, func)
if not hasattr(cls, 'add' + appendMeth):
***************
*** 574,579 ****
# There's a write lock. Not sure if I need this...
self._SO_writeLock.acquire()
! self._connection.query("UPDATE %s SET %s = %s WHERE %s = %s" % (self._table, self._SO_columnDict[name].dbName, SQLBuilder.sqlRepr(value), self._idName, SQLBuilder.sqlRepr(self.id)))
!
# _SO_autoInitDone implies there's a cached value we also
# have to update.
--- 574,581 ----
# There's a write lock. Not sure if I need this...
self._SO_writeLock.acquire()
! self._connection._SO_update(self,
! [(self._SO_columnDict[name].dbName,
! SQLBuilder.sqlRepr(value))])
!
# _SO_autoInitDone implies there's a cached value we also
# have to update.
***************
*** 610,618 ****
setattr(self, name, value)
! # Here we construct the UPDATE, simple...
! q = "UPDATE %s SET " % (self._table)
! sets = ', '.join(["%s = %s" % (self._SO_columnDict[name].dbName, SQLBuilder.sqlRepr(value)) for name, value in toUpdate.items()])
! clause = " WHERE %s = %s" % (self._idName, self.id)
! self._connection.query(q + sets + clause)
self._SO_writeLock.release()
--- 612,616 ----
setattr(self, name, value)
! self._connection._SO_update(self, [(self._SO_columnDict[name].dbName, value) for name, value in toUpdate.items()])
self._SO_writeLock.release()
***************
*** 632,639 ****
# Database (_-using) names:
dbNames = [col.dbName for col in self._columns]
!
! q = "SELECT %s FROM %s WHERE %s = %s" % \
! (', '.join(dbNames), self._table, self._idName, self.id)
! results = self._connection.queryOne(q)
assert results, "The object %s by the ID %s does not exist" % (self.__class__.__name__, self.id)
--- 630,635 ----
# Database (_-using) names:
dbNames = [col.dbName for col in self._columns]
!
! results = self._connection._SO_selectOne(self, dbNames)
assert results, "The object %s by the ID %s does not exist" % (self.__class__.__name__, self.id)
***************
*** 654,661 ****
assert not self._SO_obsolete, "%s with id %s has become obsolete" \
% (self.__class__.__name__, self.id)
! q = "SELECT %s FROM %s WHERE %s = %s" % \
! (self._SO_columnDict[name].name, self._table, self._idName, self.id)
self._SO_writeLock.acquire()
! results = self._connection.queryOne(q)
self._SO_writeLock.release()
return results[0]
--- 650,656 ----
assert not self._SO_obsolete, "%s with id %s has become obsolete" \
% (self.__class__.__name__, self.id)
! # @@: do we really need this lock?
self._SO_writeLock.acquire()
! results = self._connection._SO_selectOne(self, [self._SO_columnDict[name].dbName])
self._SO_writeLock.release()
return results[0]
***************
*** 743,753 ****
def _SO_fetchAlternateID(cls, dbIDName, value):
! q = "SELECT %s, %s FROM %s WHERE %s = %s" % \
! (cls._idName,
! ", ".join([col.dbName for col in cls._columns]),
! cls._table,
! dbIDName,
! SQLBuilder.sqlRepr(value))
! result = cls._connection.queryOne(q)
obj = cls(result[0])
if not obj._SO_autoInitDone:
--- 738,747 ----
def _SO_fetchAlternateID(cls, dbIDName, value):
! result = cls._connection._SO_selectOneAlt(
! cls,
! [cls._idName] +
! [col.dbName for col in cls._columns],
! dbIDName,
! value)
obj = cls(result[0])
if not obj._SO_autoInitDone:
***************
*** 778,785 ****
# 3-03 @@: Should these have a connection argument?
! def dropTable(cls, ifExists=False):
if ifExists and not cls._connection.tableExists(cls._table):
return
cls._connection.dropTable(cls._table)
dropTable = classmethod(dropTable)
--- 772,781 ----
# 3-03 @@: Should these have a connection argument?
! def dropTable(cls, ifExists=False, dropJoinTables=True):
if ifExists and not cls._connection.tableExists(cls._table):
return
cls._connection.dropTable(cls._table)
+ if dropJoinTables:
+ cls.dropJoinTables(ifExists=ifExists)
dropTable = classmethod(dropTable)
***************
*** 793,799 ****
def createJoinTables(cls, ifExists=False):
- # 3-03: This should be calling _connection.createTable,
- # but right now it's making tables directly.
- result = []
for join in cls._joins:
if not join.hasIntermediateTable():
--- 789,792 ----
***************
*** 808,820 ****
cls._connection.tableExists(join.intermediateTable):
continue
! self._connection.query(
! 'CREATE TABLE %s (\n%s %s,\n%s %s\n)'
! % (join.intermediateTable,
! join.joinColumn,
! self.joinSQLType(join),
! join.otherColumn,
! self.joinSQLType(join)))
createJoinTables = classmethod(createJoinTables)
def clearTable(cls):
# 3-03 @@: Maybe this should check the cache... but it's
--- 801,821 ----
cls._connection.tableExists(join.intermediateTable):
continue
! cls._connection._SO_createJoinTable(join)
!
createJoinTables = classmethod(createJoinTables)
+ def dropJoinTables(cls, ifExists=False):
+ for join in cls._joins:
+ if not join.hasIntermediateTable():
+ continue
+ if join.callingClass > join.otherClass:
+ continue
+ if ifExists and \
+ not cls._connection.tableExists(join.intermediateTable):
+ continue
+ cls._connection._SO_dropJoinTable(join)
+
+ dropJoinTables = classmethod(dropJoinTables)
+
def clearTable(cls):
# 3-03 @@: Maybe this should check the cache... but it's
***************
*** 827,834 ****
self._SO_obsolete = True
self._SO_autoInitDone = False
! self._connection.query("""
! DELETE FROM %s
! WHERE %s = %s
! """ % (self._table, self._idName, SQLBuilder.sqlRepr(self.id)))
self._connection.cache.purge(self.id)
--- 828,834 ----
self._SO_obsolete = True
self._SO_autoInitDone = False
! # huh?
! #self._SO_delete(self)
! self._connection._SO_delete(self)
self._connection.cache.purge(self.id)
***************
*** 923,932 ****
# so we have to fetch the actual class definition:
cls = findClass(self.otherClass)
! ids = inst._connection.queryAll(
! """SELECT %s FROM %s
! WHERE %s = %s""" %
! (cls._idName, cls._table,
! self.joinColumn,
! SQLBuilder.sqlRepr(inst.id)))
return [cls(id) for (id,) in ids]
--- 923,930 ----
# so we have to fetch the actual class definition:
cls = findClass(self.otherClass)
! ids = inst._connection._SO_selectJoin(
! cls,
! self.joinColumn,
! inst.id)
return [cls(id) for (id,) in ids]
***************
*** 958,992 ****
cls = findClass(self.otherClass)
me = findClass(self.callingClass)
! ids = me._connection.queryAll(
! """SELECT %s FROM %s
! WHERE %s = %s""" %
! (self.otherColumn,
! self.intermediateTable,
! self.joinColumn,
! SQLBuilder.sqlRepr(inst.id)))
return [cls(id) for (id,) in ids]
def remove(self, inst, other):
me = findClass(self.callingClass)
! me._connection.query(
! """DELETE FROM %s
! WHERE %s = %s
! AND %s = %s""" %
! (self.intermediateTable,
! self.joinColumn,
! getID(inst.id),
! self.otherColumn,
! getID(other)))
def add(self, inst, other):
me = findClass(self.callingClass)
! me._connection.query(
! """INSERT INTO %s (%s, %s)
! VALUES (%s, %s)""" %
! (self.intermediateTable,
! self.joinColumn,
! self.otherColumn,
! getID(inst),
! getID(other)))
class SelectResults(object):
--- 956,983 ----
cls = findClass(self.otherClass)
me = findClass(self.callingClass)
! ids = me._connection._SO_intermediateJoin(
! self.intermediateTable,
! self.otherColumn,
! self.joinColumn,
! inst.id)
return [cls(id) for (id,) in ids]
def remove(self, inst, other):
me = findClass(self.callingClass)
! me._connection._SO_intermediateDelete(
! self.intermediateTable,
! self.joinColumn,
! getID(inst),
! self.otherColumn,
! getID(other))
def add(self, inst, other):
me = findClass(self.callingClass)
! me._connection._SO_intermediateInsert(
! self.intermediateTable,
! self.joinColumn,
! getID(inst),
! self.otherColumn,
! getID(other))
class SelectResults(object):
|