2015-07-10 20:58:40 +00:00
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#define I18N_PHONENUMBERS_USE_BOOST
|
|
|
|
#include "phonenumbers/phonenumberutil.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "libpq/pqformat.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace i18n::phonenumbers;
|
|
|
|
|
|
|
|
PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
|
|
|
|
|
2015-07-14 19:28:55 +00:00
|
|
|
//Utility functions for error handling
|
|
|
|
|
|
|
|
void reportOutOfMemory() {
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY)));
|
|
|
|
}
|
|
|
|
|
2015-07-14 20:07:04 +00:00
|
|
|
void reportParsingError(const char* phone_number, const char* msg = "") {
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("unable to parse '%s' as a phone number", phone_number),
|
|
|
|
errdetail("%s", msg)));
|
|
|
|
}
|
|
|
|
|
2015-07-14 19:28:55 +00:00
|
|
|
void reportGenericError(std::exception& exception) {
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
|
|
|
errmsg("%s", exception.what())));
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: handle non-exception thrown types? (shouldn't happen, but you never know...)
|
|
|
|
|
2015-07-10 20:58:40 +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-14 20:07:04 +00:00
|
|
|
const char *str = PG_GETARG_CSTRING(0);
|
2015-07-10 20:58:40 +00:00
|
|
|
PhoneNumber *number;
|
|
|
|
|
|
|
|
number = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
|
2015-07-14 20:12:20 +00:00
|
|
|
//TODO: can this be removed? (palloc normally handles this, right?)
|
2015-07-14 19:28:55 +00:00
|
|
|
if(number == nullptr) {
|
|
|
|
reportOutOfMemory();
|
2015-07-14 20:12:20 +00:00
|
|
|
PG_RETURN_NULL();
|
2015-07-10 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 19:28:55 +00:00
|
|
|
try {
|
2015-07-14 20:07:04 +00:00
|
|
|
using PNU = i18n::phonenumbers::PhoneNumberUtil;
|
|
|
|
PNU::ErrorType error;
|
2015-07-14 19:28:55 +00:00
|
|
|
error = phoneUtil->Parse(str, "US", number);
|
2015-07-14 20:07:04 +00:00
|
|
|
switch(error) {
|
|
|
|
case PNU::NO_PARSING_ERROR:
|
2015-07-14 19:28:55 +00:00
|
|
|
//The number was parsed correctly; return it.
|
|
|
|
PG_RETURN_POINTER(number);
|
2015-07-14 20:07:04 +00:00
|
|
|
break;
|
2015-07-14 20:12:20 +00:00
|
|
|
case PNU::NOT_A_NUMBER:
|
|
|
|
reportParsingError(str, "String does not appear to contain a phone number.");
|
2015-07-14 20:20:50 +00:00
|
|
|
break;
|
2015-07-14 20:12:20 +00:00
|
|
|
case PNU::INVALID_COUNTRY_CODE_ERROR:
|
|
|
|
reportParsingError(str, "Invalid country code");
|
2015-07-14 20:20:50 +00:00
|
|
|
break;
|
2015-07-14 20:12:20 +00:00
|
|
|
//TODO: handle more error cases specifically.
|
2015-07-14 20:07:04 +00:00
|
|
|
default:
|
|
|
|
//We have some generic parsing error.
|
|
|
|
reportParsingError(str);
|
2015-07-14 19:28:55 +00:00
|
|
|
}
|
2015-07-14 20:07:04 +00:00
|
|
|
//TODO: check number validity.
|
2015-07-14 19:28:55 +00:00
|
|
|
} catch(std::bad_alloc& e) {
|
|
|
|
reportOutOfMemory();
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
reportGenericError(e);
|
2015-07-10 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:20:50 +00:00
|
|
|
//If we get here, we couldn't return a valid number.
|
2015-07-14 19:28:55 +00:00
|
|
|
PG_RETURN_NULL();
|
2015-07-10 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
|
|
|
|
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
phone_number_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PhoneNumber *number = (PhoneNumber*)PG_GETARG_POINTER(0);
|
|
|
|
std::string formatted;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
phoneUtil->Format(*number, PhoneNumberUtil::INTERNATIONAL, &formatted);
|
2015-07-14 20:07:04 +00:00
|
|
|
} catch(std::bad_alloc& e) {
|
|
|
|
reportOutOfMemory();
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
reportGenericError(e);
|
2015-07-10 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:07:04 +00:00
|
|
|
//Copy the formatted number to a C-style string.
|
|
|
|
//We must use the PostgreSQL allocator, not new/malloc.
|
2015-07-10 20:58:40 +00:00
|
|
|
size_t len = formatted.length();
|
2015-07-14 19:28:55 +00:00
|
|
|
ereport(INFO,
|
|
|
|
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
|
|
|
|
errmsg("Length: %zu", len)));
|
2015-07-10 20:58:40 +00:00
|
|
|
result = (char*)palloc(len + 1);
|
2015-07-14 20:07:04 +00:00
|
|
|
if(result == nullptr) {
|
|
|
|
reportOutOfMemory();
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
2015-07-10 20:58:40 +00:00
|
|
|
memcpy(result, formatted.data(), len);
|
|
|
|
result[len] = '\0';
|
|
|
|
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_recv);
|
|
|
|
|
|
|
|
PGDLLEXPORT
|
|
|
|
Datum
|
|
|
|
phone_number_recv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
|
|
|
|
PhoneNumber *result;
|
|
|
|
|
2015-07-14 20:07:04 +00:00
|
|
|
//TODO: error handling?
|
2015-07-10 20:58:40 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|