Update copyright year + FSF address
[idzebra-moved-to-github.git] / index / attribute.c
index 2f7d894..9e8f327 100644 (file)
@@ -1,48 +1,33 @@
-/*
- * Copyright (C) 1994-1998, Index Data I/S 
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Log: attribute.c,v $
- * Revision 1.9  1998-05-20 10:12:14  adam
- * Implemented automatic EXPLAIN database maintenance.
- * Modified Zebra to work with ASN.1 compiled version of YAZ.
- *
- * Revision 1.8  1998/03/05 08:45:11  adam
- * New result set model and modular ranking system. Moved towards
- * descent server API. System information stored as "SGML" records.
- *
- * Revision 1.7  1997/10/29 12:05:01  adam
- * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
- *
- * Revision 1.6  1997/09/17 12:19:11  adam
- * Zebra version corresponds to YAZ version 1.4.
- * Changed Zebra server so that it doesn't depend on global common_resource.
- *
- * Revision 1.5  1997/09/05 15:30:08  adam
- * Changed prototype for chr_map_input - added const.
- * Added support for C++, headers uses extern "C" for public definitions.
- *
- * Revision 1.4  1996/10/29 14:06:48  adam
- * Include zebrautl.h instead of alexutil.h.
- *
- * Revision 1.3  1996/05/09 07:28:54  quinn
- * Work towards phrases and multiple registers
- *
- * Revision 1.2  1995/11/15  19:13:07  adam
- * Work on record management.
- *
- *
- * This interface is used by other modules (the Z-server in particular)
- * to normalize the attributes given in queries.
- */
+/* $Id: attribute.c,v 1.26 2006-08-14 10:40:15 adam Exp $
+   Copyright (C) 1995-2006
+   Index Data ApS
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
 
 #include <stdio.h>
 
-#include <log.h>
-#include <res.h>
-#include <zebrautl.h>
-#include "zserver.h"
+#include <yaz/log.h>
+#include <yaz/diagbib1.h>
+#include <idzebra/res.h>
+#include <idzebra/util.h>
+#include <attrfind.h>
+#include "index.h"
 
 static data1_att *getatt(data1_attset *p, int att)
 {
@@ -60,21 +45,166 @@ static data1_att *getatt(data1_attset *p, int att)
     return 0;
 }
 
-int att_getentbyatt(ZebraHandle zi, attent *res, oid_value set, int att)
+static int att_getentbyatt(ZebraHandle zi, oid_value set, int att,
+                           const char **name)
 {
     data1_att *r;
     data1_attset *p;
 
-    if (!(p = data1_attset_search_id (zi->dh, set)))
+    if (!(p = data1_attset_search_id (zi->reg->dh, set)))
     {
-       zebraExplain_loadAttsets (zi->dh, zi->res);
-       p = data1_attset_search_id (zi->dh, set);
+       zebraExplain_loadAttsets (zi->reg->dh, zi->res);
+       p = data1_attset_search_id (zi->reg->dh, set);
     }
-    if (!p)
-       return -2;
+    if (!p)   /* set undefined */
+        return -2;
     if (!(r = getatt(p, att)))
        return -1;
-    res->attset_ordinal = r->parent->reference;
-    res->local_attributes = r->locals;
+    *name = r->name;
     return 0;
 }
+
+
+ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
+                                  Z_AttributeList *attr_list,
+                                  zinfo_index_category_t cat,
+                                  int index_type,
+                                  oid_value curAttributeSet,
+                                  int *ord)
+{
+    int use_value = -1;
+    const char *use_string = 0;
+    AttrType use;
+
+    attr_init_AttrList(&use, attr_list, 1);
+    use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
+
+    if (use_value < 0)
+    {
+        if (!use_string)
+            use_string = "any";
+    }
+    else
+    {
+        /* we have a use attribute and attribute set */
+        int r;
+        
+        r = att_getentbyatt(zh, curAttributeSet, use_value, &use_string);
+        if (r == -2)
+        {
+            zebra_setError_zint(zh,  YAZ_BIB1_UNSUPP_ATTRIBUTE_SET, 0);
+            return ZEBRA_FAIL;
+        }
+        if (r == -1)
+        {
+            zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE,  use_value);
+            return ZEBRA_FAIL;
+        }
+    }
+    if (!use_string)
+    {
+        zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
+        return ZEBRA_FAIL;
+    }
+    *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 
+                                        index_type, use_string);
+    if (*ord == -1)
+    {
+        if (use_value < 0)
+            zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_string);
+        else
+            zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
+        return ZEBRA_FAIL;
+    }
+    return ZEBRA_OK;
+}
+
+ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
+                            Z_AttributesPlusTerm *zapt,
+                            int index_type,
+                            const char *xpath_use,
+                            oid_value curAttributeSet,
+                            int *ord)
+{
+    ZEBRA_RES res = ZEBRA_OK;
+    AttrType relation;
+    int relation_value;
+    zinfo_index_category_t cat = zinfo_index_category_index;
+
+    attr_init_APT(&relation, zapt, 2);
+    relation_value = attr_find(&relation, NULL);
+
+    if (relation_value == 103) /* always matches */
+        cat = zinfo_index_category_alwaysmatches;
+    
+    if (!xpath_use)
+    {
+        res = zebra_attr_list_get_ord(zh, zapt->attributes,
+                                      cat, index_type,
+                                      curAttributeSet, ord);
+        /* use attribute not found. But it the relation is
+           always matches and the regulare index attribute is found
+           return a different diagnostic */
+        if (res != ZEBRA_OK && 
+            relation_value == 103
+            &&  zebra_attr_list_get_ord(
+                zh, zapt->attributes, 
+                zinfo_index_category_index, index_type,
+                curAttributeSet, ord) == ZEBRA_OK)
+            zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 103);
+    }
+    else
+    {
+        *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, index_type,
+                                            xpath_use);
+        if (*ord == -1)
+        {
+            yaz_log(YLOG_LOG, "zebra_apt_get_ord FAILED xpath=%s index_type=%c",
+                    xpath_use, index_type);
+            zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
+            res = ZEBRA_FAIL;
+        }
+        else
+        {
+            yaz_log(YLOG_LOG, "zebra_apt_get_ord OK xpath=%s index_type=%c",
+                    xpath_use, index_type);
+            
+        }
+    }
+    return res;
+}
+
+ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
+                             Z_SortAttributes *sortAttributes,
+                             int *ord,
+                             int *numerical)
+{
+    AttrType structure;
+    int structure_value;
+    attr_init_AttrList(&structure, sortAttributes->list, 4);
+
+    *numerical = 0;
+    structure_value = attr_find(&structure, 0);
+    if (structure_value == 109)
+        *numerical = 1;
+    
+    if (zebra_attr_list_get_ord(zh, sortAttributes->list,
+                                zinfo_index_category_sort,
+                                's', VAL_BIB1, ord)== ZEBRA_OK)
+        return ZEBRA_OK;
+    if (zebra_attr_list_get_ord(zh, sortAttributes->list,
+                                zinfo_index_category_sort,
+                                'S', VAL_BIB1, ord)== ZEBRA_OK)
+        return ZEBRA_OK;
+    return ZEBRA_FAIL;
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+