postgresql/contrib/tsearch2/common.c

166 lines
2.9 KiB
C
Raw Normal View History

2003-07-21 18:27:44 +08:00
#include "postgres.h"
#include "postgres.h"
#include "fmgr.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_namespace.h"
#include "utils/syscache.h"
#include "ts_cfg.h"
#include "dict.h"
#include "wparser.h"
#include "snmap.h"
#include "common.h"
#include "tsvector.h"
2003-07-21 18:27:44 +08:00
#include "common.h"
#include "wparser.h"
#include "ts_cfg.h"
#include "dict.h"
2004-08-29 13:07:03 +08:00
Oid TSNSP_FunctionOid = InvalidOid;
2003-08-04 08:43:34 +08:00
text *
char2text(char *in)
{
2003-07-21 18:27:44 +08:00
return charl2text(in, strlen(in));
}
2003-08-04 08:43:34 +08:00
text *
charl2text(char *in, int len)
{
text *out = (text *) palloc(len + VARHDRSZ);
2003-07-21 18:27:44 +08:00
memcpy(VARDATA(out), in, len);
2003-08-04 08:43:34 +08:00
VARATT_SIZEP(out) = len + VARHDRSZ;
2003-07-21 18:27:44 +08:00
return out;
}
2003-08-04 08:43:34 +08:00
char
*
text2char(text *in)
{
char *out = palloc(VARSIZE(in));
memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ);
out[VARSIZE(in) - VARHDRSZ] = '\0';
2003-07-21 18:27:44 +08:00
return out;
}
2003-08-04 08:43:34 +08:00
char
*
pnstrdup(char *in, int len)
{
char *out = palloc(len + 1);
2003-07-21 18:27:44 +08:00
memcpy(out, in, len);
2003-08-04 08:43:34 +08:00
out[len] = '\0';
2003-07-21 18:27:44 +08:00
return out;
}
2003-08-04 08:43:34 +08:00
text
*
ptextdup(text *in)
{
text *out = (text *) palloc(VARSIZE(in));
memcpy(out, in, VARSIZE(in));
2003-07-21 18:27:44 +08:00
return out;
}
2003-08-04 08:43:34 +08:00
text
*
mtextdup(text *in)
{
text *out = (text *) malloc(VARSIZE(in));
if (!out)
2003-07-21 18:27:44 +08:00
ts_error(ERROR, "No memory");
2003-08-04 08:43:34 +08:00
memcpy(out, in, VARSIZE(in));
2003-07-21 18:27:44 +08:00
return out;
}
2003-08-04 08:43:34 +08:00
void
ts_error(int state, const char *format,...)
{
va_list args;
int tlen = 128,
len = 0;
char *buf;
2003-07-21 18:27:44 +08:00
reset_cfg();
reset_dict();
reset_prs();
va_start(args, format);
buf = palloc(tlen);
2003-08-04 08:43:34 +08:00
len = vsnprintf(buf, tlen - 1, format, args);
if (len >= tlen)
{
tlen = len + 1;
buf = repalloc(buf, tlen);
vsnprintf(buf, tlen - 1, format, args);
2003-07-21 18:27:44 +08:00
}
va_end(args);
2003-08-04 08:43:34 +08:00
/* ?? internal error ?? */
elog(state, "%s", buf);
2003-07-21 18:27:44 +08:00
pfree(buf);
}
2003-08-04 08:43:34 +08:00
int
text_cmp(text *a, text *b)
{
if (VARSIZE(a) == VARSIZE(b))
return strncmp(VARDATA(a), VARDATA(b), VARSIZE(a) - VARHDRSZ);
return (int) VARSIZE(a) - (int) VARSIZE(b);
2003-07-21 18:27:44 +08:00
}
2004-08-29 13:07:03 +08:00
char *
get_namespace(Oid funcoid)
{
HeapTuple tuple;
Form_pg_proc proc;
Form_pg_namespace nsp;
Oid nspoid;
char *txt;
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
proc = (Form_pg_proc) GETSTRUCT(tuple);
nspoid = proc->pronamespace;
ReleaseSysCache(tuple);
tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for namespace oid %u", nspoid);
nsp = (Form_pg_namespace) GETSTRUCT(tuple);
txt = pstrdup(NameStr((nsp->nspname)));
ReleaseSysCache(tuple);
return txt;
}
Oid
2004-08-29 13:07:03 +08:00
get_oidnamespace(Oid funcoid)
{
HeapTuple tuple;
Form_pg_proc proc;
Oid nspoid;
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
proc = (Form_pg_proc) GETSTRUCT(tuple);
nspoid = proc->pronamespace;
ReleaseSysCache(tuple);
return nspoid;
}