00001 /*------------------------------------------------------------------------- 00002 * 00003 * dict_int.c 00004 * Text search dictionary for integers 00005 * 00006 * Copyright (c) 2007-2013, PostgreSQL Global Development Group 00007 * 00008 * IDENTIFICATION 00009 * contrib/dict_int/dict_int.c 00010 * 00011 *------------------------------------------------------------------------- 00012 */ 00013 #include "postgres.h" 00014 00015 #include "commands/defrem.h" 00016 #include "tsearch/ts_public.h" 00017 00018 PG_MODULE_MAGIC; 00019 00020 00021 typedef struct 00022 { 00023 int maxlen; 00024 bool rejectlong; 00025 } DictInt; 00026 00027 00028 PG_FUNCTION_INFO_V1(dintdict_init); 00029 Datum dintdict_init(PG_FUNCTION_ARGS); 00030 00031 PG_FUNCTION_INFO_V1(dintdict_lexize); 00032 Datum dintdict_lexize(PG_FUNCTION_ARGS); 00033 00034 Datum 00035 dintdict_init(PG_FUNCTION_ARGS) 00036 { 00037 List *dictoptions = (List *) PG_GETARG_POINTER(0); 00038 DictInt *d; 00039 ListCell *l; 00040 00041 d = (DictInt *) palloc0(sizeof(DictInt)); 00042 d->maxlen = 6; 00043 d->rejectlong = false; 00044 00045 foreach(l, dictoptions) 00046 { 00047 DefElem *defel = (DefElem *) lfirst(l); 00048 00049 if (pg_strcasecmp(defel->defname, "MAXLEN") == 0) 00050 { 00051 d->maxlen = atoi(defGetString(defel)); 00052 } 00053 else if (pg_strcasecmp(defel->defname, "REJECTLONG") == 0) 00054 { 00055 d->rejectlong = defGetBoolean(defel); 00056 } 00057 else 00058 { 00059 ereport(ERROR, 00060 (errcode(ERRCODE_INVALID_PARAMETER_VALUE), 00061 errmsg("unrecognized intdict parameter: \"%s\"", 00062 defel->defname))); 00063 } 00064 } 00065 00066 PG_RETURN_POINTER(d); 00067 } 00068 00069 Datum 00070 dintdict_lexize(PG_FUNCTION_ARGS) 00071 { 00072 DictInt *d = (DictInt *) PG_GETARG_POINTER(0); 00073 char *in = (char *) PG_GETARG_POINTER(1); 00074 char *txt = pnstrdup(in, PG_GETARG_INT32(2)); 00075 TSLexeme *res = palloc0(sizeof(TSLexeme) * 2); 00076 00077 res[1].lexeme = NULL; 00078 if (PG_GETARG_INT32(2) > d->maxlen) 00079 { 00080 if (d->rejectlong) 00081 { 00082 /* reject by returning void array */ 00083 pfree(txt); 00084 res[0].lexeme = NULL; 00085 } 00086 else 00087 { 00088 /* trim integer */ 00089 txt[d->maxlen] = '\0'; 00090 res[0].lexeme = txt; 00091 } 00092 } 00093 else 00094 { 00095 res[0].lexeme = txt; 00096 } 00097 00098 PG_RETURN_POINTER(res); 00099 }