Hi,

after solving the ValueError (see my previous posts), I try to get the 
workaround working with
SQLObject.

Basically, I try to work around bug 155341 ("MySQL-python-1.0.0 is not 
compatible with MySQL 4.1",
[1]) on RHEL 4. This bug causes the MySQLdb module to fail when parsing the 
return mysql timestamps
(see [2]). A nice workaround is [3]. Essentially, I have to define my own 
converter for timestamp
fields and pass a dictionary with appropriate references during the 
MySQLdb.connect().

In plain Python/MySQLdb:
# 
-------------------------------------------------------------------------------------------------
import copy
import time
import MySQLdb
import MySQLdb.converters

def TSHandler(s):
     """Convert a TIMESTAMP type to something sensible."""
     #Depending on the version of MySQLdb, TIMESTAMPs can come
     #in a variety of formats.  Rather than check them carefully,
     #we cope with the MySQLdb >= 4.1 standard and allow failures
     #to result in a string.
     try:
         #Assume it's expressed in MySQL standard form
         return time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))
     except ValueError:
         pass
     return str(s)

#As per the MySQLdb comments, *copy* the exting converters dict.
conversions = copy.copy(MySQLdb.converters.conversions)
#Deal with timestamps in a more rational way than forcing to int
conversions[MySQLdb.constants.FIELD_TYPE.TIMESTAMP] = TSHandler

dargs = dict()
dargs['conv'] = conversions
conn = MySQLdb.connect(host="localhost", db="xamstest", user="root", **dargs)

# 
-------------------------------------------------------------------------------------------------

How can I do this in SQLObject? Of course, the code above is specific for a 
certain environment but
this is no problem in my case.

I read http://wiki.sqlobject.org/connections.html and saw something like:
'mysql://test:[EMAIL PROTECTED]/testdb?debug=1' as a connection string. But I 
don't think a simple
string can handle complex conditions as in the situation above...

After that I found that there is some keyword support:
MySQLConnection = sqlobject.mysql.builder()
connection = MySQLConnection(user='root', db='xamstest', **kw)

I think I have to add "conv" to the list of parameters in mysqlconnection.py so 
that it is passed to 
MySQL for connect() and not during class initialization. Does this sound 
reasonable?


However, I can't test my modifications now because even this simple script 
fails with an unmodified 
SQLObject library (no database access needed so you don't need to load a 
specific schema):
-----------------------------------------------------------------------------------------------
#!/usr/bin/env python

import sqlobject
from sqlobject import *

MySQLConnection = sqlobject.mysql.builder()
connection = MySQLConnection(user='root', db='xamstest')
sqlhub.processConnection = connection

class User(SQLObject):
     class sqlmeta:
         table = "pm_users"
         fromDatabase = True
         style = MixedCaseStyle()

print User.select()

for i in User.select():
     print i
-----------------------------------------------------------------------------------------------

This is the traceback:
-----------------------------------------------------------------------------------------------
[EMAIL PROTECTED] ~]$ ./plain.py
Traceback (most recent call last):
   File "./plain.py", line 10, in ?
     class User(SQLObject):
   File "/home/fs/sqlobject/declarative.py", line 94, in __new__
     cls.__classinit__(cls, new_attrs)
   File "/home/fs/sqlobject/main.py", line 770, in __classinit__
     cls.sqlmeta.addColumnsFromDatabase()
   File "/home/fs/sqlobject/main.py", line 421, in addColumnsFromDatabase
     for columnDef in conn.columnsFromSchema(sqlmeta.table, soClass):
   File "/home/fs/sqlobject/mysql/mysqlconnection.py", line 124, in 
columnsFromSchema
     colData = self.queryAll("SHOW COLUMNS FROM %s"
   File "/home/fs/sqlobject/dbconnection.py", line 316, in queryAll
     return self._runWithConnection(self._queryAll, s)
   File "/home/fs/sqlobject/dbconnection.py", line 215, in _runWithConnection
     conn = self.getConnection()
   File "/home/fs/sqlobject/dbconnection.py", line 226, in getConnection
     conn = self.makeConnection()
   File "/home/fs/sqlobject/mysql/mysqlconnection.py", line 41, in 
makeConnection
     db=self.db, user=self.user, passwd=self.password, **self.kw)
   File "/usr/lib64/python2.3/site-packages/MySQLdb/__init__.py", line 64, in 
Connect
     return apply(Connection, args, kwargs)
   File "/usr/lib64/python2.3/site-packages/MySQLdb/connections.py", line 116, 
in __init__
     self._make_connection(args, kwargs2)
   File "/usr/lib64/python2.3/site-packages/MySQLdb/connections.py", line 41, 
in _make_connection
     apply(super(ConnectionBase, self).__init__, args, kwargs)
TypeError: an integer is required
-----------------------------------------------------------------------------------------------

Probably the solution is very easy but I don't see it even after some hours 
staring at my code.

fs

[1] https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=155341
[2] 
http://sourceforge.net/tracker/index.php?func=detail&aid=1608916&group_id=22307&atid=374932
[3] http://mail.python.org/pipermail/python-list/2004-September/284451.html


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to