not_in.c
上传用户:blenddy
上传日期:2007-01-07
资源大小:6495k
文件大小:3k
源码类别:

数据库系统

开发平台:

Unix_Linux

  1. /*-------------------------------------------------------------------------
  2.  *
  3.  * not_in.c
  4.  *   Executes the "not_in" operator for any data type
  5.  *
  6.  * Copyright (c) 1994, Regents of the University of California
  7.  *
  8.  *
  9.  * IDENTIFICATION
  10.  *   $Header: /usr/local/cvsroot/pgsql/src/backend/utils/adt/not_in.c,v 1.16.2.1 1999/08/02 05:24:55 scrappy Exp $
  11.  *
  12.  *-------------------------------------------------------------------------
  13.  */
  14. /*
  15.  *
  16.  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  17.  * X HACK WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! X
  18.  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  19.  *
  20.  * This code is the OLD not-in code that is HACKED
  21.  * into place until operators that can have arguments as
  22.  * columns are ******REALLY****** implemented!!!!!!!!!!!
  23.  *
  24.  */
  25. #include "postgres.h"
  26. #include "access/heapam.h"
  27. #include "utils/builtins.h"
  28. static int my_varattno(Relation rd, char *a);
  29. /* ----------------------------------------------------------------
  30.  *
  31.  * ----------------------------------------------------------------
  32.  */
  33. bool
  34. int4notin(int32 not_in_arg, char *relation_and_attr)
  35. {
  36. Relation relation_to_scan;
  37. int32 integer_value;
  38. HeapTuple current_tuple;
  39. HeapScanDesc scan_descriptor;
  40. bool dummy,
  41. retval;
  42. int attrid;
  43. char    *relation,
  44.    *attribute;
  45. char my_copy[NAMEDATALEN * 2 + 2];
  46. Datum value;
  47. strncpy(my_copy, relation_and_attr, sizeof(my_copy));
  48. my_copy[sizeof(my_copy) - 1] = '';
  49. relation = (char *) strtok(my_copy, ".");
  50. attribute = (char *) strtok(NULL, ".");
  51. if (attribute == NULL)
  52. elog(ERROR, "int4notin: must provide relationname.attributename");
  53. /* Open the relation and get a relation descriptor */
  54. relation_to_scan = heap_openr(relation);
  55. if (!RelationIsValid(relation_to_scan))
  56. {
  57. elog(ERROR, "int4notin: unknown relation %s",
  58.  relation);
  59. }
  60. /* Find the column to search */
  61. attrid = my_varattno(relation_to_scan, attribute);
  62. if (attrid < 0)
  63. {
  64. elog(ERROR, "int4notin: unknown attribute %s for relation %s",
  65.  attribute, relation);
  66. }
  67. scan_descriptor = heap_beginscan(relation_to_scan, false, SnapshotNow,
  68.  0, (ScanKey) NULL);
  69. retval = true;
  70. /* do a scan of the relation, and do the check */
  71. while (HeapTupleIsValid(current_tuple = heap_getnext(scan_descriptor, 0)))
  72. {
  73. value = heap_getattr(current_tuple,
  74.  (AttrNumber) attrid,
  75.  RelationGetDescr(relation_to_scan),
  76.  &dummy);
  77. integer_value = DatumGetInt32(value);
  78. if (not_in_arg == integer_value)
  79. {
  80. retval = false;
  81. break; /* can stop scanning now */
  82. }
  83. }
  84. /* close the relation */
  85. heap_close(relation_to_scan);
  86. return retval;
  87. }
  88. bool
  89. oidnotin(Oid the_oid, char *compare)
  90. {
  91. if (the_oid == InvalidOid)
  92. return false;
  93. return int4notin(the_oid, compare);
  94. }
  95. /*
  96.  * XXX
  97.  * If varattno (in parser/catalog_utils.h) ever is added to
  98.  * cinterface.a, this routine should go away
  99.  */
  100. static int
  101. my_varattno(Relation rd, char *a)
  102. {
  103. int i;
  104. for (i = 0; i < rd->rd_rel->relnatts; i++)
  105. {
  106. if (!namestrcmp(&rd->rd_att->attrs[i]->attname, a))
  107. return i + 1;
  108. }
  109. return -1;
  110. }