Added phone_number_country_code function

This commit is contained in:
Ben Merritt 2017-03-13 21:32:50 +00:00
parent c73715bd65
commit 3a071445f3
5 changed files with 78 additions and 35 deletions

View File

@ -3,10 +3,7 @@ CONFIG := release
# Extension packaging options # Extension packaging options
EXTENSION := pg_libphonenumber EXTENSION := pg_libphonenumber
version := 0.1.0 DATA := sql/$(EXTENSION)--*.sql
# TODO: include scripts for all versions.
extension_script := sql/$(EXTENSION)--$(version).sql
DATA := $(extension_script)
REGRESS := regression REGRESS := regression
# Build options # Build options

View File

@ -15,3 +15,11 @@ DETAIL: National number is too long
select parse_phone_number('555-555-55555555555', 'US'); select parse_phone_number('555-555-55555555555', 'US');
ERROR: unable to parse '555-555-55555555555' as a phone number ERROR: unable to parse '555-555-55555555555' as a phone number
DETAIL: National number is too long DETAIL: National number is too long
-- Do we get correct country codes?
-- TODO: expand.
select phone_number_country_code(parse_phone_number('+1-555-555-5555', 'USA'));
phone_number_country_code
---------------------------
1
(1 row)

View File

@ -1,9 +1,11 @@
-- complain if script is sourced in psql, rather than via CREATE EXTENSION -- Complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_libphonenumber" to load this file. \quit \echo Use "CREATE EXTENSION pg_libphonenumber" to load this file. \quit
CREATE TYPE phone_number; --
-- Phone number type
--
--Basic function definitions CREATE TYPE phone_number;
CREATE FUNCTION phone_number_in(cstring) RETURNS phone_number CREATE FUNCTION phone_number_in(cstring) RETURNS phone_number
LANGUAGE c IMMUTABLE STRICT LANGUAGE c IMMUTABLE STRICT
@ -31,12 +33,16 @@ CREATE TYPE phone_number (
STORAGE = plain STORAGE = plain
); );
--Cast definitions --
-- Casts
--
CREATE CAST (phone_number AS text) CREATE CAST (phone_number AS text)
WITH INOUT; WITH INOUT;
--Operator definitions --
-- Operators and indexing
--
CREATE FUNCTION phone_number_equal(phone_number, phone_number) RETURNS bool CREATE FUNCTION phone_number_equal(phone_number, phone_number) RETURNS bool
LANGUAGE c IMMUTABLE STRICT LANGUAGE c IMMUTABLE STRICT
@ -137,9 +143,14 @@ CREATE OPERATOR CLASS phone_number_ops
OPERATOR 5 >, OPERATOR 5 >,
FUNCTION 1 phone_number_cmp(phone_number, phone_number); FUNCTION 1 phone_number_cmp(phone_number, phone_number);
--
-- General functions -- General functions
--
CREATE FUNCTION parse_phone_number(text, text) RETURNS phone_number CREATE FUNCTION parse_phone_number(text, text) RETURNS phone_number
LANGUAGE c IMMUTABLE STRICT LANGUAGE c IMMUTABLE STRICT
AS 'pg_libphonenumber', 'parse_phone_number'; AS 'pg_libphonenumber', 'parse_phone_number';
CREATE FUNCTION phone_number_country_code(phone_number) RETURNS integer
LANGUAGE c IMMUTABLE STRICT
AS 'pg_libphonenumber', 'phone_number_country_code';

View File

@ -8,3 +8,6 @@ select parse_phone_number('555-555-5555555555', 'US');
--Produces an error from libphonenumber --Produces an error from libphonenumber
select parse_phone_number('555-555-55555555555', 'US'); select parse_phone_number('555-555-55555555555', 'US');
-- Do we get correct country codes?
-- TODO: expand.
select phone_number_country_code(parse_phone_number('+1-555-555-5555', 'USA'));

View File

@ -76,6 +76,10 @@ extern "C" {
PG_MODULE_MAGIC; PG_MODULE_MAGIC;
#endif #endif
/*
* I/O functions
*/
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_in); PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_in);
PGDLLEXPORT Datum PGDLLEXPORT Datum
@ -95,31 +99,6 @@ extern "C" {
} }
} }
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
PGDLLEXPORT Datum
parse_phone_number(PG_FUNCTION_ARGS) {
try {
const text* number_text = PG_GETARG_TEXT_P(0);
const text* country_text = PG_GETARG_TEXT_P(1);
char* number_str = text_to_c_string(number_text);
char* country = text_to_c_string(country_text);
ShortPhoneNumber* number = parse_phone_number(number_str, country);
//TODO: prevent leaks.
pfree(number_str);
pfree(country);
if(number) {
PG_RETURN_POINTER(number);
} else {
PG_RETURN_NULL();
}
} catch(std::exception& e) {
reportException(e);
}
}
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out); PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
PGDLLEXPORT Datum PGDLLEXPORT Datum
@ -186,6 +165,10 @@ extern "C" {
PG_RETURN_NULL(); PG_RETURN_NULL();
} }
/*
* Operator functions
*/
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_equal); PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_equal);
PGDLLEXPORT Datum PGDLLEXPORT Datum
@ -300,4 +283,45 @@ extern "C" {
PG_RETURN_NULL(); PG_RETURN_NULL();
} }
/*
* Other functions
*/
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
PGDLLEXPORT Datum
parse_phone_number(PG_FUNCTION_ARGS) {
try {
const text* number_text = PG_GETARG_TEXT_P(0);
const text* country_text = PG_GETARG_TEXT_P(1);
char* number_str = text_to_c_string(number_text);
char* country = text_to_c_string(country_text);
ShortPhoneNumber* number = parse_phone_number(number_str, country);
//TODO: prevent leaks.
pfree(number_str);
pfree(country);
if(number) {
PG_RETURN_POINTER(number);
} else {
PG_RETURN_NULL();
}
} catch(std::exception& e) {
reportException(e);
}
}
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_country_code);
PGDLLEXPORT Datum
phone_number_country_code(PG_FUNCTION_ARGS) {
try {
const ShortPhoneNumber* number = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
PG_RETURN_INT32(number->country_code());
} catch(std::exception& e) {
reportException(e);
}
}
} }