[Zope-CVS] CVS: Products/AdaptableStorage/gateway_sql - SQLUserList.py:1.4.2.1 PsycopgConnection.py:1.6.2.1 SQLClassification.py:1.4.2.1 SQLFolderItems.py:1.5.2.1 SQLGatewayBase.py:1.1.2.1 SQLObjectData.py:1.2.2.1 SQLProperties.py:1.1.2.1 public.py:1.3.2.1

Christian Zagrodnick cz@gocept.com
Mon, 13 Jan 2003 14:16:41 -0500


Update of /cvs-repository/Products/AdaptableStorage/gateway_sql
In directory cvs.zope.org:/tmp/cvs-serv19124

Modified Files:
      Tag: zagy-patches
	PsycopgConnection.py SQLClassification.py SQLFolderItems.py 
	SQLGatewayBase.py SQLObjectData.py SQLProperties.py public.py 
Added Files:
      Tag: zagy-patches
	SQLUserList.py 
Log Message:
merging HEAD into zagy-patches branch

=== Added File Products/AdaptableStorage/gateway_sql/SQLUserList.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""SQL folder items gateway

$Id: SQLUserList.py,v 1.4.2.1 2003/01/13 19:16:07 zagy Exp $
"""

from SQLGatewayBase import SQLGatewayBase
from mapper_public import IGateway, RowSequenceSchema


class SQLUserList (SQLGatewayBase):
    """Stores and retrieves all users for a folder at once."""

    __implements__ = IGateway

    schema = RowSequenceSchema()
    schema.addField('id', 'string', 1)
    schema.addField('password', 'string')
    schema.addField('roles', 'string:list')
    schema.addField('domains', 'string:list')

    table_base_name = 'users'

    checkexist_sql = '''SELECT key FROM %(table)s WHERE key = 0'''

    create_sql = '''CREATE TABLE %(table)s (
    key int,
    id character varying(255),
    password character varying(255)
    )'''

    create_roles_sql = '''CREATE TABLE %(roles_table)s (
    key int,
    id character varying(255),
    role character varying(255)
    )'''

    create_domains_sql = '''CREATE TABLE %(domains_table)s (
    key int,
    id character varying(255),
    domain character varying(255)
    )'''

    read_sql = '''SELECT id, password FROM %(table)s
    WHERE key = %(key)s'''

    read_roles_sql = '''SELECT id, role FROM %(roles_table)s
    WHERE key = %(key)s'''

    read_domains_sql = '''SELECT id, domain FROM %(domains_table)s
    WHERE key = %(key)s'''

    update_sql = '''UPDATE %(table)s
    SET password = %(password)s
    WHERE key = %(key)s AND id = %(id)s'''

    insert_sql = '''INSERT INTO %(table)s
    (key, id, password)
    VALUES (%(key)s, %(id)s, %(password)s)'''

    delete_sql = '''DELETE FROM %(table)s
    WHERE key = %(key)s and id = %(id)s'''

    delete_roles_sql = '''DELETE FROM %(roles_table)s
    WHERE key = %(key)s and id = %(id)s'''

    delete_domains_sql = '''DELETE FROM %(domains_table)s
    WHERE key = %(key)s and id = %(id)s'''

    insert_role_sql = '''INSERT INTO %(roles_table)s
    (key, id, role)
    VALUES (%(key)s, %(id)s, %(role)s)'''

    insert_domain_sql = '''INSERT INTO %(domains_table)s
    (key, id, domain)
    VALUES (%(key)s, %(id)s, %(domain)s)'''

    clear_roles_sql = 'DELETE FROM %(roles_table)s'

    clear_domains_sql = 'DELETE FROM %(domains_table)s'


    def setupTableNames(self):
        SQLGatewayBase.setupTableNames(self)
        self.roles_table = self.conn.prefix + '_user_roles'
        self.domains_table = self.conn.prefix + '_user_domains'

    def setupTables(self):
        conn = self.conn
        try:
            self.execute(self.checkexist_sql)
        except conn.error:
            conn.db.rollback()
            self.execute(self.create_sql)
            self.execute(self.create_roles_sql)
            self.execute(self.create_domains_sql)
            conn.db.commit()

    def execute(self, text, *args, **kw):
        text = text.replace('%(table)s', self.table)
        text = text.replace('%(roles_table)s', self.roles_table)
        text = text.replace('%(domains_table)s', self.domains_table)
        return self.conn.execute(text, *args, **kw)

    def clear(self):
        self.execute(self.clear_sql)
        self.execute(self.clear_roles_sql)
        self.execute(self.clear_domains_sql)

    def getSchema(self):
        return self.schema


    def load(self, event):
        key = event.getKeychain()[-1]
        rows = self.execute(self.read_sql, 1, key=key)
        data = {}
        for id, password in rows:
            data[id] = (password, [], [])
        rows = self.execute(self.read_roles_sql, 1, key=key)
        for id, role in rows:
            row = data.get(id)
            if row is not None:
                row[1].append(role)
        rows = self.execute(self.read_domains_sql, 1, key=key)
        for id, domain in rows:
            row = data.get(id)
            if row is not None:
                row[2].append(domain)
        records = []
        for id, (password, roles, domains) in data.items():
            records.append((id, password, tuple(roles), tuple(domains)))
        records.sort()
        return records, tuple(records)


    def store(self, event, state):
        keychain = event.getKeychain()
        keychain1 = keychain[:-1]
        key = int(keychain[-1])
        old_rows, old_serial = self.load(event)
        new_dict = {}
        for rec in state:
            new_dict[rec[0]] = rec
        old_dict = {}
        for rec in old_rows:
            old_dict[rec[0]] = rec

        # Compare / remove rows.
        for old_row in old_rows:
            id = old_row[0]
            new_row = new_dict.get(id)
            if new_row is None:
                # Remove this row.
                self.execute(self.delete_sql, key=key, id=id)
                self.execute(self.delete_roles_sql, key=key, id=id)
                self.execute(self.delete_domains_sql, key=key, id=id)
            elif new_row == old_row:
                # Don't need to update this row.
                del new_dict[id]

        # Insert / update rows.
        for new_row in new_dict.values():
            id, password, roles, domains = new_row
            old_row = old_dict.get(id)
            if old_row is not None:
                old_id, old_password, old_roles, old_domains = old_row
                if old_password != password:
                    # Update the password.
                    self.execute(self.update_sql, key=key, id=id,
                                 password=password)
            else:
                # Insert a new record.
                self.execute(self.insert_sql, key=key, id=id,
                             password=password)
                old_roles = ()
                old_domains = ()

            # Update the role list.
            if tuple(roles) != tuple(old_roles):
                self.execute(self.delete_roles_sql, key=key, id=id)
                for role in roles:
                    assert role
                    self.execute(self.insert_role_sql, key=key, id=id,
                                 role=role)

            # Update the domain list.
            if tuple(domains) != tuple(old_domains):
                self.execute(self.delete_domains_sql, key=key, id=id)
                for domain in domains:
                    assert domain
                    self.execute(self.insert_domain_sql, key=key, id=id,
                                 domain=domain)
                
        state = list(state)
        state.sort()
        return tuple(state)



=== Products/AdaptableStorage/gateway_sql/PsycopgConnection.py 1.6 => 1.6.2.1 ===
--- Products/AdaptableStorage/gateway_sql/PsycopgConnection.py:1.6	Tue Dec 31 16:47:45 2002
+++ Products/AdaptableStorage/gateway_sql/PsycopgConnection.py	Mon Jan 13 14:16:07 2003
@@ -16,8 +16,6 @@
 $Id$
 """
 
