1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "parser/parse_coerce.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "orafce.h"
#include "builtins.h"
PG_FUNCTION_INFO_V1(orafce_replace_empty_strings);
PG_FUNCTION_INFO_V1(orafce_replace_null_strings);
static void
trigger_sanity_check(FunctionCallInfo fcinfo, const char *fname)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
/* sanity checks from autoinc.c */
if (!CALLED_AS_TRIGGER(fcinfo))
elog(ERROR, "%s: not fired by trigger manager", fname);
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
elog(ERROR, "%s: must be fired for row", fname);
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
elog(ERROR, "%s: must be fired before event", fname);
if (trigdata->tg_trigger->tgnargs > 1)
elog(ERROR, "%s: only one trigger parameter is allowed", fname);
}
static HeapTuple
get_rettuple(FunctionCallInfo fcinfo)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
HeapTuple rettuple = NULL;
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
rettuple = trigdata->tg_trigtuple;
else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
rettuple = trigdata->tg_newtuple;
else
/* internal error */
elog(ERROR, "remove_empty_string: cannot process DELETE events");
return rettuple;
}
/*
* Trigger argument is used as parameter that can enforce warning about modified
* columns. When first argument is "on" or "true", then warnings will be raised.
*/
static bool
should_raise_warnings(FunctionCallInfo fcinfo, bool *raise_error)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
Trigger *trigger = trigdata->tg_trigger;
*raise_error = false;
if (trigger->tgnargs > 0)
{
char **args = trigger->tgargs;
if (strcmp(args[0], "on") == 0 ||
strcmp(args[0], "true") == 0 ||
strcmp(args[0], "warning") == 0)
return true;
if (strcmp(args[0], "error") == 0)
{
*raise_error = true;
return true;
}
}
return false;
}
/*
* Detects emty strings in type text based fields and replaces them by NULL.
*/
Datum
orafce_replace_empty_strings(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
HeapTuple rettuple = NULL;
TupleDesc tupdesc;
int *resetcols = NULL;
Datum *values = NULL;
bool *nulls = NULL;
Oid prev_typid = InvalidOid;
bool is_string = false;
bool is_bpchar = false;
int nresetcols = 0;
int attnum;
bool raise_warning = false;
bool raise_error;
char *relname = NULL;
trigger_sanity_check(fcinfo, "replace_empty_strings");
raise_warning = should_raise_warnings(fcinfo, &raise_error);
rettuple = get_rettuple(fcinfo);
tupdesc = trigdata->tg_relation->rd_att;
/* iterate over record's fields */
for (attnum = 1; attnum <= tupdesc->natts; attnum++)
{
Oid typid;
if (TupleDescAttr(tupdesc, attnum - 1)->attisdropped)
continue;
/* simple cache - lot of time columns with same type is side by side */
typid = TupleDescAttr(tupdesc, attnum - 1)->atttypid;
if (typid != prev_typid)
{
TYPCATEGORY category;
bool ispreferred;
Oid base_typid;
base_typid = getBaseType(typid);
get_type_category_preferred(base_typid, &category, &ispreferred);
is_string = (category == TYPCATEGORY_STRING);
is_bpchar = (base_typid == BPCHAROID);
prev_typid = typid;
}
if (is_string)
{
Datum value;
bool isnull;
value = SPI_getbinval(rettuple, tupdesc, attnum, &isnull);
if (!isnull)
{
text *txt = DatumGetTextP(value);
int32 len;
bool is_emptystr;
len = VARSIZE_ANY_EXHDR(txt);
is_emptystr = (len == 0);
if (is_bpchar && !is_emptystr)
{
char *s = VARDATA_ANY(txt);
int i;
is_emptystr = true;
for (i = 0; i < len; i++)
if (s[i] != ' ')
{
is_emptystr = false;
break;
}
}
/* is it empty string (has zero length */
if (is_emptystr)
{
if (!resetcols)
{
/* lazy allocation of dynamic memory */
resetcols = palloc0(tupdesc->natts * sizeof(int));
nulls = palloc0(tupdesc->natts * sizeof(bool));
values = palloc0(tupdesc->natts * sizeof(Datum));
}
resetcols[nresetcols] = attnum;
values[nresetcols] = (Datum) 0;
nulls[nresetcols++] = true;
if (raise_warning)
{
if (!relname)
relname = SPI_getrelname(trigdata->tg_relation);
elog(raise_error ? ERROR : WARNING,
"Field \"%s\" of table \"%s\" is empty string (replaced by NULL).",
SPI_fname(tupdesc, attnum), relname);
}
}
}
}
}
if (nresetcols > 0)
{
/* construct new tuple */
rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
nresetcols, resetcols,
values, nulls);
}
if (relname)
pfree(relname);
if (resetcols)
pfree(resetcols);
if (values)
pfree(values);
if (nulls)
pfree(nulls);
return PointerGetDatum(rettuple);
}
/*
* Detects NULL in type text based fields and replaces them by empty string
*/
Datum
orafce_replace_null_strings(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
HeapTuple rettuple = NULL;
TupleDesc tupdesc;
int *resetcols = NULL;
Datum *values = NULL;
bool *nulls = NULL;
Oid prev_typid = InvalidOid;
bool is_string = false;
bool is_bpchar = false;
int nresetcols = 0;
int attnum;
bool raise_warning = false;
bool raise_error;
char *relname = NULL;
trigger_sanity_check(fcinfo, "replace_null_strings");
raise_warning = should_raise_warnings(fcinfo, &raise_error);
rettuple = get_rettuple(fcinfo);
/* return fast when there are not any NULL */
if (!HeapTupleHasNulls(rettuple))
return PointerGetDatum(rettuple);
tupdesc = trigdata->tg_relation->rd_att;
/* iterate over record's fields */
for (attnum = 1; attnum <= tupdesc->natts; attnum++)
{
Oid typid;
int32 typmod;
if (TupleDescAttr(tupdesc, attnum - 1)->attisdropped)
continue;
/* simple cache - lot of time columns with same type is side by side */
typid = TupleDescAttr(tupdesc, attnum - 1)->atttypid;
typmod = TupleDescAttr(tupdesc, attnum - 1)->atttypmod;
if (typid != prev_typid)
{
TYPCATEGORY category;
bool ispreferred;
Oid base_typid;
base_typid = getBaseType(typid);
get_type_category_preferred(base_typid, &category, &ispreferred);
is_string = (category == TYPCATEGORY_STRING);
is_bpchar = base_typid == BPCHAROID;
prev_typid = typid;
}
if (is_string)
{
bool isnull;
(void) SPI_getbinval(rettuple, tupdesc, attnum, &isnull);
if (isnull)
{
if (!resetcols)
{
/* lazy allocation of dynamic memory */
resetcols = palloc0(tupdesc->natts * sizeof(int));
nulls = palloc0(tupdesc->natts * sizeof(bool));
values = palloc0(tupdesc->natts * sizeof(Datum));
}
resetcols[nresetcols] = attnum;
if (is_bpchar && typmod != -1)
{
BpChar *result;
/* code from bpcharin */
result = (BpChar *) palloc(typmod);
SET_VARSIZE(result, typmod);
memset(VARDATA(result), ' ', typmod - VARHDRSZ);
values[nresetcols] = PointerGetDatum(result);
}
else
values[nresetcols] = PointerGetDatum(cstring_to_text_with_len("", 0));
nulls[nresetcols++] = false;
if (raise_warning)
{
if (!relname)
relname = SPI_getrelname(trigdata->tg_relation);
elog(raise_error ? ERROR : WARNING,
"Field \"%s\" of table \"%s\" is NULL (replaced by '').",
SPI_fname(tupdesc, attnum), relname);
}
}
}
}
if (nresetcols > 0)
{
/* construct new tuple */
rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
nresetcols, resetcols,
values, nulls);
}
if (relname)
pfree(relname);
if (resetcols)
pfree(resetcols);
if (values)
pfree(values);
if (nulls)
pfree(nulls);
return PointerGetDatum(rettuple);
}
|