Update of /cvsroot/sqlobject/SQLObject/SQLObject
In directory sc8-pr-cvs1:/tmp/cvs-serv4903/SQLObject
Modified Files:
Col.py DBConnection.py
Log Message:
* Added Postgres support for _fromDatabase
* Moved guessClass out of Col, into DBConnection classes
* Changed tests so Postgres gets tested too
Index: Col.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/Col.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** Col.py 11 Apr 2003 16:23:18 -0000 1.9
--- Col.py 19 Apr 2003 00:04:07 -0000 1.10
***************
*** 248,264 ****
return value
- def guessClass(t):
- if t.startswith('int'):
- return IntCol, {}
- elif t.startswith('varchar'):
- return StringCol, {'length': int(t[8:-1])}
- elif t.startswith('char'):
- return StringCol, {'length': int(t[5:-1]),
- 'varchar': False}
- elif t.startswith('datetime'):
- return DateTimeCol, {}
- else:
- return Col, {}
-
_generatePythonNameRE = re.compile('_.')
def generatePythonName(name):
--- 248,251 ----
Index: DBConnection.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/DBConnection.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** DBConnection.py 17 Apr 2003 07:16:09 -0000 1.20
--- DBConnection.py 19 Apr 2003 00:04:08 -0000 1.21
***************
*** 400,404 ****
if field == 'id':
continue
! colClass, kw = Col.guessClass(t)
kw['name'] = Col.generatePythonName(field)
kw['notNull'] = not nullAllowed
--- 400,404 ----
if field == 'id':
continue
! colClass, kw = self.guessClass(t)
kw['name'] = Col.generatePythonName(field)
kw['notNull'] = not nullAllowed
***************
*** 409,412 ****
--- 409,426 ----
return results
+ def guessClass(self, t):
+ if t.startswith('int'):
+ return Col.IntCol, {}
+ elif t.startswith('varchar'):
+ return Col.StringCol, {'length': int(t[8:-1])}
+ elif t.startswith('char'):
+ return Col.StringCol, {'length': int(t[5:-1]),
+ 'varchar': False}
+ elif t.startswith('datetime'):
+ return Col.DateTimeCol, {}
+ else:
+ return Col, {}
+
+
class PostgresConnection(DBAPI):
***************
*** 478,481 ****
--- 492,551 ----
(tableName,
column.dbName))
+
+ def columnsFromSchema(self, tableName):
+
+ keyQuery = """
+ SELECT pg_catalog.pg_get_constraintdef(oid) as condef
+ FROM pg_catalog.pg_constraint r
+ WHERE r.conrelid = '%s'::regclass AND r.contype = 'f'"""
+
+ colQuery = """
+ SELECT a.attname,
+ pg_catalog.format_type(a.atttypid, a.atttypmod), a.attnotnull,
+ (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d
+ WHERE d.adrelid=a.attrelid AND d.adnum = a.attnum)
+ FROM pg_catalog.pg_attribute a
+ WHERE a.attrelid ='%s'::regclass
+ AND a.attnum > 0 AND NOT a.attisdropped
+ ORDER BY a.attnum"""
+
+ keyData = self.queryAll(keyQuery % tableName)
+ keyRE = re.compile("\((.)\) REFERENCES (.)\(")
+ keymap = {}
+ for (condef,) in keyData:
+ match = keyRE.search(condef)
+ if match:
+ field, reftable = match.groups()
+ keymap[field] = reftable.capitalize()
+ #print "keymap:", keymap
+ colData = self.queryAll(colQuery % tableName)
+ results = []
+ for field, t, notnull, defaultstr in colData:
+ if field == 'id':
+ continue
+ colClass, kw = self.guessClass(t)
+ kw['name'] = Col.generatePythonName(field)
+ kw['notNull'] = notnull
+ if defaultstr is not None:
+ kw['default'] = eval(defaultstr)
+ if keymap.has_key(field):
+ kw['foreignKey'] = keymap[field]
+ results.append(colClass(**kw))
+ return results
+
+ def guessClass(self, t):
+ if t.count('int'):
+ return Col.IntCol, {}
+ elif t.count('varying'):
+ return Col.StringCol, {'length': int(t[t.index('(')+1:-1])}
+ elif t.startswith('character('):
+ return Col.StringCol, {'length': int(t[t.index('(')+1:-1]),
+ 'varchar': False}
+ elif t=='text':
+ return Col.StringCol, {}
+ elif t.startswith('datetime'):
+ return Col.DateTimeCol, {}
+ else:
+ return Col.Col, {}
|