[Zope-CVS] CVS: Products/Ape/lib/apelib/sql -
classification.py:1.8.2.2 dbapi.py:1.11.2.2
interfaces.py:1.4.2.2 oidgen.py:1.5.2.2
properties.py:1.11.2.2 security.py:1.8.2.2
sqlbase.py:1.13.2.2 structure.py:1.11.2.2 table.py:1.1.2.2
Shane Hathaway
shane at zope.com
Tue Jul 20 22:26:28 EDT 2004
Update of /cvs-repository/Products/Ape/lib/apelib/sql
In directory cvs.zope.org:/tmp/cvs-serv32139/sql
Modified Files:
Tag: sql-types-branch
classification.py dbapi.py interfaces.py oidgen.py
properties.py security.py sqlbase.py structure.py table.py
Log Message:
Checkpoint. Started using common schemas for table specifications.
Renamed FieldSchema to ColumnSchema and got rid of RowSchema. The new
names are less abstract. This should make them easier to understand.
=== Products/Ape/lib/apelib/sql/classification.py 1.8.2.1 => 1.8.2.2 ===
--- Products/Ape/lib/apelib/sql/classification.py:1.8.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/classification.py Tue Jul 20 22:25:58 2004
@@ -16,7 +16,7 @@
$Id$
"""
-from apelib.core.schemas import FieldSchema, Column
+from apelib.core.schemas import ColumnSchema, RowSequenceSchema
from apelib.core.interfaces import OIDConflictError
from sqlbase import SQLGatewayBase
@@ -25,12 +25,11 @@
__implements__ = SQLGatewayBase.__implements__
- schema = FieldSchema('classification', 'classification')
+ schema = ColumnSchema('classification', 'classification')
table_name = 'classification'
- columns = [
- Column('class_name', 'string', 0),
- Column('mapper_name', 'string', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('class_name', 'string', 0)
+ table_schema.add('mapper_name', 'string', 0)
def load(self, event):
table = self.get_table(event)
=== Products/Ape/lib/apelib/sql/dbapi.py 1.11.2.1 => 1.11.2.2 ===
--- Products/Ape/lib/apelib/sql/dbapi.py:1.11.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/dbapi.py Tue Jul 20 22:25:58 2004
@@ -22,7 +22,7 @@
from apelib.core.interfaces import ITPCConnection
-from apelib.sql.interfaces import IRDBMSConnection
+from apelib.sql.interfaces import ISQLConnection
name_style_re = re.compile(':[A-Za-z0-9_-]+')
@@ -32,7 +32,7 @@
class AbstractSQLConnection:
- __implements__ = IRDBMSConnection, ITPCConnection
+ __implements__ = ISQLConnection, ITPCConnection
#column_type_translations = {} # { local type name -> db type name }
#column_name_translations = {} # { local col name -> db col name }
@@ -85,7 +85,7 @@
"""
raise NotImplementedError("Abstract Method")
- def execute(self, sql, kw=None, fetch=0):
+ def execute(self, sql, kw=None, fetch=False):
if self.connector is None:
raise RuntimeError('Not connected')
if kw is None:
=== Products/Ape/lib/apelib/sql/interfaces.py 1.4.2.1 => 1.4.2.2 ===
--- Products/Ape/lib/apelib/sql/interfaces.py:1.4.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/interfaces.py Tue Jul 20 22:25:58 2004
@@ -18,7 +18,7 @@
from Interface import Interface
from Interface.Attribute import Attribute
-from apelib.core.interfaces import IColumn
+from apelib.core.interfaces import IColumnSchema
class IRDBMSConnection (Interface):
@@ -39,8 +39,12 @@
'sequence'.
"""
- def define_table(name, columns):
- """Creates and returns an IRDBMSTable."""
+ def define_table(name, schema):
+ """Creates and returns an IRDBMSTable.
+
+ Does not create the table in the database. table.create()
+ creates the table.
+ """
def get_table(name):
"""Returns a previously defined IRDBMSTable."""
@@ -61,31 +65,42 @@
"""
+class ISQLConnection (IRDBMSConnection):
+
+ def execute(sql, kw=None, fetch=False):
+ """Executes a SQL query.
+
+ If kw is provided, parameters in the SQL are substituted for
+ parameter values. If fetch is true, the rows from the results
+ are returned. No type conversion happens in execute().
+ """
+
+
class IRDBMSTable (Interface):
"""A table in a database."""
def exists():
"""Returns true if this table exists in the database."""
- def select(result_columns, **filter):
+ def select(result_col_names, **filter):
"""Selects rows from a table and returns column values for those rows.
"""
- def insert(columns, row):
+ def insert(col_names, row):
"""Inserts one row in the table."""
- def set_one(oid, columns, row, is_new):
+ def set_one(oid, col_names, row, is_new):
"""Sets one row in the table.
Executes either an update or insert operation, depending
on the is_new argument and configured policies.
"""
- def set_many(oid, key_columns, other_columns, rows):
+ def set_many(oid, key_col_names, other_col_names, rows):
"""Sets multiple rows in the table.
'rows' is a sequence of tuples containing values for the
- key_columns as well as the other_columns.
+ key_col_names as well as the other_col_names.
Either deletes all rows for an oid and inserts new rows, or
examines the current state of the database and modifies it in
@@ -102,7 +117,7 @@
"""Drops the table."""
-class IRDBMSColumn (IColumn):
+class IRDBMSColumn (IColumnSchema):
"""A column associated with a specific database."""
def to_db(value):
=== Products/Ape/lib/apelib/sql/oidgen.py 1.5.2.1 => 1.5.2.2 ===
--- Products/Ape/lib/apelib/sql/oidgen.py:1.5.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/oidgen.py Tue Jul 20 22:25:58 2004
@@ -27,15 +27,14 @@
interfaces.IDatabaseInitializer)
table_name = 'oid_seq'
- columns = []
root_oid = "0"
def init(self, event):
conn = self.get_connection(event)
if not conn.exists(self.table_name, 'sequence'):
- conn.create_sequence(self.table, start=1)
+ conn.create_sequence(self.table_name, start=1)
elif event.clear_all:
- conn.reset_sequence(self.table, start=1)
+ conn.reset_sequence(self.table_name, start=1)
def new_oid(self, event):
assert interfaces.IGatewayEvent.isImplementedBy(event)
=== Products/Ape/lib/apelib/sql/properties.py 1.11.2.1 => 1.11.2.2 ===
--- Products/Ape/lib/apelib/sql/properties.py:1.11.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/properties.py Tue Jul 20 22:25:58 2004
@@ -49,11 +49,10 @@
schema.add('type', 'string')
schema.add('data', 'string')
table_name = 'properties'
- columns = [
- Column('id', 'string', 1),
- Column('type', 'string', 0),
- Column('data', 'blob', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('id', 'string', 1)
+ table_schema.add('type', 'string', 0)
+ table_schema.add('data', 'blob', 0)
def load(self, event):
table = self.get_table(event)
@@ -74,10 +73,9 @@
"""SQL fixed-schema properties gateway.
"""
- def __init__(self, conn_name, table_name, cols):
+ def __init__(self, conn_name, table_name, schema):
self.table_name = table_name
- self.columns = cols
- self.schema = None
+ self.schema = schema
SQLGatewayBase.__init__(self, conn_name)
def load(self, event):
@@ -140,10 +138,9 @@
schema = SQLProperties.schema
table_name = 'property_tables'
- columns = [
- Column('class_name', 'string', 1),
- Column('table_name', 'string', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('class_name', 'string', 1)
+ table_schema.add('table_name', 'string', 0)
oid_columns = [] # No OID column
def __init__(self, conn_name='db'):
@@ -167,7 +164,7 @@
self.fixed_props = {}
- def get_columns_for_class(self, module_name, class_name):
+ def get_schema_for_class(self, module_name, class_name):
"""Returns the class-defined property schema.
This Zope2-ism should be made pluggable later on.
@@ -175,7 +172,7 @@
d = {}
m = __import__(module_name, d, d, ('__doc__',))
klass = getattr(m, class_name)
- cols = []
+ schema = RowSequenceSchema()
props = getattr(klass, '_properties', ())
if not props:
return None
@@ -189,8 +186,8 @@
name = '_oid'
else:
name = prop_name
- cols.append((name, p['type'], 0))
- return tuple(cols)
+ schema.add(name, p['type'], 0)
+ return schema
def get_fixed_props(self, event):
@@ -211,8 +208,8 @@
raise ValueError, "Not a qualified class name: %s" % repr(cn)
module_name = cn[:pos]
class_name = cn[pos + 1:]
- cols = self.get_columns_for_class(module_name, class_name)
- if not cols:
+ schema = self.get_schema_for_class(module_name, class_name)
+ if not schema.get_columns():
# No fixed properties exist for this class.
self.fixed_props[cn] = None
return None
@@ -237,7 +234,7 @@
table.insert(('class_name', 'table_name'), (cn, table_name))
# Create the fixed properties and table
- fp = SQLFixedProperties(self.conn_name, table_name, cols)
+ fp = SQLFixedProperties(self.conn_name, table_name, schema)
fp.init(event)
# XXX If the transaction gets aborted, the table creation will
# be undone, but self.fixed_props won't see the change.
=== Products/Ape/lib/apelib/sql/security.py 1.8.2.1 => 1.8.2.2 ===
--- Products/Ape/lib/apelib/sql/security.py:1.8.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/security.py Tue Jul 20 22:25:58 2004
@@ -16,7 +16,7 @@
$Id$
"""
-from apelib.core.schemas import RowSequenceSchema
+from apelib.core.schemas import RowSequenceSchema, ColumnSchema
from sqlbase import SQLGatewayBase
@@ -32,7 +32,7 @@
schema.add('username', 'string')
table_name = 'security'
- oid_columns = [Column('oid', 'int', 0)] # Don't create a primary key
+ oid_columns = [ColumnSchema('oid', 'int', 0)] # Don't create a primary key
def load(self, event):
table = self.get_table(event)
@@ -61,13 +61,13 @@
schema.add('domains', 'string:list')
table_defs = {
- 'users': [Column('oid', 'int', 1),
- Column('id', 'string', 1),
- Column('password', 'string', 0)],
- 'user_roles': [Column('oid', 'int', 0),
- Column('id', 'string', 0),
- Column('role', 'string', 0)],
- 'user_domains': [Column('oid', 'int', 0),
+ 'users': [('oid', 'int', 1),
+ ('id', 'string', 1),
+ ('password', 'string', 0)],
+ 'user_roles': [('oid', 'int', 0),
+ ('id', 'string', 0),
+ ('role', 'string', 0)],
+ 'user_domains': [('oid', 'int', 0),
('id', 'string', 0),
('domain', 'string', 0)],
}
@@ -76,7 +76,10 @@
def init(self, event):
conn = self.get_connection(event)
for table_name, columns in self.table_defs.items():
- table = conn.define_table(table_name, columns)
+ table_schema = RowSequenceSchema()
+ for args in columns:
+ table_schema.add(*args)
+ table = conn.define_table(table_name, table_schema)
if not table.exists():
table.create()
elif event.clear_all:
=== Products/Ape/lib/apelib/sql/sqlbase.py 1.13.2.1 => 1.13.2.2 ===
--- Products/Ape/lib/apelib/sql/sqlbase.py:1.13.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/sqlbase.py Tue Jul 20 22:25:58 2004
@@ -17,7 +17,7 @@
"""
from apelib.core.interfaces import IGateway, IDatabaseInitializer
-from apelib.core.schemas import Column
+from apelib.core.schemas import ColumnSchema, RowSequenceSchema
from interfaces import IRDBMSConnection
@@ -29,17 +29,17 @@
# override these in subclasses
table_name = None
schema = None
- columns = None
- oid_columns = [Column('oid', 'int', 1)]
+ table_schema = None
+ oid_columns = [ColumnSchema('oid', 'int', 1)]
def __init__(self, conn_name='db'):
self.conn_name = conn_name
- if self.columns is None:
+ if self.table_schema is None:
if self.schema is not None:
- self.columns = self.schema.get_columns()
+ self.table_schema = schema
else:
- self.columns = []
- self.column_names = [c.name for c in self.columns]
+ self.table_schema = RowSequenceSchema()
+ self.column_names = [f.name for f in self.table_schema.get_columns()]
def get_connection(self, event):
return event.connections[self.conn_name]
@@ -54,7 +54,8 @@
def init(self, event):
conn = self.get_connection(event)
assert IRDBMSConnection.isImplementedBy(conn)
- table = conn.define_table(self.table_name, self.columns)
+ all = RowSequenceSchema(self.oid_columns + self.table_schema.get_columns())
+ table = conn.define_table(self.table_name, all)
if table.exists():
if event.clear_all:
table.delete_rows()
=== Products/Ape/lib/apelib/sql/structure.py 1.11.2.1 => 1.11.2.2 ===
--- Products/Ape/lib/apelib/sql/structure.py:1.11.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/structure.py Tue Jul 20 22:25:58 2004
@@ -16,7 +16,7 @@
$Id$
"""
-from apelib.core.schemas import FieldSchema, RowSequenceSchema, Column
+from apelib.core.schemas import ColumnSchema, RowSequenceSchema
from sqlbase import SQLGatewayBase
@@ -25,11 +25,10 @@
__implements__ = SQLGatewayBase.__implements__
- schema = FieldSchema('data', 'string')
+ schema = ColumnSchema('data', 'string')
table_name = 'object_data'
- columns = [
- Column('data', 'blob', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('data', 'blob', 0)
def load(self, event):
table = self.get_table(event)
@@ -59,10 +58,9 @@
schema.add('oid', 'string')
schema.add('classification', 'classification')
table_name = 'folder_items'
- columns = [
- Column('name', 'string', 1),
- Column('child_oid', 'int', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('name', 'string', 1)
+ table_schema.add('child_oid', 'int', 0)
def load(self, event):
table = self.get_table(event)
@@ -96,12 +94,11 @@
__implements__ = SQLGatewayBase.__implements__
- schema = FieldSchema('id', 'string')
+ schema = ColumnSchema('id', 'string')
table_name = 'folder_items'
- columns = [
- Column('child_oid', 'int', 1),
- Column('name', 'string', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('child_oid', 'int', 1)
+ table_schema.add('name', 'string', 0)
def init(self, event):
pass
@@ -126,9 +123,8 @@
__implements__ = SQLGatewayBase.__implements__
table_name = 'remainder'
- columns = [
- Column('pickle', 'blob', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('pickle', 'blob', 0)
class SQLModTime (SQLGatewayBase):
@@ -136,11 +132,10 @@
__implements__ = SQLGatewayBase.__implements__
- schema = FieldSchema('mtime', 'int') # second
+ schema = ColumnSchema('mtime', 'int') # second
table_name = 'mtime'
- columns = [
- Column('mtime', 'long', 0),
- ]
+ table_schema = RowSequenceSchema()
+ table_schema.add('mtime', 'long', 0)
def load(self, event):
table = self.get_table(event)
=== Products/Ape/lib/apelib/sql/table.py 1.1.2.1 => 1.1.2.2 ===
--- Products/Ape/lib/apelib/sql/table.py:1.1.2.1 Tue Jul 20 02:12:43 2004
+++ Products/Ape/lib/apelib/sql/table.py Tue Jul 20 22:25:58 2004
@@ -24,13 +24,17 @@
__implements__ = IRDBMSTable
- def __init__(self, connection, name, columns):
+ def __init__(self, connection, name):
self.name = name
self.execute = connection.execute
- for key, value in columns:
- assert IRDMSColumn.isImplementedBy(value)
- self.columns = dict(columns)
- self.column_order = [key for (key, value) in columns]
+ self.columns = {}
+ self.column_order = []
+
+ def add_column(self, name, column):
+ assert not self.columns.has_key(name)
+ assert IRDBMSColumn.isImplementedBy(column)
+ self.columns[name] = column
+ self.column_order.append(name)
def generate_conditions(self, col_names):
clauses = [
More information about the Zope-CVS
mailing list