Esempio n. 1
0
 def _createIndexView(self, xmlindex_list, compact=False):
     """
     Creates an index view using all given XMLIndex objects.
     """
     # sanity checks
     if not isinstance(xmlindex_list, list):
         msg = "Parameter xmlindex_list must be a list of XMLIndex objects."
         raise InvalidParameterError(msg)
     if len(xmlindex_list) < 1:
         msg = "Parameter xmlindex_list may not be empty."
         raise InvalidParameterError(msg)
     id = xmlindex_list[0].resourcetype._id
     package_id = xmlindex_list[0].resourcetype.package.package_id
     resourcetype_id = xmlindex_list[0].resourcetype.resourcetype_id
     # check if resource types are the same for all indexes:
     for xmlindex in xmlindex_list:
         if xmlindex.resourcetype._id == id:
             continue
         msg = "XmlIndex objects must be from the same resource type."
         raise InvalidParameterError(msg)
     columns = [
         document_tab.c['id'].label("document_id"),
         document_meta_tab.c['datetime'].label("document_last_modified")
     ]
     joins = document_tab.c['id'] == document_meta_tab.c['id']
     if not compact:
         # add also columns package_id and resourcetype_id and resource_name
         if not self.env.db.isSQLite():
             columns.extend([
                 sql.literal_column("(VARCHAR(255) '%s')" % \
                     package_id).label("package_id"),
                 sql.literal_column("(VARCHAR(255) '%s')" % \
                     resourcetype_id).label("resourcetype_id"),
                 resource_tab.c['name'].label("resource_name"),
             ])
         else:
             package_id = self.db.dialect.identifier_preparer.\
                 quote_identifier(package_id)
             resourcetype_id = self.db.dialect.identifier_preparer.\
                 quote_identifier(resourcetype_id)
             columns.extend([
                 sql.literal_column(package_id).label("package_id"),
                 sql.literal_column(resourcetype_id).\
                     label("resourcetype_id"),
                 resource_tab.c['name'].label("resource_name"),
             ])
     query = select(columns, joins, distinct=True)
     # add recursive all given indexes
     query, joins = self._joinIndexes(xmlindex_list, query)
     # join over resource
     oncl = resource_tab.c['id'] == document_tab.c['resource_id']
     joins = joins.join(resource_tab, onclause=oncl)
     # joins over resource type
     oncl = sql.and_(
         resourcetypes_tab.c['id'] == resource_tab.c['resourcetype_id'],
         resourcetypes_tab.c['id'] == \
             sql.literal_column("%s" % int(xmlindex.resourcetype._id)),
     )
     joins = joins.join(resourcetypes_tab, onclause=oncl)
     return query, joins
Esempio n. 2
0
 def objects_from_id(self, package_id, resourcetype_id):
     package = None
     resourcetype = None
     if not package_id:
         return package, resourcetype
     package = self.db_getPackage(package_id)
     if not package:
         msg = 'Package not present in database: %s' % str(package_id)
         raise InvalidParameterError(msg)
     if not resourcetype_id:
         return package, resourcetype
     resourcetype = self.db_getResourceType(package_id, resourcetype_id)
     if not resourcetype:
         msg = "Resourcetype not present in database: %s"
         raise InvalidParameterError(msg % str(resourcetype_id))
     return package, resourcetype
Esempio n. 3
0
 def setName(self, data):
     try:
         data = validate_id(data)
     except ValueError:
         msg = "Invalid resource name: %s"
         raise InvalidParameterError(msg % str(data))
     self._name = data
Esempio n. 4
0
 def parse(self, expr):
     self._init_parser()
     try:
         return self.setAttributes(self.parser(expr))
     except pp.ParseException, e:
         msg = "Error parsing query: Unexpected or invalid token at " + \
               "position %s: %s"
         raise InvalidParameterError(msg %
                                     (str(e.loc), str(e.markInputline())))
