operators.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » pylibs » PyDO » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » pylibs » PyDO » operators.py
# $Id$
# Time-stamp: <2003-03-28 08:46:54 drew>

########################################################################  
#  Copyright (C) 2001 Jacob Smullyan <smulloni@smullyan.org>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
#
import string, types

class SQLOperator:
    def asTuple(self):
        """
        returns a lisp-like nested tuple representation of the operator and its
        arguments, included nested operators.  The tuple is of the form (symbol, *args)
        """
        _astuple=lambda val: (isinstance(val, SQLOperator) and val.asTuple()) or val
        lst=[self.__class__.symbol]
        for v in self.values:
            lst.append(_astuple(v))
        return tuple(lst)
    
    def asSQL(self, fieldDict=None, conn=None):
        """
        this performs no escaping, as we don't necessarily know what the field
        or its type is in this context.  However, the use of type-specifier input
        classes, like FIELD, could ameliorate the problem to a degree.
        """
        _assql=lambda v: (isinstance(v, SQLOperator) and "(%s)" % v.asSQL()) \
                or (type(v) in (types.StringType, types.UnicodeType) and "'%s'" % v) or str(v)
        vallen=len(self.values)
        if vallen==1:            
            return "%s %s" % (self.__class__.symbol, _assql(self.values[0]))
        else:
            return (" %s " % self.__class__.symbol).join(map(_assql, self.values))

class MonadicOperator(SQLOperator):
    def __init__(self, val):
        self.values=(val,)
        
class DyadicOperator(SQLOperator):
    def __init__(self, lval, rval):
        self.values=(lval, rval)

class PolyadicOperator(SQLOperator):
    def __init__(self, *values):
        self.values=values

class FIELD:
    """
    a cheesy way of indicating that a string represents the
    name of a field, not a string literal
    """
    def __init__(self, fieldname):
        if type(fieldname)!=type(""):
            raise TypeError, "fieldname must be a string"
        self.fieldname=fieldname

    def __str__(self):
        return self.fieldname

    def __repr__(self):
        return "FIELD(%s)" % self.fieldname

class SET:
    """
    a cheesy way of passing a set into PyDO where clauses
    (for IN), e.g.:
    
    IN(FIELD('foo'), SET('spam', 'eggs', 'nougat'))
    """
    def __init__(self, *values):
        if not len(values):
            raise ValueError, "you must supply some values"
        self.values=tuple(values)
        
    def __str__(self):
        l=len(self.values)
        if l>1:
            return "(%s)" % ', '.join([self.__escape(x) for x in self.values])
        else:
            return "(%s)" % self.__escape(self.values[0])

    def __escape(self, thing):
        """
        this is somewhat inadequate, as it doesn't handle types
        beyond ints and strings; you'll need to perform your own
        escaping for dates and times (I can't tell what field is
        intended, whether you need a data or a time, etc.)
        """
        if type(thing) in (types.StringType, types.UnicodeType):
            return "'%s'" % string.replace(thing, "'", "\\'")
        return str(thing)
    
    def __repr__(self):
        return "SET(%s)" % self.__str__()

"""
mapping of symbols to operator classes
"""
symbol_table={}

def tupleToSQL(optuple):
    """
    takes the tuple notation, as produced by SQLOperator.asTuple(),
    and translates it into SQL
    """
    _assql=lambda v: (type(v)==type(()) and "(%s)" % tupleToSQL(v)) \
            or (type(v) in (types.StringType, types.UnicodeType) and "'%s'" % v) or str(v)
    lent=len(optuple)
    if lent<2:
        raise ValueError, "malformed input"
    if lent==2: # monadic
        return "%s %s" % (optuple[0], _assql(optuple[1]))
    elif lent>2: #dyadic or better
        return (" %s " % optuple[0]).join(map(_assql, optuple[1:]))

def tupleToSQLOperator(optuple):
    """
    inverse operator of SQLOperator.asTuple()
    """
    lent=len(optuple)
    if lent<2:
        raise ValueError, "malformed input"    
    sym=optuple[0]
    kl=symbol_table.get(sym)
    if not kl:
        raise ValueError, "symbol %s not recognized" % sym
    arglist=map(lambda x: (type(x)==type(()) and tupleToSQLOperator(x)) or x, optuple[1:])
    return kl(*arglist)

def __makeOperators():
    """
    generates the operator classes;
    more need to be added, here are some obvious ones
    """
    import new
    _factory=(  (  MonadicOperator,
                   ('NOT', '!'),
                   ),
                (  DyadicOperator,
                   ('EQ', '='),
                   ('NE', '!='),
                   ('LT', '<'),
                   ('LT_EQ', '<='),
                   ('GT', '>'),
                   ('GT_EQ', '>='),
                   ('LIKE', 'LIKE'),
                   ('IN', 'IN'),
                   ),
                (  PolyadicOperator,
                   ('AND', 'AND'),
                   ('OR', 'OR'),
                   ('PLUS', '+'),
                   ('MINUS', '-'),
                   ('MULT', '*'),
                   ('DIV', '/'),
                   )
                )
    for tup in _factory:
        base=tup[0]
        for specs in tup[1:]:
            klname, sym=specs
            kl=new.classobj(klname, (base,), {})
            kl.symbol=sym
            globals()[klname]=kl
            symbol_table[sym]=kl


            
########################################################################
            
__makeOperators()
del __makeOperators
        

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.