Compare commits
10 Commits
908dbd688d
...
6a4d18ed2a
Author | SHA1 | Date |
---|---|---|
jordi fita mas | 6a4d18ed2a | |
Chris Coutinho | 753e2fa4be | |
Ben Merritt | 5a4a37ce9a | |
Ben Merritt | 2e1dfefaed | |
Ben Merritt | 55ef25ac07 | |
Ben Merritt | 364c834aea | |
Ben Merritt | a4da0deccd | |
Ben Merritt | 4e04898fc1 | |
Ben Merritt | 62ca2ab7f3 | |
Ben Merritt | 1d146975bd |
2
Makefile
2
Makefile
|
@ -16,7 +16,7 @@ OBJS := $(patsubst %.cpp,%.o,$(cpp_files))
|
||||||
|
|
||||||
# C flags
|
# C flags
|
||||||
PG_CPPFLAGS := -fPIC -std=c++14
|
PG_CPPFLAGS := -fPIC -std=c++14
|
||||||
PG_CPPFLAGS += -Isrc/ -I/usr/include
|
PG_CPPFLAGS += -Isrc/ -I/usr/include -I/usr/local/include
|
||||||
PG_CPPFLAGS += -Wall -Wextra
|
PG_CPPFLAGS += -Wall -Wextra
|
||||||
ifeq ($(CONFIG),debug)
|
ifeq ($(CONFIG),debug)
|
||||||
PG_CPPFLAGS += -g -Og
|
PG_CPPFLAGS += -g -Og
|
||||||
|
|
10
README.md
10
README.md
|
@ -13,14 +13,10 @@ get it there.
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE EXTENSION pg_libphonenumber;
|
CREATE EXTENSION pg_libphonenumber;
|
||||||
SELECT parse_phone_number('03 7010 1234', 'AU');
|
SELECT parse_packed_phone_number('03 7010 1234', 'AU');
|
||||||
SELECT parse_phone_number('2819010011', 'US');
|
SELECT parse_packed_phone_number('2819010011', 'US');
|
||||||
|
|
||||||
CREATE TABLE foo ( ph phone_number );
|
CREATE TABLE foo ( ph packed_phone_number );
|
||||||
|
|
||||||
-- DO NOT RELY ON THIS
|
|
||||||
-- may not always this work way with implicit cast.
|
|
||||||
SELECT '2819010011'::phone_number; -- assumes US
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
pg-libphonenumber (0.0~git20200211.753e2fa-1) bullseye; urgency=medium
|
||||||
|
|
||||||
|
* Add Debian packaging
|
||||||
|
|
||||||
|
-- jordi fita mas <jordi@tandem.blog> Tue, 24 Jan 2023 13:07:39 +0000
|
|
@ -0,0 +1,26 @@
|
||||||
|
Source: pg-libphonenumber
|
||||||
|
Section: database
|
||||||
|
Priority: optional
|
||||||
|
Maintainer: jordi fita mas <jordi@tandem.blog>
|
||||||
|
Build-Depends:
|
||||||
|
debhelper-compat (= 13),
|
||||||
|
postgresql-all (>= 217~),
|
||||||
|
libphonenumber-dev
|
||||||
|
Standards-Version: 4.6.0
|
||||||
|
Vcs-Browser: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Vcs-Git: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Homepage: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Rules-Requires-Root: no
|
||||||
|
|
||||||
|
Package: postgresql-13-pg-libphonenumber
|
||||||
|
Architecture: all
|
||||||
|
Depends:
|
||||||
|
${shlibs:Depends},
|
||||||
|
${misc:Depends},
|
||||||
|
postgresql-13
|
||||||
|
description: PostgreSQL extension for libphonenumber
|
||||||
|
pg_libphonenumber is a (partially implemented!) PostgreSQL extension that
|
||||||
|
provides access to Google's libphonenumber.
|
||||||
|
.
|
||||||
|
libphonenumber is a library for parsing, formatting, and validating
|
||||||
|
international phone numbers.
|
|
@ -0,0 +1,26 @@
|
||||||
|
Source: pg-libphonenumber
|
||||||
|
Section: database
|
||||||
|
Priority: optional
|
||||||
|
Maintainer: jordi fita mas <jordi@tandem.blog>
|
||||||
|
Build-Depends:
|
||||||
|
debhelper-compat (= 13),
|
||||||
|
postgresql-all (>= 217~),
|
||||||
|
libphonenumber-dev
|
||||||
|
Standards-Version: 4.6.0
|
||||||
|
Vcs-Browser: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Vcs-Git: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Homepage: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Rules-Requires-Root: no
|
||||||
|
|
||||||
|
Package: postgresql-PGVERSION-pg-libphonenumber
|
||||||
|
Architecture: all
|
||||||
|
Depends:
|
||||||
|
${shlibs:Depends},
|
||||||
|
${misc:Depends},
|
||||||
|
postgresql-PGVERSION
|
||||||
|
description: PostgreSQL extension for libphonenumber
|
||||||
|
pg_libphonenumber is a (partially implemented!) PostgreSQL extension that
|
||||||
|
provides access to Google's libphonenumber.
|
||||||
|
.
|
||||||
|
libphonenumber is a library for parsing, formatting, and validating
|
||||||
|
international phone numbers.
|
|
@ -0,0 +1,190 @@
|
||||||
|
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
|
||||||
|
Source: https://github.com/blm768/pg-libphonenumber
|
||||||
|
Upstream-Name: pg_libphonenumber
|
||||||
|
Upstream-Contact: blm768@gmail.com
|
||||||
|
|
||||||
|
Files: *
|
||||||
|
Copyright: 2015–2019 Benjamin L. Merritt
|
||||||
|
License: Apache-2.0
|
||||||
|
|
||||||
|
Files: debian/*
|
||||||
|
Copyright: 2023 jordi fita mas
|
||||||
|
License: Apache-2.0
|
||||||
|
|
||||||
|
License: Apache-2.0
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
.
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
.
|
||||||
|
1. Definitions.
|
||||||
|
.
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
.
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
.
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
.
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
.
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
.
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
.
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
.
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
.
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
.
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
.
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
.
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
.
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
.
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
.
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
.
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
.
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
.
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
.
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
.
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
.
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
.
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
.
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
.
|
||||||
|
END OF TERMS AND CONDITIONS
|
|
@ -0,0 +1,44 @@
|
||||||
|
From 2f2046aac124a8dd4f28131980d72015ad8dda71 Mon Sep 17 00:00:00 2001
|
||||||
|
From: jordi fita mas <jordi@tandem.blog>
|
||||||
|
Date: Tue, 24 Jan 2023 14:17:19 +0100
|
||||||
|
Subject: [PATCH] Increase the length of the test phone number
|
||||||
|
|
||||||
|
Apparently, now libphonenumber does accept 555-555-55555555555 without
|
||||||
|
complain, so the expected output is wrong and to trigger the original
|
||||||
|
error message we need to add an extra 5 there.
|
||||||
|
---
|
||||||
|
expected/regression.out | 4 ++--
|
||||||
|
sql/regression.sql | 2 +-
|
||||||
|
2 files changed, 3 insertions(+), 3 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/expected/regression.out b/expected/regression.out
|
||||||
|
index 474df36..c3cf363 100644
|
||||||
|
--- a/expected/regression.out
|
||||||
|
+++ b/expected/regression.out
|
||||||
|
@@ -12,8 +12,8 @@ select parse_packed_phone_number('555-555-5555555555', 'US');
|
||||||
|
ERROR: phone number '+1 5555555555555555' is too long
|
||||||
|
DETAIL: National number is too long
|
||||||
|
--Produces an error from libphonenumber
|
||||||
|
-select parse_packed_phone_number('555-555-55555555555', 'US');
|
||||||
|
-ERROR: unable to parse '555-555-55555555555' as a phone number
|
||||||
|
+select parse_packed_phone_number('555-555-555555555555', 'US');
|
||||||
|
+ERROR: unable to parse '555-555-555555555555' as a phone number
|
||||||
|
DETAIL: National number is too long
|
||||||
|
-- Do we get correct country codes?
|
||||||
|
-- TODO: expand.
|
||||||
|
diff --git a/sql/regression.sql b/sql/regression.sql
|
||||||
|
index 221b4c2..8496e7d 100644
|
||||||
|
--- a/sql/regression.sql
|
||||||
|
+++ b/sql/regression.sql
|
||||||
|
@@ -6,7 +6,7 @@ select parse_packed_phone_number('555-555-5555', 'US');
|
||||||
|
--Produces an error in pg-libphonenumber's code
|
||||||
|
select parse_packed_phone_number('555-555-5555555555', 'US');
|
||||||
|
--Produces an error from libphonenumber
|
||||||
|
-select parse_packed_phone_number('555-555-55555555555', 'US');
|
||||||
|
+select parse_packed_phone_number('555-555-555555555555', 'US');
|
||||||
|
|
||||||
|
-- Do we get correct country codes?
|
||||||
|
-- TODO: expand.
|
||||||
|
--
|
||||||
|
2.35.3
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
0001-Increase-the-length-of-the-test-phone-number.patch
|
|
@ -0,0 +1 @@
|
||||||
|
13+
|
|
@ -0,0 +1,6 @@
|
||||||
|
#!/usr/bin/make -f
|
||||||
|
|
||||||
|
include /usr/share/postgresql-common/pgxs_debian_control.mk
|
||||||
|
|
||||||
|
%:
|
||||||
|
dh $@ --with pgxs_loop
|
|
@ -0,0 +1 @@
|
||||||
|
3.0 (quilt)
|
|
@ -7,3 +7,6 @@ pg_ctl=/usr/lib/postgresql/${PG_MAJOR}/bin/pg_ctl
|
||||||
"$pg_ctl" "--pgdata=${PGDATA}" start -w
|
"$pg_ctl" "--pgdata=${PGDATA}" start -w
|
||||||
make installcheck
|
make installcheck
|
||||||
"$pg_ctl" "--pgdata=${PGDATA}" stop
|
"$pg_ctl" "--pgdata=${PGDATA}" stop
|
||||||
|
if [ -f regression.diffs ]; then
|
||||||
|
cat regression.diffs
|
||||||
|
fi
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
CREATE EXTENSION IF NOT EXISTS pg_libphonenumber;
|
CREATE EXTENSION IF NOT EXISTS pg_libphonenumber;
|
||||||
--Test phone number parsing
|
--Test phone number parsing
|
||||||
select parse_phone_number('555-555-5555', 'US');
|
select parse_packed_phone_number('555-555-5555', 'US');
|
||||||
parse_phone_number
|
parse_packed_phone_number
|
||||||
--------------------
|
---------------------------
|
||||||
+1 555-555-5555
|
+1 555-555-5555
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
--These two should produce errors because the number is too long.
|
--These two should produce errors because the number is too long.
|
||||||
--Produces an error in pg-libphonenumber's code
|
--Produces an error in pg-libphonenumber's code
|
||||||
select parse_phone_number('555-555-5555555555', 'US');
|
select parse_packed_phone_number('555-555-5555555555', 'US');
|
||||||
ERROR: phone number '+1 5555555555555555' is too long
|
ERROR: phone number '+1 5555555555555555' is too long
|
||||||
DETAIL: National number is too long
|
DETAIL: National number is too long
|
||||||
--Produces an error from libphonenumber
|
--Produces an error from libphonenumber
|
||||||
select parse_phone_number('555-555-55555555555', 'US');
|
select parse_packed_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?
|
-- Do we get correct country codes?
|
||||||
-- TODO: expand.
|
-- TODO: expand.
|
||||||
select phone_number_country_code(parse_phone_number('+1-555-555-5555', 'USA'));
|
select phone_number_country_code(parse_packed_phone_number('+1-555-555-5555', 'USA'));
|
||||||
phone_number_country_code
|
phone_number_country_code
|
||||||
---------------------------
|
---------------------------
|
||||||
1
|
1
|
||||||
|
|
|
@ -2,56 +2,52 @@
|
||||||
\echo Use "CREATE EXTENSION pg_libphonenumber" to load this file. \quit
|
\echo Use "CREATE EXTENSION pg_libphonenumber" to load this file. \quit
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Phone number type
|
-- Packed number type
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE TYPE phone_number;
|
CREATE TYPE packed_phone_number;
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_in(cstring) RETURNS phone_number
|
CREATE FUNCTION packed_phone_number_in(cstring) RETURNS packed_phone_number
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_in';
|
AS 'pg_libphonenumber', 'packed_phone_number_in';
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_out(phone_number) RETURNS cstring
|
CREATE FUNCTION packed_phone_number_out(packed_phone_number) RETURNS cstring
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_out';
|
AS 'pg_libphonenumber', 'packed_phone_number_out';
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_recv(internal) RETURNS phone_number
|
CREATE FUNCTION packed_phone_number_recv(internal) RETURNS packed_phone_number
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_recv';
|
AS 'pg_libphonenumber', 'packed_phone_number_recv';
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_send(phone_number) RETURNS bytea
|
CREATE FUNCTION packed_phone_number_send(packed_phone_number) RETURNS bytea
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_send';
|
AS 'pg_libphonenumber', 'packed_phone_number_send';
|
||||||
|
|
||||||
CREATE TYPE phone_number (
|
CREATE TYPE packed_phone_number (
|
||||||
INTERNALLENGTH = 8,
|
INTERNALLENGTH = 8,
|
||||||
INPUT = phone_number_in,
|
INPUT = packed_phone_number_in,
|
||||||
OUTPUT = phone_number_out,
|
OUTPUT = packed_phone_number_out,
|
||||||
RECEIVE = phone_number_recv,
|
RECEIVE = packed_phone_number_recv,
|
||||||
SEND = phone_number_send,
|
SEND = packed_phone_number_send,
|
||||||
ALIGNMENT = double,
|
ALIGNMENT = double,
|
||||||
STORAGE = plain
|
STORAGE = plain
|
||||||
);
|
);
|
||||||
|
|
||||||
--
|
|
||||||
-- Casts
|
-- Casts
|
||||||
--
|
|
||||||
|
|
||||||
CREATE CAST (phone_number AS text)
|
CREATE CAST (packed_phone_number AS text)
|
||||||
WITH INOUT;
|
WITH INOUT;
|
||||||
|
|
||||||
--
|
|
||||||
-- Operators and indexing
|
-- Operators and indexing
|
||||||
--
|
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_equal(phone_number, phone_number) RETURNS bool
|
CREATE FUNCTION packed_phone_number_equal(packed_phone_number, packed_phone_number) RETURNS bool
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_equal';
|
AS 'pg_libphonenumber', 'packed_phone_number_equal';
|
||||||
|
|
||||||
CREATE OPERATOR = (
|
CREATE OPERATOR = (
|
||||||
leftarg = phone_number,
|
leftarg = packed_phone_number,
|
||||||
rightarg = phone_number,
|
rightarg = packed_phone_number,
|
||||||
procedure = phone_number_equal,
|
procedure = packed_phone_number_equal,
|
||||||
commutator = =,
|
commutator = =,
|
||||||
negator = <>,
|
negator = <>,
|
||||||
restrict = eqsel,
|
restrict = eqsel,
|
||||||
|
@ -60,97 +56,99 @@ CREATE OPERATOR = (
|
||||||
merges = true
|
merges = true
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_not_equal(phone_number, phone_number) RETURNS bool
|
CREATE FUNCTION packed_phone_number_not_equal(packed_phone_number, packed_phone_number) RETURNS bool
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_not_equal';
|
AS 'pg_libphonenumber', 'packed_phone_number_not_equal';
|
||||||
|
|
||||||
CREATE OPERATOR <> (
|
CREATE OPERATOR <> (
|
||||||
leftarg = phone_number,
|
leftarg = packed_phone_number,
|
||||||
rightarg = phone_number,
|
rightarg = packed_phone_number,
|
||||||
procedure = phone_number_not_equal,
|
procedure = packed_phone_number_not_equal,
|
||||||
commutator = <>,
|
commutator = <>,
|
||||||
negator = =,
|
negator = =,
|
||||||
restrict = neqsel,
|
restrict = neqsel,
|
||||||
join = neqjoinsel
|
join = neqjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_less(phone_number, phone_number) RETURNS bool
|
CREATE FUNCTION packed_phone_number_less(packed_phone_number, packed_phone_number) RETURNS bool
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_less';
|
AS 'pg_libphonenumber', 'packed_phone_number_less';
|
||||||
|
|
||||||
CREATE OPERATOR < (
|
CREATE OPERATOR < (
|
||||||
leftarg = phone_number,
|
leftarg = packed_phone_number,
|
||||||
rightarg = phone_number,
|
rightarg = packed_phone_number,
|
||||||
procedure = phone_number_less,
|
procedure = packed_phone_number_less,
|
||||||
commutator = >,
|
commutator = >,
|
||||||
negator = >=,
|
negator = >=,
|
||||||
restrict = scalarltsel,
|
restrict = scalarltsel,
|
||||||
join = scalarltjoinsel
|
join = scalarltjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_less_or_equal(phone_number, phone_number) RETURNS bool
|
CREATE FUNCTION packed_phone_number_less_or_equal(packed_phone_number, packed_phone_number) RETURNS bool
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_less_or_equal';
|
AS 'pg_libphonenumber', 'packed_phone_number_less_or_equal';
|
||||||
|
|
||||||
CREATE OPERATOR <= (
|
CREATE OPERATOR <= (
|
||||||
leftarg = phone_number,
|
leftarg = packed_phone_number,
|
||||||
rightarg = phone_number,
|
rightarg = packed_phone_number,
|
||||||
procedure = phone_number_less_or_equal,
|
procedure = packed_phone_number_less_or_equal,
|
||||||
commutator = >=,
|
commutator = >=,
|
||||||
negator = >,
|
negator = >,
|
||||||
restrict = scalarltsel,
|
restrict = scalarltsel,
|
||||||
join = scalarltjoinsel
|
join = scalarltjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_greater(phone_number, phone_number) RETURNS bool
|
CREATE FUNCTION packed_phone_number_greater(packed_phone_number, packed_phone_number) RETURNS bool
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_greater';
|
AS 'pg_libphonenumber', 'packed_phone_number_greater';
|
||||||
|
|
||||||
CREATE OPERATOR > (
|
CREATE OPERATOR > (
|
||||||
leftarg = phone_number,
|
leftarg = packed_phone_number,
|
||||||
rightarg = phone_number,
|
rightarg = packed_phone_number,
|
||||||
procedure = phone_number_greater,
|
procedure = packed_phone_number_greater,
|
||||||
commutator = >,
|
commutator = >,
|
||||||
negator = <=,
|
negator = <=,
|
||||||
restrict = scalargtsel,
|
restrict = scalargtsel,
|
||||||
join = scalargtjoinsel
|
join = scalargtjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_greater_or_equal(phone_number, phone_number) RETURNS bool
|
CREATE FUNCTION packed_phone_number_greater_or_equal(packed_phone_number, packed_phone_number) RETURNS bool
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_greater_or_equal';
|
AS 'pg_libphonenumber', 'packed_phone_number_greater_or_equal';
|
||||||
|
|
||||||
CREATE OPERATOR >= (
|
CREATE OPERATOR >= (
|
||||||
leftarg = phone_number,
|
leftarg = packed_phone_number,
|
||||||
rightarg = phone_number,
|
rightarg = packed_phone_number,
|
||||||
procedure = phone_number_greater_or_equal,
|
procedure = packed_phone_number_greater_or_equal,
|
||||||
commutator = >=,
|
commutator = >=,
|
||||||
negator = <,
|
negator = <,
|
||||||
restrict = scalargtsel,
|
restrict = scalargtsel,
|
||||||
join = scalargtjoinsel
|
join = scalargtjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_cmp(phone_number, phone_number) RETURNS integer
|
CREATE FUNCTION packed_phone_number_cmp(packed_phone_number, packed_phone_number) RETURNS integer
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'phone_number_cmp';
|
AS 'pg_libphonenumber', 'packed_phone_number_cmp';
|
||||||
|
|
||||||
CREATE OPERATOR CLASS phone_number_ops
|
CREATE OPERATOR CLASS packed_phone_number_ops
|
||||||
DEFAULT FOR TYPE phone_number USING btree AS
|
DEFAULT FOR TYPE packed_phone_number USING btree AS
|
||||||
OPERATOR 1 <,
|
OPERATOR 1 <,
|
||||||
OPERATOR 2 <=,
|
OPERATOR 2 <=,
|
||||||
OPERATOR 3 =,
|
OPERATOR 3 =,
|
||||||
OPERATOR 4 >=,
|
OPERATOR 4 >=,
|
||||||
OPERATOR 5 >,
|
OPERATOR 5 >,
|
||||||
FUNCTION 1 phone_number_cmp(phone_number, phone_number);
|
FUNCTION 1 packed_phone_number_cmp(packed_phone_number, packed_phone_number);
|
||||||
|
|
||||||
|
-- Constructors
|
||||||
|
|
||||||
|
CREATE FUNCTION parse_packed_phone_number(text, text) RETURNS packed_phone_number
|
||||||
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
|
AS 'pg_libphonenumber', 'parse_packed_phone_number';
|
||||||
|
|
||||||
--
|
--
|
||||||
-- General functions
|
-- General functions
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE FUNCTION parse_phone_number(text, text) RETURNS phone_number
|
CREATE FUNCTION phone_number_country_code(packed_phone_number) RETURNS integer
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
LANGUAGE c IMMUTABLE STRICT
|
||||||
AS 'pg_libphonenumber', 'parse_phone_number';
|
AS 'pg_libphonenumber', 'packed_phone_number_country_code';
|
||||||
|
|
||||||
CREATE FUNCTION phone_number_country_code(phone_number) RETURNS integer
|
|
||||||
LANGUAGE c IMMUTABLE STRICT
|
|
||||||
AS 'pg_libphonenumber', 'phone_number_country_code';
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
CREATE EXTENSION IF NOT EXISTS pg_libphonenumber;
|
CREATE EXTENSION IF NOT EXISTS pg_libphonenumber;
|
||||||
|
|
||||||
--Test phone number parsing
|
--Test phone number parsing
|
||||||
select parse_phone_number('555-555-5555', 'US');
|
select parse_packed_phone_number('555-555-5555', 'US');
|
||||||
--These two should produce errors because the number is too long.
|
--These two should produce errors because the number is too long.
|
||||||
--Produces an error in pg-libphonenumber's code
|
--Produces an error in pg-libphonenumber's code
|
||||||
select parse_phone_number('555-555-5555555555', 'US');
|
select parse_packed_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_packed_phone_number('555-555-55555555555', 'US');
|
||||||
|
|
||||||
-- Do we get correct country codes?
|
-- Do we get correct country codes?
|
||||||
-- TODO: expand.
|
-- TODO: expand.
|
||||||
select phone_number_country_code(parse_phone_number('+1-555-555-5555', 'USA'));
|
select phone_number_country_code(parse_packed_phone_number('+1-555-555-5555', 'USA'));
|
||||||
|
|
|
@ -9,7 +9,7 @@ extern "C" {
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "short_phone_number.h"
|
#include "packed_phone_number.h"
|
||||||
|
|
||||||
using namespace i18n::phonenumbers;
|
using namespace i18n::phonenumbers;
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ void reportException(const std::exception& exception) {
|
||||||
std::string phone_number = too_long->number_string();
|
std::string phone_number = too_long->number_string();
|
||||||
phone_number += '\0';
|
phone_number += '\0';
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
||||||
errmsg("phone number '%s' is too long", phone_number.data()),
|
errmsg("phone number '%s' is too long", phone_number.data()),
|
||||||
errdetail("%s", exception.what())));
|
errdetail("%s", exception.what())));
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#include "phonenumbers/phonenumberutil.h"
|
#include "phonenumbers/phonenumberutil.h"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
#include "short_phone_number.h"
|
#include "packed_phone_number.h"
|
||||||
|
|
||||||
using namespace google::protobuf;
|
using namespace google::protobuf;
|
||||||
using namespace i18n::phonenumbers;
|
using namespace i18n::phonenumbers;
|
||||||
|
|
||||||
const PhoneNumberUtil* const PhoneNumberTooLongException::phoneUtil = PhoneNumberUtil::GetInstance();
|
namespace {
|
||||||
|
const PhoneNumberUtil* phoneUtil = PhoneNumberUtil::GetInstance();
|
||||||
|
}
|
||||||
|
|
||||||
PhoneNumberTooLongException::PhoneNumberTooLongException(const PhoneNumber& number, const char* msg) :
|
PhoneNumberTooLongException::PhoneNumberTooLongException(const PhoneNumber& number, const char* msg) :
|
||||||
std::runtime_error(msg), _number(number) {};
|
std::runtime_error(msg), _number(number) {};
|
||||||
|
@ -14,7 +16,8 @@ std::string PhoneNumberTooLongException::number_string() const {
|
||||||
return formatted;
|
return formatted;
|
||||||
}
|
}
|
||||||
|
|
||||||
ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) {
|
PackedPhoneNumber::PackedPhoneNumber(const i18n::phonenumbers::PhoneNumber& number) {
|
||||||
|
// TODO: check has_national_number(), etc?
|
||||||
uint32 country_code = number.country_code();
|
uint32 country_code = number.country_code();
|
||||||
if(country_code > max_country_code) {
|
if(country_code > max_country_code) {
|
||||||
throw PhoneNumberTooLongException(number, "Country code is too long");
|
throw PhoneNumberTooLongException(number, "Country code is too long");
|
||||||
|
@ -38,7 +41,7 @@ ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ShortPhoneNumber::operator PhoneNumber() const {
|
PackedPhoneNumber::operator PhoneNumber() const {
|
||||||
PhoneNumber number;
|
PhoneNumber number;
|
||||||
number.set_country_code(country_code());
|
number.set_country_code(country_code());
|
||||||
number.set_national_number(national_number());
|
number.set_national_number(national_number());
|
|
@ -1,3 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -7,16 +9,16 @@
|
||||||
#include "mask.h"
|
#include "mask.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Raised when a phone number is too long to fit in a ShortPhoneNumber
|
* Raised when a phone number is too long to fit in a PackedPhoneNumber
|
||||||
*
|
*
|
||||||
* This shouldn't happen for any valid phone numbers.
|
* This shouldn't happen for any phone numbers that meet ITU specifications.
|
||||||
*/
|
*/
|
||||||
class PhoneNumberTooLongException : public std::runtime_error {
|
class PhoneNumberTooLongException : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
PhoneNumberTooLongException(const i18n::phonenumbers::PhoneNumber& number, const char* msg);
|
PhoneNumberTooLongException(const i18n::phonenumbers::PhoneNumber& number, const char* msg);
|
||||||
|
|
||||||
/// Returns the original phone number object
|
/// Returns the original phone number object
|
||||||
i18n::phonenumbers::PhoneNumber number() const {
|
const i18n::phonenumbers::PhoneNumber& number() const {
|
||||||
return _number;
|
return _number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,14 +26,12 @@ class PhoneNumberTooLongException : public std::runtime_error {
|
||||||
std::string number_string() const;
|
std::string number_string() const;
|
||||||
private:
|
private:
|
||||||
i18n::phonenumbers::PhoneNumber _number;
|
i18n::phonenumbers::PhoneNumber _number;
|
||||||
|
|
||||||
static const i18n::phonenumbers::PhoneNumberUtil* const phoneUtil;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a phone number (packed into a 64-bit integer)
|
* Stores a phone number (packed into a 64-bit integer)
|
||||||
*/
|
*/
|
||||||
class ShortPhoneNumber {
|
class PackedPhoneNumber {
|
||||||
public:
|
public:
|
||||||
/// The largest possible country code
|
/// The largest possible country code
|
||||||
static constexpr size_t max_country_code = 999;
|
static constexpr size_t max_country_code = 999;
|
||||||
|
@ -52,13 +52,13 @@ class ShortPhoneNumber {
|
||||||
static constexpr size_t leading_zeros_offset = country_code_offset + country_code_bits;
|
static constexpr size_t leading_zeros_offset = country_code_offset + country_code_bits;
|
||||||
static constexpr size_t national_number_offset = leading_zeros_offset + leading_zeros_bits;
|
static constexpr size_t national_number_offset = leading_zeros_offset + leading_zeros_bits;
|
||||||
|
|
||||||
ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number);
|
PackedPhoneNumber(const i18n::phonenumbers::PhoneNumber& number);
|
||||||
|
|
||||||
bool operator==(const ShortPhoneNumber other) const {
|
bool operator==(const PackedPhoneNumber other) const {
|
||||||
return this->_data == other._data;
|
return this->_data == other._data;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator != (const ShortPhoneNumber other) const {
|
bool operator != (const PackedPhoneNumber other) const {
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ class ShortPhoneNumber {
|
||||||
operator i18n::phonenumbers::PhoneNumber() const;
|
operator i18n::phonenumbers::PhoneNumber() const;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compares to another ShortPhoneNumber using a fast collation heuristic
|
* Compares to another PackedPhoneNumber using a fast collation heuristic
|
||||||
*
|
*
|
||||||
* May not produce intuitive results for numbers with the same
|
* May not produce intuitive results for numbers with the same
|
||||||
* country code but different lengths
|
* country code but different lengths
|
||||||
|
@ -76,7 +76,7 @@ class ShortPhoneNumber {
|
||||||
* - 0 (if a == b)
|
* - 0 (if a == b)
|
||||||
* - >0 (if a > b)
|
* - >0 (if a > b)
|
||||||
*/
|
*/
|
||||||
int64_t compare_fast(ShortPhoneNumber other) const {
|
int64_t compare_fast(PackedPhoneNumber other) const {
|
||||||
return other._data - this->_data;
|
return other._data - this->_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ class ShortPhoneNumber {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the size of the ShortPhoneNumber class changes for any reason, it will trip this assertion and remind us to
|
* If the size of the PackedPhoneNumber class changes for any reason, it will trip this assertion and remind us to
|
||||||
* update the SQL definition of the short_phone_number type.
|
* update the SQL definition of the short_phone_number type.
|
||||||
*/
|
*/
|
||||||
static_assert(sizeof(ShortPhoneNumber) == 8, "unexpected size for ShortPhoneNumber");
|
static_assert(sizeof(PackedPhoneNumber) == 8, "unexpected size for PackedPhoneNumber");
|
|
@ -10,17 +10,17 @@ extern "C" {
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "error_handling.h"
|
#include "error_handling.h"
|
||||||
#include "short_phone_number.h"
|
#include "packed_phone_number.h"
|
||||||
|
|
||||||
using namespace i18n::phonenumbers;
|
using namespace i18n::phonenumbers;
|
||||||
|
|
||||||
static const PhoneNumberUtil* const phoneUtil = PhoneNumberUtil::GetInstance();
|
static const PhoneNumberUtil* const phoneUtil = PhoneNumberUtil::GetInstance();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clips a value to the given (inclusive) range
|
* Clamps a value to the given (inclusive) range
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T clip(const T& n, const T& lower, const T& upper) {
|
T clamp(const T& n, const T& lower, const T& upper) {
|
||||||
return std::max(lower, std::min(n, upper));
|
return std::max(lower, std::min(n, upper));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,15 +39,13 @@ static char* text_to_c_string(const text* text) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Internal function used by phone_number_in and parse_phone_number
|
//Internal function used by packed_phone_number_in and parse_packed_phone_number
|
||||||
//TODO: take a std::string to minimize copying?
|
//TODO: take a std::string to minimize copying?
|
||||||
//TODO: rename this so we're not relying on overloading to distinguish this from
|
PackedPhoneNumber* do_parse_packed_phone_number(const char* number_str, const char* country) {
|
||||||
// its extern "C" counterpart.
|
|
||||||
ShortPhoneNumber* parse_phone_number(const char* number_str, const char* country) {
|
|
||||||
PhoneNumber number;
|
PhoneNumber number;
|
||||||
ShortPhoneNumber* short_number;
|
PackedPhoneNumber* short_number;
|
||||||
|
|
||||||
short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber));
|
short_number = (PackedPhoneNumber*)palloc0(sizeof(PackedPhoneNumber));
|
||||||
if(short_number == nullptr) {
|
if(short_number == nullptr) {
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
|
@ -56,7 +54,7 @@ ShortPhoneNumber* parse_phone_number(const char* number_str, const char* country
|
||||||
error = phoneUtil->Parse(number_str, country, &number);
|
error = phoneUtil->Parse(number_str, country, &number);
|
||||||
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
||||||
//Initialize short_number using placement new.
|
//Initialize short_number using placement new.
|
||||||
new(short_number) ShortPhoneNumber(number);
|
new(short_number) PackedPhoneNumber(number);
|
||||||
return short_number;
|
return short_number;
|
||||||
} else {
|
} else {
|
||||||
reportParseError(number_str, error);
|
reportParseError(number_str, error);
|
||||||
|
@ -80,15 +78,15 @@ extern "C" {
|
||||||
* I/O functions
|
* I/O functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_in);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_in);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_in(PG_FUNCTION_ARGS) {
|
packed_phone_number_in(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const char *number_str = PG_GETARG_CSTRING(0);
|
const char *number_str = PG_GETARG_CSTRING(0);
|
||||||
|
|
||||||
//TODO: use international format instead.
|
//TODO: use international format instead.
|
||||||
ShortPhoneNumber* number = parse_phone_number(number_str, "US");
|
PackedPhoneNumber* number = do_parse_packed_phone_number(number_str, "US");
|
||||||
if(number) {
|
if(number) {
|
||||||
PG_RETURN_POINTER(number);
|
PG_RETURN_POINTER(number);
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,12 +98,12 @@ extern "C" {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_out);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_out(PG_FUNCTION_ARGS) {
|
packed_phone_number_out(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* short_number = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* short_number = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
PhoneNumber number = *short_number;
|
PhoneNumber number = *short_number;
|
||||||
|
|
||||||
std::string formatted;
|
std::string formatted;
|
||||||
|
@ -129,17 +127,17 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_recv);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_recv);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_recv(PG_FUNCTION_ARGS) {
|
packed_phone_number_recv(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
|
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
|
||||||
ShortPhoneNumber* number;
|
PackedPhoneNumber* number;
|
||||||
|
|
||||||
number = (ShortPhoneNumber*)palloc(sizeof(ShortPhoneNumber));
|
number = (PackedPhoneNumber*)palloc(sizeof(PackedPhoneNumber));
|
||||||
//TODO: make portable (fix endianness issues, etc.).
|
//TODO: make portable (fix endianness issues, etc.).
|
||||||
pq_copymsgbytes(buf, (char*)number, sizeof(ShortPhoneNumber));
|
pq_copymsgbytes(buf, (char*)number, sizeof(PackedPhoneNumber));
|
||||||
PG_RETURN_POINTER(number);
|
PG_RETURN_POINTER(number);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
reportException(e);
|
reportException(e);
|
||||||
|
@ -148,16 +146,16 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_send);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_send);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_send(PG_FUNCTION_ARGS) {
|
packed_phone_number_send(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber *number = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber *number = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
StringInfoData buf;
|
StringInfoData buf;
|
||||||
|
|
||||||
pq_begintypsend(&buf);
|
pq_begintypsend(&buf);
|
||||||
pq_sendbytes(&buf, (const char*)number, sizeof(ShortPhoneNumber));
|
pq_sendbytes(&buf, (const char*)number, sizeof(PackedPhoneNumber));
|
||||||
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
reportException(e);
|
reportException(e);
|
||||||
|
@ -170,13 +168,13 @@ extern "C" {
|
||||||
* Operator functions
|
* Operator functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_equal);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_equal);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_equal(PG_FUNCTION_ARGS) {
|
packed_phone_number_equal(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
PG_RETURN_BOOL(*number1 == *number2);
|
PG_RETURN_BOOL(*number1 == *number2);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -186,13 +184,13 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_not_equal);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_not_equal);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_not_equal(PG_FUNCTION_ARGS) {
|
packed_phone_number_not_equal(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
PG_RETURN_BOOL(*number1 != *number2);
|
PG_RETURN_BOOL(*number1 != *number2);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -202,13 +200,13 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_less);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_less);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_less(PG_FUNCTION_ARGS) {
|
packed_phone_number_less(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
PG_RETURN_BOOL(number1->compare_fast(*number2) < 0);
|
PG_RETURN_BOOL(number1->compare_fast(*number2) < 0);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -218,13 +216,13 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_less_or_equal);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_less_or_equal);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_less_or_equal(PG_FUNCTION_ARGS) {
|
packed_phone_number_less_or_equal(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
PG_RETURN_BOOL(number1->compare_fast(*number2) <= 0);
|
PG_RETURN_BOOL(number1->compare_fast(*number2) <= 0);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -234,13 +232,13 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_greater);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_greater);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_greater(PG_FUNCTION_ARGS) {
|
packed_phone_number_greater(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
PG_RETURN_BOOL(number1->compare_fast(*number2) > 0);
|
PG_RETURN_BOOL(number1->compare_fast(*number2) > 0);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -250,13 +248,13 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_greater_or_equal);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_greater_or_equal);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_greater_or_equal(PG_FUNCTION_ARGS) {
|
packed_phone_number_greater_or_equal(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
PG_RETURN_BOOL(number1->compare_fast(*number2) >= 0);
|
PG_RETURN_BOOL(number1->compare_fast(*number2) >= 0);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
@ -266,17 +264,17 @@ extern "C" {
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_cmp);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_cmp);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_cmp(PG_FUNCTION_ARGS) {
|
packed_phone_number_cmp(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number1 = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number1 = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
const ShortPhoneNumber* number2 = (ShortPhoneNumber*)PG_GETARG_POINTER(1);
|
const PackedPhoneNumber* number2 = (PackedPhoneNumber*)PG_GETARG_POINTER(1);
|
||||||
|
|
||||||
int64_t compared = number1->compare_fast(*number2);
|
int64_t compared = number1->compare_fast(*number2);
|
||||||
|
|
||||||
PG_RETURN_INT32(clip<int64_t>(compared, -1, 1));
|
PG_RETURN_INT32(clamp<int64_t>(compared, -1, 1));
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
reportException(e);
|
reportException(e);
|
||||||
}
|
}
|
||||||
|
@ -288,10 +286,10 @@ extern "C" {
|
||||||
* Other functions
|
* Other functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_packed_phone_number);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
parse_phone_number(PG_FUNCTION_ARGS) {
|
parse_packed_phone_number(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const text* number_text = PG_GETARG_TEXT_P(0);
|
const text* number_text = PG_GETARG_TEXT_P(0);
|
||||||
const text* country_text = PG_GETARG_TEXT_P(1);
|
const text* country_text = PG_GETARG_TEXT_P(1);
|
||||||
|
@ -299,7 +297,7 @@ extern "C" {
|
||||||
char* number_str = text_to_c_string(number_text);
|
char* number_str = text_to_c_string(number_text);
|
||||||
char* country = text_to_c_string(country_text);
|
char* country = text_to_c_string(country_text);
|
||||||
|
|
||||||
ShortPhoneNumber* number = parse_phone_number(number_str, country);
|
PackedPhoneNumber* number = do_parse_packed_phone_number(number_str, country);
|
||||||
//TODO: prevent leaks.
|
//TODO: prevent leaks.
|
||||||
pfree(number_str);
|
pfree(number_str);
|
||||||
pfree(country);
|
pfree(country);
|
||||||
|
@ -314,12 +312,12 @@ extern "C" {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_country_code);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(packed_phone_number_country_code);
|
||||||
|
|
||||||
PGDLLEXPORT Datum
|
PGDLLEXPORT Datum
|
||||||
phone_number_country_code(PG_FUNCTION_ARGS) {
|
packed_phone_number_country_code(PG_FUNCTION_ARGS) {
|
||||||
try {
|
try {
|
||||||
const ShortPhoneNumber* number = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
const PackedPhoneNumber* number = (PackedPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
|
|
||||||
PG_RETURN_INT32(number->country_code());
|
PG_RETURN_INT32(number->country_code());
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
|
|
Loading…
Reference in New Issue