[Zope-CVS] SVN: mysqldbda/trunk/ Sprinting with Claudia Albisetti

Amos Brocco amos.brocco at mriyasoftware.com
Sun Oct 10 12:19:23 EDT 2004


Log message for revision 27920:
  Sprinting with Claudia Albisetti
  
  Finished updating MySQLdb DA.
  
  The Python DBI returns unicode in an encoding. This adapter now correctly converts all strings (varchar, char, text) to unicode using 
  the correct encoding.
  


Changed:
  A   mysqldbda/trunk/README.TXT
  U   mysqldbda/trunk/adapter.py
  A   mysqldbda/trunk/mysqldbda.zcml
  A   mysqldbda/trunk/tests/
  A   mysqldbda/trunk/tests/__init__.py
  A   mysqldbda/trunk/tests/test_adapter.py


-=-
Added: mysqldbda/trunk/README.TXT
===================================================================
--- mysqldbda/trunk/README.TXT	2004-10-10 15:34:28 UTC (rev 27919)
+++ mysqldbda/trunk/README.TXT	2004-10-10 16:19:23 UTC (rev 27920)
@@ -0,0 +1,72 @@
+This files helps you installing the MySQL Database adapter in Zope 3
+In this document I'm assuming your Zope3 lives in ~/Zope3.
+
+Installing mysqldbda
+
+  1. Check out the mysqldbda product into ~/Zope3/src
+
+       cd ~/Zope3/src
+       cvs -d :pserver:anonymous at cvs.zope.org:/cvs-repository login
+       cvs -d :pserver:anonymous at cvs.zope.org:/cvs-repository checkout \
+                                                mysqldbda
+
+  2. Create a file named 'mysqldbda-configure.zcml' inside
+     ~/Zope3/package-includes with the following contents:
+
+       <include package='mysqldbda' />
+
+Creating Database Connections
+
+  It is time to add some connections. A connection in Zope 3 is
+  registered as a utility.
+
+  3. Open a web browser on your Zope root folder (http://localhost:8080/ if
+     you use the default settings in zope.conf.in).
+
+  4. Click on the 'Manage Site' action on the right side of the
+     screen. You should see a screen which reads 'Common Site
+     Management Tasks'
+
+  5. Around the middle of that page, you should see a link named 'Add
+     Utility'. Click on it.
+
+  6. Select 'MySQL DA' and type in a name at the bottom of the page.
+
+  7. Enter the database connection string.  It looks like this:
+
+        dbi://username:password@host:port/databasename
+
+  8. Click on the 'Add' button.
+
+  9. You should be on a page which reads 'Add Database Connection
+     Registration'. There you can configure the permission needed to
+     use the database connection, the name of the registration and the
+     registration status. You can use any name for 'Register As'
+     field, as long as it doesn't clash with an existing one. Choose a
+     permission. Choose between 'Registered' and 'Active' for  the
+     'Registration Status'. Only one component of a kind can be
+     'Active' at a time, so be careful.
+
+  10. You should be redirected to the 'Edit' screen of the connection
+      utility.
+
+  11. If you want to, you can go to the Test page and execute arbitrary SQL
+      queries to see whether the connection is working as expected.
+
+Using SQL Scripts
+
+  You can create SQL Scripts in the content space.  For example:
+
+  12. Go to Zope root.
+
+  13. Add an SQL script (you can use the Common Tasks box on the left, or
+      the Add action on the right).
+
+  14. Click on the name of your new SQL script.
+
+  15. Choose a connection name (the one you entered in step 29) from the
+      drop-down.
+
+  16. Enter your query and click on the 'Save Changes' button.
+
+  17. You can test the script in the -- surprise! -- Test page.
\ No newline at end of file

