[SQL-CVS] r707 - in trunk/SQLObject/sqlobject: . mysql
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-04-04 05:54:22
|
Author: ianb Date: 2005-04-04 05:54:10 +0000 (Mon, 04 Apr 2005) New Revision: 707 Modified: trunk/SQLObject/sqlobject/boundattributes.py trunk/SQLObject/sqlobject/col.py trunk/SQLObject/sqlobject/main.py trunk/SQLObject/sqlobject/mysql/__init__.py Log: Added a default connection, sqlhub; made _connection.style setting in __classinit__ catch the AttributeError that hubs can throw Modified: trunk/SQLObject/sqlobject/boundattributes.py =================================================================== --- trunk/SQLObject/sqlobject/boundattributes.py 2005-04-04 02:21:00 UTC (rev 706) +++ trunk/SQLObject/sqlobject/boundattributes.py 2005-04-04 05:54:10 UTC (rev 707) @@ -100,8 +100,8 @@ factory_class = None - def make_object(cls, *args, **kw): - return cls.factory_class(*args, **kw) + def make_object(cls, added_class, attr_name, *args, **kw): + return cls.factory_class(added_class, attr_name, *args, **kw) def bind_attributes(cls, new_attrs): for name, value in new_attrs.items(): Modified: trunk/SQLObject/sqlobject/col.py =================================================================== --- trunk/SQLObject/sqlobject/col.py 2005-04-04 02:21:00 UTC (rev 706) +++ trunk/SQLObject/sqlobject/col.py 2005-04-04 05:54:10 UTC (rev 707) @@ -980,3 +980,218 @@ all.append(key) __all__.extend(all) del all + + + +# import boundattributes +# import declarative + +# class BoundCol(object): + +# __metaclass__ = declarative.DeclarativeMeta + +# sqlTypeRegistry = {} + +# def __classinit__(cls, new_attrs): +# cls.sqlTypeRegistry = cls.sqlTypeRegistry.copy() + +# def registerSQLType(cls, dbname, type_func): +# if not isinstance(dbname, (list, tuple)): +# dbname = [dbname] +# for name in dbname: +# cls.sqlTypeRegistry[name] = type_func +# registerSQLType = classmethod(registerSQLType) + +# def __init__(self, added_class, attr_name, +# dbName=None, +# default=NoDefault, +# alternateID=False, +# alternateMethodName=None, +# constraints=None, +# notNull=NoDefault, +# notNon=NoDefault, +# sqlType=None, +# validator=None, +# immutable=False, +# cascade=None, +# lazy=False, +# noCache=False, +# forceDBName=False): +# if not forceDBName: +# assert sqlbuilder.sqlIdentifier(name), ( +# "Name must be SQL-safe (letters, numbers, underscores): " +# "%s (or use forceDBName=True)" % repr(name)) +# assert attr_name != 'id', ( +# "The column name 'id' is reserved for SQLObject use (and is " +# "implicitly created).") +# assert name, "You must provide a name for all columns" +# self.immutable = immutable +# assert cascade in (None, True, False), ( +# "The cascade attribute must be one of None, True, or False " +# "(you gave: %r" % cascade) +# self.cascade = cascade + +# if not isinstance(constraints, (list, tuple)): +# constraints = [constraints] +# self.constraints = self.autoConstraints() + constraints + +# self.notNone = False +# if notNull is not NoDefault: +# self.notNone = notNull +# assert (notNone is NoDefault or +# (not notNone) == (not notNull)), ( +# "The notNull and notNone arguments are aliases, and must " +# "not conflict. You gave notNull=%r, notNone=%r" +# % (notNull, notNone)) +# elif notNone is not NoDefault: +# self.notNone = notNone +# if self.notNone: +# self.constraints = [consts.notNull] + self.constraints + +# self.name = attr_name +# self.soClass = added_class +# self._default = default +# self.customSQLType = sqlType + +# # if they don't give us a specific database name for +# # the column, we separate the mixedCase into mixed_case +# # and assume that. +# if dbName is None: +# self.dbName = added_class.sqlmeta.style.pythonAttrToDBColumn(self.name) +# else: +# self.dbName = dbName + +# # alternateID means that this is a unique column that +# # can be used to identify rows +# self.alternateID = alternateID +# if self.alternateID and alternateMethodName is None: +# self.alternateMethodName = 'by' + self.name[0].capitalize() + self.name[1:] +# else: +# self.alternateMethodName = alternateMethodName + +# if unique is NoDefault: +# self.unique = alternateID +# else: +# self.unique = unique +# self.validator = validator +# self.noCache = noCache +# self.lazy = lazy + +# self.soClass.addColumn(self.name, self) + +# def _set_validator(self, value): +# self._validator = value +# if self._validator: +# self.toPython = self._validator.toPython +# self.fromPython = self._validator.fromPython +# else: +# self.toPython = None +# self.fromPython = None + +# def _get_validator(self): +# return self._validator + +# validator = property(_get_validator, _set_validator) + +# def autoConstraints(self): +# return [] + +# def _get_default(self): +# # A default can be a callback or a plain value, +# # here we resolve the callback +# if self._default is NoDefault: +# return NoDefault +# elif hasattr(self._default, '__sqlrepr__'): +# return self._default +# elif callable(self._default): +# return self._default() +# else: +# return self._default +# default = property(_get_default, None, None) + +# def __repr__(self): +# r = '<%s %s' % (self.__class__.__name__, self.name) +# if self.default is not NoDefault: +# r += ' default=%s' % repr(self.default) +# if self.foreignKey: +# r += ' connected to %s' % self.foreignKey +# if self.alternateID: +# r += ' alternate ID' +# if self.notNone: +# r += ' not null' +# return r + '>' + +# def createSQL(self, dbname): +# if self.customSQLType is not None: +# return self.customSQLType +# try: +# func = self.sqlTypeRegistry[dbname] +# except KeyError: +# assert 0, ( +# "The database %s does not support the column type %s " +# "(no SQL type generator found; only %s found)" +# % (dbname, self.__class__.__name__, +# ', '.join(self.sqlTypeRegistry.keys()))) +# else: +# return func(self, dbname) + +# def __get__(self, obj, type=None): +# if obj is None: +# # class attribute, return the descriptor itself +# return self +# if obj.sqlmeta.obsolete: +# raise SQLObjectNotFound, ( +# "This object has been deleted from the database: %r" +# % obj) +# if obj.sqlmeta.cacheColumns: +# columns = obj.sqlmeta._columnCache +# if columns is None: +# obj.sqlmeta.loadValues() +# try: +# return columns[name] +# except KeyError: +# return obj.sqlmeta.loadColumn(self) +# else: +# return obj.sqlmeta.loadColumn(self) + +# def __set__(self, obj, value): +# if self.immutable: +# raise AttributeError("The column %s.%s is immutable" % +# (obj.__class__.__name__, +# self.name)) +# obj.sqlmeta.setColumn(self, value) + +# def __delete__(self, obj): +# raise AttributeError("I can't be deleted from %r" % obj) + +# class Col(boundattributes.BoundFactory): +# factory_class = BoundCol + +# def addToClass(self, cls, addToThisClass, name): +# me = self or cls +# me.__addtoclass__(addToThisClass, name) +# addToClass = declarative.classinstancemethod(addToClass) + +# class BoundStringCol(BoundCol): + +# def __init__(self, *args, **kw): +# self.length = popKey(kw, 'length') +# self.varchar = popKey(kw, 'varchar', 'auto') +# if not self.length: +# assert self.varchar == 'auto' or not self.varchar, ( +# "Without a length strings are treated as TEXT, not " +# "VARCHAR") +# self.varchar = False +# elif self.varchar == 'auto': +# self.varchar = True +# BoundCol.__init__(self, *args, **kw) + +# def autoConstraints(self): +# constraints = [consts.isString] +# if self.length is not None: +# constraints += [consts.MaxLength(self.length)] +# return constraints + + +# class StringCol(Col): +# factory_class = BoundStringCol Modified: trunk/SQLObject/sqlobject/main.py =================================================================== --- trunk/SQLObject/sqlobject/main.py 2005-04-04 02:21:00 UTC (rev 706) +++ trunk/SQLObject/sqlobject/main.py 2005-04-04 05:54:10 UTC (rev 707) @@ -187,10 +187,12 @@ def setClass(cls, soClass): cls.soClass = soClass if not cls.style: - if cls.soClass._connection and cls.soClass._connection.style: - cls.style = cls.soClass._connection.style - else: - cls.style = styles.defaultStyle + cls.style = styles.defaultStyle + try: + if cls.soClass._connection and cls.soClass._connection.style: + cls.style = cls.soClass._connection.style + except AttributeError: + pass if cls.table is None: cls.table = cls.style.pythonClassToDBTable(cls.soClass.__name__) if cls.idName is None: @@ -221,6 +223,8 @@ setClass = classmethod(setClass) +sqlhub = dbconnection.ConnectionHub() + class _sqlmeta_attr(object): def __init__(self, name, deprecation_level): @@ -267,7 +271,7 @@ # will be true. It's false by default: _SO_perConnection = False - _connection = None + _connection = sqlhub _columns = [] @@ -1397,4 +1401,4 @@ __all__ = ['NoDefault', 'SQLObject', 'sqlmeta', 'getID', 'getObject', - 'SQLObjectNotFound'] + 'SQLObjectNotFound', 'sqlhub'] Modified: trunk/SQLObject/sqlobject/mysql/__init__.py =================================================================== --- trunk/SQLObject/sqlobject/mysql/__init__.py 2005-04-04 02:21:00 UTC (rev 706) +++ trunk/SQLObject/sqlobject/mysql/__init__.py 2005-04-04 05:54:10 UTC (rev 707) @@ -1,4 +1,5 @@ from sqlobject.dbconnection import registerConnection +#import mysqltypes def builder(): import mysqlconnection |