* Detects emty strings in type text based fields and replaces them by NULL. */
| 78 | * Detects emty strings in type text based fields and replaces them by NULL. |
| 79 | */ |
| 80 | Datum |
| 81 | orafce_replace_empty_strings(PG_FUNCTION_ARGS) |
| 82 | { |
| 83 | TriggerData *trigdata = (TriggerData *) fcinfo->context; |
| 84 | HeapTuple rettuple = NULL; |
| 85 | TupleDesc tupdesc; |
| 86 | int *resetcols = NULL; |
| 87 | Datum *values = NULL; |
| 88 | bool *nulls = NULL; |
| 89 | Oid prev_typid = InvalidOid; |
| 90 | bool is_string = false; |
| 91 | int nresetcols = 0; |
| 92 | int attnum; |
| 93 | bool raise_warning = false; |
| 94 | char *relname = NULL; |
| 95 | |
| 96 | trigger_sanity_check(fcinfo, "replace_empty_strings"); |
| 97 | raise_warning = should_raise_warnings(fcinfo); |
| 98 | |
| 99 | rettuple = get_rettuple(fcinfo); |
| 100 | tupdesc = trigdata->tg_relation->rd_att; |
| 101 | |
| 102 | /* iterate over record's fields */ |
| 103 | for (attnum = 1; attnum <= tupdesc->natts; attnum++) |
| 104 | { |
| 105 | Oid typid; |
| 106 | |
| 107 | /* simple cache - lot of time columns with same type is side by side */ |
| 108 | typid = SPI_gettypeid(tupdesc, attnum); |
| 109 | if (typid != prev_typid) |
| 110 | { |
| 111 | TYPCATEGORY category; |
| 112 | bool ispreferred; |
| 113 | Oid base_typid; |
| 114 | |
| 115 | base_typid = getBaseType(typid); |
| 116 | get_type_category_preferred(base_typid, &category, &ispreferred); |
| 117 | |
| 118 | is_string = (category == TYPCATEGORY_STRING); |
| 119 | prev_typid = typid; |
| 120 | } |
| 121 | |
| 122 | if (is_string) |
| 123 | { |
| 124 | Datum value; |
| 125 | bool isnull; |
| 126 | |
| 127 | value = SPI_getbinval(rettuple, tupdesc, attnum, &isnull); |
| 128 | if (!isnull) |
| 129 | { |
| 130 | text *txt = DatumGetTextP(value); |
| 131 | |
| 132 | /* is it empty string (has zero length */ |
| 133 | if (VARSIZE_ANY_EXHDR(txt) == 0) |
| 134 | { |
| 135 | if (!resetcols) |
| 136 | { |
| 137 | /* lazy allocation of dynamic memory */ |
nothing calls this directly
no test coverage detected