Esempio n. 5
0
 def addResource(self, resource=Resource()):
     """
     Add a new resource to the database.
     """
     if not resource.document.data or resource.document.data == "":
         raise InvalidParameterError('Empty document!')
     try:
         self.store(resource, resource.document.meta, resource.document)
     except DbError, e:
         msg = "Error adding a resource: A resource with the given " + \
               "parameters already exists."
         raise DuplicateObjectError(msg, e)
Esempio n. 6
0
def newXMLDocument(data, id=None, uid=None):
    """
    Returns a new XmlDocument object.

    Data will be converted to unicode and a possible XML declaration will be
    removed. Use this method whenever you wish to create a XmlDocument 
    manually!
    """
    # check for data
    if len(data) == 0:
        raise InvalidParameterError("XML document is empty.")
    # convert data to unicode and remove XML declaration
    if isinstance(data, unicode):
        data, _ = parseXMLDeclaration(data, remove_decl=True)
    else:
        data, _ = toUnicode(data, remove_decl=True)
    return XmlDocument(data, id, uid)
Esempio n. 7
0
 def modifyResource(self, old_resource, resource, uid=None):
     """
     Modify an existing resource.
     
     In case of a version controlled resource a new revision is created.
     XXX: new revisions are created always, whether or not the resource's 
     document has actually changed -> compare old/new document ?
     """
     if not old_resource.resourcetype._id == resource.resourcetype._id:
         msg = "Error modifying a resource: Resourcetypes of old and " + \
               "new resource do not match. %s != %s"
         raise InvalidParameterError(
             msg %
             (old_resource.resourcetype._id, resource.resourcetype._id))
     # all document metadata may be overwritten by the new document
     resource.document.meta.uid = uid
     resource._id = old_resource._id
     if resource.resourcetype.version_control:
         self.update(resource, cascading=True)
     else:
         resource.document._id = old_resource.document._id
         resource.document.meta._id = old_resource.document.meta._id
         self.update(resource, resource.document, resource.document.meta)
Esempio n. 8
0
 def _applyOp(self, op, left, right, complement=False):
     # create sqlalchemy clauses from string operators
     if op == '==' or op == '=':
         return left == right
     elif op == '!=':
         return left != right
     elif op == '<':
         return left < right
     elif op == '>':
         return left > right
     elif op == '<=':
         return left <= right
     elif op == '>=':
         return left >= right
     elif op == 'and':
         if complement:
             return sql.or_(left, right)
         return sql.and_(left, right)
     elif op == 'or':
         if complement:
             return sql.and_(left, right)
         return sql.or_(left, right)
     raise InvalidParameterError("Operator '%s' not specified." % op)
Esempio n. 9
0
 def registerIndex(self, package_id=None, resourcetype_id=None,
                   label=None, xpath=None, type="text",
                   options=None):
     """
     Register an index.
     
     @param type: "text" | "numeric" | "float" | "datetime" | "boolean" |
                  "date" | "integer" | "timestamp"
     """
     # check for label
     if not label:
         msg = "registerIndex: No index label defined!"
         raise InvalidParameterError(msg)
     if '/' in label:
         msg = "registerIndex: Label may not include an slash!"
         raise InvalidParameterError(msg)
     if len(label) > 30:
         msg = "registerIndex: Label may not exceed 30 chars!"
         raise InvalidParameterError(msg)
     # check for XPath expression
     if not xpath:
         msg = "registerIndex: Empty XPath expression!"
         raise InvalidParameterError(msg)
     # check if valid index type
     if type.lower() not in INDEX_TYPES:
         msg = "registerIndex: Invalid index type '%s'."
         raise InvalidParameterError(msg % type)
     # check for grouping indexes
     xpath = xpath.strip()
     group_path = None
     if '#' in xpath:
         try:
             group_path, temp = xpath.split('#')
         except ValueError, e:
             msg = "registerIndex: Invalid index expression. %s"
             raise InvalidParameterError(msg % str(e))
         xpath = '/'.join([group_path, temp])
Esempio n. 10
0
 def _applyFunc(self, func, expr, q, joins):
     if func == 'not':
         # select the complementary result set
         return self._process_predicates(expr, q, joins, complement=True)
     raise InvalidParameterError("Function '%s' not specified." % func)