| rev | line source | 
| jbe@0 | 1 /* | 
| jbe@0 | 2  *  Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany | 
| jbe@0 | 3  *  Author: Jan Behrens <jan.behrens@flexiguided.de> | 
| jbe@0 | 4  *  All rights reserved. | 
| jbe@0 | 5  * | 
| jbe@0 | 6  *  Redistribution and use in source and binary forms, with or without | 
| jbe@0 | 7  *  modification, are permitted provided that the following conditions are | 
| jbe@0 | 8  *  met: | 
| jbe@0 | 9  * | 
| jbe@0 | 10  *  1. Redistributions of source code must retain the above copyright | 
| jbe@0 | 11  *     notice, this list of conditions and the following disclaimer. | 
| jbe@0 | 12  *  2. Redistributions in binary form must reproduce the above copyright | 
| jbe@0 | 13  *     notice, this list of conditions and the following disclaimer in the | 
| jbe@0 | 14  *     documentation and/or other materials provided with the distribution. | 
| jbe@0 | 15  *  3. Neither the name of the FlexiGuided GmbH nor the names of its | 
| jbe@0 | 16  *     contributors may be used to endorse or promote products derived from | 
| jbe@0 | 17  *     this software without specific prior written permission. | 
| jbe@0 | 18  * | 
| jbe@0 | 19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| jbe@0 | 20  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| jbe@0 | 21  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | 
| jbe@0 | 22  *  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | 
| jbe@0 | 23  *  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 
| jbe@0 | 24  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 
| jbe@0 | 25  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 
| jbe@0 | 26  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 
| jbe@0 | 27  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 
| jbe@0 | 28  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
| jbe@0 | 29  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| jbe@0 | 30  * | 
| jbe@0 | 31  */ | 
| jbe@0 | 32 | 
| jbe@0 | 33 | 
| jbe@0 | 34 /* | 
| jbe@0 | 35  *  File name:    pgsql/utf8proc_pgsql.c | 
| jbe@0 | 36  *  Version:      0.1 | 
| jbe@0 | 37  *  Last changed: 2006-05-31 | 
| jbe@0 | 38  * | 
| jbe@0 | 39  *  Description: | 
| jbe@0 | 40  *  PostgreSQL extension to provide a function 'unifold', which can be used | 
| jbe@0 | 41  *  to case-fold and normalize index fields. | 
| jbe@0 | 42  */ | 
| jbe@0 | 43 | 
| jbe@0 | 44 | 
| jbe@0 | 45 #include "../utf8proc.c" | 
| jbe@0 | 46 | 
| jbe@0 | 47 #include <postgres.h> | 
| jbe@0 | 48 #include <utils/elog.h> | 
| jbe@0 | 49 #include <fmgr.h> | 
| jbe@0 | 50 #include <unistd.h> | 
| jbe@0 | 51 #include <string.h> | 
| jbe@0 | 52 #include <utils/builtins.h> | 
| jbe@0 | 53 | 
| jbe@0 | 54 PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold); | 
| jbe@0 | 55 | 
| jbe@0 | 56 Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) { | 
| jbe@0 | 57   char *input_string; | 
| jbe@0 | 58   uint8_t *output_string; | 
| jbe@0 | 59   ssize_t result; | 
| jbe@0 | 60   input_string = DatumGetCString( | 
| jbe@0 | 61     DirectFunctionCall1(textout, PG_GETARG_DATUM(0)) | 
| jbe@0 | 62   ); | 
| jbe@0 | 63   result = utf8proc_map(input_string, 0, &output_string, UTF8PROC_NULLTERM | | 
| jbe@0 | 64     UTF8PROC_COMPOSE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_CASEFOLD); | 
| jbe@0 | 65   pfree(input_string); | 
| jbe@0 | 66   if (result < 0) { | 
| jbe@0 | 67     int sqlerrcode; | 
| jbe@0 | 68     switch(result) { | 
| jbe@0 | 69       case UTF8PROC_ERROR_NOMEM: | 
| jbe@0 | 70       sqlerrcode = ERRCODE_OUT_OF_MEMORY; break; | 
| jbe@0 | 71       case UTF8PROC_ERROR_OVERFLOW: | 
| jbe@0 | 72       sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break; | 
| jbe@0 | 73       case UTF8PROC_ERROR_INVALIDUTF8: | 
| jbe@0 | 74       case UTF8PROC_ERROR_NOTASSIGNED: | 
| jbe@0 | 75       sqlerrcode = ERRCODE_DATA_EXCEPTION; break; | 
| jbe@0 | 76       default: | 
| jbe@0 | 77       sqlerrcode = ERRCODE_INTERNAL_ERROR; | 
| jbe@0 | 78     } | 
| jbe@0 | 79     ereport(ERROR, ( | 
| jbe@0 | 80       errcode(sqlerrcode), | 
| jbe@0 | 81       errmsg("%s", utf8proc_errmsg(result)) | 
| jbe@0 | 82     )); | 
| jbe@0 | 83   } | 
| jbe@0 | 84   { | 
| jbe@0 | 85     Datum retval; | 
| jbe@0 | 86     retval = DirectFunctionCall1(textin, CStringGetDatum(output_string)); | 
| jbe@0 | 87     free(output_string); | 
| jbe@0 | 88     PG_RETURN_TEXT_P(DatumGetTextP(retval)); | 
| jbe@0 | 89   } | 
| jbe@0 | 90 } | 
| jbe@0 | 91 | 
| jbe@0 | 92 |