Example #1
0
 def __init__(self, connectionName='', source='', arguments=''):
     self.template = SQLDTML(source)
     self.connectionName = connectionName
     # In our case arguments should be a string that is parsed
     self.arguments = arguments
Example #2
0
class SQLScript(Persistent, Contained):
    implements(ISQLScript)

    def __init__(self, connectionName='', source='', arguments=''):
        self.template = SQLDTML(source)
        self.connectionName = connectionName
        # In our case arguments should be a string that is parsed
        self.arguments = arguments

    def setArguments(self, arguments):
        assert isinstance(arguments, StringTypes), (
               '"arguments" argument of setArguments() must be a string'
               )
        self._arg_string = arguments
        self._arguments = parseArguments(arguments)

    def getArguments(self):
        """See zope.app.sqlscript.interfaces.ISQLScript"""
        return self._arguments

    def getArgumentsString(self):
        return self._arg_string

    # See zope.app.sqlscript.interfaces.ISQLScript
    arguments = property(getArgumentsString, setArguments)

    def setSource(self, source):
        self.template.munge(source)

    def getSource(self):
        return self.template.read_raw()

    # See zope.app.sqlscript.interfaces.ISQLScript
    source = property(getSource, setSource)

    def getTemplate(self):
        """See zope.app.sqlscript.interfaces.ISQLScript"""
        return self.template

    def _setConnectionName(self, name):
        self._connectionName = name
        cache = getCacheForObject(self)
        location = getLocationForCache(self)

        if cache and location:
            cache.invalidate(location)

    def _getConnectionName(self):
        return self._connectionName

    # See zope.app.sqlscript.interfaces.ISQLScript
    connectionName = property(_getConnectionName, _setConnectionName)

    def getConnection(self):
        name = self.connectionName
        connection = zapi.getUtility(IZopeDatabaseAdapter, name)
        return connection()

    def __call__(self, **kw):
        """See zope.app.rdb.interfaces"""

        # Try to resolve arguments
        arg_values = {}
        missing = []
        for name in self._arguments.keys():
            name = name.encode('UTF-8')
            try:
                # Try to find argument in keywords
                arg_values[name] = kw[name]
            except KeyError:
                # Okay, the first try failed, so let's try to find the default
                arg = self._arguments[name]
                try:
                    arg_values[name] = arg['default']
                except KeyError:
                    # Now the argument might be optional anyways; let's check
                    try:
                        if not arg['optional']:
                            missing.append(name)
                    except KeyError:
                        missing.append(name)

        try:
            connection = self.getConnection()
        except KeyError:
            raise AttributeError("The database connection '%s' cannot be "
                                 "found." % (self.connectionName))

        query = apply(self.template, (), arg_values)
        cache = getCacheForObject(self)
        location = getLocationForCache(self)
        if cache and location:
            _marker = object()
            result = cache.query(location, {'query': query}, default=_marker)
            if result is not _marker:
                return result
        result = queryForResults(connection, query)
        if cache and location:
            cache.set(result, location, {'query': query})
        return result