Exemple #1
0
def autoschema(alias,
               schema=None,
               guesscache=True,
               module=None,
               sequence_mapper=None):
    """
    returns a dictionary of PyDO objects created automatically by
    schema introspection, keyed by class name.  Typical usage:

      globals().update(autoschema('myalias'))

    The PyDO objects created are extremely bare, but may be enough for
    quick scripts.   If you want to pickle them, pass in a module
    for them to live in for the "module" parameter.  
    """
    ns = {}
    db = getConnection(alias)
    for table in db.listTables(schema):
        d = dict(guesscache=guesscache,
                 guess_columns=True,
                 connectionAlias=alias,
                 schema=schema,
                 table=table)
        if sequence_mapper:
            d['sequence_mapper'] = sequence_mapper
        Table = table.capitalize()
        obj = type(Table, (PyDO, ), d)
        if module:
            moduleize(module, obj)
        ns[Table] = obj
    return ns
def autoschema(alias, schema=None, guesscache=True, module=None, sequence_mapper=None):
    """
    returns a dictionary of PyDO objects created automatically by
    schema introspection, keyed by class name.  Typical usage:

      globals().update(autoschema('myalias'))

    The PyDO objects created are extremely bare, but may be enough for
    quick scripts.   If you want to pickle them, pass in a module
    for them to live in for the "module" parameter.  
    """
    ns={}
    db=getConnection(alias)
    for table in db.listTables(schema):
        d=dict(guesscache=guesscache,
               guess_columns=True,
               connectionAlias=alias,
               schema=schema,
               table=table)
        if sequence_mapper:
            d['sequence_mapper'] = sequence_mapper
        Table=table.capitalize()
        obj=type(Table, (PyDO,), d)
        if module:
            moduleize(module, obj)
        ns[Table]=obj
    return ns
Exemple #3
0
    def project(cls, *fields, **kwargs):
        # also accept passing in a list or tuple (backwards compatibility)
        if len(fields) == 1 and isinstance(fields[0], (list, tuple)):
            fields = fields[0]

        acceptable = frozenset(('mutable', 'module'))
        diff = frozenset(kwargs) - acceptable
        if diff:
            raise ValueError, "unrecognized keyword arguments: %s" \
                  % ', '.join(str(x) for x in diff)
        mutable = kwargs.get('mutable', cls.mutable)
        module = kwargs.get('module', None)
        s = []
        for f in fields:
            if isinstance(f, Field):
                s.append(f.name.lower())
            elif isinstance(f, basestring):
                s.append(f.lower())
            elif isinstance(f, tuple):
                if not len(f):
                    raise ValueError, "empty tuple in field list"
                s.append(f[0].lower())
            else:
                raise ValueError, "weird thing in field list: %s" % f
        s.sort()
        if kwargs:
            s.append('0')
            s.append('_'.join(sorted('%s_%s' % x for x in kwargs.items())))
        t = tuple(s)
        if cls._projections.has_key(t):
            kls = cls._projections[t]
        else:
            klsname = 'projection_%s__%s' % (cls.__name__, '_'.join(s))
            kls = type(
                klsname, (cls, ),
                dict(fields=fields,
                     mutable=mutable,
                     table=cls.getTable(False),
                     _is_projection=True))
            cls._projections[t] = kls
        if module:
            moduleize(module, kls)
        return kls
 def project(cls, *fields, **kwargs):
     # also accept passing in a list or tuple (backwards compatibility)
     if len(fields)==1 and isinstance(fields[0], (list, tuple)):
         fields=fields[0]
         
     acceptable=frozenset(('mutable','module'))
     diff=frozenset(kwargs)-acceptable
     if diff:
         raise ValueError, "unrecognized keyword arguments: %s" \
               % ', '.join(str(x) for x in diff)
     mutable=kwargs.get('mutable', cls.mutable)
     module=kwargs.get('module', None)
     s=[]
     for f in fields:
         if isinstance(f, Field):
             s.append(f.name.lower())
         elif isinstance(f, basestring):
             s.append(f.lower())
         elif isinstance(f, tuple):
             if not len(f):
                 raise ValueError, "empty tuple in field list"
             s.append(f[0].lower())
         else:
             raise ValueError, "weird thing in field list: %s" % f
     s.sort()
     if kwargs:
         s.append('0')
         s.append('_'.join(sorted('%s_%s' % x for x in kwargs.items())))        
     t=tuple(s)
     if cls._projections.has_key(t):
         kls=cls._projections[t]
     else:
         klsname='projection_%s__%s' % (cls.__name__,
                                        '_'.join(s))
         kls=type(klsname, (cls,), dict(fields=fields,
                                        mutable=mutable,
                                        table=cls.getTable(False),
                                        _is_projection=True))
         cls._projections[t]=kls
     if module:
         moduleize(module, kls)
     return kls
    def project(cls, *fields, **kwargs):
        # also accept passing in a list or tuple (backwards compatibility)
        if len(fields) == 1 and isinstance(fields[0], (list, tuple)):
            fields = fields[0]

        acceptable = frozenset(("mutable", "module"))
        diff = frozenset(kwargs) - acceptable
        if diff:
            raise ValueError, "unrecognized keyword arguments: %s" % ", ".join(str(x) for x in diff)
        mutable = kwargs.get("mutable", cls.mutable)
        module = kwargs.get("module", None)
        s = []
        for f in fields:
            if not isinstance(f, Field):
                if isinstance(f, dict):
                    f = cls._create_field(**f)
                elif isinstance(f, (list, tuple)):
                    f = cls._create_field(*f)
                elif isinstance(f, basestring):
                    f = cls._create_field(f)
                else:
                    raise ValueError, "weird thing in field list: %s" % f
            s.append(f.asname.lower())
        s.sort()
        if kwargs:
            s.append("0")
            s.append("_".join(sorted("%s_%s" % x for x in kwargs.items())))
        t = tuple(s)
        if cls._projections.has_key(t):
            kls = cls._projections[t]
        else:
            klsname = "projection_%s__%s" % (cls.__name__, "_".join(s))
            kls = type(
                klsname, (cls,), dict(fields=fields, mutable=mutable, table=cls.getTable(False), _is_projection=True)
            )
            cls._projections[t] = kls
        if module:
            moduleize(module, kls)
        return kls