-import psycopg
-
 from mapper_public import ITPCConnection
 
 
@@ -28,9 +26,11 @@
     _final = 0
     db = None
     cursor = None
-    error = psycopg.DatabaseError
 
     def __init__(self, params='', prefix='zodb'):
+        import psycopg
+        self.error = psycopg.DatabaseError
+        self.Binary = psycopg.Binary
         self.params = params
         self.prefix = prefix
         self.connect_callbacks = []
@@ -42,6 +42,7 @@
         self.connect_callbacks.append(f)
 
     def connect(self):
+        import psycopg
         self.db = psycopg.connect(self.params)
         self.cursor = self.db.cursor()
         for f in self.connect_callbacks:
@@ -69,7 +70,7 @@
         return None
 
     def asBinary(self, data):
-        return psycopg.Binary(data)
+        return self.Binary(data)
 
     def begin(self):
         pass


=== Products/AdaptableStorage/gateway_sql/SQLClassification.py 1.4 => 1.4.2.1 ===
--- Products/AdaptableStorage/gateway_sql/SQLClassification.py:1.4	Tue Dec 31 16:47:45 2002
+++ Products/AdaptableStorage/gateway_sql/SQLClassification.py	Mon Jan 13 14:16:07 2003
@@ -16,7 +16,7 @@
 $Id$
 """
 
-from mapper_public import IGateway, FieldSchema
+from mapper_public import IGateway, FieldSchema, NoStateFoundError
 
 from SQLGatewayBase import SQLGatewayBase
 
@@ -29,12 +29,12 @@
 
     table_base_name = 'classification'
 
-    checkexist_sql = '''SELECT key FROM %(table)s LIMIT 1'''
+    checkexist_sql = '''SELECT key FROM %(table)s WHERE key = 0'''
 
     create_sql = '''CREATE TABLE %(table)s (
     key int PRIMARY KEY,
     meta_type character varying(255),
-    class_name character varying(255)  
+    class_name character varying(255)
     )'''
 
     read_sql = '''SELECT meta_type, class_name from %(table)s
@@ -63,7 +63,7 @@
             if rec[1]:
                 classification['class_name'] = rec[1]
         else:
-            rec = ()
+            raise NoStateFoundError(key)
         return classification, rec
 
     def store(self, event, classification):


=== Products/AdaptableStorage/gateway_sql/SQLFolderItems.py 1.5 => 1.5.2.1 ===
--- Products/AdaptableStorage/gateway_sql/SQLFolderItems.py:1.5	Tue Dec 31 16:47:45 2002
+++ Products/AdaptableStorage/gateway_sql/SQLFolderItems.py	Mon Jan 13 14:16:07 2003
@@ -30,7 +30,7 @@
 
     table_base_name = 'folder_items'
 
-    checkexist_sql = '''SELECT key FROM %(table)s LIMIT 1'''
+    checkexist_sql = '''SELECT key FROM %(table)s WHERE key = 0'''
 
     create_sql = '''CREATE TABLE %(table)s (
     key int,


=== Products/AdaptableStorage/gateway_sql/SQLGatewayBase.py 1.1 => 1.1.2.1 ===
--- Products/AdaptableStorage/gateway_sql/SQLGatewayBase.py:1.1	Mon Dec 23 23:29:32 2002
+++ Products/AdaptableStorage/gateway_sql/SQLGatewayBase.py	Mon Jan 13 14:16:07 2003
@@ -25,11 +25,14 @@
 
     def __init__(self, conn):
         self.conn = conn
-        self.table = conn.prefix + '_' + self.table_base_name
+        self.setupTableNames()
         if conn.isConnected():
             self.setupTables()
         else:
             conn.addConnectCallback(self.setupTables)
+
+    def setupTableNames(self):
+        self.table = self.conn.prefix + '_' + self.table_base_name
 
     def setupTables(self):
         conn = self.conn


=== Products/AdaptableStorage/gateway_sql/SQLObjectData.py 1.2 => 1.2.2.1 ===
--- Products/AdaptableStorage/gateway_sql/SQLObjectData.py:1.2	Tue Dec 31 16:47:45 2002
+++ Products/AdaptableStorage/gateway_sql/SQLObjectData.py	Mon Jan 13 14:16:07 2003
@@ -29,7 +29,7 @@
 
     table_base_name = 'object_data'
 
-    checkexist_sql = '''SELECT key FROM %(table)s LIMIT 1'''
+    checkexist_sql = '''SELECT key FROM %(table)s WHERE key = 0'''
 
     create_sql = '''CREATE TABLE %(table)s (
     key int PRIMARY KEY,


=== Products/AdaptableStorage/gateway_sql/SQLProperties.py 1.1 => 1.1.2.1 ===
--- Products/AdaptableStorage/gateway_sql/SQLProperties.py:1.1	Fri Jan  3 17:04:19 2003
+++ Products/AdaptableStorage/gateway_sql/SQLProperties.py	Mon Jan 13 14:16:07 2003
@@ -32,7 +32,7 @@
 
     table_base_name = 'properties'
 
-    checkexist_sql = '''SELECT key FROM %(table)s LIMIT 1'''
+    checkexist_sql = '''SELECT key FROM %(table)s WHERE key = 0'''
 
     create_sql = '''CREATE TABLE %(table)s (
     key int,


=== Products/AdaptableStorage/gateway_sql/public.py 1.3 => 1.3.2.1 ===
--- Products/AdaptableStorage/gateway_sql/public.py:1.3	Fri Jan  3 17:04:19 2003
+++ Products/AdaptableStorage/gateway_sql/public.py	Mon Jan 13 14:16:07 2003
@@ -25,3 +25,4 @@
 from SQLObjectData import SQLObjectData
 from SQLProperties import SQLProperties
 from SQLRemainder import SQLRemainder
+from SQLUserList import SQLUserList