Redefined parse_phone_number to take text, not cstring
This commit is contained in:
parent
7343039678
commit
89d2c9a241
|
@ -64,7 +64,16 @@ static void logInfo(const char* msg) {
|
||||||
* Utility functions
|
* Utility functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static char* textToCString(const text* text) {
|
||||||
|
size_t len = VARSIZE(text) - VARHDRSZ;
|
||||||
|
char* str = (char*)palloc(len + 1);
|
||||||
|
memcpy(str, VARDATA(text), len);
|
||||||
|
str[len] = '\0';
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
//Internal function used by phone_number_in and parse_phone_number
|
//Internal function used by phone_number_in and parse_phone_number
|
||||||
|
//TODO: take a std::string to minimize copying?
|
||||||
ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() {
|
ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() {
|
||||||
try {
|
try {
|
||||||
PhoneNumber number;
|
PhoneNumber number;
|
||||||
|
@ -132,15 +141,27 @@ extern "C" {
|
||||||
Datum
|
Datum
|
||||||
parse_phone_number(PG_FUNCTION_ARGS)
|
parse_phone_number(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
const char *number_str = PG_GETARG_CSTRING(0);
|
try {
|
||||||
const char *country = PG_GETARG_CSTRING(1);
|
const text* number_text = PG_GETARG_TEXT_P(0);
|
||||||
|
const text* country_text = PG_GETARG_TEXT_P(1);
|
||||||
|
|
||||||
|
char* number_str = textToCString(number_text);
|
||||||
|
char* country = textToCString(country_text);
|
||||||
|
|
||||||
ShortPhoneNumber* number = parsePhoneNumber(number_str, country);
|
ShortPhoneNumber* number = parsePhoneNumber(number_str, country);
|
||||||
|
//TODO: prevent leaks.
|
||||||
|
pfree(number_str);
|
||||||
|
pfree(country);
|
||||||
if(number) {
|
if(number) {
|
||||||
PG_RETURN_POINTER(number);
|
PG_RETURN_POINTER(number);
|
||||||
} else {
|
} else {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
} catch(std::bad_alloc e) {
|
||||||
|
reportOutOfMemory();
|
||||||
|
} catch(std::exception& e) {
|
||||||
|
reportGenericError(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
||||||
|
|
|
@ -7,10 +7,6 @@ CREATE FUNCTION phone_number_in(cstring) RETURNS phone_number
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_in';
|
AS 'pg_libphonenumber', 'phone_number_in';
|
||||||
|
|
||||||
CREATE FUNCTION parse_phone_number(cstring, cstring) RETURNS phone_number
|
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
|
||||||
AS 'pg_libphonenumber', 'parse_phone_number';
|
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_out(phone_number) RETURNS cstring
|
CREATE FUNCTION phone_number_out(phone_number) RETURNS cstring
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_out';
|
AS 'pg_libphonenumber', 'phone_number_out';
|
||||||
|
@ -34,4 +30,11 @@ CREATE TYPE phone_number (
|
||||||
STORAGE = plain
|
STORAGE = plain
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE CAST (phone_number AS text)
|
||||||
|
WITH INOUT;
|
||||||
|
|
||||||
|
CREATE FUNCTION parse_phone_number(text, text) RETURNS phone_number
|
||||||
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
|
AS 'pg_libphonenumber', 'parse_phone_number';
|
||||||
|
|
||||||
-- vim:syntax=sql
|
-- vim:syntax=sql
|
||||||
|
|
Loading…
Reference in New Issue