2015-07-14 20:32:22 +00:00
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "phonenumbers/phonenumberutil.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "libpq/pqformat.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:22:30 +00:00
|
|
|
#include "short_phone_number.h"
|
|
|
|
|
2015-07-14 20:32:22 +00:00
|
|
|
using namespace i18n::phonenumbers;
|
|
|
|
|
2015-07-17 20:22:30 +00:00
|
|
|
static const PhoneNumberUtil* const phoneUtil = PhoneNumberUtil::GetInstance();
|
2015-07-17 18:46:26 +00:00
|
|
|
|
|
|
|
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
|
|
|
|
using PNU = i18n::phonenumbers::PhoneNumberUtil;
|
|
|
|
switch(error) {
|
|
|
|
case PNU::NO_PARSING_ERROR:
|
|
|
|
return "Parsed successfully";
|
|
|
|
case PNU::NOT_A_NUMBER:
|
|
|
|
return "String does not appear to contain a phone number.";
|
|
|
|
case PNU::INVALID_COUNTRY_CODE_ERROR:
|
|
|
|
return "Invalid country code";
|
|
|
|
//TODO: handle more error cases specifically.
|
|
|
|
default:
|
|
|
|
//We have some generic parsing error.
|
|
|
|
return "Unable to parse number";
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 19:56:23 +00:00
|
|
|
|
2015-07-17 18:46:26 +00:00
|
|
|
/*
|
|
|
|
* Utility functions for error handling
|
|
|
|
*/
|
2015-07-14 20:32:22 +00:00
|
|
|
|
2015-07-17 18:46:26 +00:00
|
|
|
static void reportOutOfMemory() {
|
2015-07-14 20:32:22 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY)));
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:46:26 +00:00
|
|
|
static void reportParseError(const char* phone_number, PhoneNumberUtil::ErrorType err) {
|
2015-07-14 20:32:22 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("unable to parse '%s' as a phone number", phone_number),
|
2015-07-17 18:46:26 +00:00
|
|
|
errdetail("%s", parseErrorMessage(err))));
|
2015-07-14 20:32:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 18:46:26 +00:00
|
|
|
static void reportGenericError(std::exception& exception) {
|
2015-07-14 20:32:22 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
2015-07-14 20:40:13 +00:00
|
|
|
errmsg("C++ exception: %s", typeid(exception).name()),
|
2015-07-14 20:32:22 +00:00
|
|
|
errdetail("%s", exception.what())));
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:46:26 +00:00
|
|
|
static void logInfo(const char* msg) {
|
2015-07-16 18:25:44 +00:00
|
|
|
ereport(INFO,
|
|
|
|
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
|
|
|
|
errmsg("%s", msg)));
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:56:23 +00:00
|
|
|
/*
|
|
|
|
* Utility functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Internal function used by phone_number_in and parse_phone_number
|
2015-07-21 17:32:30 +00:00
|
|
|
ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() {
|
2015-07-17 18:46:26 +00:00
|
|
|
try {
|
2015-07-21 17:32:30 +00:00
|
|
|
PhoneNumber number;
|
|
|
|
ShortPhoneNumber* short_number;
|
2015-07-17 18:46:26 +00:00
|
|
|
|
2015-07-21 17:32:30 +00:00
|
|
|
short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber));
|
2015-07-17 18:46:26 +00:00
|
|
|
//TODO: can this be removed? (palloc normally handles this, right?)
|
2015-07-21 17:32:30 +00:00
|
|
|
if(short_number == nullptr) {
|
2015-07-17 18:46:26 +00:00
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
PhoneNumberUtil::ErrorType error;
|
2015-07-21 17:32:30 +00:00
|
|
|
error = phoneUtil->Parse(number_str, country, &number);
|
2015-07-17 18:46:26 +00:00
|
|
|
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
2015-07-21 17:32:30 +00:00
|
|
|
new(short_number) ShortPhoneNumber(number);
|
|
|
|
return short_number;
|
2015-07-17 18:46:26 +00:00
|
|
|
} else {
|
|
|
|
reportParseError(number_str, error);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
//TODO: check number validity.
|
|
|
|
} catch(std::bad_alloc& e) {
|
|
|
|
reportOutOfMemory();
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
reportGenericError(e);
|
2015-07-16 19:56:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 18:46:26 +00:00
|
|
|
return nullptr;
|
2015-07-16 19:56:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:32:22 +00:00
|
|
|
//TODO: handle non-exception thrown types? (shouldn't happen, but you never know...)
|
2015-07-16 19:56:23 +00:00
|
|
|
//TODO: check null args? (PG_ARGISNULL). Make non-strict?
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extension functions
|
|
|
|
*/
|
2015-07-14 20:32:22 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#ifdef PG_MODULE_MAGIC
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_in);
|
|
|
|
|
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
phone_number_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2015-07-16 19:56:23 +00:00
|
|
|
const char *number_str = PG_GETARG_CSTRING(0);
|
|
|
|
|
2015-07-21 17:32:30 +00:00
|
|
|
ShortPhoneNumber* number = parsePhoneNumber(number_str, "US");
|
2015-07-17 18:46:26 +00:00
|
|
|
if(number) {
|
|
|
|
PG_RETURN_POINTER(number);
|
|
|
|
} else {
|
|
|
|
PG_RETURN_NULL();
|
2015-07-16 19:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
|
|
|
|
|
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
parse_phone_number(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
const char *number_str = PG_GETARG_CSTRING(0);
|
|
|
|
const char *country = PG_GETARG_CSTRING(1);
|
|
|
|
|
2015-07-21 17:32:30 +00:00
|
|
|
ShortPhoneNumber* number = parsePhoneNumber(number_str, country);
|
2015-07-17 18:46:26 +00:00
|
|
|
if(number) {
|
|
|
|
PG_RETURN_POINTER(number);
|
|
|
|
} else {
|
|
|
|
PG_RETURN_NULL();
|
2015-07-14 20:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
|
|
|
|
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
phone_number_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
try {
|
2015-07-21 17:32:30 +00:00
|
|
|
const ShortPhoneNumber* short_number = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
|
|
|
PhoneNumber number = *short_number;
|
2015-07-16 18:25:44 +00:00
|
|
|
|
2015-07-21 17:32:30 +00:00
|
|
|
std::string formatted;
|
|
|
|
phoneUtil->Format(number, PhoneNumberUtil::INTERNATIONAL, &formatted);
|
2015-07-16 18:25:44 +00:00
|
|
|
|
|
|
|
//Copy the formatted number to a C-style string.
|
|
|
|
//We must use the PostgreSQL allocator, not new/malloc.
|
|
|
|
size_t len = formatted.length();
|
2015-07-21 17:32:30 +00:00
|
|
|
char* result = (char*)palloc(len + 1);
|
2015-07-16 18:25:44 +00:00
|
|
|
if(result == nullptr) {
|
2015-07-21 17:32:30 +00:00
|
|
|
throw std::bad_alloc();
|
2015-07-16 18:25:44 +00:00
|
|
|
}
|
|
|
|
memcpy(result, formatted.data(), len);
|
|
|
|
result[len] = '\0';
|
|
|
|
|
|
|
|
PG_RETURN_CSTRING(result);
|
2015-07-14 20:32:22 +00:00
|
|
|
} catch(std::bad_alloc& e) {
|
|
|
|
reportOutOfMemory();
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
reportGenericError(e);
|
|
|
|
}
|
2015-07-16 19:56:23 +00:00
|
|
|
|
2015-07-16 18:25:44 +00:00
|
|
|
PG_RETURN_NULL();
|
2015-07-14 20:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_recv);
|
|
|
|
|
2015-07-16 18:25:44 +00:00
|
|
|
//TODO: handle leading zeroes?
|
2015-07-14 20:32:22 +00:00
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
phone_number_recv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
|
|
|
|
PhoneNumber *result;
|
|
|
|
|
|
|
|
//TODO: error handling?
|
|
|
|
result = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
|
|
|
|
result->set_country_code(pq_getmsgint(buf, 4));
|
|
|
|
result->set_national_number(pq_getmsgint64(buf));
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_send);
|
|
|
|
|
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
phone_number_send(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PhoneNumber *number = (PhoneNumber*)PG_GETARG_POINTER(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendint(&buf, number->country_code(), 4);
|
|
|
|
pq_sendint64(&buf, number->national_number());
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
}
|