/* bpchar_name() * Converts a bpchar() type to a NameData type. */ Datum bpchar_name(PG_FUNCTION_ARGS) { BpChar *s = PG_GETARG_BPCHAR_P(0); Name result; int len; len = VARSIZE(s) - VARHDRSZ; /* Truncate to max length for a Name */ if (len >= NAMEDATALEN) len = NAMEDATALEN - 1; /* Remove trailing blanks */ while (len > 0) { if (*(VARDATA(s) + len - 1) != ' ') break; len--; } result = (NameData *) palloc(NAMEDATALEN); memcpy(NameStr(*result), VARDATA(s), len); /* Now null pad to full length... */ while (len < NAMEDATALEN) { *(NameStr(*result) + len) = '\0'; len++; } PG_RETURN_NAME(result); }
/* * current_database() * Expose the current database to the user */ Datum current_database(PG_FUNCTION_ARGS) { Name db; db = (Name) palloc(NAMEDATALEN); namestrcpy(db, get_database_name(MyDatabaseId)); PG_RETURN_NAME(db); }
/* * namein - converts "..." to internal representation * * Note: * [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls * Now, always NULL terminated */ Datum namein(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); NameData *result; int len; len = strlen(s); len = pg_mbcliplen(s, len, NAMEDATALEN - 1); result = (NameData *) palloc0(NAMEDATALEN); memcpy(NameStr(*result), s, len); PG_RETURN_NAME(result); }
/* * namein - converts "..." to internal representation * * Note: * [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls * Now, always NULL terminated */ Datum namein(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); Name result; int len; len = strlen(s); /* Truncate oversize input */ if (len >= NAMEDATALEN) len = pg_mbcliplen(s, len, NAMEDATALEN - 1); /* We use palloc0 here to ensure result is zero-padded */ result = (Name) palloc0(NAMEDATALEN); memcpy(NameStr(*result), s, len); PG_RETURN_NAME(result); }
/* * namerecv - converts external binary format to name */ Datum namerecv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); Name result; char *str; int nbytes; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); if (nbytes >= NAMEDATALEN) ereport(ERROR, (errcode(ERRCODE_NAME_TOO_LONG), errmsg("identifier too long"), errdetail("Identifier must be less than %d characters.", NAMEDATALEN))); result = (NameData *) palloc0(NAMEDATALEN); memcpy(result, str, nbytes); pfree(str); PG_RETURN_NAME(result); }