Modified: mysqldbda/trunk/adapter.py
===================================================================
--- mysqldbda/trunk/adapter.py	2004-10-10 15:34:28 UTC (rev 27919)
+++ mysqldbda/trunk/adapter.py	2004-10-10 16:19:23 UTC (rev 27920)
@@ -1,44 +1,86 @@
-##############################################################################
-#
-# Copyright (c) 2002 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""MySQL database adapter.
-
-$Id: Adapter.py,v 1.3 2003/06/05 16:42:31 philikon dead $
-"""
-
-import MySQLdb
-
-from zope.app.rdb import ZopeDatabaseAdapter, parseDSN
-
-dsn2option_mapping = {'dbname':'dbname',
-                      'port':'port',
-                      'host':'host',
-                      'username':'user',
-                      'password':'passwd'}
-
-class MySQLdbAdapter(ZopeDatabaseAdapter):
-    """A MySQLdb adapter for Zope3"""
-
-    def _connection_factory(self):
-        """Create a MySQLdb DBI connection based on the DSN"""
-
-        conn_info = parseDSN(self.dsn)
-        print '*'*78
-        print conn_info
-        print '*'*78
-        return MySQLdb.Connect(db=conn_info['dbname'],
-                               host=conn_info['host'],
-                               user=conn_info['username'],
-                               passwd=conn_info['password'],
-                               port=int(conn_info['port'] or '3306'))
-                      
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""MySQL database adapter.
+
+$Id: Adapter.py,v 1.1 2004/10/10 mriya3
+"""
+
+from zope.app.rdb import ZopeDatabaseAdapter, parseDSN
+
+import MySQLdb
+
+dsn2option_mapping = {'dbname':'dbname',
+                    'port':'port',
+                    'host':'host',
+                    'username':'user',
+                    'password':'passwd'}
+
+class MySQLStringConverter:
+    def __init__(self, encoding):
+        self.encoding = encoding
+
+    def __call__(self, string):
+        if isinstance(string, str):
+            return string.decode(self.encoding)
+        elif isinstance(string, unicode):
+            return string
+        else:
+            return string
+            
+            
+
+class MySQLdbAdapter(ZopeDatabaseAdapter):
+    """A MySQLdb adapter for Zope3"""
+    
+    
+    """ MySQLdb types codes"""
+    __STRINGtypes = (1, 247, 254, 253)
+    __BINARYtypes = (252, 251, 250, 249)
+    __DATEtypes = (10, 14)
+    __DATETIMEtypes = (7, 12)
+    __NUMBERtypes = (0, 5, 4, 9, 3, 8, 1, 13)
+    __TIMEtypes = (11)
+    
+    """ Default string converter """
+    __stringConverter =  MySQLStringConverter('UTF-8')
+    
+
+    def _connection_factory(self):
+        """Create a MySQLdb DBI connection based on the DSN"""
+
+        conn_info = parseDSN(self.dsn)
+
+        print '*'*78
+        print conn_info
+        print '*'*78
+        connection = MySQLdb.Connect(db=conn_info['dbname'],
+                            host=conn_info['host'],
+                            user=conn_info['username'],
+                            passwd=conn_info['password'],
+                            port=int(conn_info['port'] or '3306'))
+ 
+        self.__stringConverter = MySQLStringConverter(str(connection.character_set_name()))
+        return connection
+                    
+    def getConverter(self, type):
+        'See IDBITypeInfo'
+        if type in self.__STRINGtypes:
+            return self.__stringConverter
+        return self.identity
+   
+
+    def identity(self, x):
+        return x
+    
+    

Added: mysqldbda/trunk/mysqldbda.zcml
===================================================================
--- mysqldbda/trunk/mysqldbda.zcml	2004-10-10 15:34:28 UTC (rev 27919)
+++ mysqldbda/trunk/mysqldbda.zcml	2004-10-10 16:19:23 UTC (rev 27920)
@@ -0,0 +1 @@
+<include package="mysqldbda" />
\ No newline at end of file


Property changes on: mysqldbda/trunk/mysqldbda.zcml
___________________________________________________________________
Name: svn:executable
   + *

Added: mysqldbda/trunk/tests/__init__.py
===================================================================
--- mysqldbda/trunk/tests/__init__.py	2004-10-10 15:34:28 UTC (rev 27919)
+++ mysqldbda/trunk/tests/__init__.py	2004-10-10 16:19:23 UTC (rev 27920)
@@ -0,0 +1,19 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""Unit tests for MySQLdbDA."""
+
+"""Unit tests for MySQL Database Adapter
+
+$Id: __init__.py,v 1.0 2004/10/10
+"""


Property changes on: mysqldbda/trunk/tests/__init__.py
___________________________________________________________________
Name: svn:executable
   + *

Added: mysqldbda/trunk/tests/test_adapter.py
===================================================================
--- mysqldbda/trunk/tests/test_adapter.py	2004-10-10 15:34:28 UTC (rev 27919)
+++ mysqldbda/trunk/tests/test_adapter.py	2004-10-10 16:19:23 UTC (rev 27920)
@@ -0,0 +1,38 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Unit tests for MySQLDA.
+
+$Id: test_adapter.py,v 1.0 2004/10/10
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+import MySQLdb
+
+class TestStringConversion(TestCase):
+	def test_testStringConversion(self):
+		from mysqldbda.adapter import MySQLStringConverter
+		converter = MySQLStringConverter('latin-1')
+		b =converter('yè yè ü ü ä ä ö ö')
+		self.assertEquals(type(b), unicode)
+		converter = MySQLStringConverter('utf8')
+		b =converter(u'Hi mom!')
+		self.assertEquals(type(b),unicode)
+
+def test_suite():
+    return TestSuite((
+        makeSuite(TestStringConversion),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


Property changes on: mysqldbda/trunk/tests/test_adapter.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the Zope-CVS mailing list