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