Example #1
0
                            # coerce column to unicode with database encoding
                            newcol = unicode(col, db_encoding)
                            # Encode column as string with site_encoding
                            newcol = newcol.encode(site_encoding)
                        else:
                            newcol = col

                        columns += newcol,

                    encoded_result.append(columns)

                result = (result[0], encoded_result)

            result = Results(result, brain, p, None)

        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

    def abort(self):
        dbc, DB__ = self._get_dbc()
        try:
            DB__.tpc_abort()
Example #2
0
            #import pdb; pdb.post_mortem()
            raise
        brain = getBrain(class_file_, class_name_)
        # XXX remove this logging for performance
        LOG(__name__, INFO, "Using special brain: %r\n" % (brain, ))
    else:
        brain = getBrain(self.class_file_, self.class_name_)

    if type(result) is type(''):
        f = StringIO()
        f.write(result)
        f.seek(0)
        result = RDB.File(f, brain, p)
    else:
        result = Results(result, brain, p)
    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


def DA_upgradeSchema(self,
                     connection_id=None,
                     create_if_not_exists=False,
                     initialize=None,
                     src__=0,
                     **kw):
Example #3
0
File: DA.py Project: Elbagoury/erp5
def DA__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)

    c = kw.pop("connection_id", None)
    #if c is not None:
    #LOG("DA", 300, "connection %s provided to %s" %(c, self.id))
    # patch: dynamic brain configuration
    zsql_brain = kw.pop('zsql_brain', None)
    # patch end

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

    # Patch to implement dynamic connection id
    # Connection id is retrieve from user preference
    if c is None:
        physical_path = self.getPhysicalPath()
        # XXX cleaner solution will be needed
        if 'portal_catalog' not in physical_path and\
           'cmf_activity' not in self.connection_id and\
           'transactionless' not in self.connection_id:
            try:
                archive_id = self.portal_preferences.getPreferredArchive()
            except AttributeError:
                pass
            else:
                if archive_id not in (None, ''):
                    archive_id = archive_id.split('/')[-1]
                    #LOG("DA__call__, archive_id 2", 300, archive_id)
                    archive = self.portal_archives._getOb(archive_id, None)
                    if archive is not None:
                        c = archive.getConnectionId()
                        #LOG("DA call", INFO, "retrieved connection %s from preference" %(c,))

    if c is None:
        # 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)()
    #LOG("DA__call__ connection", 300, c)
    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)

    p = aq_parent(self)  # None if no aq_parent

    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:
        try:
            #         if 'portal_ids' in query:
            #           LOG("DA query", INFO, "query = %s" %(query,))
            result = DB__.query(query, self.max_rows_)
        except:
            LOG("DA call raise",
                ERROR,
                "DB = %s, c = %s, query = %s" % (DB__, c, query),
                error=sys.exc_info())
            raise

    # patch: dynamic brain configuration
    if zsql_brain is not None:
        try:
            class_file_, class_name_ = zsql_brain.rsplit('.', 1)
        except:
            #import pdb; pdb.post_mortem()
            raise
        brain = getBrain(class_file_, class_name_)
        # XXX remove this logging for performance
        LOG(__name__, INFO, "Using special brain: %r\n" % (brain, ))
    else:
        brain = getBrain(self.class_file_, self.class_name_)

    if type(result) is type(''):
        f = StringIO()
        f.write(result)
        f.seek(0)
        result = RDB.File(f, brain, p)
    else:
        result = Results(result, brain, p)
    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
Example #4
0
def DA__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)

    c = kw.pop("connection_id", None)
    #if c is not None:
      #LOG("DA", 300, "connection %s provided to %s" %(c, self.id))
    # patch: dynamic brain configuration
    zsql_brain = kw.pop('zsql_brain', None)
    # patch end


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

    # Patch to implement dynamic connection id
    # Connection id is retrieve from user preference
    if c is None:
      physical_path = self.getPhysicalPath()
      # XXX cleaner solution will be needed
      if 'portal_catalog' not in physical_path and\
         'cmf_activity' not in self.connection_id and\
         'transactionless' not in self.connection_id:
        try:
          archive_id = self.portal_preferences.getPreferredArchive()
        except AttributeError:
          pass
        else:
          if archive_id not in (None, ''):
            archive_id = archive_id.split('/')[-1]
            #LOG("DA__call__, archive_id 2", 300, archive_id)
            archive = self.portal_archives._getOb(archive_id, None)
            if archive is not None:
              c = archive.getConnectionId()
              #LOG("DA call", INFO, "retrieved connection %s from preference" %(c,))

    if c is None:
      # 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)()
    #LOG("DA__call__ connection", 300, c)
    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)

    p = aq_parent(self) # None if no aq_parent

    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:
      try:
#         if 'portal_ids' in query:
#           LOG("DA query", INFO, "query = %s" %(query,))
        result=DB__.query(query, self.max_rows_)
      except:
        LOG("DA call raise", ERROR, "DB = %s, c = %s, query = %s" %(DB__, c, query), error=sys.exc_info())
        raise

    # patch: dynamic brain configuration
    if zsql_brain is not None:
        try:
          class_file_, class_name_ = zsql_brain.rsplit('.', 1)
        except:
          #import pdb; pdb.post_mortem()
          raise
        brain = getBrain(class_file_, class_name_)
        # XXX remove this logging for performance
        LOG(__name__, INFO, "Using special brain: %r\n" % (brain,))
    else:
        brain = getBrain(self.class_file_, self.class_name_)

    if type(result) is type(''):
        f=StringIO()
        f.write(result)
        f.seek(0)
        result=RDB.File(f,brain,p)
    else:
        result=Results(result, brain, p)
    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