jbe@0: /* jbe@0: * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany jbe@0: * Author: Jan Behrens jbe@0: * All rights reserved. jbe@0: * jbe@0: * Redistribution and use in source and binary forms, with or without jbe@0: * modification, are permitted provided that the following conditions are jbe@0: * met: jbe@0: * jbe@0: * 1. Redistributions of source code must retain the above copyright jbe@0: * notice, this list of conditions and the following disclaimer. jbe@0: * 2. Redistributions in binary form must reproduce the above copyright jbe@0: * notice, this list of conditions and the following disclaimer in the jbe@0: * documentation and/or other materials provided with the distribution. jbe@0: * 3. Neither the name of the FlexiGuided GmbH nor the names of its jbe@0: * contributors may be used to endorse or promote products derived from jbe@0: * this software without specific prior written permission. jbe@0: * jbe@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS jbe@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT jbe@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A jbe@0: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER jbe@0: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, jbe@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, jbe@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jbe@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF jbe@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING jbe@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS jbe@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jbe@0: * jbe@0: */ jbe@0: jbe@0: jbe@0: /* jbe@0: * File name: pgsql/utf8proc_pgsql.c jbe@0: * Version: 0.1 jbe@0: * Last changed: 2006-05-31 jbe@0: * jbe@0: * Description: jbe@0: * PostgreSQL extension to provide a function 'unifold', which can be used jbe@0: * to case-fold and normalize index fields. jbe@0: */ jbe@0: jbe@0: jbe@0: #include "../utf8proc.c" jbe@0: jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: jbe@0: PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold); jbe@0: jbe@0: Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) { jbe@0: char *input_string; jbe@0: uint8_t *output_string; jbe@0: ssize_t result; jbe@0: input_string = DatumGetCString( jbe@0: DirectFunctionCall1(textout, PG_GETARG_DATUM(0)) jbe@0: ); jbe@0: result = utf8proc_map(input_string, 0, &output_string, UTF8PROC_NULLTERM | jbe@0: UTF8PROC_COMPOSE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_CASEFOLD); jbe@0: pfree(input_string); jbe@0: if (result < 0) { jbe@0: int sqlerrcode; jbe@0: switch(result) { jbe@0: case UTF8PROC_ERROR_NOMEM: jbe@0: sqlerrcode = ERRCODE_OUT_OF_MEMORY; break; jbe@0: case UTF8PROC_ERROR_OVERFLOW: jbe@0: sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break; jbe@0: case UTF8PROC_ERROR_INVALIDUTF8: jbe@0: case UTF8PROC_ERROR_NOTASSIGNED: jbe@0: sqlerrcode = ERRCODE_DATA_EXCEPTION; break; jbe@0: default: jbe@0: sqlerrcode = ERRCODE_INTERNAL_ERROR; jbe@0: } jbe@0: ereport(ERROR, ( jbe@0: errcode(sqlerrcode), jbe@0: errmsg("%s", utf8proc_errmsg(result)) jbe@0: )); jbe@0: } jbe@0: { jbe@0: Datum retval; jbe@0: retval = DirectFunctionCall1(textin, CStringGetDatum(output_string)); jbe@0: free(output_string); jbe@0: PG_RETURN_TEXT_P(DatumGetTextP(retval)); jbe@0: } jbe@0: } jbe@0: jbe@0: