Пример #1
0
    def manage_test(self, query, REQUEST=None):
        "Executes the SQL in parameter 'query' and returns results"
        dbc=self()      #get our connection
        res=dbc.query(query)

        if type(res) is type(''):
            f=StringIO()
            f.write(res)
            f.seek(0)
            result=RDB.File(f)
        else:
            result=Results(res)

        if REQUEST is None:
            return result       #return unadulterated result objects

        if result._searchable_result_columns():
            r=custom_default_report(self.id, result)
        else:
            r='This statement returned no results.'

        report=DocumentTemplate.HTML(
            '<html><body bgcolor="#ffffff" link="#000099" vlink="#555555">\n'
            '<dtml-var name="manage_tabs">\n<hr>\n%s\n\n'
            '<hr><h4>SQL Used:</strong><br>\n<pre>\n%s\n</pre>\n<hr>\n'
            '</body></html>'
            % (r, query))

        report=apply(report,(self,REQUEST),{self.id:result})

        return report
Пример #2
0
    def manage_test(self, query, REQUEST=None):
        "Executes the SQL in parameter 'query' and returns results"
        dbc = self()  #get our connection
        res = dbc.query(query)

        if type(res) is type(''):
            f = StringIO()
            f.write(res)
            f.seek(0)
            result = RDB.File(f)
        else:
            result = Results(res)

        if REQUEST is None:
            return result  #return unadulterated result objects

        if result._searchable_result_columns():
            r = custom_default_report(self.id, result)
        else:
            r = 'This statement returned no results.'

        report = HTML(
            '<html><body bgcolor="#ffffff" link="#000099" vlink="#555555">\n'
            '<dtml-var name="manage_tabs">\n<hr>\n%s\n\n'
            '<hr><h4>SQL Used:</strong><br>\n<pre>\n%s\n</pre>\n<hr>\n'
            '</body></html>' % (r, query))

        report = apply(report, (self, REQUEST), {self.id: result})

        return report
Пример #3
0
    def __call__(self, REQUEST=None, __ick__=None, src__=0, test__=0, **kw):
        """Call the database method

        The arguments to the method should be passed via keyword
        arguments, or in a single mapping object. If no arguments are
        given, and if the method was invoked through the Web, then the
        method will try to acquire and use the Web REQUEST object as
        the argument mapping.

        The returned value is a sequence of record objects.
        """

        if REQUEST is None:
            if kw: REQUEST=kw
            else:
                if hasattr(self, 'REQUEST'): REQUEST=self.REQUEST
                else: REQUEST={}

        try: dbc=getattr(self, self.connection_id)
        except AttributeError:
            raise AttributeError, (
                "The database connection <em>%s</em> cannot be found." % (
                self.connection_id))

        try: DB__=dbc()
        except: raise 'Database Error', (
            '%s is not connected to a database' % self.id)

        if hasattr(self, 'aq_parent'):
            p=self.aq_parent
            if self._isBeingAccessedAsZClassDefinedInstanceMethod():
                p=p.aq_parent
        else: p=None

        argdata=self._argdata(REQUEST)
        argdata['sql_delimiter']='\0'
        argdata['sql_quote__']=dbc.sql_quote__

        security=getSecurityManager()
        security.addContext(self)
        try:
            try:     query=apply(self.template, (p,), argdata)
            except TypeError, msg:
                msg = str(msg)
                if find(msg,'client'):
                    raise NameError("'client' may not be used as an " +
                        "argument name in this context")
                else: raise
        finally: security.removeContext(self)

        if src__: return query

        if self.cache_time_ > 0 and self.max_cache_ > 0:
            result=self._cached_result(DB__, (query, self.max_rows_))
        else: result=DB__.query(query, self.max_rows_)

        if hasattr(self, '_v_brain'): brain=self._v_brain
        else:
            brain=self._v_brain=getBrain(self.class_file_, self.class_name_)

        zc=self._zclass
        if zc is not None: zc=zc._zclass_

        if type(result) is type(''):
            f=StringIO()
            f.write(result)
            f.seek(0)
            result=RDB.File(f,brain,p, zc)
        else:
            result=Results(result, brain, p, zc)
        columns=result._searchable_result_columns()
        if test__ and columns != self._col: self._col=columns

        # If run in test mode, return both the query and results so
        # that the template doesn't have to be rendered twice!
        if test__: return query, result

        return result
Пример #4
0
    def __call__(self, REQUEST=None, __ick__=None, src__=0, test__=0, **kw):
        """Call the database method

        The arguments to the method should be passed via keyword
        arguments, or in a single mapping object. If no arguments are
        given, and if the method was invoked through the Web, then the
        method will try to acquire and use the Web REQUEST object as
        the argument mapping.

        The returned value is a sequence of record objects.
        """

        __traceback_supplement__ = (SQLMethodTracebackSupplement, self)

        if REQUEST is None:
            if kw: REQUEST = kw
            else:
                if hasattr(self, 'REQUEST'): REQUEST = self.REQUEST
                else: REQUEST = {}

        # connection hook
        c = self.connection_id
        # for backwards compatability
        hk = self.connection_hook
        # go get the connection hook and call it
        if hk: c = getattr(self, hk)()

        try:
            dbc = getattr(self, c)
        except AttributeError:
            raise AttributeError, (
                "The database connection <em>%s</em> cannot be found." % (c))

        try:
            DB__ = dbc()
        except:
            raise DatabaseError, ('%s is not connected to a database' %
                                  self.id)

        if hasattr(self, 'aq_parent'):
            p = self.aq_parent
            if self._isBeingAccessedAsZClassDefinedInstanceMethod():
                p = p.aq_parent
        else:
            p = None

        argdata = self._argdata(REQUEST)
        argdata['sql_delimiter'] = '\0'
        argdata['sql_quote__'] = dbc.sql_quote__

        security = getSecurityManager()
        security.addContext(self)
        try:
            try:
                query = apply(self.template, (p, ), argdata)
            except TypeError, msg:
                msg = str(msg)
                if find(msg, 'client') >= 0:
                    raise NameError("'client' may not be used as an " +
                                    "argument name in this context")
                else:
                    raise
        finally:
            security.removeContext(self)

        if src__: return query

        if self.cache_time_ > 0 and self.max_cache_ > 0:
            result = self._cached_result(DB__, query, self.max_rows_, c)
        else:
            result = DB__.query(query, self.max_rows_)

        if hasattr(self, '_v_brain'): brain = self._v_brain
        else:
            brain = self._v_brain = getBrain(self.class_file_,
                                             self.class_name_)

        zc = self._zclass
        if zc is not None: zc = zc._zclass_

        if type(result) is type(''):
            f = StringIO()
            f.write(result)
            f.seek(0)
            result = RDB.File(f, brain, p, zc)
        else:
            result = Results(result, brain, p, zc)
        columns = result._searchable_result_columns()
        if test__ and columns != self._col: self._col = columns

        # If run in test mode, return both the query and results so
        # that the template doesn't have to be rendered twice!
        if test__: return query, result

        return result