changeset 4:e3ee1f358fe6

new version that doesn't use ZSQLExtend but the database connection more directly. new templates to go with that (returned data structures are different)
author casties
date Mon, 07 Jun 2010 14:02:17 +0000
parents 0e2d167d0983
children 7539a9e69365
files RestDbInterface.py zpt/HTML_list_schemas.zpt zpt/HTML_list_tables.zpt zpt/HTML_table.zpt zpt/editRestDbInterface.zpt
diffstat 5 files changed, 96 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbInterface.py	Wed Jun 02 15:44:03 2010 +0000
+++ b/RestDbInterface.py	Mon Jun 07 14:02:17 2010 +0000
@@ -15,6 +15,8 @@
 #from zope.publisher.interfaces import NotFound 
 #from zope.app import zapi 
 #from zope.component import queryMultiAdapter
+import Shared.DC.ZRDB.DA
+from Products.ZSQLMethods.SQL import SQLConnectionIDs
 
 
 class RestDbInterface(Folder):
@@ -26,6 +28,12 @@
     implements(IPublishTraverse)
     
     meta_type="RESTdb"
+    manage_options=Folder.manage_options+(
+        {'label':'Config','action':'manage_editRestDbInterfaceForm'},
+        )
+
+    # management templates
+    manage_editRestDbInterfaceForm=PageTemplateFile('zpt/editRestDbInterface',globals())
 
     # data templates
     XML_list_schemas = PageTemplateFile('zpt/XML_list_schemas', globals())
@@ -37,13 +45,54 @@
 
     
     
-    def __init__(self, id, title):
+    def __init__(self, id, title, connection_id=None):
         """init"""
         self.id = id
         self.title = title
+        # database connection id
+        self.connection_id = connection_id
         # create template folder
         self.manage_addFolder('template')
 
+
+    def getCursor(self):
+        """returns fresh DB cursor"""
+        conn = getattr(self,"_v_database_connection",None)
+        if conn is None:
+            # create a new connection object
+            try:
+                if self.connection_id is None:
+                    # try to take the first existing ID
+                    connids = SQLConnectionIDs(self)
+                    if len(connids) > 0:
+                        connection_id = connids[0][0]
+                        self.connection_id = connection_id
+                        logging.debug("connection_id: %s"%repr(connection_id))
+
+                da = getattr(self, self.connection_id)
+                da.connect('')
+                # we copy the DAs database connection
+                conn = da._v_database_connection
+                #conn._register() # register with the Zope transaction system
+                self._v_database_connection = conn
+            except Exception, e:
+                raise IOError("No database connection! (%s)"%str(e))
+        
+        cursor = conn.getCursor()
+        return cursor
+    
+    def executeSQL(self, query, *args):
+        """execute query with args on database and return all results.
+        result format: {"fields":fields, "rows":data}"""
+        cur = self.getCursor()
+        cur.execute(query, args)
+        # description of returned fields 
+        fields = cur.description
+        # get all data in an array
+        data = cur.fetchall()
+        cur.close()
+        return {"fields":fields, "rows":data}
+
     
     def publishTraverse(self,request,name):
         """change the traversal"""
@@ -91,7 +140,7 @@
         # don't know what to do
         return str(REQUEST)
 
- 
+
     def showTable(self,format='XML',schema='public',table=None):
         """returns PageTemplate with tables"""
         logging.debug("showtable")
@@ -106,8 +155,7 @@
     def getTable(self,schema='public',table=None,username='guest'):
         """return table data"""
         logging.debug("gettable")
-        qstr="select * from %s"%table
-        data=self.ZSQLSimpleSearch(qstr)
+        data = self.executeSQL("select * from %s"%table)
         return data
 
     def showListOfTables(self,format='XML',schema='public'):
