-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cast functions to and from int2 and int4 types #18
Open
zilder
wants to merge
1
commit into
master
Choose a base branch
from
casts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
CREATE FUNCTION int4_to_country(int4) | ||
RETURNS country | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_to_int4(country) | ||
RETURNS int4 | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION int2_to_country(int2) | ||
RETURNS country | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_to_int2(country) | ||
RETURNS int2 | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
DO $$ | ||
DECLARE version_num integer; | ||
BEGIN | ||
SELECT current_setting('server_version_num') INTO STRICT version_num; | ||
IF version_num > 90600 THEN | ||
ALTER FUNCTION int4_to_country(int4) PARALLEL SAFE; | ||
ALTER FUNCTION country_to_int4(country) PARALLEL SAFE; | ||
ALTER FUNCTION int2_to_country(int2) PARALLEL SAFE; | ||
ALTER FUNCTION country_to_int2(country) PARALLEL SAFE; | ||
END IF; | ||
END; | ||
$$; | ||
|
||
CREATE CAST (int4 as country) | ||
WITH FUNCTION int4_to_country(int4); | ||
|
||
CREATE CAST (country as int4) | ||
WITH FUNCTION country_to_int4(country); | ||
|
||
CREATE CAST (int2 as country) | ||
WITH FUNCTION int2_to_country(int2); | ||
|
||
CREATE CAST (country as int2) | ||
WITH FUNCTION country_to_int2(country); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
CREATE FUNCTION country_in(cstring) | ||
RETURNS country | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_out(country) | ||
RETURNS cstring | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_recv(internal) | ||
RETURNS country | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_send(country) | ||
RETURNS bytea | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE TYPE country ( | ||
INPUT = country_in, | ||
OUTPUT = country_out, | ||
RECEIVE = country_recv, | ||
SEND = country_send, | ||
LIKE = "char", | ||
CATEGORY = 'S' | ||
); | ||
COMMENT ON TYPE country IS 'a country internaly stored as int8'; | ||
|
||
|
||
CREATE FUNCTION country_name(country) | ||
RETURNS TEXT | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_common_name(country) | ||
RETURNS TEXT | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION int4_to_country(int4) | ||
RETURNS country | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_to_int4(country) | ||
RETURNS int4 | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION int2_to_country(int2) | ||
RETURNS country | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_to_int2(country) | ||
RETURNS int2 | ||
AS '$libdir/country' | ||
LANGUAGE C IMMUTABLE STRICT; | ||
|
||
CREATE FUNCTION country_eq(country, country) | ||
RETURNS boolean LANGUAGE internal IMMUTABLE STRICT AS 'chareq'; | ||
|
||
CREATE FUNCTION country_ne(country, country) | ||
RETURNS boolean LANGUAGE internal IMMUTABLE STRICT AS 'charne'; | ||
|
||
CREATE FUNCTION country_lt(country, country) | ||
RETURNS boolean LANGUAGE internal IMMUTABLE STRICT AS 'charlt'; | ||
|
||
CREATE FUNCTION country_le(country, country) | ||
RETURNS boolean LANGUAGE internal IMMUTABLE STRICT AS 'charle'; | ||
|
||
CREATE FUNCTION country_gt(country, country) | ||
RETURNS boolean LANGUAGE internal IMMUTABLE STRICT AS 'chargt'; | ||
|
||
CREATE FUNCTION country_ge(country, country) | ||
RETURNS boolean LANGUAGE internal IMMUTABLE STRICT AS 'charge'; | ||
|
||
CREATE FUNCTION country_cmp(country, country) | ||
RETURNS integer LANGUAGE internal IMMUTABLE STRICT AS 'btcharcmp'; | ||
|
||
CREATE FUNCTION hash_country(country) | ||
RETURNS integer LANGUAGE internal IMMUTABLE STRICT AS 'hashchar'; | ||
|
||
DO $$ | ||
DECLARE version_num integer; | ||
BEGIN | ||
SELECT current_setting('server_version_num') INTO STRICT version_num; | ||
IF version_num > 90600 THEN | ||
ALTER FUNCTION country_in(cstring) PARALLEL SAFE; | ||
ALTER FUNCTION country_out(country) PARALLEL SAFE; | ||
ALTER FUNCTION country_recv(internal) PARALLEL SAFE; | ||
ALTER FUNCTION country_send(country) PARALLEL SAFE; | ||
ALTER FUNCTION country_name(country) PARALLEL SAFE; | ||
ALTER FUNCTION country_common_name(country) PARALLEL SAFE; | ||
ALTER FUNCTION int4_to_country(int4) PARALLEL SAFE; | ||
ALTER FUNCTION country_to_int4(country) PARALLEL SAFE; | ||
ALTER FUNCTION int2_to_country(int2) PARALLEL SAFE; | ||
ALTER FUNCTION country_to_int2(country) PARALLEL SAFE; | ||
ALTER FUNCTION country_eq(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION country_ne(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION country_lt(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION country_le(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION country_gt(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION country_ge(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION country_cmp(country, country) PARALLEL SAFE; | ||
ALTER FUNCTION hash_country(country) PARALLEL SAFE; | ||
END IF; | ||
END; | ||
$$; | ||
|
||
CREATE OPERATOR = ( | ||
LEFTARG = country, | ||
RIGHTARG = country, | ||
PROCEDURE = country_eq, | ||
COMMUTATOR = '=', | ||
NEGATOR = '<>', | ||
RESTRICT = eqsel, | ||
JOIN = eqjoinsel | ||
); | ||
COMMENT ON OPERATOR =(country, country) IS 'equals?'; | ||
|
||
CREATE OPERATOR <> ( | ||
LEFTARG = country, | ||
RIGHTARG = country, | ||
PROCEDURE = country_ne, | ||
COMMUTATOR = '<>', | ||
NEGATOR = '=', | ||
RESTRICT = neqsel, | ||
JOIN = neqjoinsel | ||
); | ||
COMMENT ON OPERATOR <>(country, country) IS 'not equals?'; | ||
|
||
CREATE OPERATOR < ( | ||
LEFTARG = country, | ||
RIGHTARG = country, | ||
PROCEDURE = country_lt, | ||
COMMUTATOR = > , | ||
NEGATOR = >= , | ||
RESTRICT = scalarltsel, | ||
JOIN = scalarltjoinsel | ||
); | ||
COMMENT ON OPERATOR <(country, country) IS 'less-than'; | ||
|
||
CREATE OPERATOR <= ( | ||
LEFTARG = country, | ||
RIGHTARG = country, | ||
PROCEDURE = country_le, | ||
COMMUTATOR = >= , | ||
NEGATOR = > , | ||
RESTRICT = scalarltsel, | ||
JOIN = scalarltjoinsel | ||
); | ||
COMMENT ON OPERATOR <=(country, country) IS 'less-than-or-equal'; | ||
|
||
CREATE OPERATOR > ( | ||
LEFTARG = country, | ||
RIGHTARG = country, | ||
PROCEDURE = country_gt, | ||
COMMUTATOR = < , | ||
NEGATOR = <= , | ||
RESTRICT = scalargtsel, | ||
JOIN = scalargtjoinsel | ||
); | ||
COMMENT ON OPERATOR >(country, country) IS 'greater-than'; | ||
|
||
CREATE OPERATOR >= ( | ||
LEFTARG = country, | ||
RIGHTARG = country, | ||
PROCEDURE = country_ge, | ||
COMMUTATOR = <= , | ||
NEGATOR = < , | ||
RESTRICT = scalargtsel, | ||
JOIN = scalargtjoinsel | ||
); | ||
COMMENT ON OPERATOR >=(country, country) IS 'greater-than-or-equal'; | ||
|
||
CREATE OPERATOR CLASS btree_country_ops | ||
DEFAULT FOR TYPE country USING btree | ||
AS | ||
OPERATOR 1 < , | ||
OPERATOR 2 <= , | ||
OPERATOR 3 = , | ||
OPERATOR 4 >= , | ||
OPERATOR 5 > , | ||
FUNCTION 1 country_cmp(country, country); | ||
|
||
CREATE OPERATOR CLASS hash_country_ops | ||
DEFAULT FOR TYPE country USING hash | ||
AS | ||
OPERATOR 1 = , | ||
FUNCTION 1 hash_country(country); | ||
|
||
CREATE CAST (int4 as country) | ||
WITH FUNCTION int4_to_country(int4); | ||
|
||
CREATE CAST (country as int4) | ||
WITH FUNCTION country_to_int4(country); | ||
|
||
CREATE CAST (int2 as country) | ||
WITH FUNCTION int2_to_country(int2); | ||
|
||
CREATE CAST (country as int2) | ||
WITH FUNCTION country_to_int2(country); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# country extension | ||
comment = 'my awesome extension' | ||
default_version = '0.0.4' | ||
default_version = '0.0.5' | ||
relocatable = true | ||
requires = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
BEGIN; | ||
CREATE EXTENSION country; | ||
SELECT 'de'::country::int4; | ||
int4 | ||
------ | ||
57 | ||
(1 row) | ||
|
||
SELECT 'de'::country::int2; | ||
int2 | ||
------ | ||
57 | ||
(1 row) | ||
|
||
SELECT 57::int4::country; | ||
country | ||
--------- | ||
de | ||
(1 row) | ||
|
||
SELECT 57::int2::country; | ||
country | ||
--------- | ||
de | ||
(1 row) | ||
|
||
ROLLBACK; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
BEGIN; | ||
CREATE EXTENSION country; | ||
SELECT 'de'::country::int4; | ||
SELECT 'de'::country::int2; | ||
SELECT 57::int4::country; | ||
SELECT 57::int2::country; | ||
ROLLBACK; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a good idea as it risk that invalid values get stored on disk which makes reading back impossible