mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Support Docs & Contrib
This commit is contained in:
83
contrib/soundex/soundex.c
Normal file
83
contrib/soundex/soundex.c
Normal file
@ -0,0 +1,83 @@
|
||||
/*****************************************************************************/
|
||||
/* soundex.c */
|
||||
/*****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "postgres.h" /* for char16, etc. */
|
||||
#include "utils/palloc.h" /* for palloc */
|
||||
#include "libpq-fe.h" /* for TUPLE */
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* prototype for soundex function */
|
||||
char *soundex(char *instr, char *outstr);
|
||||
|
||||
text *text_soundex(text *t)
|
||||
{
|
||||
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
||||
char *table = "01230120022455012623010202";
|
||||
int count = 0;
|
||||
text *new_t;
|
||||
|
||||
char outstr[6+1]; /* max length of soundex is 6 */
|
||||
char *instr;
|
||||
|
||||
/* make a null-terminated string */
|
||||
instr=palloc(VARSIZE(t)+1);
|
||||
memcpy(instr,VARDATA(t),VARSIZE(t)-VARHDRSZ);
|
||||
instr[VARSIZE(t)-VARHDRSZ] = (char)0;
|
||||
|
||||
/* load soundex into outstr */
|
||||
soundex(instr, outstr);
|
||||
|
||||
/* Now the outstr contains the soundex of instr */
|
||||
/* copy outstr to new_t */
|
||||
new_t = (text *) palloc(strlen(outstr)+VARHDRSZ);
|
||||
memset(new_t, 0, strlen(outstr)+1);
|
||||
VARSIZE(new_t) = strlen(outstr)+VARHDRSZ;
|
||||
memcpy((void *) VARDATA(new_t),
|
||||
(void *) outstr,
|
||||
strlen(outstr));
|
||||
|
||||
/* free instr */
|
||||
pfree(instr);
|
||||
|
||||
return(new_t);
|
||||
}
|
||||
|
||||
char *soundex(char *instr, char *outstr)
|
||||
{ /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
||||
char *table = "01230120022455012623010202";
|
||||
int count = 0;
|
||||
|
||||
while(!isalpha(instr[0]) && instr[0])
|
||||
++instr;
|
||||
|
||||
if(!instr[0]) { /* Hey! Where'd the string go? */
|
||||
outstr[0]=(char)0;
|
||||
return outstr;
|
||||
}
|
||||
|
||||
if(toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H') {
|
||||
instr[0] = 'F';
|
||||
instr[1] = 'A';
|
||||
}
|
||||
|
||||
*outstr++ = (char)toupper(*instr++);
|
||||
|
||||
while(*instr && count < 5) {
|
||||
if(isalpha(*instr) && *instr != *(instr-1)) {
|
||||
*outstr = table[toupper(instr[0]) - 'A'];
|
||||
if(*outstr != '0') {
|
||||
++outstr;
|
||||
++count;
|
||||
}
|
||||
}
|
||||
++instr;
|
||||
}
|
||||
|
||||
*outstr = '\0';
|
||||
return(outstr);
|
||||
}
|
||||
|
57
contrib/soundex/soundex.sql
Normal file
57
contrib/soundex/soundex.sql
Normal file
@ -0,0 +1,57 @@
|
||||
--------------- soundex.sql:
|
||||
|
||||
CREATE FUNCTION text_soundex(text) RETURNS text
|
||||
AS '/usr/local/postgres/postgres95/src/funcs/soundex.so' LANGUAGE 'c';
|
||||
|
||||
SELECT text_soundex('hello world!');
|
||||
|
||||
CREATE TABLE s (nm text)\g
|
||||
|
||||
insert into s values ('john')\g
|
||||
insert into s values ('joan')\g
|
||||
insert into s values ('wobbly')\g
|
||||
|
||||
select * from s
|
||||
where text_soundex(nm) = text_soundex('john')\g
|
||||
|
||||
select nm from s a, s b
|
||||
where text_soundex(a.nm) = text_soundex(b.nm)
|
||||
and a.oid <> b.oid\g
|
||||
|
||||
CREATE FUNCTION text_sx_eq(text, text) RETURNS bool AS
|
||||
'select text_soundex($1) = text_soundex($2)'
|
||||
LANGUAGE 'sql'\g
|
||||
|
||||
CREATE FUNCTION text_sx_lt(text,text) RETURNS bool AS
|
||||
'select text_soundex($1) < text_soundex($2)'
|
||||
LANGUAGE 'sql'\g
|
||||
|
||||
CREATE FUNCTION text_sx_gt(text,text) RETURNS bool AS
|
||||
'select text_soundex($1) > text_soundex($2)'
|
||||
LANGUAGE 'sql';
|
||||
|
||||
CREATE FUNCTION text_sx_le(text,text) RETURNS bool AS
|
||||
'select text_soundex($1) <= text_soundex($2)'
|
||||
LANGUAGE 'sql';
|
||||
|
||||
CREATE FUNCTION text_sx_ge(text,text) RETURNS bool AS
|
||||
'select text_soundex($1) >= text_soundex($2)'
|
||||
LANGUAGE 'sql';
|
||||
|
||||
CREATE FUNCTION text_sx_ne(text,text) RETURNS bool AS
|
||||
'select text_soundex($1) <> text_soundex($2)'
|
||||
LANGUAGE 'sql';
|
||||
|
||||
DROP OPERATOR #= (text,text)\g
|
||||
|
||||
CREATE OPERATOR #= (leftarg=text, rightarg=text, procedure=text_sx_eq,
|
||||
commutator=text_sx_eq)\g
|
||||
|
||||
SELECT *
|
||||
FROM s
|
||||
WHERE text_sx_eq(nm,'john')\g
|
||||
|
||||
SELECT *
|
||||
from s
|
||||
where s.nm #= 'john';
|
||||
|
Reference in New Issue
Block a user