[Zope-CVS] CVS: Products/Ape/lib/apelib/sql - oidgen.py:1.2
classification.py:1.6 properties.py:1.6 querygen.py:1.5
security.py:1.5 sqlbase.py:1.10 structure.py:1.6 keygen.py:NONE
Shane Hathaway
shane at zope.com
Mon Feb 2 10:07:52 EST 2004
Update of /cvs-repository/Products/Ape/lib/apelib/sql
In directory cvs.zope.org:/tmp/cvs-serv26672/lib/apelib/sql
Modified Files:
classification.py properties.py querygen.py security.py
sqlbase.py structure.py
Added Files:
oidgen.py
Removed Files:
keygen.py
Log Message:
Moved ape-0_8-branch to the HEAD.
>From CHANGES.txt:
- Major restructuring to reduce the number of concepts in
Ape. Keychains and keys have been replaced with simple string OIDs.
There is now a flat namespace of mappers instead of a tree. Only
one classifier and one OID generator are used in any object
database.
- The ZODB root object is now stored on the filesystem.
=== Products/Ape/lib/apelib/sql/oidgen.py 1.1 => 1.2 ===
--- /dev/null Mon Feb 2 10:07:52 2004
+++ Products/Ape/lib/apelib/sql/oidgen.py Mon Feb 2 10:07:21 2004
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# 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 OID generator
+
+$Id$
+"""
+
+from apelib.core import interfaces
+
+import sqlbase
+
+
+class SQLOIDGenerator (sqlbase.SQLGatewayBase):
+
+ __implements__ = (interfaces.IOIDGenerator,
+ interfaces.IDatabaseInitializer)
+
+ table_base_name = 'oid_seq'
+ column_defs = ()
+
+ root_oid = "0"
+
+ def init(self, event):
+ conn = event.connections[self.conn_name]
+ first_time = 0
+ try:
+ rows = self.execute(event, 'sequence_check', 1)
+ if len(rows) == 0:
+ first_time = 1
+ except conn.error:
+ conn.db.rollback()
+ self.execute(event, 'sequence_create')
+ first_time = 1
+ if first_time:
+ self.execute(event, 'sequence_insert')
+ if event.clear_all:
+ self.execute(event, 'sequence_clear')
+ conn.db.commit()
+
+ def new_oid(self, event, name, stored):
+ if not stored:
+ raise exceptions.ConfigurationError(
+ 'OIDs generated by SQLOIDGenerator must be stored')
+ if interfaces.ISDEvent.isImplementedBy(event):
+ # Request that the other side do the work (for ZEO)
+ n = event.obj_db.new_oid()
+ else:
+ self.execute(event, 'sequence_update')
+ n = self.execute(event, 'sequence_read', 1)[0][0]
+ return str(n)
+
=== Products/Ape/lib/apelib/sql/classification.py 1.5 => 1.6 ===
--- Products/Ape/lib/apelib/sql/classification.py:1.5 Wed Jul 9 11:40:06 2003
+++ Products/Ape/lib/apelib/sql/classification.py Mon Feb 2 10:07:21 2004
@@ -17,7 +17,7 @@
"""
from apelib.core.schemas import FieldSchema
-from apelib.core.exceptions import NoStateFoundError
+from apelib.core.interfaces import OIDConflictError
from sqlbase import SQLGatewayBase
@@ -27,7 +27,7 @@
schema = FieldSchema('classification', 'classification')
- table_base_name = 'classification2' # Version 2
+ table_base_name = 'classification'
column_defs = (
('class_name', 'string', 0),
@@ -35,8 +35,7 @@
)
def load(self, event):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
classification = {}
if items:
rec = items[0]
@@ -45,15 +44,17 @@
if rec[1]:
classification['mapper_name'] = rec[1]
else:
- raise NoStateFoundError(key)
+ raise KeyError(event.oid)
return classification, rec
def store(self, event, classification):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
+ if items and event.is_new:
+ # Something is already stored at this OID.
+ raise OIDConflictError(event.oid)
cn = classification.get('class_name', '')
mn = classification.get('mapper_name', '')
- kw = {'key': key, 'class_name': cn, 'mapper_name': mn}
+ kw = {'oid': event.oid, 'class_name': cn, 'mapper_name': mn}
if items:
self.execute(event, 'update', **kw)
else:
=== Products/Ape/lib/apelib/sql/properties.py 1.5 => 1.6 ===
--- Products/Ape/lib/apelib/sql/properties.py:1.5 Mon Aug 11 14:02:16 2003
+++ Products/Ape/lib/apelib/sql/properties.py Mon Feb 2 10:07:21 2004
@@ -41,40 +41,38 @@
)
def load(self, event):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
items.sort()
return items, tuple(items)
def store(self, event, state):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
state_dict = {}
for row in state:
id = row[0]
state_dict[id] = row
items_dict = {}
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
for old_row in items:
id = old_row[0]
items_dict[id] = old_row
state_row = state_dict.get(id)
if state_row is None:
# Remove a property
- self.execute(event, 'delete', key=key, id=id)
+ self.execute(event, 'delete', oid=event.oid, id=id)
elif old_row != state_row:
# Update a property
id, t, v = state_row
data = conn.asBinary(v)
self.execute(event, 'update',
- key=key, id=id, type=t, data=data)
+ oid=event.oid, id=id, type=t, data=data)
for row in state:
if not items_dict.has_key(row[0]):
# Add a property
id, t, v = row
data = conn.asBinary(v)
self.execute(event, 'insert',
- key=key, id=id, type=t, data=data)
+ oid=event.oid, id=id, type=t, data=data)
state = list(state)
state.sort()
return tuple(state)
@@ -103,7 +101,7 @@
"""Creates the fixed property table without triggering an error.
"""
# Note: event is any kind of IGatewayEvent.
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
if conn.prefix:
full_table_name = '%s_%s' % (conn.prefix, self.table_base_name)
else:
@@ -124,8 +122,7 @@
def load(self, event):
- key = long(event.getKey())
- recs = self.execute(event, 'read', fetch=1, key=key)
+ recs = self.execute(event, 'read', fetch=1, oid=event.oid)
if not recs:
return (), ()
if len(recs) > 1:
@@ -172,12 +169,11 @@
raise ValueError(
"Extra properties provided for fixed schema: %s"
% statedict.keys())
- key = event.getKey()
- recs = self.execute(event, 'read', fetch=1, key=key)
+ recs = self.execute(event, 'read', fetch=1, oid=event.oid)
if not recs:
- self.execute(event, 'insert', key=key, _data=data)
+ self.execute(event, 'insert', oid=event.oid, _data=data)
else:
- self.execute(event, 'update', key=key, _data=data)
+ self.execute(event, 'update', oid=event.oid, _data=data)
return tuple(record)
@@ -196,19 +192,15 @@
self.var_props = SQLProperties(conn_name=conn_name)
self.fixed_props = {} # class name -> SQLFixedProperties instance
- def getSchema(self):
- return self.schema
-
- def getSources(self, event):
+ def getPollSources(self, event):
return None
-
def init(self, event):
self.var_props.init(event)
- if event.clearing():
+ if event.clear_all:
# Clear the fixed property tables by searching for tables
# with a special name.
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
if conn.prefix:
to_find = '%s_fp_' % conn.prefix
else:
@@ -238,8 +230,8 @@
return None
for p in props:
prop_name = p['id']
- if prop_name == 'key':
- name = '_key'
+ if prop_name == 'oid':
+ name = '_oid'
else:
name = prop_name
cols.append((name, p['type'], 0))
=== Products/Ape/lib/apelib/sql/querygen.py 1.4 => 1.5 ===
--- Products/Ape/lib/apelib/sql/querygen.py:1.4 Mon Aug 11 13:56:05 2003
+++ Products/Ape/lib/apelib/sql/querygen.py Mon Feb 2 10:07:21 2004
@@ -36,12 +36,12 @@
column_type_translations = None # { local type name -> db type name }
column_name_translations = None # { local col name -> db col name }
- key_column = ('key', 'int', 1)
+ oid_column = ('oid', 'int', 1)
def __init__(self, table_name, column_defs):
# column_defs is a sequence of (column_name, column_type, unique)
self.table_name = table_name
- self.column_defs = (self.key_column,) + tuple(column_defs)
+ self.column_defs = (self.oid_column,) + tuple(column_defs)
def translateName(self, column_name):
"""Returns a column name for a variable name.
@@ -64,7 +64,7 @@
def gen_check(self):
- kc = self.translateName(self.key_column[0])
+ kc = self.translateName(self.oid_column[0])
# This is meant to raise an error if the table is missing.
# Returns nothing if the table exists.
return 'SELECT %s FROM %s WHERE 0 = 1' % (kc, self.table_name)
@@ -84,10 +84,10 @@
for name, typ, u in self.column_defs[1:]:
trans_name = self.translateName(name)
cols.append(trans_name)
- key_column_name = self.key_column[0]
- trans_kc = self.translateName(key_column_name)
+ oid_column_name = self.oid_column[0]
+ trans_kc = self.translateName(oid_column_name)
return 'SELECT %s FROM %s WHERE %s = %%(%s)s' % (
- ', '.join(cols), self.table_name, trans_kc, key_column_name)
+ ', '.join(cols), self.table_name, trans_kc, oid_column_name)
def gen_simple_search(self):
@@ -157,7 +157,9 @@
'date_international': 'date',
}
- column_name_translations = {}
+ column_name_translations = {
+ 'oid': 'objoid',
+ }
def gen_table_names(self):
# Returns the names of the tables.
@@ -195,7 +197,7 @@
}
column_name_translations = {
- 'key': 'objkey',
+ 'oid': 'objoid',
}
def gen_table_names(self):
=== Products/Ape/lib/apelib/sql/security.py 1.4 => 1.5 ===
--- Products/Ape/lib/apelib/sql/security.py:1.4 Mon May 19 15:32:34 2003
+++ Products/Ape/lib/apelib/sql/security.py Mon Feb 2 10:07:21 2004
@@ -34,22 +34,20 @@
table_base_name = 'security'
def load(self, event):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
items.sort()
return items, tuple(items)
def store(self, event, state):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
items.sort()
state = list(state)
state.sort()
if state != items:
- self.execute(event, 'delete', key=key)
+ self.execute(event, 'delete', oid=event.oid)
for row in state:
self.execute(
- event, 'insert', key=key, declaration_type=row[0],
+ event, 'insert', oid=event.oid, declaration_type=row[0],
role=row[1], permission=row[2], username=row[3])
return tuple(state)
@@ -77,9 +75,9 @@
def init(self, event):
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
try:
- if event.clearing():
+ if event.clear_all:
self.execute(event, 'users', 'clear')
self.execute(event, 'user_roles', 'clear')
self.execute(event, 'user_domains', 'clear')
@@ -94,7 +92,7 @@
def execute(self, event, table, operation, *args, **kw):
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
query = conn.getQuery(
table, self.table_defs[table], operation)
if query == '':
@@ -104,17 +102,16 @@
def load(self, event):
- key = event.getKeychain()[-1]
- rows = self.execute(event, 'users', 'read', 1, key=key)
+ rows = self.execute(event, 'users', 'read', 1, oid=event.oid)
data = {}
for id, password in rows:
data[id] = (password, [], [])
- rows = self.execute(event, 'user_roles', 'read', 1, key=key)
+ rows = self.execute(event, 'user_roles', 'read', 1, oid=event.oid)
for id, role in rows:
row = data.get(id)
if row is not None:
row[1].append(role)
- rows = self.execute(event, 'user_domains', 'read', 1, key=key)
+ rows = self.execute(event, 'user_domains', 'read', 1, oid=event.oid)
for id, domain in rows:
row = data.get(id)
if row is not None:
@@ -127,9 +124,6 @@
def store(self, event, state):
- keychain = event.getKeychain()
- keychain1 = keychain[:-1]
- key = long(keychain[-1])
old_rows, old_serial = self.load(event)
new_dict = {}
for rec in state:
@@ -144,9 +138,11 @@
new_row = new_dict.get(id)
if new_row is None:
# Remove this row.
- self.execute(event, 'users', 'delete', key=key, id=id)
- self.execute(event, 'user_roles', 'delete', key=key, id=id)
- self.execute(event, 'user_domains', 'delete', key=key, id=id)
+ self.execute(event, 'users', 'delete', oid=event.oid, id=id)
+ self.execute(
+ event, 'user_roles', 'delete', oid=event.oid, id=id)
+ self.execute(
+ event, 'user_domains', 'delete', oid=event.oid, id=id)
elif new_row == old_row:
# Don't need to update this row.
del new_dict[id]
@@ -160,29 +156,31 @@
if old_password != password:
# Update the password.
self.execute(event, 'users', 'update',
- key=key, id=id, password=password)
+ oid=event.oid, id=id, password=password)
else:
# Insert a new record.
self.execute(event, 'users', 'insert',
- key=key, id=id, password=password)
+ oid=event.oid, id=id, password=password)
old_roles = ()
old_domains = ()
# Update the role list.
if tuple(roles) != tuple(old_roles):
- self.execute(event, 'user_roles', 'delete', key=key, id=id)
+ self.execute(
+ event, 'user_roles', 'delete', oid=event.oid, id=id)
for role in roles:
assert role
self.execute(event, 'user_roles', 'insert',
- key=key, id=id, role=role)
+ oid=event.oid, id=id, role=role)
# Update the domain list.
if tuple(domains) != tuple(old_domains):
- self.execute(event, 'user_domains', 'delete', key=key, id=id)
+ self.execute(
+ event, 'user_domains', 'delete', oid=event.oid, id=id)
for domain in domains:
assert domain
self.execute(event, 'user_domains', 'insert',
- key=key, id=id, domain=domain)
+ oid=event.oid, id=id, domain=domain)
state = list(state)
state.sort()
=== Products/Ape/lib/apelib/sql/sqlbase.py 1.9 => 1.10 ===
--- Products/Ape/lib/apelib/sql/sqlbase.py:1.9 Wed Jul 30 17:33:05 2003
+++ Products/Ape/lib/apelib/sql/sqlbase.py Mon Feb 2 10:07:21 2004
@@ -31,19 +31,14 @@
def __init__(self, conn_name='db'):
self.conn_name = conn_name
- if self.column_defs is None:
- schema = self.getSchema()
- if schema is not None:
- self.column_defs = tuple(schema.getColumnDefs())
-
- def getSchema(self):
- return self.schema
+ if self.column_defs is None and self.schema is not None:
+ self.column_defs = tuple(self.schema.getColumnDefs())
def init(self, event):
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
assert ISQLConnection.isImplementedBy(conn)
try:
- if event.clearing():
+ if event.clear_all:
self.execute(event, 'clear')
else:
self.execute(event, 'check')
@@ -53,7 +48,7 @@
conn.db.commit()
def execute(self, event, operation, *args, **kw):
- conn = event.getConnection(self.conn_name)
+ conn = event.connections[self.conn_name]
query = conn.getQuery(
self.table_base_name, self.column_defs, operation)
if query == '':
@@ -67,5 +62,5 @@
def store(self, event, obj):
raise NotImplementedError, "abstract method"
- def getSources(self, event):
+ def getPollSources(self, event):
return None
=== Products/Ape/lib/apelib/sql/structure.py 1.5 => 1.6 ===
--- Products/Ape/lib/apelib/sql/structure.py:1.5 Mon May 19 15:32:34 2003
+++ Products/Ape/lib/apelib/sql/structure.py Mon Feb 2 10:07:21 2004
@@ -34,8 +34,7 @@
)
def load(self, event):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
if items:
state = items[0][0]
else:
@@ -43,11 +42,10 @@
return state, state
def store(self, event, state):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
col_name = self.column_defs[0][0]
- conn = event.getConnection(self.conn_name)
- kw = {'key': key, col_name: conn.asBinary(state)}
+ conn = event.connections[self.conn_name]
+ kw = {'oid': event.oid, col_name: conn.asBinary(state)}
if items:
# update.
self.execute(event, 'update', **kw)
@@ -63,52 +61,42 @@
__implements__ = SQLGatewayBase.__implements__
schema = RowSequenceSchema()
- schema.addField('id', 'string', 1)
- schema.addField('keychain', 'keychain')
+ schema.addField('key', 'string', 1)
+ schema.addField('oid', 'string')
table_base_name = 'folder_items'
column_defs = (
('name', 'string', 1),
- ('child_key', 'int', 0),
+ ('child_oid', 'int', 0),
)
def load(self, event):
- keychain = event.getKeychain()
- key = long(keychain[-1])
- prefix = keychain[:-1]
- rows = self.execute(event, 'read', 1, key=key)
+ rows = self.execute(event, 'read', 1, oid=event.oid)
rows.sort()
- res = [(row[0], prefix + (long(row[1]),)) for row in rows]
+ res = [(row[0], str(row[1])) for row in rows]
return res, tuple(res)
def store(self, event, state):
- keychain = event.getKeychain()
- keychain1 = keychain[:-1]
- key = long(keychain[-1])
state_dict = {}
- for name, child_keychain in state:
+ for name, child_oid in state:
state_dict[name] = 1
- rows = self.execute(event, 'read', 1, key=key)
+ rows = self.execute(event, 'read', 1, oid=event.oid)
db_dict = {}
- for name, child_key in rows:
+ for name, child_oid in rows:
if state_dict.has_key(name):
- db_dict[name] = child_key
+ db_dict[name] = child_oid
else:
# Remove this item from the database.
- self.execute(event, 'delete', key=key, name=name)
+ self.execute(event, 'delete', oid=event.oid, name=name)
state = list(state)
state.sort()
- for name, child_keychain in state:
- if child_keychain[:-1] != keychain1:
- raise ValueError(
- "SQLFolderItems cannot store cross-domain references")
- child_key = long(child_keychain[-1])
- kw = {'key': key, 'name': name, 'child_key': child_key}
+ for name, child_oid in state:
+ kw = {'oid': event.oid, 'name': name, 'child_oid': child_oid}
if db_dict.has_key(name):
- if db_dict[name] != child_key:
+ if db_dict[name] != child_oid:
# Change this item to point to a different OID.
self.execute(event, 'update', **kw)
else:
@@ -130,15 +118,16 @@
table_base_name = 'folder_items'
- column_defs = (('child_key', 'int', 1),
- ('name', 'string', 0),)
+ column_defs = (
+ ('child_oid', 'int', 1),
+ ('name', 'string', 0),
+ )
def init(self, event):
pass
def load(self, event):
- key = long(event.getKey())
- rows = self.execute(event, 'simple_search', 1, child_key=key)
+ rows = self.execute(event, 'simple_search', 1, child_oid=event.oid)
assert len(rows) >= 1
name = rows[0][1] # Accept only the first result
return name, None
@@ -173,8 +162,7 @@
)
def load(self, event):
- key = long(event.getKey())
- items = self.execute(event, 'read', 1, key=key)
+ items = self.execute(event, 'read', 1, oid=event.oid)
if items:
state = long(items[0][0])
else:
@@ -182,10 +170,9 @@
return state, state
def store(self, event, state):
- key = long(event.getKey())
state = long(state)
- items = self.execute(event, 'read', 1, key=key)
- kw = {'key': key, 'mtime': state}
+ items = self.execute(event, 'read', 1, oid=event.oid)
+ kw = {'oid': event.oid, 'mtime': state}
if items:
# update.
self.execute(event, 'update', **kw)
=== Removed File Products/Ape/lib/apelib/sql/keygen.py ===
More information about the Zope-CVS
mailing list