@@ -129,7 +177,7 @@
             WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
             AND pg_catalog.pg_table_is_visible(c.oid)"""
         #qstr="select attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from pg_attribute, pg_class where attrelid = pg_class.oid and pg_attribute.attnum > 0"
-        data=self.ZSQLSimpleSearch(qstr)
+        data=self.executeSQL(qstr)
         return data
 
     def showListOfSchemas(self,format='XML'):
@@ -146,10 +194,22 @@
         """return list of schemas"""
         logging.debug("getlistofschemas")
         # TODO: really look up schemas
-        data=['public']
+        data={'fields':'schemas','data':'public'}
         return data
 
         
+    def manage_editRestDbInterface(self, title=None, connection_id=None,
+                     REQUEST=None):
+        """Change the object"""
+        if title is not None:
+            self.title = title
+            
+        if connection_id is not None:
+            self.connection_id = connection_id
+                
+        #checkPermission=getSecurityManager().checkPermission
+        REQUEST.RESPONSE.redirect('manage_main')
+
         
 manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals())
 
--- a/zpt/HTML_list_schemas.zpt	Wed Jun 02 15:44:03 2010 +0000
+++ b/zpt/HTML_list_schemas.zpt	Mon Jun 07 14:02:17 2010 +0000
@@ -1,19 +1,19 @@
-<html tal:define="format python:request.get('format','table')">
+<html tal:define="format python:request.get('format','table'); element_id python:request.get('element_id',None)">
   <head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
     <title tal:content="template/title">The title</title>
-    <meta http-equiv="content-type" content="text/html;charset=utf-8">
   </head>
   <body>
     <h2>List of schemas</h2>
     <tal:block tal:condition="python:format=='select'">
-    <select id="schemas">
-      <option tal:repeat="sch options/data" tal:content="sch"/>
+    <select tal:attributes="id element_id">
+      <option tal:repeat="sch options/data/data" tal:content="sch"/>
     </select>
     </tal:block>
     <tal:block tal:condition="python:format=='table'">
-    <table id="schemas">
-      <tr tal:repeat="sch options/data">
-	<td><a tal:attributes="href sch" tal:content="sch"/></td>
+    <table tal:attributes="id element_id">
+      <tr tal:repeat="sch options/data/data">
+        <td><a tal:attributes="href sch" tal:content="sch"/></td>
       </tr>
     </table>
     </tal:block>
--- a/zpt/HTML_list_tables.zpt	Wed Jun 02 15:44:03 2010 +0000
+++ b/zpt/HTML_list_tables.zpt	Mon Jun 07 14:02:17 2010 +0000
@@ -1,19 +1,19 @@
-<html tal:define="format python:request.get('format','table')">
+<html tal:define="format python:request.get('format','table'); element_id python:request.get('element_id',None)">
   <head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
     <title tal:content="template/title">The title</title>
-    <meta http-equiv="content-type" content="text/html;charset=utf-8">
   </head>
   <body>
     <h2>List of tables for schema <span tal:replace="options/schema"/></h2>
     <tal:block tal:condition="python:format=='select'">
-    <select id="tables">
-      <option tal:repeat="tbl options/data" tal:content="tbl/relname"/>
+    <select tal:attributes="id element_id">
+      <option tal:repeat="tbl options/data/rows" tal:content="python:tbl[0]"/>
     </select>
     </tal:block>
     <tal:block tal:condition="python:format=='table'">
-    <table id="tables">
-      <tr tal:repeat="tbl options/data">
-	<td><a tal:attributes="href tbl/relname" tal:content="tbl/relname"/></td>
+    <table tal:attributes="id element_id">
+      <tr tal:repeat="row options/data/rows">
+        <td><a tal:define="tbl python:row[0]" tal:attributes="href tbl" tal:content="tbl"/></td>
       </tr>
     </table>
     </tal:block>
--- a/zpt/HTML_table.zpt	Wed Jun 02 15:44:03 2010 +0000
+++ b/zpt/HTML_table.zpt	Mon Jun 07 14:02:17 2010 +0000
@@ -1,21 +1,21 @@
-<html tal:define="format python:request.get('format','table'); table_id python:request.get('table_id',None); 
-    fields options/data/names">
+<html tal:define="format python:request.get('format','table'); element_id python:request.get('element_id',None); 
+    fields options/data/fields; rows options/data/rows;">
   <head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
     <title tal:content="template/title">The title</title>
-    <meta http-equiv="content-type" content="text/html;charset=utf-8">
   </head>
   <body>
     <h2>table <span tal:replace="options/tablename"/></h2>
     <tal:block tal:condition="python:format=='table'">
-    <table tal:attributes="id table_id">
+    <table tal:attributes="id element_id">
       <thead>
         <tr>
-	      <th tal:repeat="field fields" tal:content="field"/>
+	      <th tal:repeat="field fields" tal:content="python:field[0]"/>
         </tr>
       </thead>
       <tbody>
-        <tr tal:repeat="row options/data">
-	      <td tal:repeat="field fields" tal:content="python:row[field]"/>
+        <tr tal:repeat="row rows">
+	      <td tal:repeat="col row" tal:content="col"/>
         </tr>
       </tbody>
     </table>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zpt/editRestDbInterface.zpt	Mon Jun 07 14:02:17 2010 +0000
@@ -0,0 +1,9 @@
+  <div tal:replace="structure here/manage_page_header">Header</div>
+  <h2>Edit a RESTdb</h2>
+  <form method="post" action="manage_editRestDbInterface">
+    <p class="form-label">Title</p>
+        <p class="form-element"><input size="80" name="title" tal:attributes="value here/title"/></p>
+    <p class="form-label">Connection ID</p>
+        <p class="form-element"><input size="80" name="connection_id" tal:attributes="value here/connection_id"/></p>
+     <p><input type="submit" value="Change" /></p>
+  </form>